How to realize nonlinear regression in python
This article introduces you how to achieve nonlinear regression in python, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Nonlinear regression analysis by python
Use matplotlib to visualize the scatter plot.
Csv files with date-dependent number of infections
The code is as follows:
From pandas import read_csv
Import matplotlib.pyplot as plt
Plt.rcParams ['font.family'] =' SimHei'# solves Chinese fonts
Data=read_csv ('FRV 2.csventing camera encodingless GBK')
Plt.scatter (data. Date, data. Number of infections)
Data.corr ()
LrModel=LinearRegression ()
X=data [['date']]
Y=data [['number of infections']]
Plt.scatter (XBI y)
Plt.xlabel ('date ordinal')
Plt.ylabel ('number of infections')
Plt.show ()
The result is as shown in the picture (I will take March 11 as my first day)
According to the figure is very close to the quadratic function and the cubic function, I choose to use the cubic function to fit. The code is as follows:
From sklearn.preprocessing import
PolynomialFeatures as pf
Pd=pf (degree=3)
X1=pd.fit_transform (x)
Irmodle=LinearRegression ()
Irmodle.fit (x1BI y)
A=irmodle.score (x1BI y)
B=pd.fit_transform ([[18]])
C=irmodle.predict (b)
Print (a)
Print ("estimated number of infections in 3 / 29 days", irmodle.predict (pd.fit_transform ([17])
Print ("estimated number of infections in 3 / 30 days", c)
Print ("estimated number of infections in March / 31 is", irmodle.predict (pd.fit_transform ([19])
Print ("estimated number of infections per 4 / 1 day", irmodle.predict (pd.fit_transform ([[20]])
The results are as follows:
On how to achieve nonlinear regression in python to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.