<jupyter_start><jupyter_text>Clustering algorithms help identify distinct groups of data. An example is to use clustering to group customers based on their behavior. There are many clustering algorithms, but the most commonly used algorithm is K-Means. In this video, I'll show you how to use K-Means clustering to find some underlying structure in your data.   Import Libraries<jupyter_code>%matplotlib inline

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

from sklearn.cluster import KMeans<jupyter_output><empty_output><jupyter_text>Load the DatasetThe Iris dataset is one of datasets scikit-learn comes with that do not require the downloading of any file from some external website. The code below loads the iris dataset.<jupyter_code>data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
df.head()<jupyter_output><empty_output><jupyter_text>Arrange Data into Features Matrix K-Means is considered an unsupervised learning algorthm. This means you only need a features matrix. In the iris dataset, there are four features. In this notebook, the features matrix will only be two features as it is easier to visualize clusters in two dimensions.<jupyter_code>features = ['petal length (cm)','petal width (cm)']

# Create features matrix
x = df.loc[:, features].values<jupyter_output><empty_output><jupyter_text>The variable y below is for demonstational purposes in this notebook and not needed if you want to do K-Means.<jupyter_code>y = data.target<jupyter_output><empty_output><jupyter_text>Standardize the DataKMeans is effected by scale so you need to scale the features in the data before using KMeans. You can transform the data onto unit scale (mean = 0 and variance = 1) for better performance. Scikit-Learn's `StandardScaler` helps standardize the dataset’s features.<jupyter_code># Apply Standardization to features matrix X
x = df.loc[:, features].values
x = StandardScaler().fit_transform(x)<jupyter_output><empty_output><jupyter_text>Plot Data to Estimate Number of ClustersIf your data is 2 or three dimensional, it is a good idea to plot your data before clustering. Hopefully you can see if there are any natural looking clusters.<jupyter_code># Plot 
pd.DataFrame(x, columns = features).plot.scatter('petal length (cm)','petal width (cm)' )

# Add labels
plt.xlabel('petal length (cm)');
plt.ylabel('petal width (cm)');<jupyter_output><empty_output><jupyter_text>KMeans ClusteringIn K-Means clustering, you need to specify the number of clusters (k) you want. In Scikit-Learn, this parameter is called `n_clusters`. In the case of the code below, the number of clusters is set to 3 because most people who use the Iris dataset happen to know there are three species.<jupyter_code># Make an instance of KMeans with 3 clusters
kmeans = KMeans(n_clusters=3, random_state=1)

# Fit only on a features matrix
kmeans.fit(x)
# Get labels and cluster centroids
labels = kmeans.labels_
centroids = kmeans.cluster_centers_<jupyter_output><empty_output><jupyter_text>Visually Evaluate the Clusters<jupyter_code>x = pd.DataFrame(x, columns = features)
colormap = np.array(['r', 'g', 'b'])
plt.scatter(x['petal length (cm)'], x['petal width (cm)'], c=colormap[labels])
plt.scatter(centroids[:,0], centroids[:,1], s = 300, marker = 'x', c = 'k')

plt.xlabel('petal length (cm)')
plt.ylabel('petal width (cm)');<jupyter_output><empty_output><jupyter_text>Visually Evaluate the Clusters and Compare Species<jupyter_code>plt.figure(figsize=(8,4))

plt.subplot(1, 2, 1)
plt.scatter(x['petal length (cm)'], x['petal width (cm)'], c=colormap[labels])
plt.xlabel('petal length (cm)')
plt.ylabel('petal width (cm)');
plt.title('K-Means Clustering (k = 3)')
 
plt.subplot(1, 2, 2)
plt.scatter(x['petal length (cm)'], x['petal width (cm)'], c=colormap[y], s=40)
plt.xlabel('petal length (cm)')
plt.ylabel('petal width (cm)');
plt.title('Flower Species')

plt.tight_layout()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10097799511002445, 'fraction_numerical': 0.005623471882640587, 'mean_word_length': 6.22791519434629, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '106016', 'license': 'MIT', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1286, 'n_words': 458, 'path': 'scikitlearn/Ex_Files_ML_SciKit_Learn/Exercise Files/03_02_KMeans.ipynb', 'repo_name': 'aswinjose89/MachineLearningTutorial', 'size': '4090'}