<jupyter_start><jupyter_text>KMeans Notebook  *Author: <NAME>* *University of Chicago, CAPP'20*<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from sklearn.cluster import KMeans
from collections import Counter<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>mall = pd.read_csv("Mall_Customers.csv")
mall.head()<jupyter_output><empty_output><jupyter_text>Data Cleaning<jupyter_code>mall.isnull().sum(axis=0)<jupyter_output><empty_output><jupyter_text>No value missing.  Feature Selection<jupyter_code>X = mall.iloc[:, 3:5].values.astype(float)
X.shape<jupyter_output><empty_output><jupyter_text>Model Training<jupyter_code>def find_best_k(ax, X, max_k=10, seed=123, insert_vertical_at=None):
    wcss = []
    
    for i in range(1, max_k+1):
        kms = KMeans(n_clusters=i, init='k-means++', random_state=123)
        kms.fit(X)
        wcss.append(kms.inertia_)
    
    ax.plot(range(1, max_k+1), wcss)
    ax.set_title("KMeans Elbow: k from 1 to {}".format(max_k),
                 fontdict={'fontsize': 15, 'fontweight': "bold"})
    ax.set_xlabel("Number of clusters", fontsize=13)
    ax.set_ylabel("Within-cluster Sum of Squares (WCSS)", fontsize=13)
    
    ax.set_xticks(range(1, max_k+1))
    ax.get_xaxis().set_tick_params(direction='in', length=3)
    
    ax.set_yticks([])
    
    if insert_vertical_at:
        ax.axvline(x=insert_vertical_at, color='r', linestyle='--')
        print("The best number of clusters is {}.".format(insert_vertical_at))
fig = plt.figure(figsize=[12, 8])
ax = fig.add_subplot(1, 1, 1)

find_best_k(ax, X, max_k=20, insert_vertical_at=5)
plt.show()
kms = KMeans(n_clusters=5, init='k-means++', random_state=123)
y_kmeans = kms.fit_predict(X)
Counter(y_kmeans)
fig = plt.figure(figsize=[12, 8])

plt.scatter(X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], c="red", alpha=0.8,
            edgecolors="black", label="Standard")
plt.scatter(X[y_kmeans == 1, 0], X[y_kmeans == 1, 1], c="blue", alpha=0.8,
            edgecolors="black", label="Target")
plt.scatter(X[y_kmeans == 2, 0], X[y_kmeans == 2, 1], c="green", alpha=0.8,
            edgecolors="black", label="Careful")
plt.scatter(X[y_kmeans == 3, 0], X[y_kmeans == 3, 1], c="cyan", alpha=0.8,
            edgecolors="black", label="Sensible")
plt.scatter(X[y_kmeans == 4, 0], X[y_kmeans == 4, 1], c="magenta", alpha=0.8,
            edgecolors="black", label="Careless")

plt.title("Customers Segmented by KMeans",
          fontdict={'fontsize': 15, 'fontweight': "bold"})
plt.xlabel("Annual Income (k$)", fontsize=13)
plt.ylabel("Spending Score (1-100)", fontsize=13)
plt.legend(fontsize=12)

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17075892857142858, 'fraction_numerical': 0.03162202380952381, 'mean_word_length': 4.85838779956427, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '38238', 'license': 'MIT', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1064, 'n_words': 187, 'path': 'Part 4 - Clustering/Section 25 - Hierarchical Clustering/.ipynb_checkpoints/Hierarchical Clustering Notebook-checkpoint.ipynb', 'repo_name': 'KunyuHe/Machine-Learning-A-Z', 'size': '2690'}