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

首頁(yè) > 開(kāi)發(fā) > Python > 正文

python機(jī)器學(xué)習(xí)實(shí)現(xiàn)決策樹(shù)

2024-09-09 19:02:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了python機(jī)器學(xué)習(xí)實(shí)現(xiàn)決策樹(shù)的具體代碼,供大家參考,具體內(nèi)容如下

# -*- coding: utf-8 -*-"""Created on Sat Nov 9 10:42:38 2019@author: asus""""""決策樹(shù)目的:1. 使用決策樹(shù)模型2. 了解決策樹(shù)模型的參數(shù)3. 初步了解調(diào)參數(shù)要求:基于乳腺癌數(shù)據(jù)集完成以下任務(wù):1.調(diào)整參數(shù)criterion,使用不同算法信息熵(entropy)和基尼不純度算法(gini)2.調(diào)整max_depth參數(shù)值,查看不同的精度3.根據(jù)參數(shù)criterion和max_depth得出你初步的結(jié)論。"""import matplotlib.pyplot as pltimport numpy as npimport pandas as pdimport mglearn from sklearn.model_selection import train_test_split#導(dǎo)入乳腺癌數(shù)據(jù)集from sklearn.datasets import load_breast_cancerfrom sklearn.tree import DecisionTreeClassifier#決策樹(shù)并非深度越大越好,考慮過(guò)擬合的問(wèn)題#mglearn.plots.plot_animal_tree()#mglearn.plots.plot_tree_progressive()#獲取數(shù)據(jù)集cancer = load_breast_cancer()#對(duì)數(shù)據(jù)集進(jìn)行切片X_train,X_test,y_train,y_test = train_test_split(cancer.data,cancer.target,       stratify = cancer.target,random_state = 42)#查看訓(xùn)練集和測(cè)試集數(shù)據(jù)      print('train dataset :{0} ;test dataset :{1}'.format(X_train.shape,X_test.shape))#建立模型(基尼不純度算法(gini)),使用不同最大深度和隨機(jī)狀態(tài)和不同的算法看模型評(píng)分tree = DecisionTreeClassifier(random_state = 0,criterion = 'gini',max_depth = 5)#訓(xùn)練模型tree.fit(X_train,y_train)#評(píng)估模型print("Accuracy(準(zhǔn)確性) on training set: {:.3f}".format(tree.score(X_train, y_train)))print("Accuracy(準(zhǔn)確性) on test set: {:.3f}".format(tree.score(X_test, y_test)))print(tree)# 參數(shù)選擇 max_depth,算法選擇基尼不純度算法(gini) or 信息熵(entropy)def Tree_score(depth = 3,criterion = 'entropy'): """ 參數(shù)為max_depth(默認(rèn)為3)和criterion(默認(rèn)為信息熵entropy), 函數(shù)返回模型的訓(xùn)練精度和測(cè)試精度 """ 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算法,深度對(duì)模型精度的影響depths = range(2,25)#考慮到數(shù)據(jù)集有30個(gè)屬性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),深度對(duì)模型精度的影響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()

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 99re热视频这里只精品 | 欧美成人精品一级 | 少妇激情视频 | 国产精品久久久久久久久久久久久久久久 | 日韩色电影 | hd性videos意大利复古 | 在线亚洲播放 | 国产成人精品区一区二区不卡 | 欧美日韩亚洲在线观看 | 国产中出视频 | 亚洲一级毛片 | 精品久久久久久久久久中文字幕 | 一级黄色免费 | 国产精品片一区二区三区 | 日韩av影片在线观看 | 中文字幕精品一区久久久久 | 欧美性生活xxxxx | 国产影院在线观看 | 国产在线精品一区二区三区不卡 | 亚洲精品午夜视频 | 欧美日韩一区,二区,三区,久久精品 | 亚洲成人伊人 | h视频在线免费观看 | 91成人亚洲 | 国产精品区一区二区三区 | 久久久三区 | 国产正在播放 | 色戒在线版 | 国产自在线 | 国产一级毛片国产 | 久久免费精品视频 | 亚洲第一页中文字幕 | 天天夜碰日日摸日日澡性色av | 亚洲精华液久久含羞草 | 在线免费观看毛片 | 在线免费日本 | 草草久久久| 久久欧美亚洲另类专区91大神 | 欧美成人一区免费视频 | 亚洲网站免费 | 国产午夜三级一区二区三桃花影视 |