How to do Affinity based Clustering in Python?
MACHINE LEARNING RECIPES

How to do Affinity based Clustering in Python?

How to do Affinity based Clustering in Python?

This recipe helps you do Affinity based Clustering in Python

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

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    from sklearn import datasets
    from sklearn.preprocessing import StandardScaler
    from sklearn.cluster import AffinityPropagation
    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()

    fig = plt.figure(figsize=(10,10));
    sns.heatmap(cor, square = True); plt.show()

    # Standarize features
    scaler = StandardScaler()
    X_std = scaler.fit_transform(X)

    # Conduct AffinityPropagation Clustering
    clt = AffinityPropagation(damping=0.5, max_iter=500, affinity='euclidean')

    # Train model
    model = clt.fit(X_std)
    n_clusters_ = len(model.cluster_centers_indices_)
    print('Number of Clusters: ',n_clusters_)

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

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

Snippet_158()
******************How to do Affinity based Clustering in Python*******************
Number of Clusters:  9

Relevant Projects

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

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.

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.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.