How to do KMeans Clustering in Python?
MACHINE LEARNING RECIPES

How to do KMeans Clustering in Python?

How to do KMeans Clustering in Python?

This recipe helps you do KMeans Clustering in Python

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

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    from sklearn import datasets
    from sklearn.preprocessing import StandardScaler
    from sklearn.cluster import KMeans
    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()
    # Plot correlation using seaborn
    fig = plt.figure(figsize=(12,10));
    sns.heatmap(cor, square = True); plt.show()

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

    # Conduct KMeans Clustering
    clt = KMeans(n_clusters=3)

    # 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(figsize=(12,10)); ax = fig.add_subplot(111)
    scatter = ax.scatter(data[0],data[1], c=data['Cluster'],s=50)
    ax.set_title('KMeans Clustering')
    ax.set_xlabel('X0'); ax.set_ylabel('X1')
    plt.colorbar(scatter); plt.show()

Snippet_157()
**********************How to do KMeans Clustering in Python***********************

Relevant Projects

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

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.

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.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

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

Anomaly Detection Using Deep Learning and Autoencoders
Deep Learning Project- Learn about implementation of a machine learning algorithm using autoencoders for anomaly detection.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

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.

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.