『数据挖掘』scikit-learn包的进阶学习笔记——第二章:线性回归

来源:互联网 发布:86编程键盘 编辑:程序博客网 时间:2024/06/04 18:46

代码部分参考:2-linear-regression

# coding:utf-8__author__ = "LCG22_2016_05_30"import matplotlib.pyplot as pltfrom matplotlib.font_manager import FontProperties# font = FontProperties(fname=r"C:\Users\LCG22\Desktop\work\learn\Python\PythonLearn\DataSet\learn_data_set\pizza.xlsx", size=10)# print fontdef runplt():    plt.figure()    # plt.title("匹萨价格与直径数据", fontproperties=font)    # plt.xlabel("直径(英寸)", fontproperties=font)    # plt.ylabel("价格(美元)", fontproperties=font)    plt.title("1")    plt.xlabel("2")    plt.ylabel("3")    plt.axis([0, 25, 0, 25])    plt.grid(True)    return pltplt = runplt()x = [[6], [8], [10], [14], [18]]y = [[7], [9], [13], [17.5], [18]]plt.plot(x, y, "k.")#plt.show()from sklearn.linear_model import LinearRegression# 创建并拟合模型model = LinearRegression()model.fit(x, y)print ("预测一张12英寸匹萨价格:$%.2f" % model.predict([12])[0])plt = runplt()plt.plot(x, y, "k.")x2 = [[0], [10], [14], [25]]model = LinearRegression()model.fit(x, y)y2 = model.predict(x2)plt.plot(x, y, "k.")plt.plot(x2, y2, "g-")# 残差预测值yr = model.predict(x)for idx, x_value in enumerate(x):    plt.plot([x_value, x_value], [y[idx], yr[idx]], "r-")#plt.show()import numpy as npprint "残差平方和: %.2f" % np.mean((model.predict(x) - y) ** 2)


0 0
原创粉丝点击