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

學無先后,達者為師

網站首頁 編程語言 正文

Python機器學習應用之基于決策樹算法的分類預測篇_python

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

一、決策樹的特點

1.優點

  • 具有很好的解釋性,模型可以生成可以理解的規則。
  • 可以發現特征的重要程度。
  • 模型的計算復雜度較低。

2.缺點

  • 模型容易過擬合,需要采用減枝技術處理。
  • 不能很好利用連續型特征。
  • 預測能力有限,無法達到其他強監督模型效果。
  • 方差較高,數據分布的輕微改變很容易造成樹結構完全不同。

二、決策樹的適用場景

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

三、demo

#%%demo
##  基礎函數庫導入
import numpy as np 

## 導入畫圖庫
import matplotlib.pyplot as plt
import seaborn as sns

## 導入決策樹模型函數
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
import pydotplus 
from IPython.display import Image
##Demo演示DecisionTree分類
## 構造數據集
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])
## 調用決策樹回歸模型
tree_clf = DecisionTreeClassifier()
## 調用決策樹模型擬合構造的數據集
tree_clf = tree_clf.fit(x_fearures, y_label)
## 可視化構造的數據樣本點
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") 
# 模型預測
## 創建新樣本
x_fearures_new1 = np.array([[0, -1]])
x_fearures_new2 = np.array([[2, 1]])

## 在訓練集和測試集上分布利用訓練好的模型進行預測
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)

運行結果

訓練集決策樹

明天繼續,還有一個決策樹在真實數據集上的應用,明天出。先搞課題~

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

欄目分類
最近更新