網站首頁 編程語言 正文
一、決策樹的特點
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
- 上一篇:用C語言畫一個圓_C 語言
- 下一篇:SQL基礎查詢和LINQ集成化查詢_MsSql
相關推薦
- 2022-11-11 C#中的Hashtable?類使用詳解_C#教程
- 2022-12-13 Kubernetes中創建命名空間實現方法_云其它
- 2022-07-03 C語言中二級指針解析(指向指針的指針)_C 語言
- 2022-07-25 C++超詳細講解內存空間分配與this指針_C 語言
- 2022-07-14 Android實現手機多點觸摸畫圓_Android
- 2022-06-08 FreeRTOS實時操作系統的任務創建和刪除_操作系統
- 2022-08-11 python中的多線程鎖lock=threading.Lock()使用方式_python
- 2022-10-02 golang?NewRequest/gorequest實現http請求的示例代碼_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支