預測蘑菇是否可食
既然你已經探索過資料,現在是時候建立第一個模型來預測蘑菇是否可食。
資料集以 mushrooms 提供給你。由於特徵與目標皆為類別型,已為你轉換成「虛擬」二元變數。
我們先從樸素貝氏分類器(使用 scikit-learn 的 GaussianNB)開始,看看這個演算法在此問題上的表現。
本練習屬於課程
Python 的 Ensemble 方法
練習說明
- 建立一個名為
clf_nb的GaussianNB分類器。 - 將
clf_nb擬合到訓練資料X_train與y_train。 - 計算測試集上的預測值。這些預測會用來以準確率評估效能。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Instantiate a Naive Bayes classifier
clf_nb = ____
# Fit the model to the training set
____
# Calculate the predictions on the test set
pred = ____
# Evaluate the performance using the accuracy score
print("Accuracy: {:0.4f}".format(accuracy_score(y_test, pred)))