1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python】scikit-learnの決定木の特徴

Posted at

決定木はモデルの内容が理解しやすいアルゴリズムです。

次のツリーは、決定木でirisの花を4つに分類したときの過程です。

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

tree = DecisionTreeClassifier(max_depth=3)
tree.fit(X_train, y_train)

このように分類の過程が可視化できるため、決定木は内容が理解しやすいモデルです。

不純度は上のツリーの「gini」の数値です。この数値が下がる場合は、データを分割したほうが良いと判断され分割されます。

引数「max_depth」は決定木の深さを指定します。上の例は「max_depth=3」で指定しているため、親ノードの下が3層になっています。

「max_depth」は指定しなくても学習モデルを作成できますが、その場合は次のツリーのようにすべての不純度が「0」になるまで分割し続けます。

これは訓練データに過剰適合しているため、テストデータに対する精度が下がります。過剰適合した場合は、「グリッドサーチ」で最適な深さを探るか、決定木を複数作るアルゴリズム「ランダムフォレスト」を採用します。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?