Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Quant
- 비트코인
- 실기
- sarima
- lstm
- 데이터분석
- 파트5
- 변동성돌파전략
- Python
- 백테스트
- SQL
- 파이썬
- PolynomialFeatures
- 파이썬 주식
- Programmers
- 볼린저밴드
- 코딩테스트
- 빅데이터분석기사
- GridSearchCV
- Crawling
- hackerrank
- 주식
- backtest
- 데이터분석전문가
- ADP
- 토익스피킹
- 프로그래머스
- TimeSeries
- docker
- randomforest
Archives
- Today
- Total
데이터 공부를 기록하는 공간
[clustering] Mall_Customers 본문
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
df = pd.read_csv('./Mall_Customers/Mall_Customers.csv')
print(df.shape)
df.head(3)
df = df.rename(columns = {"Annual Income (k$)": "income",
"Spending Score (1-100)":"score", "Gender":"gender", "Age":"age"})
sns.pairplot(df, hue='gender')
df.drop('CustomerID',axis=1,inplace=True)
plt.figure(figsize=(14,6))
sns.scatterplot(data=df, x='income',y='score',hue='gender')
for col in ['age','income','score']:
fig, ax= plt.subplots(figsize=(5,3))
sns.boxplot(data=df, x='gender', y=col,ax=ax)
ax.set_title(f"{col}")
plt.show()
X = df.drop(columns='gender',axis=1)
from sklearn.cluster import KMeans
clusters = []
for i in range(1,11):
km = KMeans(n_clusters=i).fit(X)
clusters.append(km.inertia_)
fig, ax = plt.subplots(figsize=(12,8))
sns.lineplot(x=list(range(1,11)), y=clusters, ax=ax)
ax.set_title("Searching for Elbow")
ax.set_xlabel("Clusters")
ax.set_ylabel("Inertia")
ax.annotate("Possible Elbow Point", xy=(3,140000), xytext=(3,50000), xycoords='data', arrop)
km3 = KMeans(n_clusters=3).fit(X)
X['labels'] = km3.labels_
plt.figure(figsize=(12,8))
sns.scatterplot(X['income'],X['score'], hue=X['labels'], palette=sns.color_palette("hls",3))
sns.scatterplot(km3.cluster_centers_[:,1],km3.cluster_centers_[:,2],color='orange',s=100)
plt.title("KMeans with 3 Clusters")
plt.show()
km5 = KMeans(n_clusters=5).fit(X)
X['labels'] = km5.labels_
plt.figure(figsize=(12,8))
sns.scatterplot(X['income'],X['score'], hue=X['labels'], palette=sns.color_palette("hls",5))
sns.scatterplot(km5.cluster_centers_[:,1],km5.cluster_centers_[:,2],color='orange',s=100)
plt.title("KMeans with 5 Clusters")
plt.show()
fig = plt.figure(figsize=(20,6))
ax = fig.add_subplot(121)
sns.swarmplot(x='labels',y='income', data=X, ax=ax)
ax.set_title("Labels according to income")
ax = fig.add_subplot(122)
sns.swarmplot(x='labels',y='score', data=X, ax=ax)
ax.set_title("Labels according to score")
# Agglomerative
from sklearn.cluster import AgglomerativeClustering
agglom = AgglomerativeClustering(n_clusters=5, linkage='average').fit(X)
X['labels'] = agglom.labels_
plt.figure(figsize=(12,8))
sns.scatterplot(data=X, x='income', y='score', hue='labels',
palette=sns.color_palette("hls",len(np.unique(X.labels))))
# Dendrogram
import scipy.cluster.hierarchy as sch
fig = plt.figure(figsize=(20,6))
ax = fig.add_subplot(111)
dendrogram = sch.dendrogram(sch.linkage(X, method = 'ward'),ax=ax)
ax.set_title('Dendrogam', fontsize = 20)
ax.set_xlabel('Customers')
ax.set_ylabel('Ecuclidean Distance')
plt.show()
'STUDY > ADP, 빅데이터분석기사' 카테고리의 다른 글
[arima] smp (0) | 2021.03.21 |
---|---|
[pca] iris (0) | 2021.03.21 |
[classification] STAY or LEAVE (0) | 2021.03.21 |
[ARIMA] airplane (0) | 2021.03.20 |
[classification] titanic (0) | 2021.03.20 |
Comments