# An alternative algorithm to sklearn KMeans that separates values by similarity?

I have the following dataset:

        node        bc cluster
1    russian  0.457039       1
48       man  0.286875       1
155    woman  0.129939       0
3        bit  0.092721       0
5      write  0.065424       0
98       age  0.064347       0
97     escap  0.062675       0
74      game  0.062606       0


Then I perform kMeans clustering by bc value to separate the nodes into two different groups. Right now with the code below I get the result above (the clustering result is in the cluster column).

    bc_df = pd.DataFrame({"node": bc_nodes, "bc": bc_values})
bc_df = bc_df.sort_values("bc", ascending=False)
km = KMeans(n_clusters=2).fit(bc_df[['bc']])
bc_df.loc[:,'cluster'] = km.labels_


Which is pretty good, but I would like it to work slightly differently and to select the first 4 nodes into the first cluster and then the other ones in the 2nd one, because they are more similar to each other.

Can I do some adjustment to kMeans or maybe you know another algorithm in sklearn that can do that?

• If you want to separate 2 classes (clusters) by 1 numerical feature, maybe setting a threahould with otsu would wield better results Apr 2 '19 at 22:00
• @PedroHenriqueMonforte what is otsu and how do I set a threshold with it? It wouldn't be a numerical feature, I need the algorithm to know where to separate. Apr 2 '19 at 22:01
• So, otsu is a threshoulding algorithm that finds the value that minimizes the intra-class variance and maximazes the inter-class variance. Sorry if that is not clear, never defined otsu's algorithm in English. It is common in image processing and it is kind of a clustering algorithm. Apr 2 '19 at 22:06
• Give me a minute to find a implementation for you Apr 2 '19 at 22:07
• Please don't call-post duplicates: stackoverflow.com/q/55476863/1060350 Apr 2 '19 at 22:14