建立糖尿病分類器
你將使用 Pima Indians 糖尿病資料集,透過羅吉斯回歸來預測一個人是否有糖尿病。這個資料集中有 8 個特徵與 1 個目標。資料已分割為訓練集與測試集,並預先載入為 X_train、y_train、X_test 與 y_test。
StandardScaler() 實例已預先定義為 scaler,LogisticRegression() 實例則為 lr。
本練習屬於課程
Python 的降維
練習說明
- 在訓練特徵上對 scaler 進行擬合並一次完成特徵轉換。
- 在縮放後的訓練資料上訓練羅吉斯回歸模型。
- 對測試特徵進行縮放。
- 在縮放後的測試集上預測是否存在糖尿病。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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))))