[์ ์ฒ๋ฆฌ] Data Transformation(๋ฐ์ดํฐ ๋ณํ) - ์ ๊ทํ
2022.03.04 - [๋ฐ์ดํฐ ๋ถ์/02. Data Processing] - [์ ์ฒ๋ฆฌ] Types of data processing ๋ฐ ๋ฐ์ดํฐ ํด๋ฆฌ๋ [์ ์ฒ๋ฆฌ] Types of data processing ๋ฐ ๋ฐ์ดํฐ ํด๋ฆฌ๋ Data Processing : Data Processing(๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ..
xod22.tistory.com
์ ๋ฒ ํฌ์คํ ์์ ๋ฐ์ดํฐ ๋ณํ์ ๋ํด ๊ณต๋ถ๋ฅผ ํด๋ณด์์ต๋๋ค.
์ด๋ฒ ํฌ์คํ ์์๋ ์ ์ฒ๋ฆฌ์ ์ธ๋ฒ์งธ ์ข ๋ฅ์ธ Data Reduction(๋ฐ์ดํฐ ์ถ์)์ ๋ํด ๊ณต๋ถํด๋ณด๋ ค๊ณ ํฉ๋๋ค!
1. Data Cleaning(๋ฐ์ดํฐ ํด๋ฆฌ๋) - ๊ฒฐ์ธก์น์ฒ๋ฆฌ,,
2. Data Transformation(๋ฐ์ดํฐ ๋ณํ) - ์ ๊ทํ,,,๋ฑ๋ฑ
3. Data Reduction(๋ฐ์ดํฐ ์ถ์) - ์ฐจ์์ถ์,,,
Data Reduction(๋ฐ์ดํฐ ์ถ์)
๋ฐ์ดํฐ๋ฅผ ์ถ์ํ๋ ๋ฐฉ๋ฒ์๋ ๋๊ฐ์ง๊ฐ ์๋ค.
1. Dimensionality reduction - ์ฐจ์์ถ์
2. Numerosity reduction - ๋ฐ์ดํฐ ํฌ๊ธฐ ์ค์
*๋จผ์ ์ฒซ๋ฒ์งธ ๋ฐฉ๋ฒ์ธ Dimensionality reduction์ ๋ํด ์ ์ด๋ณด๊ฒ ์ต๋๋ค..!
Dimensionality reduction(์ฐจ์์ถ์)
1. SVD
: ํ๋ ฌ A๋ฅผ m x m ํฌ๊ธฐ์ธ U, m x n ํฌ๊ธฐ์ธ ∑, n x n ํฌ๊ธฐ์ธ Vt ๋ก ํน์ด๊ฐ ๋ถํด(SVD)ํ๋ ๊ฒ์ Full SVD๋ผ๊ณ ํฉ๋๋ค.
ํ์ง๋ง Full SVD๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๋ ๋๋ญ ๋๋ค. ์ค์ ๋ก๋ ∑์ ๋น๋๊ฐ ๋ถ๋ถ๊ณผ ๋๊ฐ ์์ ์ค ํน์ด๊ฐ์ด 0์ธ ๋ถ๋ถ์ ๋ชจ๋ ์ ๊ฑฐํ๊ณ , ์ ๊ฑฐ๋ ∑์ ๋์๋๋ U์ V ์์๋ ํจ๊ป ์ ๊ฑฐํด ์ฐจ์์ ์ค์ธ ํํ๋ก SVD๋ฅผ ์ ์ฉํฉ๋๋ค. ์ด๋ฐ ๋ฐฉ์์ Truncated SVD๋ผ๊ณ ํฉ๋๋ค.
ํน์ด๊ฐ์ ๋ช๊ฐ๋ก ์ง์ ํ๋๋๋ฅผ ๋ค๋ฅด๊ฒ ์ค์ ํ ์ ์์ต๋๋ค.
~์ค์ต~
1. ํจํค์ง ์ํฌํธ
from numpy import array
from numpy import diag
from numpy import zeros
from scipy.linalg import svd
2. 5 by 10 matrix ์์ฑ
A=array([[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,32,33,34,35,36,37,38,39,40],[51,52,53,54,55,56,57,58,59,60]])
print(A)
3. SVD ์ํ
=> SVDํจ์๋ฅผ ํตํด 3๊ฐ์ ๋ฐํ๊ฐ์ ์ ์ฅํจ
U, s, VT = svd(A)
4. ๋ด์ ์ ํตํด ์๋ณธํ๋ ฌ๋ก ์๋ณตํ๊ธฐ!
~s๋ 1์ฐจ์ ๋ฒกํฐ๋ก ์ ์ฅ๋๋ฏ๋ก ๋ค์ ๋๊ฐํ๋ ฌ๋ก ๋ณต์~
# sํ๋ ฌ->1์ฐจ์์ด๋ฏ๋ก 0์ ํฌํจํ ๋๊ฐํ๋ ฌ๋ก ๋ณต์
#์ํ๋ ฌ
smatrix = zeros((len(s), len(s)))
#๋๊ฐํ๋ ฌ
smatrix[:len(s), :len(s)] = diag(s)
~k=4(ํน์ด๊ฐ 4๊ฐ๋ก ์ง์ )~
k=4
#k=4,3,2๋ก ๋ณ๊ฒฝํ์๋ ๊ฐ์ ๊ฒฐ๊ณผ!
smatrix=smatrix[:k, :k]
VT=VT[:k, :]
U=U[:,:k]
#๋ด์
B=U.dot(smatrix.dot(VT))
print(B)
#์๋ณธ ํ๋ ฌ๊ณผ ์ ํํ๊ฒ ์ผ์น
~k=1์ผ๋~
k=1
smatrix=smatrix[:k, :k]
VT=VT[:k, :]
U=U[:,:k]
#๋ด์
B=U.dot(smatrix.dot(VT))
print(B)
#์๋ณธํ๋ ฌ๊ณผ ์ผ์นํ์ง ์์!
-> k=1์ผ๋๋ ์๋ณธํ๋ ฌ๊ณผ ์ผ์นํ์ง ์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค..!
2. PCA
: ์ฃผ์ฑ๋ถ์ ๊ณ์ฐํ๊ณ ๋ฐ์ดํฐ๋ฅผ ์ค๋ช ํ๊ธฐ ์ํด ์ฒ์ ๋ช๊ฐ์ ์ฃผ์ฑ๋ถ๋ง์ ์ฌ์ฉํ๋ ํ๋ก์ธ์ค.
~์ค์ต~
iris๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ PCA ์ฃผ์ฑ๋ถ ๋ถ์์ ์ํํด๋ณด๊ฒ ์ต๋๋ค.
1. ํจํค์ง ์ํฌํธ ๋ฐ ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
from sklearn import datasets
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas as pd
iris=datasets.load_iris()
2. x,y ์ปฌ๋ผ ๋ฐ๋ก ์ ์ฅ
#y๋ฐ์ดํฐ๋ labels์ ์ ์ฅ
labels=pd.DataFrame(iris.target)
labels.columns=['labels']
#x๋ฐ์ดํฐ๋ data์ ์ ์ฅ
data=pd.DataFrame(iris.data, columns=['Sepal length', 'Sepal width', 'Petal length', 'Petal width'])
3. ์๋ฐ์ดํฐ plot
fig = plt.figure( figsize=(6,6))
ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)
ax.scatter(data['Sepal length'],data['Sepal width'],data['Petal length'],c=labels,alpha=0.5)
ax.set_xlabel('Sepal lenth')
ax.set_ylabel('Sepal width')
ax.set_zlabel('Petal length')
plt.show()
-> ๋ฐ์ดํฐ๋ฅผ ํํํ๊ธฐ ์ํด์ 3Dplot์ ํ์ฉํด์ผํจ...
4. pca ๋ถ์
~ํจํค์ง ์ํฌํธ~
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
import matplotlib.pyplot as plt
#์ ๊ทํ
scaler=StandardScaler()
#PCA
pca=PCA()
#pipeline๋ง๋ฌ
pipeline=make_pipeline(scaler,pca)
pipeline.fit(data)
#๋ฐ์ดํฐ ๋ถ์ฐ์ ๊ฐ์ฅ "์ ์ํ"์ฑ๋ถ์ด ๋ฌด์์ธ์ง ํ์ธ
from sklearn.decomposition import PCA
import pandas as pd
print(pca.explained_variance_ratio_) #๋ถ์ฐ
print(pd.DataFrame(pca.components_, columns=iris.feature_names))
์ฒซ๋ฒ์งธ ์ฃผ์ฑ๋ถ, ๋๋ฒ์งธ ์ฃผ์ฑ๋ถ์ผ๋ก 96%์ ๋ถ์ฐ์ ์ค๋ช ํ ์ ์์!
๊ฐ ์ปฌ๋ผ๋ณ ๊ธฐ์ฌ๋(?)๋ฅผ ํ์ธํ๋ฉด ์์ ํ์ ๊ฐ๋ค..!
5. ๋ถ์ฐ plotting ํด๋ณด๊ธฐ
features=range(pca.n_components_)
plt.bar(features, pca.explained_variance_)
plt.xlabel('PCA feature')
plt.ylabel('variance')
plt.xticks(features)
plt.show()
# ๋๊ฐ์ ์ปดํฌ๋ํธ๋ก ์ด 96%๋ฅผ ์ค๋ช
= > ๋๊ฐ์ ์ฃผ์ฑ๋ถ์ผ๋ก 96%๋ฅผ ์ค๋ช ํ๋ ๊ฒ์ ํ์ธ
6. PCA๋ก ๋๊ฐ์ ์ฃผ์ฑ๋ถ์ผ๋ก ์ถ์๋ ๋ฐ์ดํฐ plot
# ์๋ณธ ๋ฐ์ดํฐ์ ํน์ฑ 4๊ฐ๋ฅผ ์ฃผ์ฑ๋ถ 2๊ฐ๋ก ์ถ์ํด์ ๋ํ๋ผ ์ ์์
model=PCA(n_components=2)
pca_features=model.fit_transform(data)
xf=pca_features[:,0]
yf=pca_features[:,1]
plt.scatter(xf, yf, c=iris.target);
plt.show();
์ ๋ฐ์ดํฐ๋ 3D plot์ ์ด์ฉํ์ฌ plotting๋์์ง๋ง pca๋ก ์ฐจ์์ ์ถ์ํ ํ์ ๊ฐ๋จํ๊ฒ plotํ ์ ์๋ค!