์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- ํฌ๋กค๋ง
- datascience
- ํ ์คํธ๋ถ์
- dataframe
- t-test
- ๋ฐ์ดํฐ๋ถ์์ ๋ฌธ๊ฐ
- ์ธ๋์ํ๋ง
- ์ฃผ์ฑ๋ถ๋ถ์
- ๋ฐ์ดํฐ๋ถ์์ค์ ๋ฌธ๊ฐ
- numpy
- ADP
- ์๋ํด๋ผ์ฐ๋
- ํ์ด์ฌ
- LDA
- ADsP
- PCA
- ๋ฐ์ดํฐ๋ถ์
- opencv
- ๋ ๋ฆฝํ๋ณธ
- DBSCAN
- iloc
- ์ค๋ฒ์ํ๋ง
- Python
- ๊ตฐ์งํ
- ๋น ๋ฐ์ดํฐ
- Lambda
- ๋ฐ์ดํฐ๋ถ๊ท ํ
- ๋น ๋ฐ์ดํฐ๋ถ์๊ธฐ์ฌ
- ๋์ํ๋ณธ
- pandas
Data Science LAB
[Python] SVD(Singular Value Decomposition) ๋ณธ๋ฌธ
[Python] SVD(Singular Value Decomposition)
ใ ใ ใ ใ 2022. 3. 7. 21:46SVD ๊ฐ์
SVD๋ PCA์ ๋น์ทํ๊ฒ ํ๋ ฌ ๋ถํด ๊ธฐ๋ฒ์ ์ด์ฉํ์ง๋ง, PCA๋ ์ ๋ฐฉํ๋ ฌ๋ง์ ๊ณ ์ ๋ฒกํฐ๋ก ๋ถํดํ๋ ๋ฐ๋ฉด, SVD๋ ํ๊ณผ ์ด์ด ๋ค๋ฅธ ๋ชจ๋ ํ๋ ฌ์ ์ ์ฉํ ์ ์๋ค.
์ผ๋ฐ์ ์ผ๋ก, SVD๋ m×n ํฌ๊ธฐ์ ํ๋ ฌ A๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ๋ถํดํ๋ ๊ฒ์ ์๋ฏธํ๋ค.
SVD๋ ํน์ด๊ฐ ๋ถํด๋ผ๊ณ ๋ ๋ถ๋ฆฌ๋ฉฐ, ํ๋ ฌ U์ V์ ์ํ๋ ๋ฒกํฐ๋ ํน์ด๋ฒกํฐ์ด๋ค. ๋ชจ๋ ํน์ด๋ฒกํฐ๋ ์๋กํ๋ ์ฑ์ง์ ๊ฐ์ง๋ค.
U : m×m ํฌ๊ธฐ์ ํ๋ ฌ, ์ญํ๋ ฌ์ด ๋์นญ ํ๋ ฌ
∑ : m×n ํฌ๊ธฐ์ ํ๋ ฌ, ๋น๋๊ฐ ์ฑ๋ถ์ด 0
V : n×nํฌ๊ธฐ์ ํ๋ ฌ, ์ญํ๋ ฌ์ด ๋์นญ
V,U๋ ์ง๊ตํ๋ ฌ
๋๋คํ๋ ฌ ์์ฑ
import numpy as np
from numpy.linalg import svd
np.random.seed(121)
a = np.random.randn(4,4)
print(np.round(a,3))
๊ฐ๋ณ ๋ก์ฐ์ ์์กด์ฑ์ ์์ ๊ธฐ ์ํด ๋๋ค์ผ๋ก 4×4์ ๋๋คํ๋ ฌ ์์ฑ
U,Sigma,Vt ๋์ถ
U,Sigma,Vt = svd(a)
print(U.shape,Sigma.shape,Vt.shape)
print("U : \n",np.round(U,3))
print("Sigma Value \n",np.round(Sigma,3))
print("V transpose matrix :\n",np.round(Vt,3))
svd์ ํ๋ผ๋ฏธํฐ๋ก ์๋ณธ ํ๋ ฌ์ ์ ๋ ฅํ๋ฉด, U,Sigma, V์ ์น ํ๋ ฌ์ ๋ฐํํ๋ค.
∑(Sigma)ํ๋ ฌ์ ๋๊ฐ์ ์์นํ ๊ฐ๋ง 0์ด ์๋๊ณ , ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ๋ ๋ชจ๋ 0์ด๋ฏ๋ก 0์ด ์๋ ๊ฒฝ์ฐ๋ง 1์ฐจ์ ํ๋ ฌ๋ก ํํํ๋ค.
Sigma๋ฅผ ๋ค์ 0์ ํฌํจํ ๋์นญํ๋ ฌ๋ก ๋ฐํ
sigma_mat = np.diag(Sigma)
a_ = np.dot(np.dot(U,sigma_mat),Vt)
print(np.round(a_,3))
Sigma ํ๋ ฌ์ ๊ฒฝ์ฐ 0์ด์๋ ๊ฐ๋ง 1์ฐจ์์ผ๋ก ์ถ์ถํ์์ผ๋ฏ๋ก 0์ ํฌํจํ ๋์นญํ๋ ฌ๋ก ๋ณํํ ํ ๋ด์ ์ ์ํํด์ผํ๋ค.
U, Sigma, Vt ๋ฅผ ๋ด์ ํ์ฌ ์๋ณธํ๋ ฌ์ ๋ณต์ํ ๊ฒฐ๊ณผ, ์๋ณธ๊ณผ ๋์ผํ๊ฒ ๋ณต์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
๋ฐ์ดํฐ ๋ก์ฐ ๊ฐ ์์กด์ฑ์ด ์์ ๊ฒฝ์ฐ
a[2] = a[0]+a[1]
a[3] = a[0]
print(np.round(a,3))
๋ฐ์ดํฐ ๋ก์ฐ๊ฐ์ ์์กด์ฑ์ด ์์ ๊ฒฝ์ฐ ์ด๋ป๊ฒ Sigma ๊ฐ์ด ๋ณํ๊ณ , ์ด์ ๋ฐ๋ฅธ ์ฐจ์์ถ์๊ฐ ์งํ๋๋ ์ง ์์๋ณด๊ธฐ ์ํด aํ๋ ฌ์ 3๋ฒ์งธ ๋ก์ฐ๋ฅผ (์ฒซ๋ฒ์งธ ๋ก์ฐ + ๋๋ฒ์งธ ๋ก์ฐ)๋ก ์ ๋ฐ์ดํธํ๊ณ , 4๋ฒ์งธ๋ ์ฒซ๋ฒ์งธ ๋ก์ฐ์ ๊ฐ๋๋ก ์ ๋ฐ์ดํธํ์๋ค.
๋ค์ SVD ์ํํด Sigma ๊ฐ ํ์ธ
U,Sigma,Vt = svd(a)
print(U.shape,Sigma.shape,Vt.shape)
print("Sigma Value : \n",np.round(Sigma,3))
๋ค์ SVD๋ก ๋ถํดํ ๊ฒฐ๊ณผ, ์ด์ ๊ณผ ์ฐจ์์ ๋์ผํ์ง๋ง, Sigma ๊ฐ ์ค 2๊ฐ๊ฐ 0์ผ๋ก ๋ณํ๋ค. ์ฆ, ์ ํ ๋ ๋ฆฝ์ธ ๋ก์ฐ ๋ฒกํฐ์ ๊ฐ์๊ฐ 2๊ฐ๋ผ๋ ์๋ฏธ์ด๋ค.
#U ํ๋ ฌ์ Sigma์ ๋ด์ ์ ์ํํ๋ฏ๋ก Sigma์ ์ 2ํ์ ๋์๋๋ ์ด๋ง ์ถ์ถ
U_=U[:,:2]
Sigma_ = np.diag(Sigma[:2])
#V์ ์น ํ๋ ฌ์ ์2ํ๋ง ์ถ์ถ
Vt_ = Vt[:2]
print(U_.shape,Sigma_.shape,Vt_.shape)
#U,Sigma,Vt์ ๋ด์ ์ ์ํํ๋ฉฐ ๋ค์ ์๋ณธ ํ๋ ฌ ๋ณต์
a_ = np.dot(np.dot(U_,Sigma_),Vt_)
print(np.round(a_,3))
๋ค์ ๋ด์ ํ์ฌ ๋ณต์ํ ๊ฒฐ๊ณผ, ์ ํํ๊ฒ ๋ณต์๋์ง๋ ์์์ง๋ง, ์๋ณธํ๋ ฌ์ ๊ฐ๊น๊ฒ ๋ณต์๋ ๊ฒ์ ํ์ธํ ์ ์์๋ค.
Truncated SVD
import numpy as np
from scipy.sparse.linalg import svds
from scipy.linalg import svd
#์๋ณธ ํ๋ ฌ ์ถ๋ ฅํ๊ณ SVD ์ ์ฉํ ๊ฒฝ์ฐ U, Sigma, Vt์ ์ฐจ์ํ์ธ
np.random.seed(121)
matrix = np.random.random((6,6))
print("์๋ณธ ํ๋ ฌ : \n",matrix)
U,Sigma,Vt = svd(matrix,full_matrices=False)
print("Sigma ๊ฐ ํ๋ ฌ : \n",Sigma)
#Truncated SVD๋ก Sigma ํ๋ ฌ์ ํน์ด๊ฐ์ 4๊ฐ๋ก ํ์ฌ Truncated SVD ์ํ
num_components = 4
U_tr,Sigma_tr, Vt_tr = svds(matrix,k=num_components)
print("Truncated SVD ๋ถํด ํ๋ ฌ ์ฐจ์: ",U_tr.shape,Sigma_tr.shape,Vt_tr.shape)
print("Truncated SVD Sigma๊ฐ ํ๋ ฌ : ",Sigma_tr)
matrix_tr = np.dot(np.dot(U_tr,np.diag(Sigma_tr)),Vt_tr)
print("Truncated SVD๋ก ๋ถํด ํ ๋ณต์ ํ๋ ฌ : \n",matrix_tr)
6×6 ํ๋ ฌ์ SVD ๋ถํดํ๋ฉด U,Sigma,Vt๊ฐ ๊ฐ๊ฐ (6,6),(6,),(6,6)์ฐจ์์ด์ง๋ง,
TruncatedSVD์ n_components๋ฅผ 4๋ก ์ค์ ํ๋ฉด U,Sigma,Vt๊ฐ ๊ฐ๊ฐ (6,4),(4,),(4,6)์ฐจ์์ผ๋ก ๋ถํด๋๋ ๊ฒ์ ์ ์ ์๋ค. (์๋ฒฝํ๊ฒ ๋ณต์๋์ง ์์)
์ฌ์ดํท๋ฐ TruncatedSVD ํด๋์ค๋ฅผ ์ด์ฉํ ๋ณํ
from sklearn.decomposition import TruncatedSVD,PCA
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
%matplotlib inline
iris = load_iris()
iris_ftrs = iris.data
#2๊ฐ์ ์ฃผ์ ์ปดํฌ๋ํธ๋ก TruncatedSVD ๋ณํ
tsvd = TruncatedSVD(n_components=2)
tsvd.fit(iris_ftrs)
iris_tsvd = tsvd.transform(iris_ftrs)
#์ฐ์ ๋ 2์ฐจ์์ผ๋ก TruncatedSVD ๋ณํ๋ ๋ฐ์ดํฐ ํํ, ํ์ข
์ ์๊น๋ก ๊ตฌ๋ถ
plt.scatter(x=iris_tsvd[:,0],y=iris_tsvd[:,1],c=iris.target)
plt.xlabel('TruncatedSVD Component 1')
plt.ylabel('TruncatedSVD Component 2')
ํ์ข ๋ณ๋ก ์ด๋ ์ ๋ ํด๋ฌ์คํฐ๋ง์ด ๊ฐ๋ฅํ ์ ๋๋ก ๊ฐ ๋ณํ ์์ฑ์ผ๋ก ๋ฐ์ด๋ ๊ณ ์ ์ฑ์ ๊ฐ์ง๊ณ ์๋ค.
iris data ์ค์ผ์ผ๋ง ๋ณํ ํ TruncatedSVD ์ PCA ํด๋์ค ๋ณํ
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
iris_scaled = scaler.fit_transform(iris_ftrs)
#์ค์ผ์ผ๋ง๋ ๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก TruncatedSVD ๋ณํ
tsvd = TruncatedSVD(n_components = 2)
tsvd.fit(iris_scaled)
iris_tsvd = tsvd.transform(iris_scaled)
#์ค์ผ์ผ๋ง๋ ๋ฐ์ดํฐ ๊ธฐ๋ฐ์ผ๋ก PCA ๋ณํ ์ํ
pca = PCA(n_components=2)
pca.fit(iris_scaled)
iris_pca = pca.transform(iris_scaled)
#TruncatedSVD ๋ณํ ๋ฐ์ดํฐ๋ฅผ ์ผ์ชฝ, PCA ๋ณํ๋ฐ์ดํฐ๋ฅผ ์ค๋ฅธ์ชฝ
fig,(ax1,ax2) = plt.subplots(figsize=(9,4), ncols=2)
ax1.scatter(x=iris_tsvd[:,0],y=iris_tsvd[:,1],c=iris.target)
ax2.scatter(x=iris_pca[:,0],y=iris_pca[:,1],c=iris.target)
ax1.set_title('Truncated SVD Transformed')
ax2.set_title('PCA Transformed')
print((iris_pca-iris_tsvd).mean())
print((pca.components_-tsvd.components_).mean())
๋ชจ๋ 0์ ๊ฐ๊น์ด ๊ฐ์ผ๋ก 2๊ฐ์ ๋ณํ์ด ๊ฑฐ์ ๋์ผํจ์ ์ ์ ์๋ค. ์ฆ ๋ฐ์ดํฐ์ ์ด ์ค์ผ์ผ๋ง์ผ๋ก ๋ฐ์ดํฐ ์ค์ฌ์ด ๋์ผํด์ง๋ฉด ์ฌ์ดํท๋ฐ์ SVD์ PCA๋ ์๋ก ๋์ผํ ๋ณํ์ ์ํํ๋ค.
ํ์ง๋ง PCA๋ ๋ฐ์งํ๋ ฌ์ ๋ํ ๋ณํ๋ง ๊ฐ๋ฅํ๋ฉฐ, SVD๋ ํฌ์ ํ๋ ฌ์ ๋ํ ๋ณํ๋ ๊ฐ๋ฅํ๋ค.
SVD๋ PCA์ ์ ์ฌํ๊ฒ ์ปดํจํฐ ๋น์ ์์ญ์์ ์ด๋ฏธ์ง ์์ถ์ ํตํ ํจํด ์ธ์๊ณผ ์ ํธ ์ฒ๋ฆฌ ๋ถ์ผ์ ์ฌ์ฉ๋๋ค. ๋ํ, ํ ์คํธ์ ํ ํฝ ๋ชจ๋ธ๋ง ๊ธฐ๋ฒ์ธ LSA์ ๊ธฐ๋ฐ ์๊ณ ๋ฆฌ์ฆ์ด๋ค.
'๐ Machine Learning > ์ฐจ์ ์ถ์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python]NMF (0) | 2022.03.08 |
---|---|
[Python] LDA(Linear Discriminant Analysis) (0) | 2022.03.07 |
[Python] PCA ์์ (0) | 2022.03.06 |
[Python] PCA(Principal Component Analysis) (0) | 2022.03.05 |