개념
차원 축소 알고리즘의 하나로 데이터에서 가장 분산이 큰 방향을 찾는 방법이다.
여기서 큰 방향이란 주성분이다. 원본 데이터를 주성분에 투영하여 새로운 특성을 만든다.
문법
from sklearn.decomposition import PCA # PCA 모델 사용
# n_components : 주성분의 개수 지정
pca = PCA(n_components=None)
pca.fit(fruits_2d)
# 훈련 세트에서 찾은 주성분
pca.components_
# 차원 축소
pca.transform(fruits_2d)
# 축소된 데이터 복원
pca.inverse_transform(fruits_pca)
# 설명된 분산의 비율
pca.explained_variance_ratio_
실습
!wget https://bit.ly/fruits_300_data -O fruits_300.npy # 데이터
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA # PCA모델 사용
fruits = np.load('fruits_300.npy')
fruits_2d = fruits.reshape(-1, 100 * 100)
pca = PCA(n_components=50)
pca.fit(fruits_2d)
draw_fruits(pca.components_.reshape(-1,100,100))
fruits_pca = pca.transform(fruits_2d)
print(fruits_pca.shape)
'Data science > 머신러닝' 카테고리의 다른 글
housing 데이터를 이용한 머신러닝 학습 (0) | 2025.03.28 |
---|---|
Machine Learning란? (0) | 2025.03.16 |
K-means clustering(k-평균 알고리즘) (0) | 2022.07.23 |
Loss function(손실 함수) (0) | 2022.07.17 |
Gradient Descent(경사 하강법) (0) | 2022.07.17 |