python机器学习3-5代码
来源:互联网 发布:万象数据库默认密码 编辑:程序博客网 时间:2024/05/22 03:06
import urllib.requestimport numpyfrom sklearn import datasets, linear_modelfrom math import sqrtimport matplotlib.pyplot as plttarget_url = ("http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv")data = urllib.request.urlopen(target_url)xList = []labels = []names = []firstLine = Truefor line in data: if firstLine == True: names = line.strip().split(";".encode(encoding='utf-8')) firstLine = False else: row = line.strip().split(";".encode(encoding='utf-8')) labels.append(float(row[-1])) row.pop()#python中pop默认删除row最后一个元素,row.pop(i)就是删除row第i+1个元素,因为列表索引是从0开始的,pop删除元素并且可以将其返回 floatRow = [float(num) for num in row] xList.append(floatRow)indices = range(len(xList))xListTest = [xList[i] for i in indices if i%3 == 0]xListTrain = [xList[i] for i in indices if i%3 != 0]labelsTest = [labels[i] for i in indices if i%3 == 0]labelsTrain = [labels[i] for i in indices if i%3 != 0]xTrain = numpy.array(xListTrain)yTrain = numpy.array(labelsTrain)xTest = numpy.array(xListTest)yTest = numpy.array(labelsTest)alphaList = [0.1**i for i in [0, 1, 2, 3, 4, 5, 6]]rmsError = []for alph in alphaList: wineRidgeModel = linear_model.Ridge(alpha=alph) wineRidgeModel.fit(xTrain, yTrain) rmsError.append(numpy.linalg.norm((yTest-wineRidgeModel.predict(xTest)), 2) / sqrt(len(yTest)))#numpy.linalg.norm(,2)表示L2范数,如sqrt((yTest-wineRidgeModel.predict(xTest)) ** 2)其实就是求预测误差的标准差print("RMS Error alpha")for i in range(len(rmsError)): print(rmsError[i], alphaList[i])x = range(len(rmsError))plt.plot(x, rmsError, 'k')plt.xlabel('-log(alpha)')plt.ylabel('Error (RMS)')plt.show()indexBest = rmsError.index(min(rmsError))#求出均方根误差最小的值所对应的索引alph = alphaList[indexBest]wineRidgeModel = linear_model.Ridge(alpha=alph)wineRidgeModel.fit(xTrain, yTrain)errorVector = yTest - wineRidgeModel.predict(xTest)plt.hist(errorVector)plt.xlabel("Bin Boundaries")plt.ylabel("Counts")plt.show()plt.scatter(wineRidgeModel.predict(xTest), yTest, s=100, alpha=0.10)plt.xlabel('Predicted Taste Score')plt.ylabel('Actual Taste Score')plt.show()
运行结果如下:
RMS Error alpha0.659578817634 1.00.657861091881 0.10.657617214464 0.0100000000000000020.657521648264 0.00100000000000000020.657419068011 0.000100000000000000020.657394162885 1.0000000000000003e-050.657391308716 1.0000000000000004e-06
阅读全文
0 0
- python机器学习3-5代码
- python机器学习4-3代码讲解
- python机器学习4-5代码及运行结果
- 从机器学习实战代码3-5,窥探Python函数属性问题
- 机器学习python处理文本数据 代码
- 机器学习实战:KNN 代码注释(python)
- 机器学习实战:python算法代码汇总
- 机器学习svm算法python代码实现
- Python机器学习代码中的命名
- 机器学习实战-决策树ID3-python代码
- 机器学习实战-AdaBoost-python代码
- python机器学习day'5
- Python与机器学习3
- python机器学习day'3
- 机器学习 in action 分类器0 python代码解析
- 【机器学习系列】SVD奇异值分解(python代码)
- 机器学习完整过程案例分布解析,python代码解析
- 机器学习完整过程案例分布解析,python代码解析
- 动态库调用对话框资源
- poj 2800 LA 5916 求一个数模1到n 得到的和
- 运用JS构造菱形/三角形
- JavaScript:JSON数组根据属性排序
- JavaMail邮件开发(一)
- python机器学习3-5代码
- hibernate 直接用懒加载属性显示数据为空
- 兴趣篇——用C语言写打字母游戏
- Codeforces Round #432 C. Arpa and a game with Mojtaba 裸博弈+质数拆解
- 51Nod 1674——区间的价值 V2
- UIApplicationDelegate 执行顺序
- JOB:前端面试题5
- 洛谷日记7
- Python学习笔记(七)selenium自动化测试框架