학습/테스트 분할 + 정확도 계산
이제 churn_df 데이터 세트로 데이터를 학습 세트와 테스트 세트로 분할하는 실습 문제를 풀어볼 시간입니다!
특성을 담은 X와 타깃 변수를 담은 y로 NumPy 배열이 이미 생성되어 있습니다.
이 연습은 강의의 일부입니다
scikit-learn으로 배우는 지도 학습
연습 안내
sklearn.model_selection에서train_test_split을 Import 하세요.X와y를 학습 세트와 테스트 세트로 분할하세요. 이때test_size를 20%로,random_state를42로 설정하고, 타겟 레이블 비율이 원본 데이터 세트와 같도록 설정하세요.knn모델을 학습 데이터에 학습시키세요.- 테스트 데이터에 대한 모델의 정확도를 계산하고 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Import the module
from ____ import ____
X = churn_df.drop("churn", axis=1).values
y = churn_df["churn"].values
# Split into training and test sets
X_train, X_test, y_train, y_test = ____(____, ____, test_size=____, random_state=____, stratify=____)
knn = KNeighborsClassifier(n_neighbors=5)
# Fit the classifier to the training data
____
# Print the accuracy
print(knn.score(____, ____))