我想展示一個示例并在這里尋求解決方案。這里有很多與決策樹相關的查詢,以及關于選擇序數與分類資料等。我的示例如下代碼所示:
from sklearn import tree
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
c1=pd.Series([0,1,2,2,2,0,1,2,0,1,2])
c2=pd.Series([0,1,1,2,0,1,0,0,2,1,1])
c3=pd.Series([0,1,1,2,0,1,1,2,0,2,2])
c4=pd.Series([0,1,2,0,0,2,2,1,2,0,1])# My encoding : Veg:0, Glut:1, None:2
labels=pd.Series([0,0,0,0,1,1,1,0,0,1,1])
dnl=pd.concat([c1,c2,c3,c4],axis=1)
d=dnl.to_numpy()
clf = tree.DecisionTreeClassifier(criterion="entropy",random_state=420,max_depth=2,splitter='best')
clf_tree = clf.fit(d, labels.to_numpy())
print(clf_tree)
score=clf_tree.score(d,labels.to_numpy())
error=1-score
print("The error= ",error)
from sklearn.tree import plot_tree
fig, ax = plt.subplots(figsize=(6, 6)) #figsize value changes the size of plot
plot_tree(clf_tree,ax=ax)
plt.show()
from sklearn.metrics import confusion_matrix
yp=clf_tree.predict(dnl)
print(yp)
print(labels.to_numpy())
cm = confusion_matrix(labels, yp)
print("The confusion matrix= ",cm)
結果:

將c4編碼(交換 1 和 0)更改為下面會更改樹!錯誤分類錯誤更小!
c4=pd.Series([1,0,2,1,1,2,2,0,2,1,0])# Modified encoding: Veg:1, Glut:0,None:2

為什么決策樹無法選擇中間值作為條件?
uj5u.com熱心網友回復:
我假設數字 0,1,2 代表不同的類別。然后,您應該在構建樹之前使用 one-hot 編碼。結果將獨立于類別的標簽,例如“2”將被視為類似于“1”。在您的設定中,“2”將大于“1”大于“0”,這意味著類別有順序。
編輯:
from sklearn import tree
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.preprocessing import OneHotEncoder
enc= OneHotEncoder(sparse=False)
c1=pd.Series(['0','1','2','2','2','0','1','2','0','1','2'])
c2=pd.Series(['0','1','1','2','0','1','0','0','2','1','1'])
c3=pd.Series(['0','1','1','2','0','1','1','2','0','2','2'])
c4=pd.Series(['0','1','2','0','0','2','2','1','2','0','1'])# My encoding : Veg:0, Glut:1, None:2
labels=pd.Series(['0','0','0','0','1','1','1','0','0','1','1'])
dnl=pd.concat([c1,c2,c3,c4],axis=1)
dnl=dnl.to_numpy()
enc.fit(dnl)
dnl=enc.transform(dnl)
clf = tree.DecisionTreeClassifier(criterion="entropy",random_state=420,max_depth=2,splitter='best')
clf_tree = clf.fit(d, labels.to_numpy())
print(clf_tree)
score=clf_tree.score(d,labels.to_numpy())
error=1-score
print("The error= ",error)
from sklearn.tree import plot_tree
fig, ax = plt.subplots(figsize=(6, 6)) #figsize value changes the size of plot
plot_tree(clf_tree,ax=ax)
plt.show()
from sklearn.metrics import confusion_matrix
yp=clf_tree.predict(dnl)
print(yp)
print(labels.to_numpy())
cm = confusion_matrix(labels, yp)
print("The confusion matrix= \n",cm)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/445408.html
上一篇:keras圖層類中缺少特定選項
