<jupyter_start><jupyter_text>Clustering<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv("mall.csv")
df.head()
plt.figure(figsize=(5,5))
sns.scatterplot(df["INCOME"],df["SPEND"])
plt.show()<jupyter_output><empty_output><jupyter_text>KMeans<jupyter_code>from sklearn.cluster import KMeans
ke1 = KMeans(n_clusters=3, init="k-means++", n_init=10, max_iter=300,random_state=1)
ke1.fit(df)
ke1.inertia_
centroids = ke1.cluster_centers_
centroids
centroid_df = pd.DataFrame(centroids,columns=["X","Y"])
centroid_df
plt.figure(figsize=(5,5))
sns.scatterplot(df["INCOME"],df["SPEND"])
plt.scatter(centroid_df["X"],centroid_df["Y"], c='black',s=150, alpha=0.6);
plt.show()<jupyter_output><empty_output><jupyter_text>K values and Elbow method<jupyter_code>wcss = []

for k in range(1,30):
    ke2 = KMeans(n_clusters=k, init="k-means++", n_init=10, max_iter=300,random_state=1)
    ke2.fit(df)
    wcss.append(ke2.inertia_)
plt.plot(range(1,30),wcss, 'bx-')
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()
ke3 = KMeans(n_clusters=5, init="k-means++", n_init=10, max_iter=300,random_state=1)
labels = ke3.fit_predict(df)
ke3.inertia_
centroids = ke3.cluster_centers_
centroids
centroid_df = pd.DataFrame(centroids,columns=["X","Y"])
centroid_df
labels
df_new = pd.concat([df, pd.DataFrame({'labels':labels})], axis=1)
df_new.head()
plt.figure(figsize=(10,10))
sns.scatterplot(x=df["INCOME"],y=df["SPEND"],hue=labels,palette="viridis")
plt.scatter(centroid_df["X"],centroid_df["Y"], c='black',s=150, alpha=0.6);
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18625678119349007, 'fraction_numerical': 0.03375527426160337, 'mean_word_length': 9.31055900621118, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '637436', 'license': 'MIT', 'n_tokens_mistral': 754, 'n_tokens_neox': 717, 'n_words': 95, 'path': 'Clustering.ipynb', 'repo_name': 'laukik1/ML', 'size': '1659'}