Pythonで学ぶ木ベースのMachine Learning
Elie Kawerk
Data Scientist
理解しやすい。
解釈しやすい。
使いやすい。
柔軟性:非線形な依存関係を表現できる。
前処理:特徴量の標準化や正規化が不要。
分類:直交した決定境界しか生成できない。
訓練データの小さな変動に敏感。
高分散:制約のないCARTは訓練データに過学習しやすい。
解決策:アンサンブル学習。
同じデータセットで異なるモデルを訓練する。
各モデルが予測を行う。
メタモデル:各モデルの予測を集約する。
最終予測:よりロバストでエラーが少ない。
最良の結果:モデルが異なる側面で優れている場合。

二値分類タスク。
$N$個の分類器が予測を行う:$P_1$, $P_2$, ..., $P_N$($P_i$ = 0または1)。
メタモデルの予測:ハード投票。

# Import functions to compute accuracy and split data
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
# Import models, including VotingClassifier meta-model
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier as KNN
from sklearn.ensemble import VotingClassifier
# Set seed for reproducibility
SEED = 1
# Split data into 70% train and 30% test X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.3, random_state= SEED) # Instantiate individual classifiers lr = LogisticRegression(random_state=SEED) knn = KNN() dt = DecisionTreeClassifier(random_state=SEED)# Define a list called classifier that contains the tuples (classifier_name, classifier) classifiers = [('Logistic Regression', lr), ('K Nearest Neighbours', knn), ('Classification Tree', dt)]
# Iterate over the defined list of tuples containing the classifiers
for clf_name, clf in classifiers:
#fit clf to the training set
clf.fit(X_train, y_train)
# Predict the labels of the test set
y_pred = clf.predict(X_test)
# Evaluate the accuracy of clf on the test set
print('{:s} : {:.3f}'.format(clf_name, accuracy_score(y_test, y_pred)))
Logistic Regression: 0.947
K Nearest Neighbours: 0.930
Classification Tree: 0.930
# Instantiate a VotingClassifier 'vc'
vc = VotingClassifier(estimators=classifiers)
# Fit 'vc' to the traing set and predict test set labels
vc.fit(X_train, y_train)
y_pred = vc.predict(X_test)
# Evaluate the test-set accuracy of 'vc'
print('Voting Classifier: {.3f}'.format(accuracy_score(y_test, y_pred)))
Voting Classifier: 0.953
Pythonで学ぶ木ベースのMachine Learning