<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
data=pd.read_csv('/content/drive/MyDrive/Colab Notebooks/Mall_Customers.csv')
data.head()<jupyter_output><empty_output><jupyter_text><jupyter_code>L=['Age','Annual Income (k$)','Spending Score (1-100)']
for i in range(len(L)):
  plt.figure(figsize=(12,7))
  plt.subplot(3,1,i+1)
  sns.boxplot(data[L[i]])

data.info()
data.describe(percentiles=[0.95])
data.isnull().sum()
q1=data['Annual Income (k$)'].quantile(0.25)
q2=data['Annual Income (k$)'].quantile(0.75)
IQR=q2-q1
IQR


lower_limit=q1-1.5*IQR
upper_limit=q2+1.5*IQR
lower_limit
lower_limit=min(data['Annual Income (k$)'])
for i in data['Annual Income (k$)']:
  if i>upper_limit:
    i=upper_limit
    
sns.boxplot(data['Annual Income (k$)'])
upper_limit
data['Annual Income (k$)'].describe()
data['Annual Income (k$)']=np.where(data['Annual Income (k$)']>upper_limit,upper_limit,data['Annual Income (k$)'])   
data['Annual Income (k$)'].describe()
data.head()
for i in range(len(L)):
  plt.figure(figsize=(6,6))
  plt.subplot(3,1,i+1)
  sns.distplot(data[L[i]])
sns.histplot(x='Age',data=data)
data['Gender'].value_counts().plot(kind='bar')
sns.pairplot(data,corner=True)
data.iloc[:,3]
k=data.iloc[:,[3,4]].values
k
from sklearn.cluster import KMeans
inertia=[]
for i in range(1,11):
  Kmeans=KMeans(n_clusters=i,random_state=42,init='k-means++')
  Kmeans.fit(k)
  inertia.append(Kmeans.inertia_)
plt.plot(range(1,11),inertia)
inertia
data.info()
kmeans=KMeans(n_clusters=4,init='k-means++',random_state=42)
kmeans.fit(k)
Y=kmeans.predict(k)
Y
k[Y==0]
plt.figure(figsize=(12,12))

plt.scatter(k[Y==0,0],k[Y==0,1],s=30,c='red',label='Cluster1')
plt.scatter(k[Y==1,0],k[Y==1,1],s=30,c='blue',label='Cluster2')
plt.scatter(k[Y==2,0],k[Y==2,1],s=30,c='green',label='Cluster3')
plt.scatter(k[Y==3,0],k[Y==3,1],s=30,c='yellow',label='Cluster4')

for i in range(4):
  plt.scatter(np.mean(k[Y==i,0]),np.mean(k[Y==i,1]),s=100)
kmeans_5=KMeans(n_clusters=5,init='k-means++',random_state=42)
kmeans_5.fit(k)
Y=kmeans_5.predict(k)
Y
plt.figure(figsize=(12,12))

plt.scatter(k[Y==0,0],k[Y==0,1],s=30,c='red',label='Cluster1')
plt.scatter(k[Y==1,0],k[Y==1,1],s=30,c='blue',label='Cluster2')
plt.scatter(k[Y==2,0],k[Y==2,1],s=30,c='green',label='Cluster3')
plt.scatter(k[Y==3,0],k[Y==3,1],s=30,c='yellow',label='Cluster4')
plt.scatter(k[Y==4,0],k[Y==4,1],s=30,c='yellow',label='Cluster5')
for i in range(5):
  plt.scatter(np.mean(k[Y==i,0]),np.mean(k[Y==i,1]),s=100)

from google.colab import drive
drive.mount('/content/drive')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2441243366186505, 'fraction_numerical': 0.052312357846853674, 'mean_word_length': 9.349019607843138, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '131723', 'license': 'MIT', 'n_tokens_mistral': 1306, 'n_tokens_neox': 1224, 'n_words': 137, 'path': 'cluster_1_customer_segmentation.ipynb', 'repo_name': 'Lomesh2000/kmeans-clustering----customer-segmentation', 'size': '2638'}