博雅数据机器学习04
线性回归
一元线性回归
import pandas as pd
from sklearn import linear_model
insurance = pd.read_csv('insurance.csv')
age = insurance['age'].values
charges = insurance['charges'].values
# 请在下方作答
# 定义一元线性回归函数
def linearRegression(xArr,yArr):
mean_x = xArr.mean()
mean_y = yArr.mean()
numerator = sum(xArr*yArr) - len(xArr)*mean_x*mean_y
denominator = sum(xArr*xArr) - len(xArr)*mean_x*mean_x
w1 = float(numerator)/denominator
w0 = mean_y - w1*mean_x
return round(w0,2),round(w1,2)
# 模型训练,得到参数值
w0, w1 = linearRegression(age, charges)
# sklearn的训练结果
regr = linear_model.LinearRegression()
regr.fit(age.reshape(-1,1), charges.reshape(-1,1))
print(round(regr.coef_,2))
print(round(regr.intercept_,2))