Professional Documents
Culture Documents
DBSCAN
DBSCAN
# Display the first few rows of the dataset to understand its structure
print(data.head())
# Assuming your dataset has features that you want to use for clustering, let's say
'feature1' and 'feature2'
X = data[['feature1', 'feature2']]
# Initialize DBSCAN
# You need to specify the epsilon (eps) and the minimum samples (min_samples)
parameters
# You might need to experiment with these parameters to get the desired clustering
eps = 0.5 # Epsilon neighborhood radius
min_samples = 5 # Minimum number of samples in a neighborhood
dbscan = DBSCAN(eps=eps, min_samples=min_samples)
# Assigning a different color to each cluster, and black for noise (if present)
unique_labels = set(labels)
colors = [plt.cm.Spectral(each) for each in range(len(unique_labels))]
for k, col in zip(unique_labels, colors):
if k == -1:
# Black used for noise.
col = [0, 0, 0, 1]
class_member_mask = (labels == k)
xy = X_scaled[class_member_mask]
plt.scatter(xy[:, 0], xy[:, 1], s=50, c=[col], marker='o', alpha=0.5)