You are on page 1of 6

-To assist a supermarket increase their membership card conversion rate, explore

different clustering techniques and perform a customer segmentation analysis.

-Customer segmentation describes the process of identifying similar groups of


customers based on commonalities like shopping preferences and purchasing history
and allows companies to market to each group more effectively.

-Please develop several clustering models to gain a better understanding of the types of
customers the company has and identify strategies to increase their membership
conversion rate, including K-Means Clustering, Hierarchical Clustering, and DBSCAN.

In [1]: #K-Means Clastering


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

In [2]: kevin = pd.read_csv('Mall_Customers.csv')


kevin.head()

Out[2]: CustomerID Genre Age Annual Income (k$) Spending Score (1-100)

0 1 Male 19 15 39

1 2 Male 21 15 81

2 3 Female 20 16 6

3 4 Female 23 16 77

4 5 Female 31 17 40

In [3]: x = kevin.iloc[:, [3,4]].values

In [4]: from sklearn.cluster import KMeans

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_)

plt.plot(range(1,11), wcss)
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
In [5]: kmeans = KMeans(n_clusters = i, init = 'k-means++', random_state = 42)
y_kmeans = kmeans.fit_predict(x)

C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(

In [6]: y_kmeans

Out[6]: array([8, 7, 3, 7, 8, 7, 3, 7, 3, 7, 3, 7, 3, 7, 3, 7, 8, 7, 8, 7, 8, 7,
3, 7, 3, 7, 8, 0, 8, 7, 3, 7, 3, 7, 3, 7, 3, 7, 8, 7, 8, 7, 8, 0,
8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 1, 4, 1, 4, 9, 4, 9, 4,
1, 4, 9, 4, 9, 4, 9, 4, 9, 4, 1, 4, 9, 4, 1, 4, 1, 4, 9, 4, 9, 4,
9, 4, 9, 4, 9, 4, 1, 4, 9, 4, 1, 4, 9, 4, 1, 4, 9, 4, 9, 4, 9, 4,
9, 4, 9, 4, 1, 2, 6, 2, 1, 2, 6, 2, 6, 2, 6, 2, 6, 2, 6, 2, 6, 2,
6, 2])

In [7]: plt.scatter(x[y_kmeans == 0, 0], x[y_kmeans == 0,1], s= 100, c= 'blue', label =


plt.scatter(x[y_kmeans == 1, 0], x[y_kmeans == 1,1], s= 100, c= 'yellow', label
plt.scatter(x[y_kmeans == 2, 0], x[y_kmeans == 2,1], s= 100, c= 'red', label = '
plt.scatter(x[y_kmeans == 3, 0], x[y_kmeans == 3,1], s= 100, c= 'green', label =
plt.scatter(x[y_kmeans == 4, 0], x[y_kmeans == 4,1], s= 100, c= 'orange', label
plt.scatter(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1], s=300 ,
plt.title('clusters of customers')
plt.xlabel('Annual Income')
plt.ylabel('Spending Score')
plt.legend()
plt.show()
In [8]: from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler

In [9]: #praproses
scaler = StandardScaler()
x = scaler.fit_transform(x)
x[:5]

Out[9]: array([[-1.73899919, -0.43480148],


[-1.73899919, 1.19570407],
[-1.70082976, -1.71591298],
[-1.70082976, 1.04041783],
[-1.66266033, -0.39597992]])

In [10]: #Evaluasi
#sum of Squared errors

sse = []
index = range(1, 11)
for i in index:
kmeans = KMeans(n_clusters=i, random_state=42)
kmeans.fit(x)
sse_ = kmeans.inertia_
sse.append(sse_)
print(i, sse_)
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(
1 399.99999999999994
2 270.70426664306717
3 157.70400815035947
4 108.92131661364357
5 65.56840815571681
6 55.057348270386
7 44.91118554999014
8 37.7364034625416
9 32.37525368158286
10 29.090568897369717
C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklea
rn\cluster\_kmeans.py:870: FutureWarning: The default value of `n_init` will ch
ange from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress
the warning
warnings.warn(

In [11]: plt.plot(index, sse)


plt.xlabel('n_cluster')
plt.ylabel('SSE')
plt.show()

In [ ]:

You might also like