python多项式回归的最终初学者指南

本文介绍了机器学习中的回归概念,特别是多项式回归。通过Python示例展示了如何建立多项式模型,分析数据点之间的关系,并利用r平方值评估拟合优度。文章还讨论了如何利用多项式回归预测未来值。
摘要由CSDN通过智能技术生成

Machine Learning is making the computer learn from studying data and statistics. Machine Learning is a step into the direction of artificial intelligence (AI). Machine Learning is a program that analyses data and learns to predict the outcome.

机器学习使计算机从研究数据和统计信息中学习。 机器学习是迈向人工智能(AI)方向的一步。 机器学习是一个程序,可以分析数据并学习预测结果。

什么是回归? (What is Regression?)

The term regression is used when you try to find the relationship between variables. In Machine Learning and statistical modelling, that relationship is used to predict the outcome of future events.

当您尝试查找变量之间的关系时,将使用术语回归。 在机器学习和统计建模中,该关系用于预测未来事件的结果。

Polynomial Regression

多项式回归

Polynomial regression, like linear regression, uses the relationship between the variables x and y to find the best way to draw a line through the data points.

像线性回归一样,多项式回归使用变量x和y之间的关系来找到绘制数据点线的最佳方法。

Image for post

Python has methods for finding a relationship between data-points and to draw a line of polynomial regression. We will show you how to use these methods instead of going through the mathematic formula. In the example below, we have registered 18 cars as they were passing a certain tollbooth.

Python有一些方法可以找到数据点之间的关系并画出多项式回归线。 我们将向您展示如何使用这些方法而不是通过数学公式。 在下面的示例中,我们登记了18辆经过特定收费站的汽车。

The x-axis represents the hours of the day and the y-axis represents the speed:

x轴表示一天中的小时,y轴表示速度:

import matplotlib.pyplot as plt
x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y = [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]
plt.scatter(x, y)
plt.show()

Result

结果

Image for post

Import the modules you need:

导入所需的模块:

import numpy
import matplotlib.pyplot as plt

Create the arrays that represent the values of the x and y-axis:

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

x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y = [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]

NumPy has a method that lets us make a polynomial model:

NumPy有一种方法可以让我们建立多项式模型:

mymodel = numpy.poly1d(numpy.polyfit(x, y, 3))

Then specify how the line will display, we start at position 1, and end at position 22:

然后指定行的显示方式,我们从位置1开始,到位置22结束:

myline = numpy.linspace(1, 22, 100)

Draw the original scatter plot:

绘制原始散点图:

plt.scatter(x, y)

Draw the line of polynomial regression:

画出多项式回归线:

plt.plot(myline, mymodel(myline))

Display the diagram:

显示图:

plt.show()

It is important to know how well the relationship between the values of the x- and the y-axis is if there are no relationship the polynomial regression can not be used to predict anything.

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

The relationship is measured with a value called the r-squared. The r-squared value ranges from 0 to 1, where 0 means no relationship, and 1 means 100% related.

该关系用一个称为r平方的值来度量。 r平方值的范围是0到1,其中0表示不相关,而1表示100%相关。

How well does my data fit in a polynomial regression?

我的数据在多项式回归中的拟合度如何?

import numpy
from sklearn.metrics import r2_score
x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y = [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]
mymodel = numpy.poly1d(numpy.polyfit(x, y, 3))
print(r2_score(y, mymodel(x)))

The result 0.94 shows that there is a very good relationship, and we can use polynomial regression in future predictions.

结果0.94表明存在很好的关系,可以在未来的预测中使用多项式回归。

预测未来价值 (Predict Future Values)

Now we can use the information we have gathered to predict future values. Predict the speed of a car passing at 17 P.M:

现在我们可以使用收集到的信息来预测未来价值。 预测下午17点过车的速度:

import numpy
from sklearn.metrics import r2_score
x = [1,2,3,5,6,7,8,9,10,12,13,14,15,16,18,19,21,22]
y = [100,90,80,60,60,55,60,65,70,70,75,76,78,79,90,99,99,100]
mymodel = numpy.poly1d(numpy.polyfit(x, y, 3))
speed = mymodel(17)
print(speed)

结论 (Conclusion)

I hope after this article you have a basic understanding of polynomial Regression and how to use it in Python and that you will be able to run yourself some scripts of Machine Learning now!

希望本文结束后,您对多项式回归以及如何在Python中使用它有基本的了解,并且现在您可以运行一些机器学习脚本!

翻译自: https://medium.com/swlh/the-ultimate-beginners-guide-to-polynomial-regression-in-python-a04f6beac182

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值