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

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

# coding:utf-8
__author__ = "LCG22_2016_05_30"

import matplotlib.pyplot as plt
from 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 font

def 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 plt
plt = 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 np
print "残差平方和: %.2f" % np.mean((model.predict(x) - y) ** 2)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值