科学の箱

科学・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でデータ分析を行う際には基本のデータ構造を理解しておく必要がある。 scikit-learnなどのフレームワークは特定のデータ構造を入力とすることを前提にしている。用意したデータがフレーム …

no image

jupyterで目的のディレクトリから実行する方法

AnacondaをインストールするとJupyter Notebookを実行するショートカットが作成される。これをクリックするとJupyter Notebookは起動できるわけだが、残念ながら必要なファ …

no image

kaggle Titanic Tutorial – 3

DecitionTreeのパラメータを調整する。 まずはMaxDepthから from sklearn.model_selection import LeaveOneOut from sklearn. …

no image

selectorとxpathを手軽に取得する方法

スクレイピングをするプログラムを開発するときに対象となる項目を取得するためにselector/xpathで指定する。 Google Chromeではselector/xpath値を簡単に取得できる。 …

no image

Anaconda Jupyterで自動補完を使う手順

まずはnbexensionsをインストール。これは拡張モジュールを管理する機能。 conda install -y -c conda-forge jupyter_contrib_nbextension …

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

side bar top



アーカイブ

カテゴリー