麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > Python > 正文

python機器學習實現決策樹

2024-09-09 19:02:39
字體:
來源:轉載
供稿:網友

本文實例為大家分享了python機器學習實現決策樹的具體代碼,供大家參考,具體內容如下

# -*- coding: utf-8 -*-"""Created on Sat Nov 9 10:42:38 2019@author: asus""""""決策樹目的:1. 使用決策樹模型2. 了解決策樹模型的參數3. 初步了解調參數要求:基于乳腺癌數據集完成以下任務:1.調整參數criterion,使用不同算法信息熵(entropy)和基尼不純度算法(gini)2.調整max_depth參數值,查看不同的精度3.根據參數criterion和max_depth得出你初步的結論。"""import matplotlib.pyplot as pltimport numpy as npimport pandas as pdimport mglearn from sklearn.model_selection import train_test_split#導入乳腺癌數據集from sklearn.datasets import load_breast_cancerfrom sklearn.tree import DecisionTreeClassifier#決策樹并非深度越大越好,考慮過擬合的問題#mglearn.plots.plot_animal_tree()#mglearn.plots.plot_tree_progressive()#獲取數據集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)#查看訓練集和測試集數據      print('train dataset :{0} ;test dataset :{1}'.format(X_train.shape,X_test.shape))#建立模型(基尼不純度算法(gini)),使用不同最大深度和隨機狀態和不同的算法看模型評分tree = DecisionTreeClassifier(random_state = 0,criterion = 'gini',max_depth = 5)#訓練模型tree.fit(X_train,y_train)#評估模型print("Accuracy(準確性) on training set: {:.3f}".format(tree.score(X_train, y_train)))print("Accuracy(準確性) on test set: {:.3f}".format(tree.score(X_test, y_test)))print(tree)# 參數選擇 max_depth,算法選擇基尼不純度算法(gini) or 信息熵(entropy)def Tree_score(depth = 3,criterion = 'entropy'): """ 參數為max_depth(默認為3)和criterion(默認為信息熵entropy), 函數返回模型的訓練精度和測試精度 """ tree = DecisionTreeClassifier(criterion = criterion,max_depth = depth) tree.fit(X_train,y_train) train_score = tree.score(X_train, y_train) test_score = tree.score(X_test, y_test) return (train_score,test_score)#gini算法,深度對模型精度的影響depths = range(2,25)#考慮到數據集有30個屬性scores = [Tree_score(d,'gini') for d in depths]train_scores = [s[0] for s in scores]test_scores = [s[1] for s in scores]plt.figure(figsize = (6,6),dpi = 144)plt.grid()plt.xlabel("max_depth of decision Tree")plt.ylabel("score")plt.title("'gini'")plt.plot(depths,train_scores,'.g-',label = 'training score')plt.plot(depths,test_scores,'.r--',label = 'testing score')plt.legend()#信息熵(entropy),深度對模型精度的影響scores = [Tree_score(d) for d in depths]train_scores = [s[0] for s in scores]test_scores = [s[1] for s in scores]plt.figure(figsize = (6,6),dpi = 144)plt.grid()plt.xlabel("max_depth of decision Tree")plt.ylabel("score")plt.title("'entropy'")plt.plot(depths,train_scores,'.g-',label = 'training score')plt.plot(depths,test_scores,'.r--',label = 'testing score')plt.legend()

運行結果:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成人免费一区二区三区在线观看 | 色诱亚洲精品久久久久久 | xnxx18日本 | 久久久国产精品免费观看 | 狠狠干导航 | 精品国产一区二区三区久久久蜜月 | 看一级毛片 | 在线观看精品视频 | 久久伊人精品视频 | 午夜精品在线视频 | 福利在线小视频 | 成人毛片免费播放 | 黄色av网站在线观看 | 午夜视频中文字幕 | 超久久 | 亚洲免费在线看 | 伊人亚洲精品 | 亚洲精品成人在线视频 | 广州毛片 | 日韩精品中文字幕在线播放 | 亚洲视频综合 | 久草视频手机在线观看 | 亚洲精品日韩色噜噜久久五月 | 免费啪视频在线观看 | 国产精品99久久久久久大便 | 依依成人精品视频 | 国产精品一区二区三区在线播放 | 免费一级特黄毛片 | 午夜精品福利视频 | 亚洲第一色片 | 日韩精品无码一区二区三区 | 亚洲国产精品二区 | 91精品国产99久久久久久红楼 | 中文字幕极速在线观看 | 欧美一级做性受免费大片免费 | 国产精选在线 | 免费看搡女人无遮挡的视频 | 日韩视频一区二区在线观看 | 成人不卡免费视频 | 亚洲网站一区 | 逼片视频|