<jupyter_start><jupyter_text>Customer segementation using K means cluster algorithm<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.cluster import KMeans<jupyter_output><empty_output><jupyter_text>data collection and analysis<jupyter_code>df=pd.read_csv("Mall_Customers.csv")
customer_data=df
customer_data.head(5)
customer_data.shape
customer_data.info()
## gives missing data
customer_data.isnull().sum() 
## choosing income and spending score
X=customer_data.iloc[:,[3,4]].values
## : -> rows, [3,4] -> column
X
# Choosing clusters
## WCSS -> within clusters sum of squares
WCSS =[]
for i in range(1,11):
    kmeans=KMeans(n_clusters=i, init='k-means++', random_state=42)
    kmeans.fit(X)
    WCSS.append(kmeans.inertia_)
#plot
sns.set()
plt.plot(range(1,11),WCSS)
plt.title("The Elbow point graph")
plt.xlabel("No of clusters")
plt.ylabel("WCSS")
plt.show()
## elbow points are calculated where after that point there is no sudden drop , here we can take it as 5
##trainig the kmeans clustering model - unsupervised learning model
#optimum value =5
kmeans=KMeans(n_clusters=5,init='k-means++',random_state=2)

#return a label for each data point based on their cluster
Y=kmeans.fit_predict(X)
print(Y)
## visualizing the clusters
plt.figure(figsize=(7,7))
plt.scatter(X[Y==0,0],X[Y==0,1],s=50,c='violet',label='Cluster 1')
plt.scatter(X[Y==1,0],X[Y==1,1],s=50,c='cyan',label='Cluster 2')
plt.scatter(X[Y==2,0],X[Y==2,1],s=50,c='orange',label='Cluster 3')
plt.scatter(X[Y==3,0],X[Y==3,1],s=50,c='yellow',label='Cluster 4')
plt.scatter(X[Y==4,0],X[Y==4,1],s=50,c='pink',label='Cluster 5')

#plot centroids
plt.scatter(kmeans.cluster_centers_[:,0],kmeans.cluster_centers_[:,1],s=100,c='black',label='Centroid')
plt.title("Customer groups")
plt.xlabel("Annual Income")
plt.ylabel("Spendig Score")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1712938711367208, 'fraction_numerical': 0.030906233630172867, 'mean_word_length': 7.883720930232558, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, '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': '335710', 'license': 'MIT', 'n_tokens_mistral': 782, 'n_tokens_neox': 734, 'n_words': 146, 'path': 'Customer segmentation.ipynb', 'repo_name': 'naveeramesh/customer_segmentation', 'size': '1909'}