博雅数据机器学习05
多元线性回归
from sklearn import linear_model
from numpy import mat, linalg, column_stack, ones
import pandas as pd
insurance = pd.read_csv('insurance.csv')
# 请在下方作答 #
# 定义多元线性回归函数
def linearRegression(xArr,yArr):
xMat = mat(xArr)
yMat = mat(yArr).T
xTx = xMat.T*xMat
if linalg.det(xTx) == 0.0:
print("singular matrix, can't do inverse")
ws = linalg.solve(xTx, xMat.T*yMat)
return ws
# 模型训练,得到参数值
X = insurance[['age', 'bmi', 'children']].values
X = column_stack((X,ones(X.shape[0])))
y = insurance['charges']
ws = linearRegression(X, y)
# sklearn的训练结果
regr = linear_model.LinearRegression()
regr.fit(X, y)
print(regr.coef_)
print(regr.intercept_)