python機(jī)器學(xué)習(xí)實(shí)現(xiàn)決策樹
本文實(shí)例為大家分享了python機(jī)器學(xué)習(xí)實(shí)現(xiàn)決策樹的具體代碼,供大家參考,具體內(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ù)集完成以下任務(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 plt
import numpy as np
import pandas as pd
import mglearn
from sklearn.model_selection import train_test_split
#導(dǎo)入乳腺癌數(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ù)集進(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)練集和測試集數(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)練精度和測試精度
"""
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個(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),深度對模型精度的影響
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é)果:



很明顯看的出來,決策樹深度越大,訓(xùn)練集擬合效果越好,但是往往面對測試集的預(yù)測效果會(huì)下降,這就是過擬合。
參考書籍: 《Python機(jī)器學(xué)習(xí)基礎(chǔ)教程》
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python機(jī)器學(xué)習(xí)算法之決策樹算法的實(shí)現(xiàn)與優(yōu)缺點(diǎn)
- Python機(jī)器學(xué)習(xí)之決策樹
- Python機(jī)器學(xué)習(xí)算法庫scikit-learn學(xué)習(xí)之決策樹實(shí)現(xiàn)方法詳解
- python機(jī)器學(xué)習(xí)理論與實(shí)戰(zhàn)(二)決策樹
- Python機(jī)器學(xué)習(xí)之決策樹算法
- python機(jī)器學(xué)習(xí)之決策樹分類詳解
- Python機(jī)器學(xué)習(xí)之決策樹算法實(shí)例詳解
- 機(jī)器學(xué)習(xí)python實(shí)戰(zhàn)之決策樹
- 分析機(jī)器學(xué)習(xí)之決策樹Python實(shí)現(xiàn)
相關(guān)文章
python使用PySimpleGUI設(shè)置進(jìn)度條及控件使用
PySimpleGUI是一個(gè)在tkinter基礎(chǔ)上的,足夠簡單,方便,pythonic的GUI庫.本文給大家介紹python使用PySimpleGUI設(shè)置進(jìn)度條的方法及進(jìn)度條控件使用代碼,感興趣的朋友跟隨小編一起看看吧2021-06-06
如何使用Typora+MinIO+Python代碼打造舒適協(xié)作環(huán)境
這篇文章主要介紹了如何使用Typora+MinIO+Python代碼打造舒適協(xié)作環(huán)境,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
Python接口自動(dòng)化淺析logging日志原理及模塊操作流程
這篇文章主要為大家介紹了Python接口自動(dòng)化系列文章淺析logging日志原理及模塊操作流程,文中詳細(xì)說明了為什么需要日志?日志是什么?以及日志用途等基本的原理2021-08-08

