Implementation of K Means Clustering for Customer Segmentation














































Implementation of K Means Clustering for Customer Segmentation



    Implementation of K Means Clustering for Customer Segmentation



Aim:


   To Implement the K means clustering for customer segmentation.



Theory:


    K-means clustering is one of the simplest and popular unsupervised machine learning algorithms.Typically, unsupervised algorithms make inferences from datasets using only input vectors without referring to known, or labelled, outcomes.

 

    K-means clustering aims to partition data into k clusters in a way that data points in the same cluster are similar and data points in the different clusters are farther apart. Similarity of two points is determined by the distance between them. The similarity measure is at the core of k-means clustering.



DATASET:
first download the dataset links in this link

https://drive.google.com/drive/folders/1tzFtW4qGA3nyYErD-zjvmSppTikIYyEy?usp=sharing


SOURCE CODE:

import pandas as pd
import matplotlib.pyplot as plt
data=pd.read_csv("Mall_Customers.csv")
data.head()
data.info() 
data.isnull().sum() 
from sklearn.cluster import KMeans 
wcss=[] 
for i in range(1,11): 
      kmeans=KMeans(n_clusters=i,init="k-means++")
      kmeans.fit(data.iloc[:,3:]) 
      wcss.append(kmeans.inertia_) 
plt.plot(range(1,11),wcss)
plt.xlabel("no of clusters") 
plt.ylabel("wcss") 
plt.title("Elbow Method")
km=KMeans(n_clusters=5)
km.fit(data.iloc[:,3:]) 
y_pred=km.predict(data.iloc[:,3:])
data["cluster"]=y_pred 
df0=data[data["cluster"]==0] 
df1=data[data["cluster"]==1] 
df2=data[data["cluster"]==2] 
df3=data[data["cluster"]==3]
df4=data[data["cluster"]==4] 
plt.scatter(df0["Annual Income"],df0["Score"],c="red",label="cluster0")
plt.scatter(df1["Annual Income"],df1["Score"],c="black",label="cluster1") plt.scatter(df2["Annual Income"],df2["Score"],c="blue",label="cluster2")
plt.scatter(df3["Annual Income"],df3["Score"],c="green",label="cluster3") plt.scatter(df4["Annual Income"],df4["Score"],c="magenta",label="cluster4")
plt.legend() plt.title("Customer Segments")







OUTPUT :




More Articles of Eedula Ganesh Reddy:

Name Views Likes
Create a Virtual Private Cloud (VPC) Using AWS Coursera answers 560 17
WORD ORDER PROBLEM 527 110
Palindrome related problem code - Python 555 123
Music Player - Python 347 31
Implementation of SVM For Spam Mail Detection - Python 429 17
Implementation of SVM For Spam Mail Detection - THEORY 510 209
Implementation of Movie Recommender System 322 57
Implementation of K Means Clustering for Customer Segmentation 534 224
Implementation of Decision Tree Regressor Model for Predicting the Salary of the Employee 304 72
Implementation of Decision Tree Classifier Model for Predicting Employee Churn 376 83
f-strings in Python 389 154
Implementation of Logistic Regression Using Gradient Descent - SOURCE CODE 303 149
Implementation of Logistic Regression Using Gradient Descent - THEORY 341 164
Implementation of Logistic Regression Model to Predict the Placement Status of Student 319 6
Implementation of Multivariate Linear Regression Model for Sales Prediction 1304 27
Colour Detection using Pandas & OpenCV -PYTHON CODE 339 17
Colour Detection using Pandas & OpenCV 352 46
Implementation of Simple Linear Regression Model for Predicting the Marks Scored 283 152
Python Program to generate one-time password (OTP) 464 117
Create a Screen recorder using Python 288 22
Face Detection Using Python 347 20
Linear Regression Lab 1350 180
MAKING A RECTANGULAR SPIRAL IN MS PAINT 310 118
pyautogui library Theory 315 25
Multiple message sending program 350 195
Sklearn 279 156
LinearRegression 315 167

Comments