日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Python機(jī)器學(xué)習(xí)應(yīng)用之基于決策樹算法的分類預(yù)測篇_python

作者:柚子味的羊 ? 更新時間: 2022-03-30 編程語言

一、決策樹的特點(diǎn)

1.優(yōu)點(diǎn)

  • 具有很好的解釋性,模型可以生成可以理解的規(guī)則。
  • 可以發(fā)現(xiàn)特征的重要程度。
  • 模型的計算復(fù)雜度較低。

2.缺點(diǎn)

  • 模型容易過擬合,需要采用減枝技術(shù)處理。
  • 不能很好利用連續(xù)型特征。
  • 預(yù)測能力有限,無法達(dá)到其他強(qiáng)監(jiān)督模型效果。
  • 方差較高,數(shù)據(jù)分布的輕微改變很容易造成樹結(jié)構(gòu)完全不同。

二、決策樹的適用場景

  • 決策樹模型多用于處理自變量與因變量是非線性的關(guān)系。
  • 梯度提升樹(GBDT),XGBoost以及LightGBM等先進(jìn)的集成模型均采用決策樹作為基模型。(多粒度聯(lián)森林模型)
  • 決策樹在一些明確需要可解釋性或者提取分類規(guī)則的場景中被廣泛應(yīng)用。在醫(yī)療輔助系統(tǒng)中為了方便專業(yè)人員發(fā)現(xiàn)錯誤,常常將決策樹算法用于輔助病癥檢測。

三、demo

#%%demo
##  基礎(chǔ)函數(shù)庫導(dǎo)入
import numpy as np 

## 導(dǎo)入畫圖庫
import matplotlib.pyplot as plt
import seaborn as sns

## 導(dǎo)入決策樹模型函數(shù)
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
import pydotplus 
from IPython.display import Image
##Demo演示DecisionTree分類
## 構(gòu)造數(shù)據(jù)集
x_fearures = np.array([[-1, -2], [-2, -1], [-3, -2], [1, 3], [2, 1], [3, 2]])
y_label = np.array([0, 1, 0, 1, 0, 1])
## 調(diào)用決策樹回歸模型
tree_clf = DecisionTreeClassifier()
## 調(diào)用決策樹模型擬合構(gòu)造的數(shù)據(jù)集
tree_clf = tree_clf.fit(x_fearures, y_label)
## 可視化構(gòu)造的數(shù)據(jù)樣本點(diǎn)
plt.figure()
plt.scatter(x_fearures[:,0],x_fearures[:,1], c=y_label, s=50, cmap='viridis')
plt.title('Dataset')
plt.show()
## 可視化決策樹
import graphviz
dot_data = tree.export_graphviz(tree_clf, out_file=None)
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf("D:\Python\ML\DecisionTree.pdf") 
# 模型預(yù)測
## 創(chuàng)建新樣本
x_fearures_new1 = np.array([[0, -1]])
x_fearures_new2 = np.array([[2, 1]])

## 在訓(xùn)練集和測試集上分布利用訓(xùn)練好的模型進(jìn)行預(yù)測
y_label_new1_predict = tree_clf.predict(x_fearures_new1)
y_label_new2_predict = tree_clf.predict(x_fearures_new2)

print('The New point 1 predict class:\n',y_label_new1_predict)
print('The New point 2 predict class:\n',y_label_new2_predict)

運(yùn)行結(jié)果

訓(xùn)練集決策樹

明天繼續(xù),還有一個決策樹在真實(shí)數(shù)據(jù)集上的應(yīng)用,明天出。先搞課題~

原文鏈接:https://blog.csdn.net/qq_43368987/article/details/122357174

欄目分類
最近更新