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


原创粉丝点击