开发手册 欢迎您!
软件开发者资料库

Python 机器学习 线性回归(Linear Regression)

机器学习使计算机从研究数据和统计数据中学习机器学习是向人工智能(AI)方向迈进的一步。机器学习是一个分析数据并学习预测结果的程序。本文主要介绍Python 机器学习 线性回归(Linear Regression)。

1、回归(Regression)

当您尝试查找变量之间的关系时,会使用术语回归。

在机器学习和统计建模中,该关系用于预测未来事件的结果。

2、线性回归(Linear Regression)

线性回归使用数据点之间的关系在所有数据点之间画一条直线。

这条线可以用来预测未来价值。

httpswwwwonherocom

在机器学习中,预测未来非常重要。

3、线性回归是如何工作的?

Python提供了一些方法来查找数据点之间的关系并绘制线性回归线。我们将向您展示如何使用这些方法而不是通过数学公式。

在下面的示例中,x轴表示年龄,y轴表示速度。我们已经记录了13辆汽车通过收费站时的年龄和速度。让我们看看我们收集的数据是否可以用于线性回归:

例如:

首先绘制散点图:

import matplotlib.pyplot as pltx = [5,7,8,7,2,17,2,9,4,11,12,9,6]y = [99,86,87,88,111,86,103,87,94,78,77,85,86]plt.scatter(x, y)plt.show()

 Result:

httpswwwwonherocom

例如:

导入scipy并绘制线性回归线:

import matplotlib.pyplot as pltfrom scipy import statsx = [5,7,8,7,2,17,2,9,4,11,12,9,6]y = [99,86,87,88,111,86,103,87,94,78,77,85,86]slope, intercept, r, p, std_err = stats.linregress(x, y)def myfunc(x):return slope * x + interceptmymodel = list(map(myfunc, x))plt.scatter(x, y)plt.plot(x, mymodel)plt.show()

 Result:

httpswwwwonherocom

示例说明

导入所需的模块。

您可以在我们的Matplotlib教程中了解Matplotlib模块。

您可以在我们的SciPy教程中了解SciPy模块。

import matplotlib.pyplot as plt
from scipy import stats

创建表示x和y轴值的数组:

x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y = [99,86,87,88,111,86,103,87,94,78,77,85,86]

执行返回线性回归的一些重要键值的方法:

slope, intercept, r,   p, std_err = stats.linregress(x, y)

创建一个使用slopeintercept值返回新值的函数。 这个新值表示相应的x值将在y轴上放置的位置:

def myfunc(x):
  return slope * x + intercept

通过函数运行x数组的每个值。 这将导致一个新的数组,其中的y轴具有新的值:

mymodel = list(map(myfunc, x))

绘制原始散点图:

plt.scatter(x, y)

画出线性回归线:

plt.plot(x, mymodel)

显示图:

plt.show()

4、关系R

重要的是要知道x轴的值和y轴的值之间的关系如何,如果没有关系,则线性回归不能用于预测任何东西。

这种关系-相关系数-称为r

r值的范围是0到1,其中0表示没有关系,而1表示100%相关。

Python和Scipy模块将为您计算该值,您要做的就是将x和y值提供给它。

例如:

我的数据在线性回归中的拟合度如何?

from scipy import statsx = [5,7,8,7,2,17,2,9,4,11,12,9,6]y = [99,86,87,88,111,86,103,87,94,78,77,85,86]slope, intercept, r, p, std_err = stats.linregress(x, y)print(r)

注意:结果-0.76表明存在关系,但不是完美的关系,但它表明我们可以在将来的预测中使用线性回归。

5、预测未来值

现在,我们可以使用收集到的信息来预测未来价值。

示例:让我们尝试预测一辆拥有10年历史的汽车的速度。

为此,我们需要与上例相同的myfunc()函数:

def myfunc(x):
  return slope * x + intercept

例如:

预测一辆使用十年的汽车的速度:

from scipy import statsx = [5,7,8,7,2,17,2,9,4,11,12,9,6]y = [99,86,87,88,111,86,103,87,94,78,77,85,86]slope, intercept, r, p, std_err = stats.linregress(x, y)def myfunc(x):return slope * x + interceptspeed = myfunc(10)print(speed)

该示例预测速度为85.6,我们也可以从图中读取:

httpswwwwonherocom

6、不适合使用?

让我们创建一个示例,其中线性回归并不是预测未来价值的最佳方法。

例如:

x和y轴的这些值将导致线性回归的拟合度非常差:

import matplotlib.pyplot as pltfrom scipy import statsx = [89,43,36,36,95,10,66,34,38,20,26,29,48,64,6,5,36,66,72,40]y = [21,46,3,35,67,95,53,72,58,10,26,34,90,33,38,20,56,2,47,15]slope, intercept, r, p, std_err = stats.linregress(x, y)def myfunc(x):return slope * x + interceptmymodel = list(map(myfunc, x))plt.scatter(x, y)plt.plot(x, mymodel)plt.show()

 Result:

httpswwwwonherocom

r建立关系?

例如:

您应该得到一个非常低的r值。

import numpyfrom scipy import statsx = [89,43,36,36,95,10,66,34,38,20,26,29,48,64,6,5,36,66,72,40]y = [21,46,3,35,67,95,53,72,58,10,26,34,90,33,38,20,56,2,47,15]slope, intercept, r, p, std_err = stats.linregress(x, y)print(r)

结果:0.013表明关系很差,并告诉我们该数据集不适合线性回归。