PCA 사용하기
이 연습 문제에서는 wine 데이터셋에 PCA를 적용해 모델의 정확도를 높일 수 있는지 확인해 보겠습니다.
이 연습은 강의의 일부입니다
Python으로 배우는 Machine Learning 전처리
연습 안내
PCA객체를 인스턴스화하세요.wine에서 특징(X)과 레이블(y)을 정의하세요. 레이블은"Type"열을 사용합니다.- 데이터 누수를 피하면서
X_train과X_test에 PCA를 적용하고, 변환된 값을 각각pca_X_train과pca_X_test로 저장하세요. - 각 주성분이 설명하는 분산 비율을 확인하기 위해
pca의.explained_variance_ratio_속성을 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Instantiate a PCA object
pca = ____()
# Define the features and labels from the wine dataset
X = wine.drop(____, ____)
y = wine["Type"]
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42)
# Apply PCA to the wine dataset X vector
pca_X_train = ___.____(____)
pca_X_test = ___.____(____)
# Look at the percentage of variance explained by the different components
print(____)