简体   繁体   中英

Clustering by minimum-spanning-tree in python

I have an array (X), contains scores values of 10 users with their score. I used Minimum Spanning Tree Clustering (MST) to cluster the users based on their values. MST did not cluster the data, return 0 clusters instead!!!!

The following code:

import warnings
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn_extra.cluster import KMedoids
from tabulate import tabulate
from termcolor import colored, cprint 
from prettytable import PrettyTable
from mst_clustering import MSTClustering
%matplotlib inline
warnings.filterwarnings('ignore')

X = np.array([0.85142858,0.85566274,0.85364912,0.81536489,0.84929932,0.85042336,0.84899714,
         0.82019115, 0.86112067,0.8312496 ])
X=X.reshape(-1, 1)

MST = MSTClustering(cutoff_scale=2, approximate=False)
labels = MST.fit_predict(X)

dfMST= pd.DataFrame(zip(labels,X))
dfMST = dfMST.rename({0: 'cluster', 1: 'values'}, axis=1)
dfMST['user'] = dfMST.index
dfMST = dfMST[['cluster', 'user',  'values']]
print ('clustering the data using MST')
print(tabulate(dfMST, headers='keys', tablefmt='psql')) 

produced the output as follows:

clustering the data using MST
+----+-----------+--------+----------+
|    |   cluster |   user |   values |
|----+-----------+--------+----------|
|  0 |         0 |      0 | 0.851429 |
|  1 |         0 |      1 | 0.855663 |
|  2 |         0 |      2 | 0.853649 |
|  3 |         0 |      3 | 0.815365 |
|  4 |         0 |      4 | 0.849299 |
|  5 |         0 |      5 | 0.850423 |
|  6 |         0 |      6 | 0.848997 |
|  7 |         0 |      7 | 0.820191 |
|  8 |         0 |      8 | 0.861121 |
|  9 |         0 |      9 | 0.83125  |
+----+-----------+--------+----------+

How could I cluster users by MST in the correct way? should I suppose specify other cutoff scale parameters?

I have an array (X), contains scores values of 10 users with their score. I used Minimum Spanning Tree Clustering (MST) to cluster the users based on their values. MST did not cluster the data, return 0 clusters instead!!!!

The following code:

import warnings
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn_extra.cluster import KMedoids
from tabulate import tabulate
from termcolor import colored, cprint 
from prettytable import PrettyTable
from mst_clustering import MSTClustering
%matplotlib inline
warnings.filterwarnings('ignore')

X = np.array([0.85142858,0.85566274,0.85364912,0.81536489,0.84929932,0.85042336,0.84899714,
         0.82019115, 0.86112067,0.8312496 ])
X=X.reshape(-1, 1)

MST = MSTClustering(cutoff_scale=2, approximate=False)
labels = MST.fit_predict(X)

dfMST= pd.DataFrame(zip(labels,X))
dfMST = dfMST.rename({0: 'cluster', 1: 'values'}, axis=1)
dfMST['user'] = dfMST.index
dfMST = dfMST[['cluster', 'user',  'values']]
print ('clustering the data using MST')
print(tabulate(dfMST, headers='keys', tablefmt='psql')) 

produced the output as follows:

clustering the data using MST
+----+-----------+--------+----------+
|    |   cluster |   user |   values |
|----+-----------+--------+----------|
|  0 |         0 |      0 | 0.851429 |
|  1 |         0 |      1 | 0.855663 |
|  2 |         0 |      2 | 0.853649 |
|  3 |         0 |      3 | 0.815365 |
|  4 |         0 |      4 | 0.849299 |
|  5 |         0 |      5 | 0.850423 |
|  6 |         0 |      6 | 0.848997 |
|  7 |         0 |      7 | 0.820191 |
|  8 |         0 |      8 | 0.861121 |
|  9 |         0 |      9 | 0.83125  |
+----+-----------+--------+----------+

How could I cluster users by MST in the correct way? should I suppose specify other cutoff scale parameters?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM