So you have an array
1
2
3
60
70
80
100
220
230
250
For a better understanding:
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.