机器学习——多项式回归、拟合(疫情新增病例与道琼斯指数分析)

多项式回归定义公式及案例

1、前言

之前写了一元和多元线性回归用来描述线性问题

机器学习:利用sklearn方法的一元线性回归模型(通过成绩预测绩点)_天海一直在的博客-CSDN博客_sklearn一元线性回归

机器学习:利用sklearn方法的多元线性回归模型(通过成绩预测绩点)_天海一直在的博客-CSDN博客_线性回归预测成绩

但如果我们找的不是直线或者超平面,而是一条曲线,那么就可以用多项式回归来分析和预测。

2、定义及公式

多项式回归可以写成:
Y i = β 0 + β 1 X i + β 2 X i 2 + . . . + β k X i k Y_{i} = \beta_{0} +\beta_{1}X_{i}+\beta_{2}X_{i}^2+...+\beta_{k}X_{i}^k Yi=β0+β1Xi+β2Xi2+...+βkXik
例如二次曲线:
y = a t 2 + b t + c y=at^2+bt+c y=at2+bt+c

3、案例代码

1、数据解析

首先有数据2020年2月24日至2020年4月30日疫情每日新增病例数和道琼斯指数,数据如下。

datenew_casesvolume
2020/2/242346399452730
2020/2/252668449962408
2020/2/263064408252788
2020/2/274327568873667
2020/2/284487796064921
2020/3/26430554364655
2020/3/38440552440168
2020/3/47920401575883
2020/3/59798401427499
2020/3/613360507343739
2020/3/915520629729505
2020/3/1017552555995247
2020/3/1127846550900431
2020/3/1221923777537526
2020/3/1352553728758617
2020/3/1654676681933454
2020/3/1758444703490970
2020/3/1871579757446929
2020/3/1998881677652753
2020/3/20110981746325491
2020/3/23151590694579371
2020/3/24144168701764913
2020/3/25180691698482135
2020/3/26220423613646149
2020/3/27225788498053291
2020/3/30222071475736601
2020/3/31262115507392584
2020/4/1275848441398052
2020/4/2277356440119768
2020/4/3283024358248473
2020/4/6241421512454039
2020/4/7237964499183085
2020/4/8276586399118977
2020/4/9284948462544792
2020/4/13230474338246109
2020/4/14279820427996199
2020/4/15257846375498608
2020/4/16316447416772131
2020/4/17284802458032076
2020/4/20240897358856528
2020/4/21242502420993963
2020/4/22263142306933373
2020/4/23268410334639614
2020/4/24293886333127761
2020/4/27223849337304670
2020/4/28238705340993299
2020/4/29247023396245166
2020/4/30260239416383852

2、绘制散点图

对于该数据我们通过绘制散点图可以看出,这是一个多项式回归的模型

import matplotlib.pyplot as plt
import xlrd
import numpy as np
# 载入数据,打开excel文件
ExcelFile = xlrd.open_workbook("sandian.xls")
sheet1 = ExcelFile.sheet_by_index(0)
x = sheet1.col_values(0)
y = sheet1.col_values(1)
# 将列表转换为matrix
x = np.matrix(x).reshape(48, 1)
y = np.matrix(y).reshape(48, 1)

# 划线y
plt.title("Epidemic and Dow Jones data analysis")
plt.xlabel("new cases")
plt.ylabel("Dow Jones Volume")
plt.plot(x, y, 'b.')
plt.show()

image-20220514121935162

3、多项式回归、拟合

通过散点图的趋势,我们选择拟合3次来防止过拟合和欠拟合。

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.metrics import r2_score
from matplotlib.font_manager import FontProperties  # 导入FontProperties

font = FontProperties(fname="SimHei.ttf", size=14)  # 设置字体

x = pd.read_excel('last_data.xls')['new_cases']
y = pd.read_excel('last_data.xls')['volume']
 
# 进行多项式拟合(这里选取3次多项式拟合)
z = np.polyfit(x, y, 3) # 用3次多项式拟合
 
# 获取拟合后的多项式
p = np.poly1d(z)
print(p)  # 在屏幕上打印拟合多项式
 
# 计算拟合后的y值
yvals=p(x)
 
# 计算拟合后的R方,进行检测拟合效果
r2 = r2_score(y, yvals)
print('多项式拟合R方为:', r2)
 
# 计算拟合多项式的极值点。
peak = np.polyder(p, 1)
print(peak.r)

# 画图对比分析
plot1 = plt.plot(x, y, '*', label='original values', color='red')
plot2 = plt.plot(x, yvals, '-', label='fitting values', color='blue',linewidth=2)
 
plt.xlabel('new_cases',fontsize=13, fontproperties=font)
plt.ylabel('Dow Jones Volume',fontsize=13, fontproperties=font)
plt.legend(loc="best")
plt.title('fitting diagram')
plt.show()

最后结果如下图

image-20220514122949618

输出公式:
9.631e-08 x - 0.05356 x + 7169 x + 4.713e+08
多项式拟合R方为: 0.6462955787806361
[283148.64883622  87629.61932583]
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天海一直在AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值