日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python决策树的应用_机器学习-决策树实战应用

發(fā)布時(shí)間:2025/3/8 python 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python决策树的应用_机器学习-决策树实战应用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.下載

2.安裝:雙擊

3.創(chuàng)建桌面快捷方式

安裝目錄\bin文件夾\:找到gvedit.exe文件右鍵 發(fā)送到桌面快捷方式,如下圖:

4.配置環(huán)境變量

將graphviz安裝目錄下的bin文件夾添加到Path環(huán)境變量中:

5.驗(yàn)證是否安裝并配置成功

進(jìn)入windows命令行界面,輸入dot -version,然后按回車,如果顯示graphviz的相關(guān)版本信息,則安裝配置成功。如圖:

6.python環(huán)境中安裝:(pycharm中)

File->Settings->Project:Python

然后輸入graphivz安裝

安裝需要等待一會(huì)。。。。

決策樹實(shí)戰(zhàn)代碼

# -*- coding:utf-8 -*-

from sklearn.feature_extraction import DictVectorizer

import csv

from sklearn import preprocessing

from sklearn import tree

from sklearn.externals.six import StringIO

#read the csv file

allElectronicsDate = open(r'D:\Python\date\AllElectronics.csv','rt')

reader = csv.reader(allElectronicsDate)

headers = next(reader)

# headers = reader.next()

print(headers)#打印輸出第一行標(biāo)題

#['RID', 'age', 'income', 'student', 'credit_rating', 'Class_buys_computer']

featureList = [] #用來存儲(chǔ)特征值

labelList = [] #用來存儲(chǔ)類標(biāo)簽

#獲取特征值并打印輸出

for row in reader:

labelList.append(row[len(row) - 1])#每一行最后的值,類標(biāo)簽

rowDict = {}

for i in range(1,len(row) - 1):#每一行 遍歷除第一列和最后一列的值

rowDict[headers[i]] = row[i]

featureList.append(rowDict)

print(featureList)

#vectorize feature 使用sklearn自帶的方法將特征值離散化為數(shù)字標(biāo)記

vec = DictVectorizer()

dummyX = vec.fit_transform(featureList).toarray()

print("dummyY:" + str(dummyX))

print(vec.get_feature_names())

# print("feature_name" + str(vec.get_feature_names()))

print("labelList:" + str(labelList))

#vectorize class labels #數(shù)字化類標(biāo)簽

lb = preprocessing.LabelBinarizer()

dummyY = lb.fit_transform(labelList)

print("dummyY:" + str(dummyY))

#use the decision tree for classification

clf = tree.DecisionTreeClassifier(criterion='entropy')

clf = clf.fit(dummyX,dummyY) #構(gòu)建決策樹

#打印構(gòu)建決策樹采用的參數(shù)

print("clf:" + str(clf))

#visilize the model

with open("allElectronicInformationGainOri.dot",'w') as f:

f=tree.export_graphviz(clf,feature_names=vec.get_feature_names(),out_file=f)

#這時(shí)就生成了allElectronicInformationGainOri.dot文件

# dot -Tpdf in.dot -o out.pdf dot文件輸出為pdf文件

#驗(yàn)證數(shù)據(jù),取出一行數(shù)據(jù),修改幾個(gè)屬性預(yù)測結(jié)果

oneRowX = dummyX[0,:]

print("oneRowX:" + str(oneRowX))

newRowX = oneRowX

newRowX[0] = 1

newRowX[2] = 0

print("newRowX:" + str(newRowX))

predictedY = clf.predict(newRowX)

print("predictedY:"+str(predictedY))

結(jié)果:

['RID', 'age', 'income', 'student', 'credit_rating', 'class_buys_computer']

[{'income': 'high', 'age': 'youth', 'student': 'no', 'credit_rating': 'fair'}, {'income': 'high', 'age': 'youth', 'student': 'no', 'credit_rating': 'excellent'}, {'income': 'high', 'age': 'middle_aged', 'student': 'no', 'credit_rating': 'fair'}, {'income': 'medium', 'age': 'senior', 'student': 'no', 'credit_rating': 'fair'}, {'income': 'low', 'age': 'senior', 'student': 'yes', 'credit_rating': 'fair'}, {'income': 'low', 'age': 'senior', 'student': 'yes', 'credit_rating': 'excellent'}, {'income': 'low', 'age': 'middle_aged', 'student': 'yes', 'credit_rating': 'excellent'}, {'income': 'medium', 'age': 'youth', 'student': 'no', 'credit_rating': 'fair'}, {'income': 'low', 'age': 'youth', 'student': 'yes', 'credit_rating': 'fair'}, {'income': 'medium', 'age': 'senior', 'student': 'yes', 'credit_rating': 'fair'}, {'income': 'medium', 'age': 'youth', 'student': 'yes', 'credit_rating': 'excellent'}, {'income': 'medium', 'age': 'middle_aged', 'student': 'no', 'credit_rating': 'excellent'}, {'income': 'high', 'age': 'middle_aged', 'student': 'yes', 'credit_rating': 'fair'}, {'income': 'medium', 'age': 'senior', 'student': 'no', 'credit_rating': 'excellent'}]

dummyY:[[ 0. 0. 1. 0. 1. 1. 0. 0. 1. 0.]

[ 0. 0. 1. 1. 0. 1. 0. 0. 1. 0.]

[ 1. 0. 0. 0. 1. 1. 0. 0. 1. 0.]

[ 0. 1. 0. 0. 1. 0. 0. 1. 1. 0.]

[ 0. 1. 0. 0. 1. 0. 1. 0. 0. 1.]

[ 0. 1. 0. 1. 0. 0. 1. 0. 0. 1.]

[ 1. 0. 0. 1. 0. 0. 1. 0. 0. 1.]

[ 0. 0. 1. 0. 1. 0. 0. 1. 1. 0.]

[ 0. 0. 1. 0. 1. 0. 1. 0. 0. 1.]

[ 0. 1. 0. 0. 1. 0. 0. 1. 0. 1.]

[ 0. 0. 1. 1. 0. 0. 0. 1. 0. 1.]

[ 1. 0. 0. 1. 0. 0. 0. 1. 1. 0.]

[ 1. 0. 0. 0. 1. 1. 0. 0. 0. 1.]

[ 0. 1. 0. 1. 0. 0. 0. 1. 1. 0.]]

['age=middle_aged', 'age=senior', 'age=youth', 'credit_rating=excellent', 'credit_rating=fair', 'income=high', 'income=low', 'income=medium', 'student=no', 'student=yes']

labelList:['no', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no']

dummyY:[[0]

[0]

[1]

[1]

[1]

[0]

[1]

[0]

[1]

[1]

[1]

[1]

[1]

[0]]

clf:DecisionTreeClassifier(class_weight=None, criterion='entropy', max_depth=None,

max_features=None, max_leaf_nodes=None, min_samples_leaf=1,

min_samples_split=2, min_weight_fraction_leaf=0.0,

random_state=None, splitter='best')

oneRowX:[ 0. 0. 1. 0. 1. 1. 0. 0. 1. 0.]

newRowX:[ 1. 0. 0. 0. 1. 1. 0. 0. 1. 0.]

predictedY:[1]

在項(xiàng)目路徑里面打開dot文件

將dot文件轉(zhuǎn)化為直觀的PDF文件(dos 里面輸入dot -Tpdf D:\Python\機(jī)器學(xué)習(xí)\allElectronicInformationGainOri.dot -o D:\Python\機(jī)器學(xué)習(xí)\out.pdf 然后回車)

dot -Tpdf D:\Python\機(jī)器學(xué)習(xí)\allElectronicInformationGainOri.dot -o D:\Python\機(jī)器學(xué)習(xí)\out.pdf

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的python决策树的应用_机器学习-决策树实战应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。