决策树是一种主要应用于数据分类场景的算法。它是一个树形结构,其中每个节点代表要素,每个边缘代表所做出的决策。从根节点开始,我们继续评估分类特征,并决定遵循特定的优势。每当有新数据点出现时,都会反复应用相同的方法,然后在研究所有必需的特征或将其应用于分类方案时得出最终结论。因此,决策树算法是一种监督学习模型,用于预测具有一系列训练变量的因变量。
示例
我们将获取kaggle提供的药物测试数据。第一步,我们将使用pandas从csv文件读取数据,并查看其内容和结构。import pandas as pd
datainput = pd.read_csv("drug.csv", delimiter=",") #https://www.kaggle.com/gangliu/drugsets
print(datainput)
运行上面的代码将为我们提供以下结果:Age Sex BP Cholesterol Na_to_K Drug
0 23 F HIGH HIGH 25.355 drugY
1 47 M LOW HIGH 13.093 drugC
2 47 M LOW HIGH 10.114 drugC
3 28 F NORMAL HIGH 7.798 drugX
4 61 F LOW HIGH 18.043 drugY
.. ... .. ... ... ... ...
195 56 F LOW HIGH 11.567 drugC
196 16 M LOW HIGH 12.006 drugC
197 52 M NORMAL HIGH 9.894 drugX
[200 rows x 6 columns]
预处理数据
在下一步中,我们对上述数据进行预处理,以获取数据中不同文本值的数值。这对于训练和测试有关在给定的年龄,性别,血压等值下使用某种药物的决定的样本数据很有用。
示例import numpy as np
import pandas as pd
from sklearn.metrics import confusion_matrix
datainput = pd.read_csv("drug.csv", delimiter=",")
X = datainput[['Age', 'Sex', 'BP', 'Cholesterol', 'Na_to_K']].values
from sklearn import preprocessing
label_gender = preprocessing.LabelEncoder()
label_gender.fit(['F','M'])
X[:,1] = label_gender.transform(X[:,1])
label_BP = preprocessing.LabelEncoder()
label_BP.fit([ 'LOW', 'NORMAL', 'HIGH'])
X[:,2] = label_BP.transform(X[:,2])
label_Chol = preprocessing.LabelEncoder()
label_Chol.fit([ 'NORMAL', 'HIGH'])
X[:,3] = label_Chol.transform(X[:,3])
# Printing the first 6 records
print(X[0:6])
运行上面的代码将为我们提供以下结果-[[23 0 0 0 25.355]
[47 1 1 0 13.093]
[47 1 1 0 10.113999999999999]
[28 0 2 0 7.797999999999999]
[61 0 1 0 18.043]
[22 0 2 0 8.607000000000001]
]
转换因变量
接下来,我们还将因变量转换为数值,以便可以将其用于训练以及评估数据集中。
示例import pandas as pd
datainput = pd.read_csv("drug.csv", delimiter=",")
X = datainput[['Age', 'Sex', 'BP', 'Cholesterol', 'Na_to_K']].values
y = datainput["Drug"]
print(y[0:6])
输出结果
运行上面的代码将为我们提供以下结果:0 drugY
1 drugC
2 drugC
3 drugX
4 drugY
5 drugX
Name: Drug, dtype: object
训练数据集
接下来,我们使用提供的数据的30%作为训练数据集。这将用作为其余70%的百分比创建分类的基础,我们将其称为测试数据。
示例import pandas as pd
from sklearn.model_selection import train_test_split
datainput = pd.read_csv("drug.csv", delimiter=",")
X = datainput[['Age', 'Sex', 'BP', 'Cholesterol', 'Na_to_K']].values
y = datainput["Drug"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=3)
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
输出结果
运行上面的代码将为我们提供以下结果:(140, 5)
(60, 5)
(140,)
(60,)
从训练数据集中获取结果
接下来,我们可以应用决策树来查看经过训练的数据集的结果。在这里,我们根据输入的内容并使用称为熵的条件来创建树。最后,我们计算决策树的准确性。
示例import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn import metrics
datainput = pd.read_csv("drug.csv", delimiter=",")
X = datainput[['Age', 'Sex', 'BP', 'Cholesterol', 'Na_to_K']].values
# Data Preprocessing
from sklearn import preprocessing
label_gender = preprocessing.LabelEncoder()
label_gender.fit(['F', 'M'])
X[:, 1] = label_gender.transform(X[:, 1])
label_BP = preprocessing.LabelEncoder()
label_BP.fit(['LOW', 'NORMAL', 'HIGH'])
X[:, 2] = label_BP.transform(X[:, 2])
label_Chol = preprocessing.LabelEncoder()
label_Chol.fit(['NORMAL', 'HIGH'])
X[:, 3] = label_Chol.transform(X[:, 3])
y = datainput["Drug"]
# train_test_split
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=3)
drugTree = DecisionTreeClassifier(criterion="entropy", max_depth=4)
drugTree.fit(X_train, y_train)
predicted = drugTree.predict(X_test)
print(predicted)
print("\nDecisionTrees's Accuracy: ", metrics.accuracy_score(y_test, predicted))
输出结果
运行上面的代码将为我们提供以下结果:['drugY' 'drugX' 'drugX' 'drugX' 'drugX' 'drugC' 'drugY' 'drugA' 'drugB'
'drugA' 'drugY' 'drugA' 'drugY' 'drugY' 'drugX' 'drugY' 'drugX' 'drugX'
'drugB' 'drugX' 'drugX' 'drugY' 'drugY' 'drugY' 'drugX' 'drugB' 'drugY'
'drugY' 'drugA' 'drugX' 'drugB' 'drugC' 'drugC' 'drugX' 'drugX' 'drugC'
'drugY' 'drugX' 'drugX' 'drugX' 'drugA' 'drugY' 'drugC' 'drugY' 'drugA'
'drugY' 'drugY' 'drugY' 'drugY' 'drugY' 'drugB' 'drugX' 'drugY' 'drugX'
'drugY' 'drugY' 'drugA' 'drugX' 'drugY' 'drugX']
DecisionTrees's Accuracy: 0.9833333333333333