DBSCAN in scikit-learn of Python: save the cluster points in an array

22,592

Solution 1

The first cluster is X[labels == 0], etc.:

clusters = [X[labels == i] for i in xrange(n_clusters_)]

and the outliers are

outliers = X[labels == -1]

Solution 2

What do you mean by "of each cluster"?

In DBSCAN, clusters are not represented as centroids as in k-means, so there is no obvious representation of the cluster except its members. You already have the x and y position of the cluster members, as they are the input data.

So I'm not sure what the question is.

Share:
22,592

Related videos on Youtube

Gianni Spear
Author by

Gianni Spear

Updated on July 09, 2022

Comments

  • Gianni Spear
    Gianni Spear almost 2 years

    following the example Demo of DBSCAN clustering algorithm of Scikit Learning i am trying to store in an array the x, y of each clustering class

    import numpy as np
    from sklearn.cluster import DBSCAN
    from sklearn import metrics
    from sklearn.datasets.samples_generator import make_blobs
    from sklearn.preprocessing import StandardScaler
    from pylab import *
    
    # Generate sample data
    centers = [[1, 1], [-1, -1], [1, -1]]
    X, labels_true = make_blobs(n_samples=750, centers=centers, cluster_std=0.4, random_state=0)
    X = StandardScaler().fit_transform(X) 
    
    xx, yy = zip(*X)
    scatter(xx,yy)
    show()
    

    enter image description here

    db = DBSCAN(eps=0.3, min_samples=10).fit(X)
    core_samples = db.core_sample_indices_
    labels = db.labels_
    n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
    print n_clusters_
    3
    

    enter image description here

    I'm trying to understand the DBSCAN implementation by scikit-learn, but from this point I'm having trouble. The number of cluster is 3 (n_clusters_) and I wish to store the x, y of each cluster in an array

  • Gianni Spear
    Gianni Spear almost 11 years
    hey Andreas, sorry if i was not clear. If you see in figure 2 each points (x, y) has a different color due to the cluster (e.g., 3 clusters in the example and back dots are noise). I wish to have an array with 3 blocks. Each block store the x, y points values of that cluter.
  • user3378649
    user3378649 about 10 years
    You have x and y position of the cluster members, but how can you know in which cluster these points are ?
  • GoingMyWay
    GoingMyWay over 6 years
    @user3378649, there are lables corresponding to its cluster. dbscan.labels_