9951 explained code solutions for 126 technologies


python-scipyHow do I use Python Scipy Linkage to cluster data?


To use Python Scipy Linkage to cluster data, you must first import the necessary libraries:

import numpy as np
from scipy.cluster.hierarchy import linkage, dendrogram

Next, you must create a dataset, which can be done with the following code:

data = np.array([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]])

Then, you can use the linkage function to generate the hierarchical clustering:

Z = linkage(data, 'ward')

The dendrogram function can then be used to create a visualization of the hierarchical clustering:

dendrogram(Z)

The output of the dendrogram function is a visualization of the hierarchical clustering:

dendrogram

Code explanation

  • import numpy as np: imports the numpy library and assigns it to the alias np
  • from scipy.cluster.hierarchy import linkage, dendrogram: imports the linkage and dendrogram functions from the scipy library
  • data = np.array([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]]): creates an array of data points
  • Z = linkage(data, 'ward'): performs hierarchical clustering on the data points using the ward method
  • dendrogram(Z): creates a visualization of the hierarchical clustering

Helpful links

Edit this code on GitHub