How to do Agglomerative Clustering in Python?
MACHINE LEARNING RECIPES

How to do Agglomerative Clustering in Python?

How to do Agglomerative Clustering in Python?

This recipe helps you do Agglomerative Clustering in Python

0
In [2]:
## How to do Agglomerative Clustering in Python
def Snippet_156():
    print()
    print(format('How to do Agglomerative Clustering in Python','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    from sklearn import datasets
    from sklearn.preprocessing import StandardScaler
    from sklearn.cluster import AgglomerativeClustering
    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt

    # Load data
    iris = datasets.load_iris()
    X = iris.data; data = pd.DataFrame(X)
    cor = data.corr()
    sns.heatmap(cor, square = True); plt.show()

    # Standarize features
    scaler = StandardScaler()
    X_std = scaler.fit_transform(X)
    # Conduct Agglomerative Clustering
    clt = AgglomerativeClustering(linkage='complete',
                affinity='euclidean', n_clusters=5)

    # Train model
    model = clt.fit(X_std)

    # Predict clusters
    clusters = pd.DataFrame(model.fit_predict(X_std))
    data['Cluster'] = clusters

    # Visualise cluster membership
    fig = plt.figure(); ax = fig.add_subplot(111)
    scatter = ax.scatter(data[0],data[1], c=data['Cluster'],s=50)
    ax.set_title('Agglomerative Clustering')
    ax.set_xlabel('X0'); ax.set_ylabel('X1')
    plt.colorbar(scatter); plt.show()

Snippet_156()
*******************How to do Agglomerative Clustering in Python*******************

Relevant Projects

Data Science Project-All State Insurance Claims Severity Prediction
Data science project in R to develop automated methods for predicting the cost and severity of insurance claims.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.