決策樹部分理論支撐
1* 通過選取一定的特征來降低數據的不確定性(熵)
2* 建議尋找多分類問題的最優特征的最優候選值。把多分類問題轉換成多幾層遞歸的二分類問題,防止數據對特征值的控制敏感。
3* 停止條件
- 取得了最夠好的分類結果
- 遞歸到了預定的最深深度
- 葉子節點的純度
- 分裂次數達到極限
- 最大特征數
- . . .
4* 相關公式
-
e n t r o p y ( D ) = ? ∑ i = 1 n P i l o g 2 P i entropy(D) = -\sum_{i=1}^n P_ilog_2 P_i
e
n
t
r
o
p
y
(
D
)
=
?
∑
i
=
1
n
?
P
i
?
l
o
g
2
?
P
i
?
e n t r o p y ( D , A ) = ∑ i = 1 k D A i D l o g 2 D A i entropy(D,A) = \sum_{i=1}^k \frac {D_{A_i}}{D} log_2D_{A_i} e n t r o p y ( D , A ) = ∑ i = 1 k ? D D A i ? ? ? l o g 2 ? D A i ? ?
g a i n ( D , A ) = e n t r o p y ( D ) ? e n t r o p y ( D , A ) gain(D,A) = entropy(D) - entropy(D,A) g a i n ( D , A ) = e n t r o p y ( D ) ? e n t r o p y ( D , A )
原本的熵 減去 考慮某種特征條件A之后的熵,得到信息增益
g a i n r a t e ( D , A ) = g a i n ( D , A ) / e n t r o p y ( D , A ) gain_rate(D,A) = gain(D,A)/entropy(D,A) g a i n r ? a t e ( D , A ) = g a i n ( D , A ) / e n t r o p y ( D , A )
同理,根據同樣的方法可以得到 信息增益率
import
pandas
as
pd
import
numpy
as
np
df
=
pd
.
read_csv
(
'C:\\Users\\76485\\Desktop\\column.2C.csv'
)
'''
導入數據源&導入基本包;
交互命令窗口輸入df.head()查看前五行數據
'''
X
=
df
.
drop
(
'V7'
,
axis
=
1
)
#drop進行有選擇的數據刪除,刪除頭標簽為'V7'的列數據
y
=
df
.
V7
'''
軸用來為超過一維的數組定義屬性,二維數據:0軸沿著行的方向向下,1軸沿著列的水平方向延伸
'''
'''
#將文字轉換為數字的標準化程序
from sklearn.preprocessing import LabelEncoder
labelencoder = LabelEncoder()
for col in data.columns:
data[col] = labelencoder.fit_transform(data[col])
'''
#from sklearn.cross_validation import train_test_split#該包在新版本中,如下實現
from
sklearn
.
model_selection
import
train_test_split
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
random_state
=
1
)
#將數據集拆分為訓練集和測試集
from
sklearn
import
tree
clf
=
tree
.
DecisionTreeClassifier
(
max_depth
=
4
)
#建樹
clf
=
clf
.
fit
(
X_train
,
y_train
)
test_rec
=
X_test
.
iloc
[
1
,
:
]
clf
.
predict
(
[
test_rec
]
)
#測試集測試,交互窗口輸入
'''
Ans:
Out[16]: array(['NO'], dtype=object)
'''
y_test
.
iloc
[
1
]
#調出真實結果,交互窗口輸入
'''
Ans:
Out[17]: 'NO'
'''
from
sklearn
.
metrics
import
accuracy_score
rate_ac
=
accuracy_score
(
y_test
,
clf
.
predict
(
X_test
)
)
#測試模型準確率
print
(
rate_ac
)
'''
0.8205128205128205 穩定在80%左右,建樹層數對準確率影響較小
'''
'''決策樹可視化'''
'''
with open("lc-is.dot", 'w') as f:
f = tree.export_graphviz(clf,
out_file=f,
max_depth = 3,
impurity = True,
feature_names = list(X_train),
class_names = ['AB', 'NO'],
rounded = True,
filled= True )
'''
from
sklearn
.
tree
import
DecisionTreeClassifier
import
pydotplus
#若提示沒有此包,需在cmd-Anaconda Prompt鍵入install pydotplus
from
IPython
.
display
import
Image
from
IPython
.
display
import
display
from
sklearn
.
tree
import
export_graphviz
#需手動下載并配置絕對路徑
import
os
os
.
environ
[
"PATH"
]
+=
os
.
pathsep
+
'C:/Program Files (x86)/Graphviz2.38/bin/'
#沙雕pydotplus,配置環境變量(路徑)
dot_tree
=
tree
.
export_graphviz
(
clf
,
out_file
=
None
,
feature_names
=
[
'V1'
,
'V2'
,
'V3'
,
'V4'
,
'V5'
,
'V6'
]
,
class_names
=
[
'AB'
,
'NO'
]
,
filled
=
True
,
rounded
=
True
,
special_characters
=
True
)
graph
=
pydotplus
.
graph_from_dot_data
(
dot_tree
)
img
=
Image
(
graph
.
create_png
(
)
)
graph
.
write_png
(
"out.png"
)
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
