データサイエンス基本(決定木のモデルを作る)-Pythonとjupyter-
データサイエンスを学ぶために、SIGNATEを始めました。
その中でjupyterでpythonのコマンドを入力するにあたり、復習も兼ねてメモしてみました。
見てくださった方のお役に立てたら幸いです。
決定木のモデルを作ってみる
決定木を作るための読み込み
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
from sklearn.tree import DecisionTreeClassifier as DT
from sklearn.tree import export_graphviz
from IPython.display import Image
説明関数をまとめて取り出す
AAAx = AAA.iloc[:,0:10]
目的関数が左から数えて11番目にあり、目的関数以外のデータの数が10の場合。
説明変数を取り出す
y = AAA["y"]
決定木のモデルの箱を用意する
clf1 = DT(max_depth=2,min_sample_leaf=500
決定木のモデルを作る
clf1.fit(AAAx,y)
できた木を確認する
export_graphviz(clf1, out_file="tree.dot", feature_names=AAAx.columns, class_names=["0","1"], filled=True, rounded=True)
書き出せたら写真として表示する
g = pydotplus.graph_from_dot_file(path="tree.dot")
Image(g.create_png())