科学の箱

科学・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

Pythonで文字列を生成するときの方法についてまとめる

Pythonで文字列を生成するときには様々な方法があるのでまとめる。 まず最も基本となるのは生成したい文字をシングルクォーテーションもしくはダブルクォーテーションで囲む方法である。どちらの方法を使って …

no image

SIGNATE お弁当の需要予測-3

Seabornを利用してデータをビジュアル化してみる。 まずは売り上げの分布図から sns.distplot(d_train[‘y’], kde=False, rug=False, bins=50) …

no image

bing image searchをpythonで利用

Azureへの登録+Bing Search APIを登録についてはこちらのサイトが詳しい。 https://qiita.com/ysdyt/items/49e99416079546b65dfc ただし …

no image

Hello Worldを実行

環境設定を完了したらまずはお約束のHello Worldからである。 変数helloに文字列”Hello Python!”を代入する。Pythonにおいては型の指定は最初のうち …

no image

automated the boring – day5

さて、ここまででフロー、文字列、型、ファイル等を扱い、基礎プログラミングとしては一段落できた。 今日からは後半戦にはいり、開発をするうえで実践で必要になる技術を学んでいく。 まずはデバッグからである。 …

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

side bar top



アーカイブ

カテゴリー