1. 基本環(huán)境
創(chuàng)新互聯(lián)專業(yè)提供服務(wù)器托管服務(wù),為用戶提供五星數(shù)據(jù)中心、電信、雙線接入解決方案,用戶可自行在線購買服務(wù)器托管服務(wù),并享受7*24小時金牌售后服務(wù)。安裝 anaconda 環(huán)境, 由于國內(nèi)登陸不了他的官網(wǎng) https://www.continuum.io/downloads, 不過可以使用國內(nèi)的鏡像站點: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
添加繪圖工具 Graphviz http://www.graphviz.org/Download_windows.php
安裝后, 將bin 目錄內(nèi)容添加到環(huán)境變量path 即可
參考blog : https://www.jb51.net/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. 實現(xiàn)
數(shù)據(jù)文件:
這是一個給定 4 個屬性, age, income, student, credit_rating 以及 一個 標(biāo)記屬性 class_buys_computer 的數(shù)據(jù)集, 我們需要根據(jù)這個數(shù)據(jù)集進(jìn)行分析并構(gòu)建一顆決策樹
代碼實現(xiàn):
核心就是調(diào)用 tree 的 DecisionTreeClassifier 方法對數(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))
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。