日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

绘制决策树

發(fā)布時(shí)間:2025/3/21 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 绘制决策树 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

繪制出決策樹

經(jīng)過訓(xùn)練的決策樹,我們可以使用 export_graphviz 導(dǎo)出器以 Graphviz 格式導(dǎo)出決策樹. 如果你是用 conda 來管理包,那么安裝 graphviz 二進(jìn)制文件和 python 包可以用以下指令安裝


conda install python-graphviz

或者,可以從 graphviz 項(xiàng)目主頁(yè)下載 graphviz 的二進(jìn)制文件,并從 pypi 安裝 Python 包裝器,并安裝 ‘pip install graphviz` .以下是在整個(gè) iris 數(shù)據(jù)集上訓(xùn)練的上述樹的 graphviz 導(dǎo)出示例; 其結(jié)果被保存在 iris.pdf 中:

from sklearn.datasets import load_iris from sklearn import tree iris = load_iris() clf_iris = tree.DecisionTreeClassifier() clf_iris = clf.fit(iris.data, iris.target)

* 下面的代碼可以到處我們的決策樹 *

:func:export_graphviz 出導(dǎo)出還支持各種美化,包括通過他們的類著色節(jié)點(diǎn)(或回歸值),如果需要,使用顯式變量和類名。

* 注意:默認(rèn)情況下,會(huì)導(dǎo)出圖形文件*

* 更詳細(xì)的內(nèi)容請(qǐng)參考 sklearn官方文檔:sklearn.tree.export_graphviz*

Jupyter notebook也可以自動(dòng)找出相同的模塊

import graphviz # doctest: +SKIP dot_data = tree.export_graphviz(clf, out_file=None) # doctest: +SKIP graph = graphviz.Source(dot_data) # doctest: +SKIP graph.render("iris") # doctest: +SKIPdot_data = tree.export_graphviz(clf, out_file=None, # doctest: +SKIPfeature_names=iris.feature_names, # doctest: +SKIPclass_names=iris.target_names, # doctest: +SKIPfilled=True, rounded=True, # doctest: +SKIPspecial_characters=True) # doctest: +SKIP graph = graphviz.Source(dot_data) # doctest: +SKIP graph # doctest: +SKIP

* 之后依舊可以使用該函數(shù)進(jìn)行預(yù)測(cè)數(shù)據(jù)等操作*

clf_iris.predict(iris.data[:1, :]) array([0])

畫出決策樹的分類區(qū)域

  • sklearn對(duì)應(yīng)文檔
import numpy as np import matplotlib.pyplot as pltfrom sklearn.datasets import load_iris from sklearn.tree import DecisionTreeClassifier# Parameters n_classes = 3 plot_colors = "ryb" plot_step = 0.02# Load data iris = load_iris()for pairidx, pair in enumerate([[0, 1], [0, 2], [0, 3],[1, 2], [1, 3], [2, 3]]):# We only take the two corresponding featuresX = iris.data[:, pair]y = iris.target# Trainclf = DecisionTreeClassifier().fit(X, y)# Plot the decision boundaryplt.subplot(2, 3, pairidx + 1)x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),np.arange(y_min, y_max, plot_step))plt.tight_layout(h_pad=0.5, w_pad=0.5, pad=2.5)Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])Z = Z.reshape(xx.shape)cs = plt.contourf(xx, yy, Z, cmap=plt.cm.RdYlBu)plt.xlabel(iris.feature_names[pair[0]])plt.ylabel(iris.feature_names[pair[1]])# Plot the training pointsfor i, color in zip(range(n_classes), plot_colors):idx = np.where(y == i)plt.scatter(X[idx, 0], X[idx, 1], c=color, label=iris.target_names[i],cmap=plt.cm.RdYlBu, edgecolor='black', s=15)plt.suptitle("Decision surface of a decision tree using paired features") plt.legend(loc='lower right', borderpad=0, handletextpad=0) plt.axis("tight") plt.show()

參考資料

  • sklearn官方文檔:決策樹
  • sklearn官方文檔:繪制決策樹
  • sklearn官方文檔:sklearn.tree.export_graphviz

總結(jié)

以上是生活随笔為你收集整理的绘制决策树的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。