import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
# 模型评价
# MSE, RMES, R-square
from sklearn import metrics
rng = np.random.RandomState(1)
xtrain = 10 * rng.rand(30)#创建随机数
ytrain = 15 + 4 * xtrain + rng.rand(30) * 3
# 创建数据
model = LinearRegression()
model.fit(xtrain[:,np.newaxis],ytrain)
ytest = model.predict(xtrain[:,np.newaxis])
mse = metrics.mean_squared_error(ytrain,ytest)
rmse = np.sqrt(mse)# 求出均方根
#ssr = ((ytest - ytrain.mean())**2).sum()
#sst = ((ytrain - ytrain.mean())**2).sum()
#r2 = ssr / sst # 求出确定系数
r2 = model.score(xtrain[:,np.newaxis],ytrain)
print("均方差MSE为: %.5f" % mse)
print("均方根RMSE为: %.5f" % rmse)
print("确定系数R-square为: %.5f" % r2)
浅谈简单线性回归(Simple linear regression)part10代码实现
最新推荐文章于 2023-03-20 14:11:25 发布