代码部分参考: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)