python3 sklearn之決策樹

2021-09-24 10:17:03 字數 3727 閱讀 7045

1,問題描述:

小王的目的是通過下週天氣預報尋找什麼時候人們會運動,以適時調整雇員數量。因此首先他必須了解人們決定是否運動的原因。

在2周時間內我們得到以下記錄:

天氣狀況有晴,雲和雨;氣溫用華氏溫度表示;相對濕度用百分比;還有有無風。當然還有顧客是不是在這些日子是否運動。最終他得到了14列5行的資料**:

2,決策樹的演算法描述:建立乙個data.py檔案

from sklearn.feature_extraction import dictvectorizer #將dict型別的list資料,轉換成numpy array

import csv #讀取csv檔案

from sklearn import tree #匯入決策樹演算法

from sklearn import preprocessing #資料預處理

alldata=open(r'c:\users\14355\sun.csv','r',encoding='utf-8') # 讀入資料

reader=csv.reader(alldata) #用reader來代表所有資料

headers=next(reader) #header是指資料的第一行,就是資料的特徵

print(headers) #因為sk包的資料輸入是有一定的要求的,他的輸入不能是類別。只能是 0,1。所以要先將資料轉化成dict型別

label= #定義乙個存放標籤的列表

#然後用dictvextorizer的函式進行轉換

feature= #定義乙個存放特徵的列表

for row in reader: #以行遍歷所有資料

rowdict={} #定義乙個字典型別

for i in range(1,len(row)-1 ):

if row[i].isdigit() and i>1:#對那些連續的屬性值做乙個簡單的離散化

row[i]= int(int(row[i])/10)

rowdict[headers[i]]=row[i] #把每條資料的 特徵:屬性 插入字典

print()

print(label)

print()

print(feature)

vec=dictvectorizer() #例項化

dummx=vec.fit_transform(feature).toarray() #使用例項內的方法對資料進行轉換

print()

print(vec.get_feature_names())#列印出轉換後的資料的表頭

print()

print(str(dummx)) #列印出轉換後的資料的錶值

lb=preprocessing.labelbinarizer() #例項化

dummy=lb.fit_transform(label) #資料的轉換

print(str(dummy))

#上面的過程就是把資料都轉換成符合格式的資料了

#可以呼叫sk包中的決策樹演算法進行分類

clf=tree.decisiontreeclassifier(criterion='gini') #使用基尼指數的方法

clf=clf.fit(dummx,dummy) #構建決策樹

print("clf: " + str(clf))#列印

#輸出到td3.dot檔案中

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

f = tree.export_graphviz(clf, feature_names=vec.get_feature_names(), out_file=f) #要安裝視覺化工具graphviz

#**資料

onerow=dummx[0,:]#取出一條資料進行修改

newrow=onerow

print(newrow)

onerow[0]=6

onerow[2]=1

print(onerow)

#進行**

predicty=clf.predict([newrow])

print("predicty"+str(predicty))

執行結果如下:

runfile('c:/users/14355/decitree.py', wdir='c:/users/14355')

['no', 'outlook', 'temperature', 'humidity', 'windy', 'play']

['n', 'n', 'y', 'y', 'y', 'n', 'y', 'n', 'y', 'n', 'y', 'n', 'y', 'n']

[, , , , , , , , , , , , , ]

['humidity', 'outlook=overcast', 'outlook=rain', 'outlook=sunny', 'temperature', 'windy=false', 'windy=true']

[[6. 0. 0. 1. 7. 1. 0.]

[8. 0. 0. 1. 6. 0. 1.]

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

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

[8. 0. 1. 0. 5. 1. 0.]

[7. 0. 1. 0. 6. 0. 1.]

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

[9. 0. 0. 1. 7. 1. 0.]

[7. 0. 0. 1. 6. 1. 0.]

[8. 0. 1. 0. 7. 1. 0.]

[6. 0. 0. 1. 7. 0. 1.]

[8. 1. 0. 0. 7. 0. 1.]

[9. 1. 0. 0. 8. 1. 0.]

[8. 0. 1. 0. 7. 0. 1.]]

[[0]

[0][1]

[1][1]

[0][1]

[0][1]

[0][1]

[0][1]

[0]]

clf: decisiontreeclassifier(class_weight=none, criterion='gini', max_depth=none,

max_features=none, max_leaf_nodes=none,

min_impurity_decrease=0.0, min_impurity_split=none,

min_samples_leaf=1, min_samples_split=2,

min_weight_fraction_leaf=0.0, presort=false, random_state=none,

splitter='best')

[6. 0. 0. 1. 7. 1. 0.]

[6. 0. 1. 1. 7. 1. 0.]

predicty[0]

輸出檔案為123.dot檔案。

digraph tree
4,window下面dot檔案轉為pdf檔案。

轉dot為pdf圖如下所示:

python機器學習之 sklearn 嶺回歸

今天看了會l2正則化,大致的意思就是在懲戒函式中加入高次項的懲戒項,用來減小線性回歸中高次項的係數,避免過擬合的狀態。這樣就產生了一種具有正則化的線性回歸模型 嶺回歸。sklearn.linear model.ridge alpha 1.0 具有正則化的線性最小二乘法 alpha 表示正則化力度 c...

基於sklearn的感知機python3

首先,本文還是選用python裡面自帶的digits資料集 from sklearn.datasets import load digits digits load digits 資料標準化 from sklearn.preprocessing import standardscaler scale...

sklearn之嶺回歸

嶺回歸 ridge regression 如果資料的特徵比樣本點還多,資料特徵n,樣本個數m,如果n m,則計算 xtx 的逆矩陣時會出錯,因為 xtx 不是滿秩矩陣,所以不可逆。注 xt表示x的轉置矩陣 嶺回歸是有偏估計 嶺回歸代價函式這裡就不貼了 選取 值使得 1.各回歸係數的嶺估計基本穩定 2...