Cust_Spend_Data.csv 파일을 통해서 고객의 의류소비, 음료소비, 음식소비 대이터를 통해서 비슷한 고객으로 그루핑 하자
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data/Cust_Spend_Data.csv')
X = df.iloc[:, 2:]
import scipy.cluster.hierarchy as sch
sch.dendrogram( sch.linkage(X, method='ward') )
plt.title('Dendrogram')
plt.xlabel('Customers')
plt.ylabel('Eculidaen Distances')
plt.show()
out
in
X.shape
out
(10, 5)
in
from sklearn.cluster import AgglomerativeClustering
y_pred = hc.fit_predict(X)
hc = []
for k in range(2, 10+1):
kmeans = KMeans(n_clusters= k , random_state = 33)
kmeans.fit_predict(X)
wcss.append(kmeans.inertia_)
wcss
out
[14500293.600000001,
5387740.75,
2187714.5,
854349.4166666667,
333469.3333333333,
125160.0]
in
plt.plot(range(2, 7+1), wcss)
plt.show()
out
in
kmeans = KMeans(n_clusters=4, random_state=76)
y_pred = kmeans.fit_predict(X)
y_pred
out
array([2, 0, 0, 0, 0, 3, 1, 1, 1, 1])
'Python > Machine Learning' 카테고리의 다른 글
python pandas resample (0) | 2022.05.12 |
---|---|
Machine Learning : 문자열 데이터를 숫자로 바꿔주는 CountVectorizer와 analyzer파라미터 (0) | 2022.05.10 |
Machine Learning : K-means 의 WCSS와 Elbow Method 설명 (0) | 2022.05.09 |
Machine Learning : WordCloud 라이브러리 사용법과 Stopwords 적용하는 방법 (0) | 2022.05.09 |
Machine Learning : Hierarchical Clustering (0) | 2022.05.09 |