-2

I'm working on k-mean algorthim to cluster list of number, let we say my list is

 my_list = [13, 15, 13, 23, 45, 25, 7]

how could I use k-mean to grouped into clusters of similar numbers? So the output would be this:

clusters = {
    1 : [7],
    2 : [13, 15, 13],
    3 : [23, 25],
    4 : [45]
}

then randomly select one number to represent each cluster?

(for example max value in each cluster), 7, 15, 25, and 45 are selected respectively. how can I do it?

man.utd_21
  • 19
  • 7
  • 2
    It's not clear what you are asking. Are you asking us to show you how to do k-means clustering from scratch? Seems like it might be better to seek out the *many* online tutorials or use a library. Or are you simply asking how to select the max value from your clusters dict as the title suggests? – Mark Jul 19 '20 at 20:19

2 Answers2

0
mylist.sort(reverse=True)
print(mylist[0])

Can be sorted with sort() if you want min to max.

Can be sorted with sort(reverse=True) if you wants max to min I hope I helped

for clusters:

clusters = {
1 : [7],
2 : [13, 15, 13],
3 : [23, 25],
4 : [45]
}
for i in clusters:
    a = clusters[i]
    a.sort(reverse=True)
    print(a[0])
0

If i have an array (X)

  X=array([[0.85142858],[0.85566274],[0.85364912],[0.81536489],[0.84929932],[0.85042336],[0.84899714],[0.82019115], [0.86112067],[0.8312496 ]])

then I run the following code

from sklearn.cluster import AgglomerativeClustering

cluster = AgglomerativeClustering(n_clusters=5, affinity='euclidean', linkage='ward')
cluster.fit_predict(X)

for i in range(len(X)):
    print("%4d " % cluster.labels_[i], end=""); print(X[i])

i got the results

1 [0.85142858]
   3 [0.85566274]
   3 [0.85364912]
   0 [0.81536489]
   1 [0.84929932]
   1 [0.85042336]
   1 [0.84899714]
   0 [0.82019115]
   4 [0.86112067]
   2 [0.8312496]

how to get the max number in each cluster ? like this

0: 0.82019115
1: 0.85142858
2: 0.8312496
3: 0.85566274
4: 0.86112067
man.utd_21
  • 19
  • 7