June 2020
Intermediate to advanced
382 pages
11h 39m
English
Let's learn how we can code a hierarchical algorithm in Python:
We will first import AgglomerativeClustering from the sklearn.cluster library, along with the pandas and numpy packages:
from sklearn.cluster import AgglomerativeClusteringimport pandas as pdimport numpy as np
Then we will create 20 data points in a two-dimensional problem space:
dataset = pd.DataFrame({ 'x': [11, 21, 28, 17, 29, 33, 24, 45, 45, 52, 51, 52, 55, 53, 55, 61, 62, 70, 72, 10], 'y': [39, 36, 30, 52, 53, 46, 55, 59, 63, 70, 66, 63, 58, 23, 14, 8, 18, 7, 24, 10]})
Then we create the hierarchical cluster by specifying the hyperparameters. We use the fit_predict function to actually process the algorithm:
cluster = AgglomerativeClustering(n_clusters=2, ...Read now
Unlock full access