# 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_
print(bc_df.head(8))


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, 2019 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, 2019 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, 2019 at 22:06
• Give me a minute to find a implementation for you Apr 2, 2019 at 22:07
• Please don't call-post duplicates: stackoverflow.com/q/55476863/1060350 Apr 2, 2019 at 22:14

## 1 Answer

Welcome, Dmitry!

A very first red alarm ... If you are doing clustering, then let it tell you who is similar to who! If you already know your clusters, then why are you doing clustering indeed?!

And about the algorithms: Yes there are many more, for example, Spectral Clustering which makes use of different similarity measures (via Gaussian Kernel or K-NN connectivity matrix) to find clusters or DBSCAN which is based on similarity according to the density of data points.

If you needed more help please drop a comment here!

Good Luck!

• Great comment :) but I don't know the clusters, rather, I know the principles how I want my clusters to be selected. Do you know how I could apply these other algorithms you mentioned to this problem? Thanks! Apr 2, 2019 at 14:36
• If you mean programming-wise, there are as simple as kmeans. All of them have a fit method which gets your data and outputs the model. Documentation will defenitely help. Everything is exactly the same e.g. "clustering = DBSCAN(eps=3, min_samples=2).fit(bc_df[['bc']])" Apr 2, 2019 at 14:41