Polynomial regression in mathematical modeling


foreword

For the polynomial regression method, if your data points are not suitable for linear regression (that is, most of the straight lines do not pass through the data points), then it is more appropriate to use this method.
And polynomial regression, like linear regression, uses the relationship between the variables x and y to find the best way to draw a line of data points.


1. The principle of polynomial regression

There are methods in Python to find the relationship between data points and draw a polynomial regression line.
Example: Studying ethanol conversion as a function of temperature

# 建立一元一次方程y = ax + b
import numpy as np
import matplotlib.pyplot as plt

# A1组温度与乙醇转换率的关系图-散点图
x=np.array([250,275,300,325,350]).reshape((-1, 1))
y=np.array([2.07,5.85,14.97,19.68,36.80])

plt.scatter(x, y) # 绘制散点图
plt.title('A1')
plt.show()

The result is as follows:
insert image description here

Obviously, if linear regression is used, all data points cannot fall on the same straight line

Do it with the polynomial regression method:

# 多项式拟合
import numpy
import matplotlib.pyplot as plt

x=[250,275,300,325,350]
y=[2.07,5.85,14.97,19.68,36.80]

mymodel = numpy.poly1d(numpy.polyfit(x, y, 4)) # 四阶

myline = numpy.linspace(250, 350, 100) # 从位置250开始,到位置350结束

plt.scatter(x, y) # 散点图
plt.plot(myline, mymodel(myline), label='A1') # 多项式回归
plt.legend() # 设置图例
plt.show() # 显示
print('A1组中温度与乙醇转换率的关系中阶数的系数:', numpy.polyfit(x, y, 4))

The result is as follows:

A1组中温度与乙醇转换率的关系中阶数的系数: [ 2.83413333e-06 -3.36325333e-03  1.49119667e+00 -2.92472967e+02
  2.14005200e+04]
温度和乙醇转换率的关系为:
# y=2.83413333e-06 * x ^4 -3.36325333e-03 * x^3 + 1.49119667e+00  * x ^2 -2.92472967e+02 * x + 2.14005200e+04

insert image description here
Detailed explanation of the above code:

  1. import module:`
import numpy
import matplotlib.pyplot as plt
  1. Create an array of x and y axes
x=[250,275,300,325,350]
y=[2.07,5.85,14.97,19.68,36.80]
  1. numpy-poly1d method to build polynomial models
mymodel = numpy.poly1d(numpy.polyfit(x, y, 4)) # 四阶
  1. Specify the display method of the line, starting from position 250 and ending at position 350
myline = numpy.linspace(250, 350, 100) # 从位置250开始,到位置350结束

linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
Function: within the specified large interval (start, stop), return the data at fixed intervals. They return num equally spaced samples.
Parameter introduction:

start : scalar (scalar) — the start value of the queue
stop: scalar — the end value of the queue (when endpoint=False, this point is not included.)
num: int, optional (optional), the number of generated sequences, default is 50 and must be an integer.
endpoint: bool, when True stop is the last sample. stop is not included when False. The default is True.
retstep: bool, when it is True, the output of the calculation will be changed, and the output is a tuple. The two elements of the tuple are the sequence to be generated and the step value of the sequence. The default is False.

  1. Draw a scatterplot
plt.scatter(x, y) # 散点图
  1. polynomial regression line mymodel (row position variable)
plt.plot(myline, mymodel(myline), label='A1') # 多项式回归

2. Polynomial regression method

2.1 polyfit function

polyfit corresponds to polynomial coefficients

The function prototype is:

numpy.polyfit(x,y,num)

A polynomial fit can be performed on a set of data.

example:

import matplotlib.pyplot as plt
import numpy as np
 
# 构建噪声数据xu,yu
xu = np.random.rand(50) * 4 * np.pi - 2 * np.pi
def f(x):
    return np.sin(x) + 0.5 * x
yu = f(xu)
 
plt.figure(figsize=(8, 4))
# 用噪声数据xu,yu,得到拟合多项式系数,自由度为5
reg = np.polyfit(xu, yu, 5)
# 计算多项式的函数值。返回在x处多项式的值,p为多项式系数,元素按多项式降幂排序
ry = np.polyval(reg, xu)
# 原先函数绘制
plt.plot(xu, yu, 'bo', label='f(x)')#蓝色虚线
# 拟合绘制
plt.plot(xu, ry, 'r.', label='regression')#红色点状
plt.legend(loc=0)# 位置
plt.show()# 显示

The result is as follows:
insert image description here

2.2 poly1d function

poly1d corresponds to a polynomial expression

The numpy.poly1d() function has the following parameters.
They are:

  1. The parameters are: coefficient
import numpy

a= numpy.array([2,1,1])
f = numpy.poly1d(a)
print('表达式为:\n', f)

insert image description here

  1. The parameter is: bool
    takes the value of the array as the root, and then deduces the polynomial in reverse, for example: (x-2)(x-4)(x-5)
    insert image description here

Then: (x - 2)(x - 4)(x - 5) = x^3 - 11x^2 + 38x -40

  1. poly1d can also calculate the value of polynomials
    insert image description here

2.3 Poly1d and polyfit are used at the same time

example:

import numpy
import matplotlib.pyplot as plt

x=[250,275,300,325,350]
y=[2.07,5.85,14.97,19.68,36.80]

polyfit=numpy.polyfit(x, y, 4)# 多项式系数
poly1d = numpy.poly1d(polyfit)# 多项式表达式

myline = numpy.linspace(250, 350, 100) # 从位置250开始,到位置350结束

plt.scatter(x, y) # 散点图
plt.plot(myline, poly1d(myline), label='A1') # 多项式回归
plt.legend() # 设置图例
plt.show() # 显示
print('系数:\n', polyfit)
print('表达式:\n', poly1d)

insert image description here
insert image description here


Guess you like

Origin blog.csdn.net/ex_6450/article/details/126518793