評估糖尿病預測分類器
在本章中,你將使用先前介紹過的 diabetes_df 資料集。
目標是根據身體質量指數(BMI)與年齡(單位:年)這兩個特徵,預測個體是否可能患有糖尿病。因此,這是一個二元分類問題。0 表示該個體「沒有」糖尿病,1 表示該個體「有」糖尿病。
diabetes_df 已經替你預先載入為一個 pandas DataFrame,並切分為 X_train、X_test、y_train 和 y_test。此外,KNeighborsClassifier() 已經建立並指定給 knn。
你將先擬合模型、在測試集上進行預測,接著產生混淆矩陣與分類報告。
本練習屬於課程
使用 scikit-learn 進行監督式學習
練習說明
- 匯入
confusion_matrix與classification_report。 - 將模型擬合至訓練資料。
- 預測測試集的標籤,並將結果存成
y_pred。 - 計算並印出測試標籤與預測標籤的混淆矩陣與分類報告。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import confusion matrix
____
knn = KNeighborsClassifier(n_neighbors=6)
# Fit the model to the training data
____
# Predict the labels of the test data: y_pred
y_pred = ____
# Generate the confusion matrix and classification report
print(____(____, ____))
print(____(____, ____))