【Python数据分析】基于自回归积分滑动平均模型的疫情分析报告 附完整python代码

资源地址:Python数据分析大作业 2000+字 图文分析文档 疫情分析+完整python代码

数据分析

数据来自法国疫情数据

资源地址:Python数据分析大作业 2000+字 图文分析文档 疫情分析+完整python代码

代码详解

完整代码文件

主要是对时间序列数据进行分析和预测。让我们逐步解释每一部分:

  1. 导入必要的库

    python 复制代码
    from math import *
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
    from pylab import *
    • math: 导入数学函数库,但实际上在后续的代码中没有用到。
    • numpypandasmatplotlib.pyplot: 分别是用于数值计算、数据处理和可视化的常用库。
    • statsmodels.graphics.tsaplots.plot_acfstatsmodels.graphics.tsaplots.plot_pacf:用于绘制自相关性和偏自相关性图。
    • pylab: 导入了 *,所以其下所有函数都可直接使用。
  2. 设置中文字体和负号显示

    python 复制代码
    plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体为黑体
    plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题
  3. 读取数据

    python 复制代码
    cas_confirmes = pd.read_csv('cas_confirmes.csv', index_col=0)
    hospitalises = pd.read_csv('hospitalises.csv', index_col=0)

    从文件中读取了两个时间序列数据,分别是患病确诊人数和住院人数。

  4. 数据处理

    python 复制代码
    cas_confirmes.fillna(np.nanmean(cas_confirmes) + 30 * np.random.random(), inplace=True)
    hospitalises.fillna(np.nanmean(hospitalises), inplace=True)

    使用每列的均值填充缺失值。

  5. 数据可视化

    python 复制代码
    cas_confirmes.plot() 
    plt.title('Change in the number of cases')
    plt.show()
    hospitalises.plot()
    plt.title('Changes in the number of people in the hospital')
    plt.show()

    绘制了患病确诊人数和住院人数的变化趋势图。

  6. 自相关性分析

    python 复制代码
    plot_acf(cas_confirmes)
    plt.title('The autocorrelation of the number of patients')
    plot_pacf(cas_confirmes)
    plt.title('Partial autocorrelation of the number of patients')
    plt.show()
    
    plot_acf(hospitalises)
    plt.title('Autocorrelation graph of the number of people in the hospital')
    plot_pacf(hospitalises)
    plt.title('Partial autocorrelation graph of the number of people in the hospital')
    plt.show()

    绘制了患病确诊人数和住院人数的自相关性和偏自相关性图。

  7. ARIMA 模型定阶

    python 复制代码
    train_results = sm.tsa.arma_order_select_ic(cas_confirmes['2020-03-19':'2021-06-09'], ic=['bic'], trend='nc', max_ar=5, max_ma=5)
    print('BIC for the number of patients', train_results.bic_min_order)

    使用 BIC 准则确定 ARIMA 模型的阶数。

  8. 构建 ARIMA 模型

    python 复制代码
    model = ARIMA(cas_confirmes['2020-03-19':'2021-05-09'], order=(2,0,1))
    results_comfirm = model.fit();

    使用确定的阶数构建 ARIMA 模型,并对患病确诊人数和住院人数分别进行建模。

  9. 模型诊断

    python 复制代码
    print('The white noise test result of the diseased difference sequence was:', acorr_ljungbox(resid1.values.squeeze(), lags=1))
    print('The white noise test result of hospitalization difference sequence is:', acorr_ljungbox(resid2.values.squeeze(), lags=1))

    对模型的残差进行自相关性分析,检验残差序列是否为白噪声。

  10. 模型预测

    python 复制代码
    predict_comfirm=results_comfirm.forecast(30)

    使用训练好的 ARIMA 模型对未来一段时间内的患病确诊人数和住院人数进行预测。

  11. 可视化预测结果

    python 复制代码
    plt.plot(list(range(1,418)),predict_sunspots_comfirm,label='predict comfirmed')
    plt.plot(smooth_comfirm.loc['2020-03-18':'2021-06-09'],label='true comfirmed')
    plt.plot(list(range(417,447)),predict_comfirm[0],'g',label='future predict')
    plt.title('Actual and predicted disease graphs')
    plt.legend()

    绘制预测结果和真实数据的对比图。

完整代码文件&2000+图文分析报告

相关推荐
懵逼的小黑子15 分钟前
解释两个 Django 命令 makemigrations和migrate
数据库·django
lilye661 小时前
精益数据分析(17/126):精益画布与创业方向抉择
大数据·数据挖掘·数据分析
Lxinccode2 小时前
Java查询数据库表信息导出Word-获取数据库实现[1]:KingbaseES
java·数据库·word·获取数据库信息·获取kingbasees信息
豆沙沙包?3 小时前
5.学习笔记-SpringMVC(P61-P70)
数据库·笔记·学习
思通数科AI全行业智能NLP系统4 小时前
AI视频技术赋能幼儿园安全——教师离岗报警系统的智慧守护
大数据·人工智能·安全·目标检测·目标跟踪·自然语言处理·ocr
ocr_sinosecu15 小时前
OCR定制识别:解锁文字识别的无限可能
人工智能·机器学习·ocr
奋斗者1号5 小时前
分类数据处理全解析:从独热编码到高维特征优化
人工智能·机器学习·分类
朴拙数科5 小时前
MongoDB Atlas与MongoDB连接MCP服务器的区别解析
服务器·数据库·mongodb
柏油5 小时前
MySQL InnoDB 行锁
数据库·后端·mysql
A-Kamen6 小时前
MySQL 存储引擎对比:InnoDB vs MyISAM vs Memory
数据库·mysql·spark