博雅数据机器学习06
线性回归
from sklearn import linear_model
from numpy import mat, array, mean
# 根据X和y训练模型并计算预测值y_pred
X = insurance.drop(['charges'], axis=1)
y = insurance['charges']
ws = linearRegression(X, y)
y_pred = mat(X.values)*ws
y_pred = array(y_pred).reshape(y_pred.shape[0],) # 将矩阵转换为一行多列的array格式
# 自定义决定系数函数,并对训练得到的模型进行评价
def r2_Score(y_true, y_pred):
sst = sum((y_true-mean(y_true))**2)
ssr = sum((y_true-y_pred)**2)
sse = sum((y_pred-mean(y_true))**2)
r2 = 1-float(ssr)/sst
return round(r2,2)
score = r2_Score(y, y_pred)
print(score)
# sklearn模型训练结果
from sklearn import linear_model, metrics
regr = linear_model.LinearRegression(fit_intercept=False)
regr.fit(X, y)
y_pred = regr.predict(X)
score_sklearn = round(metrics.r2_score(y, y_pred),2)
print(score_sklearn)