당뇨병 분류기 만들기
로지스틱 회귀를 사용해 Pima Indians 당뇨병 데이터셋에서 개인의 당뇨병 유무를 예측해 보겠습니다. 이 데이터셋에는 8개의 특성과 1개의 타깃이 있습니다. 데이터는 학습 세트와 테스트 세트로 분할되어 있으며, X_train, y_train, X_test, y_test로 미리 로드되어 있어요.
StandardScaler() 인스턴스는 scaler로, LogisticRegression() 인스턴스는 lr로 미리 정의되어 있습니다.
이 연습은 강의의 일부입니다
Python으로 배우는 차원 축소
연습 안내
- 학습용 특성에 스케일러를 fit하고, 이 특성들을 한 번에 transform까지 수행하세요.
- 스케일된 학습 데이터로 로지스틱 회귀 모델을 학습(fit)하세요.
- 테스트 특성을 스케일링하세요.
- 스케일된 테스트 세트에서 당뇨병 여부를 예측하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Fit the scaler on the training features and transform these in one go
X_train_std = scaler.____(____)
# Fit the logistic regression model on the scaled training data
lr.____(____, ____)
# Scale the test features
X_test_std = scaler.____(____)
# Predict diabetes presence on the scaled test set
y_pred = lr.____(____)
# Prints accuracy metrics and feature coefficients
print(f"{accuracy_score(y_test, y_pred):.1%} accuracy on test set.")
print(dict(zip(X.columns, abs(lr.coef_[0]).round(2))))