Unsupervised Learning in Python
Benjamin Wilson
Director of Research at lateral.io
fcluster()
functionfrom scipy.cluster.hierarchy import linkage mergings = linkage(samples, method='complete') from scipy.cluster.hierarchy import fcluster
labels = fcluster(mergings, 15, criterion='distance') print(labels)
[ 9 8 11 20 2 1 17 14 ... ]
Given a list of strings country_names
:
import pandas as pd
pairs = pd.DataFrame({'labels': labels, 'countries': country_names})
print(pairs.sort_values('labels'))
countries labels
5 Belarus 1
40 Ukraine 1
...
36 Spain 5
8 Bulgaria 6
19 Greece 6
10 Cyprus 6
28 Moldova 7
...
Unsupervised Learning in Python