分類模型

Python 的模型驗證

Kasey Jones

Data Scientist

分類模型

  • 類別型回應:
    • 新生兒的髮色
    • 籃球比賽的勝方
    • 廣播下一首歌的曲風
Python 的模型驗證

圈叉遊戲資料集

... 左下 下中 右下 類別
... X O O positive
... O X O positive
... O O X positive
... X X O negative
... ... ... ... ...
Python 的模型驗證

如果你想,Google 可以和你玩圈叉遊戲。到 Google 搜尋 Tic-Tac-Toe games 即可。

Python 的模型驗證

使用 .predict() 做分類

from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier(random_state=1111)
rfc.fit(X_train, y_train)
rfc.predict(X_test)
array([1, 1, 1, 1, 0, 1, ...])
pd.Series(rfc.predict(X_test)).value_counts()
1    627
0    331
Python 的模型驗證

預測機率

rfc.predict_proba(X_test)
array([[0. , 1. ],
       [0.1, 0.9],
       [0.1, 0.9],
       ...])
Python 的模型驗證
rfc = RandomForestClassifier(random_state=1111)
rfc.get_params()
{'bootstrap': True,
 'class_weight': None,
 'criterion': 'gini',
 ...}
rfc.fit(X_train, y_train)
rfc.score(X_test, y_test)
0.8989
Python 的模型驗證

一起來分類圈叉遊戲終局情境

Python 的模型驗證

Preparing Video For Download...