<jupyter_start><jupyter_text>Importing Libraries<jupyter_code>import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Importing File<jupyter_code>df = pd.read_csv('mall customers.csv')
df
df=df.rename(columns={'Annual Income (k$)': 'Income', 'Spending Score (1-100)': 'Score'})
df=df.drop('CustomerID',axis=1)
df<jupyter_output><empty_output><jupyter_text>Data Visualization<jupyter_code>plt.scatter(df.Age, df.Score)
plt.scatter(df.Age, df.Score)
plt.scatter(df.Income, df.Score)
plt.ylabel('Income')
plt.xlabel('Score')
sns.pairplot(df[['Age', 'Income', 'Score']])<jupyter_output><empty_output><jupyter_text>Cluster - KMeans Algorithm<jupyter_code>import sklearn.cluster as cluster
kmeans = cluster.KMeans(n_clusters=5)
predicted_cluster = kmeans.fit_predict(df[['Income','Score']])
predicted_cluster
df['predicted_cluster']=predicted_cluster
df
centroid = kmeans.cluster_centers_
centroid
df0 = df[df.predicted_cluster==0]
df1 = df[df.predicted_cluster==1]
df2 = df[df.predicted_cluster==2]
df3 = df[df.predicted_cluster==3]
df4 = df[df.predicted_cluster==4]

plt.figure(figsize=(15,10))

plt.scatter(df0['Score'],df0['Income'],color='green',label='Cluster 0')
plt.scatter(df1['Score'],df1['Income'],color='orange',label='Cluster 1')
plt.scatter(df2['Score'],df2['Income'],color='blue',label='Cluster 2')
plt.scatter(df3['Score'],df3['Income'],color='red',label='Cluster 3')
plt.scatter(df4['Score'],df4['Income'],color='black',label='Cluster 4')

plt.scatter(centroid[:,0],centroid [:,1],color='magenta',marker="P",label='Centroids')

plt.legend()<jupyter_output><empty_output><jupyter_text>Hyper Tuning - Find the best Centroid value<jupyter_code>from sklearn.cluster import KMeans
wcss = []
cluster = range(1,15)
for k in range(1,15):
    km = KMeans(n_clusters=k)
    km.fit(df[['Score','Income']])
    wcss.append(km.inertia_)
wcss
plt.xlabel('Number of Clusters')
plt.ylabel('Sum of the Squared Error')
plt.plot(cluster, wcss)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17750611246943765, 'fraction_numerical': 0.021026894865525673, 'mean_word_length': 9.941176470588236, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '273839', 'license': 'MIT', 'n_tokens_mistral': 835, 'n_tokens_neox': 798, 'n_words': 112, 'path': 'class-11/Class-11(Practice).ipynb', 'repo_name': 'Anikcb/Machine_Learning_Daffodil', 'size': '2045'}