How would you group/cluster these three areas in arrays in python?

后端 未结 5 885
挽巷
挽巷 2021-02-01 07:42

So you have an array

1
2
3
60
70
80
100
220
230
250

For a better understanding:

\"

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-01 08:07

    Observe that your data points are actually one-dimensional if x just represents an index. You can cluster your points using Scipy's cluster.vq module, which implements the k-means algorithm.

    >>> import numpy as np
    >>> from scipy.cluster.vq import kmeans, vq
    >>> y = np.array([1,2,3,60,70,80,100,220,230,250])
    >>> codebook, _ = kmeans(y, 3)  # three clusters
    >>> cluster_indices, _ = vq(y, codebook)
    >>> cluster_indices
    array([1, 1, 1, 0, 0, 0, 0, 2, 2, 2])
    

    The result means: the first three points form cluster 1 (an arbitrary label), the next four form cluster 0 and the last three form cluster 2. Grouping the original points according to the indices is left as an exercise for the reader.

    For more clustering algorithms in Python, check out scikit-learn.

提交回复
热议问题