使用python機器學習怎么實現(xiàn)決策樹-創(chuàng)新互聯(lián)

這期內(nèi)容當中小編將會給大家?guī)碛嘘P使用python機器學習怎么實現(xiàn)決策樹,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)專注于靖邊網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供靖邊營銷型網(wǎng)站建設,靖邊網(wǎng)站制作、靖邊網(wǎng)頁設計、靖邊網(wǎng)站官網(wǎng)定制、成都微信小程序服務,打造靖邊網(wǎng)絡公司原創(chuàng)品牌,更為您提供靖邊網(wǎng)站排名全網(wǎng)營銷落地服務。

具體內(nèi)容如下

# -*- coding: utf-8 -*-
"""
Created on Sat Nov 9 10:42:38 2019

@author: asus
"""
"""
決策樹
目的:
1. 使用決策樹模型
2. 了解決策樹模型的參數(shù)
3. 初步了解調(diào)參數(shù)
要求:
基于乳腺癌數(shù)據(jù)集完成以下任務:
1.調(diào)整參數(shù)criterion,使用不同算法信息熵(entropy)和基尼不純度算法(gini)
2.調(diào)整max_depth參數(shù)值,查看不同的精度
3.根據(jù)參數(shù)criterion和max_depth得出你初步的結論。
"""

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import mglearn 
from sklearn.model_selection import train_test_split
#導入乳腺癌數(shù)據(jù)集
from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier


#決策樹并非深度越大越好,考慮過擬合的問題
#mglearn.plots.plot_animal_tree()
#mglearn.plots.plot_tree_progressive()

#獲取數(shù)據(jù)集
cancer = load_breast_cancer()
#對數(shù)據(jù)集進行切片
X_train,X_test,y_train,y_test = train_test_split(cancer.data,cancer.target,
       stratify = cancer.target,random_state = 42)
#查看訓練集和測試集數(shù)據(jù)      
print('train dataset :{0} ;test dataset :{1}'.format(X_train.shape,X_test.shape))
#建立模型(基尼不純度算法(gini)),使用不同大深度和隨機狀態(tài)和不同的算法看模型評分
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)


# 參數(shù)選擇 max_depth,算法選擇基尼不純度算法(gini) or 信息熵(entropy)
def Tree_score(depth = 3,criterion = 'entropy'):
 """
 參數(shù)為max_depth(默認為3)和criterion(默認為信息熵entropy),
 函數(shù)返回模型的訓練精度和測試精度
 """
 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)#考慮到數(shù)據(jù)集有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()

運行結果:

使用python機器學習怎么實現(xiàn)決策樹

使用python機器學習怎么實現(xiàn)決策樹

使用python機器學習怎么實現(xiàn)決策樹

上述就是小編為大家分享的使用python機器學習怎么實現(xiàn)決策樹了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)成都網(wǎng)站設計公司行業(yè)資訊頻道。

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

文章標題:使用python機器學習怎么實現(xiàn)決策樹-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://muchs.cn/article24/dchgje.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供電子商務全網(wǎng)營銷推廣、ChatGPT動態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、標簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設