机器学习- 一元线性回归
通过分析披萨的直径与价格的线性关系,来预测任一直径的披萨的价格
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
def runplt():
plt.figure()
plt.title("Pizza price plotted against diameter")
plt.xlabel('Diameter')
plt.ylabel('Price')
plt.grid(True)
plt.xlim(0, 25)
plt.ylim(0, 25)
return plt
pizza = pd.DataFrame({'Diameter':[6 ,8, 10, 14, 18],'Price':[7.0,9.0,13.0,17.5,18.0]})
dia = pizza.loc[:,'Diameter'].values
price = pizza.loc[:,'Price'].values
print(dia)
print(price)
#plt = runplt()
#plt.plot(dia, price,'k.')
#plt.show()
variance = np.var(dia,ddof=1)
covariance = np.cov(dia,price)[0][1]
w = covariance/variance
b = np.mean(price)-w*np.mean(dia)
print("w = %f\n b = %f" % (w,b))
plt = runplt()
y_price = w*dia + b
plt.plot(dia,price,'k.')
plt.plot(dia,y_price,'k-')
X2 = 0 # 取个0点预测值
y2 = w*X2 + b # 进行预测
print(y2)
#plt.show()
model = LinearRegression() # 创建模型
X = dia.reshape((-1,1))#将dia转换为1列
y = price
model.fit(X, y) # 拟合
X2 = [[0], [25]] # 取两个预测值
y2 = model.predict(X2) # 进行预测
print(y2) # 查看预测值
#plt = runplt()
plt.plot(dia, price, 'k.')
plt.plot(X2, y2, 'b--') # 画出拟合曲线
# 画出残差
yr = model.predict(X)
for index, x in enumerate(X):
plt.plot([x, x], [y[index], yr[index]], 'r-')
plt.show()