科学の箱

科学・IT・登山の話題

Python

graphvizによる決定木の表示

投稿日:

graphvizの実行ファイルをインストール

https://graphviz.gitlab.io/_pages/Download/Download_windows.html

pythonにgraphvizをインストール

pip install graphviz

pythonからサンプルコード実行

from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier

cancer = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, stratify=cancer.target, random_state=42)
tree = DecisionTreeClassifier(random_state=0)
tree.fit(X_train, y_train)

from sklearn.tree import export_graphviz
export_graphviz(tree, out_file=”tree.dot”, class_names=[“malignant”, “benign”],feature_names=cancer.feature_names, impurity=False, filled=True)

import graphviz
with open(“tree.dot”) as f:
dot_graph = f.read()
display(graphviz.Source(dot_graph))

メタ情報

inarticle



メタ情報

inarticle



-Python

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

pyplotでx軸のラベルを90度回転させる

pyplotでx軸にラベルを記載するとラベル文字数が長すぎるためにお互いにオーバーラップしてみにくい。 このよう場合にはpyplot.xticksを実行する際にrotationを指定すればよい。 Ro …

no image

kaggle Titanic Tutorial – 11

kaggleで人気があるlightGBMをつかってみる。   インストール pip install lightgbm 特に問題がなく終了。 コード、関係するところだけ記載。 split_be …

no image

pycharmでターミナルからconda環境を利用する

pycharmでTerminalを開く conda環境を開く %windir%\System32\cmd.exe “/K” C:\ProgramData\Anaconda3\S …

no image

PythonでMicrosoft Visual C++ 14.0 is required エラーが出た場合

Visual Studio 2017だけでは不十分である。   https://visualstudio.microsoft.com/ja/downloads/から下記のリンク経由でBuil …

no image

automated the boring – day4

本日からOS操作。面倒くさいことを自動化するのであればOSコマンドは避けては通れない。 pythonではすでにosパッケージが用意されているので、これを利用すればコードとしてはそれほどはややこしくない …

2018年11月
« 10月   12月 »
 1234
567891011
12131415161718
19202122232425
2627282930  

side bar top



アーカイブ

カテゴリー