随机森林算法构建红酒口感预测模型
来源:互联网 发布:新手如何开好淘宝店 编辑:程序博客网 时间:2024/04/27 19:01
# -*- coding:utf-8 -*-import numpyimport urllib.requestfrom sklearn.model_selection import train_test_splitfrom sklearn import ensemblefrom sklearn.metrics import mean_squared_errorimport pylab as plot#从网页中读取数据url="http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv"data=urllib.request.urlopen(url)#将数据中第一行的属性读取出来放在names列表中,将其他行的数组读入row中,并将row中最后一列提取#出来放在labels中作为标签,并使用pop将该列从row去去除掉,最后将剩下的属性值转化为float类型存入xList中xlist=[]labels=[]names=[]firstline=Truefor line in data: if firstline: names=line.strip().split(b';') firstline=False else: row=line.strip().split(b';') labels.append(float(row[-1])) row.pop() floatrow=[float(num) for num in row] xlist.append(floatrow)#计算几行几列nrows=len(xlist)ncols=len(xlist[1])#转化为numpy格式x=numpy.array(xlist)y=numpy.array(labels)winenames=numpy.array(names)#随机抽30%的数据用于测试,随机种子为531固定值,确保多次运行结果相同便于优化算法xtrain,xtest,ytrain,ytest=train_test_split(x,y,test_size=0.30,random_state=531)mseoos=[]#测试50棵~500棵决策树的方差(步长10)ntreelist=range(50,500,10)for itrees in ntreelist: depth=None maxfeat=4 #随机森林算法生成训练 winerandomforestmodel=ensemble.RandomForestRegressor(n_estimators=itrees,max_depth=depth,max_features=maxfeat,oob_score=False,random_state=531) winerandomforestmodel.fit(xtrain,ytrain) #测试方差放入列表 prediction=winerandomforestmodel.predict(xtest) mseoos.append(mean_squared_error(ytest,prediction))print("MSE")print(mseoos[-1])plot.plot(ntreelist,mseoos)plot.xlabel("number of trees")plot.ylabel("fang cha")plot.show()#用feature_importances_方法提取属性重要性numpy数组featureimportance=winerandomforestmodel.feature_importances_#归一化?featureimportance=featureimportance/featureimportance.max()#argsort方法返回array类型的索引sorted_idx=numpy.argsort(featureimportance)#函数说明:arange([start,] stop[, step,], dtype=None)根据start与stop指定的范围以及step设定的步长,生成一个 ndarraybarpos=numpy.arange(sorted_idx.shape[0]) + .5plot.barh(barpos,featureimportance[sorted_idx],align='center')plot.yticks(barpos,winenames[sorted_idx])plot.xlabel("variable importance")plot.show()
注意:
from sklearn.cross_validation import train_test_split 改为from sklearn.model_selection import train_test_split上面那个已经不用了
阅读全文
0 0
- 随机森林算法构建红酒口感预测模型
- 梯度提升&随机森林混杂算法预测红酒口感
- 使用sklearn包构建二元决策树预测红酒口感
- 评估红酒口感
- 【机器学习算法模型】分类算法——随机森林
- 随机森林算法构建岩石水雷分类器
- 随机森林算法实现
- 随机森林算法
- 随机森林算法实现
- 随机森林算法
- 随机森林算法
- 随机森林算法
- 随机森林算法
- 随机森林算法
- 随机森林算法
- 随机森林算法
- 随机森林算法
- 随机森林算法小结
- 获取动态加载ajax的数据
- cocos2dx常见49种Action
- linux驱动学习记录(一)-字符设备框架
- 序列比对算法-计算生物学
- Android第三方库和框架
- 随机森林算法构建红酒口感预测模型
- 爬取图片
- <Android开源库> Picasso
- zabbix安装window客户端
- Spring学习笔记:IOC容器
- 转发和重定向
- 电影票房的实时统计
- 一道js面试题:请写出尽可能多的使用JavaScript产生一个Image 标签的方法
- HDU 5955 AC自动机+高斯消元