9951 explained code solutions for 126 technologies


python-scikit-learnHierarchical clustering example


from sklearn import datasets, cluster

X, _ = datasets.load_iris(return_X_y=True)
model = cluster.AgglomerativeClustering(n_clusters = 3)

clusters = model.fit_predict(X)ctrl + c
from sklearn import

import module from scikit-learn

load_iris

loads Iris dataset

.AgglomerativeClustering(

create agglomerative clustering model (hierarchical clustering model)

n_clusters

number of clusters we want to see

.fit_predict(

trains model and returns predicted cluster labels


Usage example

from sklearn import datasets, cluster

X, _ = datasets.load_iris(return_X_y=True)
model = cluster.AgglomerativeClustering(n_clusters = 3)
clusters = model.fit_predict(X)
print(clusters)
output
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 2 2 2 2 0 2 2 2 2
 2 2 0 0 2 2 2 2 0 2 0 2 0 2 2 0 0 2 2 2 2 2 0 0 2 2 2 0 2 2 2 0 2 2 2 0 2
 2 0]