Python uses Scikit born Hierarchical clustering
Preparation work:
Before using Scikit ear for Hierarchical clustering, we need to set up the Python environment and install the necessary libraries.
1. Install Python: You can download it from the official Python website( https://www.python.org )Download and install the appropriate Python version for your operating system.
2. Install the Scikit learn library: In the Python environment, you can use the pip command to install the Scikit learn library. Open a terminal or command prompt and enter the following command:
bash
pip install scikit-learn
3. Install other dependent libraries: When using the Hierarchical clustering algorithm, we also need to install some other libraries, such as NumPy and Matplotlib. Execute the following command to install:
bash
pip install numpy matplotlib
Dependent class libraries:
In the Hierarchical clustering task, we will use the sklearn.cluster.AgglomerativeClustering class in the Scikit-learn library to perform Hierarchical clustering.
Dataset introduction and download website:
For this example, let's use the Iris dataset from the UCI machine learning library. This dataset contains 150 samples, divided into 3 categories, each with 50 instances.
The dataset can be downloaded from this website: https://archive.ics.uci.edu/ml/datasets/iris
Sample data:
The Iris dataset contains four features: calyx length, calyx width, petal length, and petal width. Each sample has a corresponding category label, which is the variety of iris.
Complete sample code:
The following is a complete Python code example of Hierarchical clustering using iris dataset:
python
import numpy as np
from sklearn.cluster import AgglomerativeClustering
from sklearn.datasets import load_iris
#Load Iris Dataset
iris = load_iris()
X = iris.data
y = iris.target
#Execute Hierarchical clustering
clustering = AgglomerativeClustering(n_clusters=3).fit(X)
#Output clustering labels for each sample
Print ("Cluster label of sample:")
print(clustering.labels_)
This code example loads the Iris dataset and divides it into 3 clusters. Then, Hierarchical clustering is performed by calling the fit method of the AgglomerativeClustering class.
Finally, we print out the clustering labels for each sample.
Summary:
This example implements a simple Hierarchical clustering using the AgglomerativeClustering class of the Scikit-learn library. In practical applications, we can adjust the parameters and data sets according to the needs to carry out more complex Hierarchical clustering tasks.