它是线性回归的最基本版本,可使用单个功能预测响应。 SLR中的假设是两个变量线性相关。
无涯教程可以通过两种方式在Python中实现SLR,一种是提供自己的数据集,另一种是使用scikit-learn python库中的数据集。
示例1 - 在以下Python实现示例中,使用了自己的数据集。
首先,将从导入必要的包开始,如下所示:
%matplotlib inline import numpy as np import matplotlib.pyplot as plt
接下来,定义一个函数,该函数将计算SLR的重要值-
def coef_estimation(x, y):
以下脚本行将给出观察值n-
n=np.size(x)
x和y向量的均值可以计算如下-
m_x, m_y=np.mean(x), np.mean(y)
可以找到关于x的交叉偏差和偏差,如下所示:
SS_xy=np.sum(y*x) - n*m_y*m_x SS_xx=np.sum(x*x) - n*m_x*m_x
接下来,回归系数即b可以计算如下-
b_1=SS_xy/SS_xx b_0=m_y - b_1*m_x return(b_0, b_1)
接下来,需要定义一个函数,该函数将绘制回归线以及预测响应向量-
def plot_regression_line(x, y, b):
以下脚本行将实际点绘制为散点图-
plt.scatter(x, y, color="m", marker="o", s=30)
以下脚本行将预测响应向量-
y_pred=b[0] + b[1]*x
以下脚本行将绘制回归线并在其上放置标签-
来源:LearnFk无涯教程网
plt.plot(x, y_pred, color="g") plt.xlabel('x') plt.ylabel('y') plt.show()
最后,无涯教程需要定义main()函数以提供数据集并调用上面定义的函数-
def main(): x=np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) y=np.array([100, 300, 350, 500, 750, 800, 850, 900, 1050, 1250]) b=coef_estimation(x, y) print("Estimated coefficients:\nb_0={}\nb_1={}".format(b[0], b[1])) plot_regression_line(x, y, b) if __name__ == "__main__": main()
输出
Estimated coefficients: b_0=154.5454545454545 b_1=117.87878787878788
示例2 - 在以下Python实现示例中,使用了scikit-learn的糖尿病数据集。
首先,将从导入必要的包开始,如下所示:
%matplotlib inline import matplotlib.pyplot as plt import numpy as np from sklearn import datasets, linear_model from sklearn.metrics import mean_squared_error, r2_score
接下来,将加载糖尿病数据集并创建其对象-
diabetes=datasets.load_diabetes()
在实现SLR时,将仅使用一种功能,如下所示-
X=diabetes.data[:, np.newaxis, 2]
接下来,需要将数据分为以下训练集和测试集:
X_train=X[:-30] X_test=X[-30:]
接下来,需要将目标分为以下训练集和测试集:
y_train=diabetes.target[:-30] y_test=diabetes.target[-30:]
现在,要训练模型,无涯教程需要创建线性回归对象,如下所示:
regr=linear_model.LinearRegression()
接下来,使用训练集训练模型,如下所示:
regr.fit(X_train, y_train)
接下来,使用测试集进行预测,如下所示:
y_pred=regr.predict(X_test)
接下来,将打印一些系数,例如MSE,方差得分等,如下所示-
print('Coefficients:\n', regr.coef_) print("Mean squared error: %.2f" % mean_squared_error(y_test, y_pred)) print('Variance score: %.2f' % r2_score(y_test, y_pred))
现在,绘制输出,如下所示:
plt.scatter(X_test, y_test, color='blue') plt.plot(X_test, y_pred, color='red', linewidth=3) plt.xticks(()) plt.yticks(()) plt.show()
输出
Coefficients: [941.43097333] Mean squared error: 3035.06 Variance score: 0.41
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)