某程序员退休后决定练习书法,于是重金购买文房四宝。一日,饭后突生雅兴,一番研墨拟纸,并点上上好檀香。定神片刻,泼墨挥毫,郑重地写下一行字:hello world!
import numpy as np import pandas as pd import matplotlib as mpl import matplotlib.pyplot as plt from sklearn.tree import DecisionTreeClassifier 12345
mpl.rcParams['font.sans-serif'] = [u'SimHei'] mpl.rcParams['axes.unicode_minus'] = False 12
iris_feature = u'花萼长度', u'花萼宽度', u'花瓣长度', u'花瓣宽度' path = 'datas/iris.data' # 数据文件路径 data = pd.read_csv(path, header=None) x_prime = data[list(range(4))] y = pd.Categorical(data[4]).codes 12345
#进行特征比较 feature_pairs = [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]] plt.figure(figsize=(9, 6), facecolor='w') for i, pair in enumerate(feature_pairs): #获取数据 x = x_prime[pair] #决策树学习 clf = DecisionTreeClassifier(criterion='gini', max_depth=5) clf.fit(x, y) y_hat = clf.predict(x) score = clf.score(x, y) y2 = y.reshape(-1) c = np.count_nonzero(y_hat == y) #统计预测正确的个数 print ('特征: ', iris_feature[pair[0]], ' + ', iris_feature[pair[1]]) print ('t预测正确数目:', c) print ('t准确率: %.2f%%' % (score*100)) N, M = 500, 500 # 横纵各采样多少个值 x1_min, x2_min = x.min() x1_max, x2_max = x.max() t1 = np.linspace(x1_min, x1_max, N) t2 = np.linspace(x2_min, x2_max, M) x1, x2 = np.meshgrid(t1, t2) # 生成网格采样点 x_test = np.dstack((x1.flat, x2.flat))[0] # 测试点 cm_light = mpl.colors.ListedColormap(['#A0FFA0', '#FFA0A0', '#A0A0FF']) cm_dark = mpl.colors.ListedColormap(['g', 'r', 'b']) y_hat = clf.predict(x_test) # 预测值 y_hat = y_hat.reshape(x1.shape) plt.subplot(2, 3, i+1) plt.pcolormesh(x1, x2, y_hat, cmap=cm_light) # 预测值 plt.scatter(x[pair[0]], x[pair[1]], c=y, edgecolors='k', cmap=cm_dark) # 样本 plt.xlabel(iris_feature[pair[0]], fontsize=10) plt.ylabel(iris_feature[pair[1]], fontsize=10) plt.xlim(x1_min, x1_max) plt.ylim(x2_min, x2_max) plt.grid() plt.title(u'准确率:%.2f%%' % (score * 100), fontdict={'fontsize':15}) plt.suptitle(u'鸢尾花数据在决策树中两两特征属性对目标属性的影响', fontsize=18, y = 1) plt.tight_layout(2) plt.subplots_adjust(top=0.92) plt.show()
1234567891011121314151617181920212223242526272829303132333435363738394041424344特征: 花萼长度 + 花萼宽度
预测正确数目: 127
准确率: 84.67%
特征: 花萼长度 + 花瓣长度
预测正确数目: 148
准确率: 98.67%
特征: 花萼长度 + 花瓣宽度
预测正确数目: 146
准确率: 97.33%
特征: 花萼宽度 + 花瓣长度
预测正确数目: 146
准确率: 97.33%
特征: 花萼宽度 + 花瓣宽度
预测正确数目: 145
准确率: 96.67%
特征: 花瓣长度 + 花瓣宽度
预测正确数目: 149
准确率: 99.33%
相关知识
基于机器学习的鸢尾花数据集的三分类算法的实现 C++
机器学习算法之决策树实现鸢尾花数据分类
【机器学习】KNN算法实现鸢尾花分类
机器学习案例——鸢尾花数据集分析
分类算法3:决策树及R语言实现
【机器学习】基于KNN算法实现鸢尾花数据集的分类
机器学习之路:经典的鸢尾花数据集
python利用c4.5决策树对鸢尾花卉数据集进行分类(iris)
探索机器学习的起点:鸢尾花数据集
鸢尾花数据集在机器学习中的应用与分析
网址: 机器学习算法之决策树实现鸢尾花数据特征属性比较 https://m.huajiangbk.com/newsview1114428.html
上一篇: 花鳗鲡成鳗阶段粉状配合饲料的制作 |
下一篇: 钝叶柃不同性别花的花部形态与传粉 |