机器学习——决策树的实现
生活随笔
收集整理的這篇文章主要介紹了
机器学习——决策树的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#!/usr/bin/env python
#-*-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#讀數據
allElectronicsData=open(r'jueceshu.csv','rb')
reader=csv.reader(allElectronicsData)
headers=reader.next()featureList=[]
labelList=[]
#分析數據
for row in reader:#print(row)if(row):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)
#轉化數據
vec=DictVectorizer()
dummyX=vec.fit_transform(featureList).toarray()print('dummyX:'+str(dummyX))
print(vec.get_feature_names())print('labelList:'+str(labelList))lb=preprocessing.LabelBinarizer()
dummyY=lb.fit_transform(labelList)
print('dummyY:'+str(dummyX))#訓練數據
clf=tree.DecisionTreeClassifier(criterion='entropy')
clf=clf.fit(dummyX,dummyY)
print('clf'+str(clf))
#轉化為dot模式
with open('allElectronicInformationGainDri.dot','w') as f:f=tree.export_graphviz(clf,feature_names=vec.get_feature_names(),out_file=f)#決策樹的預測
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))
samples = 14
value = [5, 9]
True
entropy = 0.0
samples = 5
value = [0, 5]
False
age=senior <= 0.5
entropy = 0.5436
samples = 8
value = [1, 7] age=youth <= 0.5
entropy = 0.9183
samples = 6
value = [4, 2]
credit_rating=excellent <= 0.5
entropy = 0.9183
samples = 3
value = [1, 2] credit_rating=excellent <= 0.5
entropy = 0.9183
samples = 3
value = [1, 2]
entropy = 0.0
samples = 2
value = [0, 2]
entropy = 0.0
samples = 1
value = [1, 0]
entropy = 0.0
samples = 2
value = [0, 2]
entropy = 0.0
samples = 3
value = [3, 0]
entropy = 0.0
samples = 1
2. ?Python機器學習的庫:scikit-learn
2.1: 特性: 簡單高效的數據挖掘和機器學習分析 對所有用戶開放,根據不同需求高度可重用性 基于Numpy, SciPy和matplotlib 開源,商用級別:獲得 BSD許可
2.2 覆蓋問題領域: 分類(classification), 回歸(regression), 聚類(clustering), 降維(dimensionality reduction) 模型選擇(model selection), 預處理(preprocessing)
3. 使用用scikit-learn 安裝scikit-learn: pip, easy_install, windows installer 安裝必要package:numpy, SciPy和matplotlib, 可使用Anaconda (包含numpy, scipy等科學計算常用 package) ? ? ?安裝注意問題:Python解釋器版本(2.7 or 3.4?), 32-bit or 64-bit系統
4. 例子:
解釋Python代碼
安裝?Graphviz:?http://www.graphviz.org/ 配置環境變量
? ? ? 轉化dot文件至pdf可視化決策樹:dot -Tpdf iris.dot -o outpu.pdf
1,youth,high,no,fair,no
2,youth,high,no,excellent,no
3,middle_aged,high,no,fair,yes
4,senior,medium,no,fair,yes
5,senior,low,yes,fair,yes
6,senior,low,yes,excellent,no
7,middle_aged,low,yes,excellent,yes
8,youth,medium,no,fair,no
9,youth,low,yes,fair,yes
10,senior,medium,yes,fair,yes
11,youth,medium,yes,excellent,yes
12,middle_aged,medium,yes,excellent,yes
13,middle_aged,high,yes,fair,yes
14,senior,medium,no,excellent,no
student=no <= 0.5
entropy = 0.9403samples = 14
value = [5, 9]
True
entropy = 0.0
samples = 5
value = [0, 5]
False
age=senior <= 0.5
entropy = 0.5436
samples = 8
value = [1, 7] age=youth <= 0.5
entropy = 0.9183
samples = 6
value = [4, 2]
credit_rating=excellent <= 0.5
entropy = 0.9183
samples = 3
value = [1, 2] credit_rating=excellent <= 0.5
entropy = 0.9183
samples = 3
value = [1, 2]
entropy = 0.0
samples = 2
value = [0, 2]
entropy = 0.0
samples = 1
value = [1, 0]
entropy = 0.0
samples = 2
value = [0, 2]
entropy = 0.0
samples = 3
value = [3, 0]
entropy = 0.0
samples = 1
value = [1, 0]
1. Python
2. ?Python機器學習的庫:scikit-learn
2.1: 特性: 簡單高效的數據挖掘和機器學習分析 對所有用戶開放,根據不同需求高度可重用性 基于Numpy, SciPy和matplotlib 開源,商用級別:獲得 BSD許可
2.2 覆蓋問題領域: 分類(classification), 回歸(regression), 聚類(clustering), 降維(dimensionality reduction) 模型選擇(model selection), 預處理(preprocessing)
3. 使用用scikit-learn 安裝scikit-learn: pip, easy_install, windows installer 安裝必要package:numpy, SciPy和matplotlib, 可使用Anaconda (包含numpy, scipy等科學計算常用 package) ? ? ?安裝注意問題:Python解釋器版本(2.7 or 3.4?), 32-bit or 64-bit系統
4. 例子:
解釋Python代碼
安裝?Graphviz:?http://www.graphviz.org/ 配置環境變量
? ? ? 轉化dot文件至pdf可視化決策樹:dot -Tpdf iris.dot -o outpu.pdf
數據集:
RiD,age,income,student,credit_rating,Class_buys_computer1,youth,high,no,fair,no
2,youth,high,no,excellent,no
3,middle_aged,high,no,fair,yes
4,senior,medium,no,fair,yes
5,senior,low,yes,fair,yes
6,senior,low,yes,excellent,no
7,middle_aged,low,yes,excellent,yes
8,youth,medium,no,fair,no
9,youth,low,yes,fair,yes
10,senior,medium,yes,fair,yes
11,youth,medium,yes,excellent,yes
12,middle_aged,medium,yes,excellent,yes
13,middle_aged,high,yes,fair,yes
14,senior,medium,no,excellent,no
結果:
結果解釋:
?字典代表每一行,每個測試樣例,每個測試集
dummyX代表格式的轉換
? ? ? ? labelList代表結果集
dummyY代表結果集的格式化
clfDecisionTreeClassifier代表決策樹分類器
oneRowX代表其中一個測試集
? ? ? ? newRowX代表一個新的測試集
? ? ? ? predictedY代表預測的結果
? ? ?
? ??
? ? ??
總結
以上是生活随笔為你收集整理的机器学习——决策树的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构实验之栈:行编辑器
- 下一篇: mongodb最详细的安装与配置