#%%demo ## 基础函数库导入 import numpy as np ## 导入画图库 import matplotlib.pyplot as plt import seaborn as sns ## 导入决策树模型函数 from sklearn.tree import DecisionTreeClassifier from sklearn import tree import pydotplus from IPython.display import Image ##Demo演示DecisionTree分类 ## 构造数据集 x_fearures = np.array([[-1, -2], [-2, -1], [-3, -2], [1, 3], [2, 1], [3, 2]]) y_label = np.array([0, 1, 0, 1, 0, 1]) ## 调用决策树回归模型 tree_clf = DecisionTreeClassifier() ## 调用决策树模型拟合构造的数据集 tree_clf = tree_clf.fit(x_fearures, y_label) ## 可视化构造的数据样本点 plt.figure() plt.scatter(x_fearures[:,0],x_fearures[:,1], c=y_label, s=50, cmap='viridis') plt.title('Dataset') plt.show() ## 可视化决策树 import graphviz dot_data = tree.export_graphviz(tree_clf, out_file=None) graph = pydotplus.graph_from_dot_data(dot_data) graph.write_pdf("D:\Python\ML\DecisionTree.pdf") # 模型预测 ## 创建新样本 x_fearures_new1 = np.array([[0, -1]]) x_fearures_new2 = np.array([[2, 1]]) ## 在训练集和测试集上分布利用训练好的模型进行预测 y_label_new1_predict = tree_clf.predict(x_fearures_new1) y_label_new2_predict = tree_clf.predict(x_fearures_new2) print('The New point 1 predict class:\n',y_label_new1_predict) print('The New point 2 predict class:\n',y_label_new2_predict)
运行结果
训练集决策树
明天继续,还有一个决策树在真实数据集上的应用,明天出。先搞课题~