numpy实现线性回归
Let’s first briefly recall what linear regression is:
让我们首先简要回顾一下线性回归是什么:
Linear regression is estimating an unknown variable in a linear fashion based on some other known variables. Visually, we fit a line (or a hyperplane in higher dimensions) through our data points.
线性回归是基于一些其他已知变量以线性方式估计未知变量。 在视觉上,我们通过数据点拟合一条线(或较大尺寸的超平面)。
If you’re not comfortable with this concept or want to understand better the math behind it, you can read my previous article about linear regression:
如果您对这个概念不满意,或者想更好地理解其背后的数学运算,可以阅读我以前关于线性回归的文章:
Now, let’s focus on implementation.
现在,让我们专注于实现。
Firstly, we need to, obviously, import some libraries. We import numpy
as it is the main thing we use for the implementation, matplotlib
for visualizing our results, and make_regression
function, from sklearn
, which we will be using to generate a regression dataset for using as an example.
首先,显然,我们需要导入一些库。 我们导入numpy
因为它是用于实现的主要内容,而matplotlib
用于可视化我们的结果,而make_regression
函数是从sklearn
,我们将使用它们生成一个回归数据集以用作示例。
import numpy as npimport matplotlib.pyplot as pltfrom sklearn.datasets import make_regression
Then we will create a LinearRegression
class with the following methods:
然后,我们将使用以下方法创建LinearRegression
类:
.fit()
— this method will do the actual learning of our linear regression model; here we will find the optimal weights.fit()
-此方法将实际学习我们的线性回归模型; 在这里我们将找到最佳权重.predict()
— this one will be used for prediction; it will return the output of our linear model.predict()
-这将用于预测; 它将返回线性模型的输出.rmse()
— computes the root mean squared error of our model with the given data; this metric is kind of “the average distance from our model’s