1.课本课后习题
import graphviz
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn import preprocessing
from sklearn import tree
import matplotlib.pyplot as plt
features = ["年龄", "有工作", "有自己的房子", "信贷情况"]
x_train = pd.DataFrame([
["青年", "否", "否", "一般"],
["青年", "否", "否", "好"],
["青年", "是", "否", "好"],
["青年", "是", "是", "一般"],
["青年", "否", "否", "一般"],
["中年", "否", "否", "一般"],
["中年", "否", "否", "好"],
["中年", "是", "是", "好"],
["中年", "否", "是", "非常好"],
["中年", "否", "是", "非常好"],
["老年", "否", "是", "非常好"],
["老年", "否", "是", "好"],
["老年", "是", "否", "好"],
["老年", "是", "否", "非常好"],
["老年", "否", "否", "一般"]
])
y_train = pd.DataFrame(["否", "否", "是", "是", "否", "否", "否", "是", "是", "是", "是", "是", "是", "是", "否"])
# 数据预处理
# LabelEncoder()打标签,对特征进行硬编码
le_x = preprocessing.LabelEncoder()
le_x.fit(np.unique(x_train))
x_train = x_train.apply(le_x.transform)
le_y = preprocessing.LabelEncoder()
le_y.fit(np.unique(y_train))
y_train = y_train.apply(le_y.transform)
# 建立模型
model_tree = DecisionTreeClassifier()
model_tree.fit(x_train,y_train)
# 可视化
dot_data = tree.export_graphviz(model_tree,out_file='tree.dot',
feature_names=features,
class_names=[str(k) for k in np.unique(y_train)],
filled= True,rounded=True,
special_characters=True)
graph = graphviz.Source(dot_data)
2.视频课后作业
一般默认使用基尼指数即可,因为熵中有对数运算,耗时
from sklearn.tree import DecisionTreeClassifier
from sklearn import preprocessing
import numpy as np
import pandas as pd
import time
# 可视化
from IPython.display import Image
from sklearn import tree
import pydotplus
def show(clf,features,y_types):
"""决策树的可视化"""
dot_data = tree.export_graphviz(clf, out_file=None,
feature_names=features,
class_names=y_types,
filled=True, rounded=True,
special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png()) #jupyter里可以显示,pycharm显示不出
graph.write_png(r'DT_show.png')
def main():
star=time.time()
# 原始样本数据
features=["age","work","house","credit"]
X_train=pd.DataFrame([
["青年", "否", "否", "一般"],
["青年", "否", "否", "好"],
["青年", "是", "否", "好"],
["青年", "是", "是", "一般"],
["青年", "否", "否", "一般"],
["中年", "否", "否", "一般"],
["中年", "否", "否", "好"],
["中年", "是", "是", "好"],
["中年", "否", "是", "非常好"],
["中年", "否", "是", "非常好"],
["老年", "否", "是", "非常好"],
["老年", "否", "是", "好"],
["老年", "是", "否", "好"],
["老年", "是", "否", "非常好"],
["老年", "否", "否", "一般"]
])
y_train = pd.DataFrame(["否", "否", "是", "是", "否", "否", "否", "是", "是", "是", "是", "是", "是", "是", "否"])
# 数据预处理
le_x=preprocessing.LabelEncoder()
le_x.fit(np.unique(X_train))
X_train=X_train.apply(le_x.transform)
le_y=preprocessing.LabelEncoder()
le_y.fit(np.unique(y_train))
y_train=y_train.apply(le_y.transform)
# 调用sklearn.DT建立训练模型
clf=DecisionTreeClassifier()
clf.fit(X_train,y_train)
# 可视化
show(clf,features,[str(k) for k in np.unique(y_train)])
# 用训练得到模型进行预测
X_new=pd.DataFrame([["青年", "否", "是", "一般"]])
X=X_new.apply(le_x.transform)
y_predict=clf.predict(X)
# 结果输出
X_show=[{features[i]:X_new.values[0][i]} for i in range(len(features))]
print("{0}被分类为:{1}".format(X_show,le_y.inverse_transform(y_predict)))
print("time:{:.4f}s".format(time.time()-star))
if __name__=="__main__":
main()
[{'age': '青年'}, {'work': '否'}, {'house': '是'}, {'credit': '一般'}]被分类为:['是']
time:0.1602s