python使用sklearn實(shí)現(xiàn)決策樹的方法示例
1. 基本環(huán)境
安裝 anaconda 環(huán)境, 由于國(guó)內(nèi)登陸不了他的官網(wǎng) https://www.continuum.io/downloads, 不過可以使用國(guó)內(nèi)的鏡像站點(diǎn): https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
添加繪圖工具 Graphviz http://www.graphviz.org/Download_windows.php
安裝后, 將bin 目錄內(nèi)容添加到環(huán)境變量path 即可
參考blog : http://www.dbjr.com.cn/article/169878.htm
官網(wǎng)技術(shù)文檔 : http://scikit-learn.org/stable/modules/tree.html#tree-algorithms-id3-c4-5-c5-0-and-cart
2. 遇到的一些問題
csv 文件讀取 https://docs.python.org/3.5/library/csv.html?highlight=csv#module-csv
https://docs.python.org/2/library/csv.html?highlight=csv#module-csv
3. 實(shí)現(xiàn)
數(shù)據(jù)文件:
這是一個(gè)給定 4 個(gè)屬性, age, income, student, credit_rating 以及 一個(gè) 標(biāo)記屬性 class_buys_computer 的數(shù)據(jù)集, 我們需要根據(jù)這個(gè)數(shù)據(jù)集進(jìn)行分析并構(gòu)建一顆決策樹
代碼實(shí)現(xiàn):
核心就是調(diào)用 tree 的 DecisionTreeClassifier 方法對(duì)數(shù)據(jù)進(jìn)行 訓(xùn)練得到一顆決策樹
# -*- coding: utf-8 -*- """ Created on Sun Dec 25 11:25:40 2016 @author: Administrator """ from sklearn.feature_extraction import DictVectorizer import csv from sklearn import tree from sklearn import preprocessing from sklearn.externals.six import StringIO import pydotplus from IPython.display import Image # Read in the csv file and put features into list of dict and list of class label allElectornicsData = open('AllElectronics.csv', 'r') reader = csv.reader(allElectornicsData) # headers = reader.next() python2.7 supported 本質(zhì)獲取csv 文件的第一行數(shù)據(jù) #headers = reader.__next__() python 3.5.2 headers = next(reader) print(headers) featureList = [] labelList = [] for row in reader: labelList.append(row[len(row) - 1]) rowDict = {} for i in range(1, len(row) - 1): rowDict[headers[i]] = row[i] featureList.append(rowDict) print(featureList) print(labelList) # Vetorize features vec = DictVectorizer() dummyX = vec.fit_transform(featureList).toarray() print("dummyX: " + str(dummyX)) print(vec.get_feature_names()) print("labelList: " + str(labelList)) # vectorize class labels lb = preprocessing.LabelBinarizer() dummyY = lb.fit_transform(labelList) print("dummyY: ", str(dummyY)) # Using decision tree for classification ===========【此處調(diào)用為算法核心】============ #clf = tree.DecisionTreeClassifier(criterion='entropy') clf = tree.DecisionTreeClassifier(criterion='gini') clf = clf.fit(dummyX, dummyY) print("clf: ", str(clf)) # Visualize model # dot -Tpdf iris.dot -o ouput.pdf with open("allElectronicInformationGainOri.dot", 'w') as f: f = tree.export_graphviz(clf, feature_names = vec.get_feature_names(), out_file = f) # predict 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é)果:
ID3 算法
CART 算法
4. 決策樹的優(yōu)缺點(diǎn)
決策樹的優(yōu)勢(shì)
- 簡(jiǎn)單易用,而且輸出的結(jié)果易于解釋,樹能夠被圖形化,加深了直觀的理解。
- 幾乎不需要對(duì)數(shù)據(jù)進(jìn)行預(yù)處理。
- 算法的開銷不大,而且決策樹一旦建立,對(duì)于未知樣本的分類十分快,最壞情況下的時(shí)間復(fù)雜度是O(w),w是樹的最大深度。
- 能夠用于多類的分類。
- 能夠容忍噪點(diǎn)。
決策樹的劣勢(shì)
- 容易過擬合。
- 容易被類別中占多數(shù)的類影響而產(chǎn)生bias,所以推薦在送入算法之間先平衡下數(shù)據(jù)中各個(gè)類別所占的比例。
- 決策樹采用的是自頂向下的遞歸劃分法,因此自定而下到了末端枝葉包含的數(shù)據(jù)量會(huì)很少,我們會(huì)依據(jù)很少的數(shù)據(jù)量取做決策,這樣的決策是不具有統(tǒng)計(jì)意義的,這就是數(shù)據(jù)碎片的問題。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python環(huán)境功能強(qiáng)大的pip-audit安全漏洞掃描工具
這篇文章主要為大家介紹了python環(huán)境中功能強(qiáng)大的pip-audit安全漏洞掃描工具的功能介紹及安裝使用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02Python如何利用IMAP實(shí)現(xiàn)郵箱客戶端功能
IMAP是另一種讀取電子郵件的協(xié)議,IMAP是讀取郵件服務(wù)器的電子郵件與公布欄信息的方法,也就是說IMAP 允許客戶端的郵件程序存取遠(yuǎn)程的信息,這篇文章主要給大家介紹了關(guān)于Python如何利用IMAP實(shí)現(xiàn)郵箱客戶端功能的相關(guān)資料,需要的朋友可以參考下2021-09-09python無(wú)限生成不重復(fù)(字母,數(shù)字,字符)組合的方法
今天小編就為大家分享一篇python無(wú)限生成不重復(fù)(字母,數(shù)字,字符)組合的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12關(guān)于阿里云oss獲取sts憑證 app直傳 python的實(shí)例
今天小編就為大家分享一篇關(guān)于阿里云oss獲取sts憑證 app直傳 python的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08