numpy实现线性回归_如何用numpy实现线性回归

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

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值