์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
- ๋ฐ์ดํฐ๋ถ๊ท ํ
- PCA
- t-test
- ํฌ๋กค๋ง
- ๋ฐ์ดํฐ๋ถ์
- ๋ฐ์ดํฐ๋ถ์์ค์ ๋ฌธ๊ฐ
- ๊ตฐ์งํ
- ADP
- ADsP
- numpy
- iloc
- dataframe
- ์ธ๋์ํ๋ง
- Lambda
- ๋ ๋ฆฝํ๋ณธ
- datascience
- ๋น ๋ฐ์ดํฐ
- opencv
- ๋ฐ์ดํฐ๋ถ์์ ๋ฌธ๊ฐ
- ์ฃผ์ฑ๋ถ๋ถ์
- LDA
- ํ์ด์ฌ
- Python
- ์๋ํด๋ผ์ฐ๋
- ๋์ํ๋ณธ
- DBSCAN
- ํ ์คํธ๋ถ์
- ๋น ๋ฐ์ดํฐ๋ถ์๊ธฐ์ฌ
- pandas
- ์ค๋ฒ์ํ๋ง
Data Science LAB
[Python] PCA ์์ ๋ณธ๋ฌธ
2022.03.05 - [Python] PCA(Principal Component Analysis)
์ง๋ ํฌ์คํ ์์ ๊ณต๋ถํ์๋ PCA๋ฅผ ๋ค๋ฅธ ๋ฐ์ดํฐ์ ์ ์ด์ฉํ์ฌ ์ค์ตํด ๋ณด๋ ค๊ณ ํ๋ค.
๋ฐ์ดํฐ์ ๋ค์ด๋ก๋
https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients
๋ฐ์ดํฐ์ ๋ก๋ฉ
import pandas as pd
df = pd.read_excel('default of credit card clients.xls',header=1,sheet_name='Data').iloc[0:,1:]
print(df.shape)
df.head()
#DataFrame์ผ๋ก ๋ณํ
df.rename(columns = {'PAY_0':'PAY1','default payment next month':'default'},inplace=True)
y_target = df['default']
X_features = df.drop('default',axis=1)
์์ฑ๊ฐ์ ์๊ด๋๋ฅผ heatmap์ผ๋ก ์๊ฐํ
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
corr = X_features.corr()
plt.figure(figsize=(14,14))
sns.heatmap(corr,annot=True,fmt='.1g')
BILL_AMTI1 ~ BILL_AMTI6์ 6๊ฐ ์์ฑ๋ผ๋ฆฌ์ ์๊ด๋๊ฐ ๋๋ถ๋ถ 0.9 ์ด์์ผ๋ก ๋งค์ฐ ๋์ ๊ฒ์ ์ ์ ์๋ค.
๋ํ, PAY_1 ~ 6์ ์๊ด๋ ์ญ์ ๋๋ค. ์ด๋ ๊ฒ ๋์ ์๊ด๋๋ฅผ ๊ฐ์ง ์์ฑ๋ค์ ์์์ PCA๋ง์ผ๋ก๋ ์์ฐ์ค๋ฝ๊ฒ ์ด ์์ฑ๋ค์ ๋ณ๋์ฑ์ ์์ฉํ ์ ์๋ค.
bill_amti1~6์ ์์ฑ๋ผ๋ฆฌ ์๊ด๋๊ฐ ๋งค์ฐ ๋์ PCA๋ก 2๊ฐ ์์ฑ์ผ๋ก ๋ณํ
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
#BILL_AMT 6๊ฐ ์์ฑ๋ช
์์ฑ
cols_bill = ['BILL_AMT'+str(i) for i in range(1,7)]
print("๋์ ์์ฑ๋ช
: ",cols_bill)
#2๊ฐ์ PCA ์์ฑ์ ๊ฐ์ง PCA ๊ฐ์ฒด ์์ฑ, explained_variance_ratio ๊ณ์ฐ์ ์ํด fit() ํธ์ถ
scaler = StandardScaler()
df_cols_scaled = scaler.fit_transform(X_features[cols_bill])
pca = PCA(n_components=2)
pca.fit(df_cols_scaled)
print("PCA Components๋ณ ๋ณ๋์ฑ : ",pca.explained_variance_ratio_)
2๊ฐ์ PCA Components ๋ง์ผ๋ก 6๊ฐ์ ์์ฑ์ ๋ณ๋์ฑ์ ์ฝ 95%์ด์ ์ค๋ช ํ ์ ์์ผ๋ฉฐ ํนํ ์ฒซ ๋ฒ์งธ PCA ์ถ์ 90%์ ๋ณ๋์ฑ์ ์์ฉํ ์ ์๋ค.
์๋ณธ ๋ฐ์ดํฐ์ VS PCA ๋ณํ ๋ฐ์ดํฐ์ ๋ถ๋ฅ ์์ธก ๊ฒฐ๊ณผ ๋น๊ต
- ์๋ณธ๋ฐ์ดํฐ์ ์ ๋๋คํฌ๋ ์คํธ ์ ์ฉ
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
rf = RandomForestClassifier(n_estimators=300,random_state = 156)
scores = cross_val_score(rf,X_features,y_target,scoring='accuracy',cv=3)
print("CV=3์ธ ๊ฒฝ์ฐ์ ๊ฐ๋ณ Fold์ธํธ๋ณ ์ ํ๋ : ",scores)
print("ํ๊ท ์ ํ๋ : {0:.4f}".format(np.mean(scores)))
-PCA ๋ณํ ๋ฐ์ดํฐ์ ์ ๋๋คํฌ๋ ์คํธ ์ ์ฉ
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
df_scaled = scaler.fit_transform(X_features)
pca = PCA(n_components = 6)
df_pca = pca.fit_transform(df_scaled)
scores_pca = cross_val_score(rf,df_pca,y_target,scoring='accuracy',cv=3)
print("CV=3์ธ ๊ฒฝ์ฐ์ PCA ๊ฐ๋ณ Fold ์ธํธ๋ณ ์ ํ๋ : ",scores_pca)
print("PCA ๋ณํ ๋ฐ์ดํฐ ์ธํธ ํ๊ท ์ ํ๋ : {0:.4f}".format(np.mean(scores_pca)))
์ ์ฒด 23๊ฐ ์์ฑ์ค 6๊ฐ์ PCA ์ปดํฌ๋ํธ๋ง์ผ๋ก๋ ์๋ณธ ๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ ๋ถ๋ฅ ์์ธก ๊ฒฐ๊ณผ๋ณด๋ค ์ฝ 1~2%์ ์์ธก ์ฑ๋ฅ ์ ํ๊ฐ ๋ฐ์ํ์๋ค. ์ ์ฒด ์์ฑ์ ¼์์ค์ผ๋ก ์ด์ ๋ ์์น์ ์์ธก ์ฑ๋ฅ์ ์ ์งํ ์ ์๋ ๊ฒ์ PCA์ ์ฑ๋ฅ์ ์ ๋ํ๋ธ๋ค.
'๐ Machine Learning > ์ฐจ์ ์ถ์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python]NMF (0) | 2022.03.08 |
---|---|
[Python] SVD(Singular Value Decomposition) (0) | 2022.03.07 |
[Python] LDA(Linear Discriminant Analysis) (0) | 2022.03.07 |
[Python] PCA(Principal Component Analysis) (0) | 2022.03.05 |