如何用LSTM、GRU、回归和ARIMA预测COVID-19新增病例,实现长尾词时间序列建模?

2026-04-02 01:572阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计991个文字,预计阅读时间需要4分钟。

如何用LSTM、GRU、回归和ARIMA预测COVID-19新增病例,实现长尾词时间序列建模?

该数据基于全球各国提供的新病例数据。获取时间序列数据,使用以下代码:pythondf=pd.read_csv(C:/global.csv)为了找出每天的新病例,我们需要对数据进行探索。此表中的数据以累计形式呈现,为了找出每天的新病例,我们需要减少这些值。如下所示:pythondf

该数据根据世界各国提供的新病例数据提供。

获取时间序列数据

df=pd.read_csv("C://global.csv")

探索数据

此表中的数据以累积的形式呈现,为了找出每天的新病例,我们需要减去这些值

df.head(10)


这些数据是根据国家和地区报告新病例的数据,但我们只想预测国家的新病例,因此我们使用 groupby 根据国家对它们进行分组


总结数据

执行 groupby 以根据一个国家的新病例来汇总数据,而不是根据地区

d1=df.groupby(['Country/Region']).sum()


描述随机选择的国家的累计新病例增长

  1. from numpy.random import seed
  2. plt.plot(F[i], label = RD[i])
  3. plt.show()


1. # 我们不需要前两列
2. d1=d1.iloc[:,2:]


如何用LSTM、GRU、回归和ARIMA预测COVID-19新增病例,实现长尾词时间序列建模?

1. # # 检查是否有空值
2.
3. d1.isnull().sum().any()

我们可以对每个国家进行预测,也可以对所有国家进行预测,这次我们对所有国家进行预测

dlycnmdcas.head()

dalycnfreces.index

dal_cnre_ces.index = pd.to_datetime(dailyonfrmd_as.index)

plt.plot(dalnimedases)


1. ne_ces = daiy_onme_as.diff().dropna().astype(np.int64)
2. newcaes

plt.plot(ne_s[1:])

nw_s.shape

(153,)

将数据拆分为训练和测试数据


1.
2. ct=0.75
3. trin_aa,tet_aa = train_test_split(ne_ces, pct)
(116,)1. plt.plot(tainta)
2. plt.plot(tesata)

数据标准化

scaler = MinMaxScaler()testa.shape(38, 1)

创建序列

    1.
    2. lentTe = len(ts_data)
    3. for i in range(timmp, lenhTe):
    4. X_st.append(tst_aa[i-tmStap:i])
    5. y_tt.append(tesata[i])
    6.
    7. X_tet=np.array(X_ts)
    8. ytes=np.array(y_tt)
    X_st.shape

    Xtrn.shape


    1.
    2. # 序列的样本
    3. X_trn[0], yran[0]


    为股票价格预测设计RNN模型

    模型:

    1. LSTM
    2. GRU
    model.summary()

    model.summary()


    model.fit(X_trn y_rin, epochs=50, batch_size=200)




    1.
    2. yprd = (mod.predict(X_test))
    3. MSE = mean_squared_error(ytue, y_rd)
    4.
    5. plt.figure(figsize=(14,6))

    1. meRU= Sqtal([
    2. keras.layers.GRU(
    3.
    4.
    5.
    6.
    7. model_GRU.fit(Xtrn, ytin,epochs=50,batch_size=150)


    1. pe_rut = {}
    2.
    3. y_ue = (y_et.reshape(-1,1))
    4. y_prd = (modlGU.predict(X_test))
    5. MSE = mean_squared_error(y_ue, ed)

    用于预测新病例的机器学习算法

    准备数据

    d__in.shape

    moel=LinearRegression(nos=-2)

    ARIMA

    COVID-19 新病例预测的自回归综合移动平均线

    #我们不需要前两列


    1. df1.head()
    2. daly_nfrd_cses = df1.sum(axis=0)
    3. day_cnir_ase.index = pd.to_datetime(da_onieses.index)
    4. new_cs = dacofmecss.diff().dropna().astype(np.int64)
    5.
    6. tri_ta,tet_ata = trintt_it(nw_es, pct)


    1.
    2. ero = men_squred_eror(ts_ar, pricos)


    1. plt.figure(figsize=(12,7))
    2. plt.plot(tanat)


    本文共计991个文字,预计阅读时间需要4分钟。

    如何用LSTM、GRU、回归和ARIMA预测COVID-19新增病例,实现长尾词时间序列建模?

    该数据基于全球各国提供的新病例数据。获取时间序列数据,使用以下代码:pythondf=pd.read_csv(C:/global.csv)为了找出每天的新病例,我们需要对数据进行探索。此表中的数据以累计形式呈现,为了找出每天的新病例,我们需要减少这些值。如下所示:pythondf

    该数据根据世界各国提供的新病例数据提供。

    获取时间序列数据

    df=pd.read_csv("C://global.csv")

    探索数据

    此表中的数据以累积的形式呈现,为了找出每天的新病例,我们需要减去这些值

    df.head(10)


    这些数据是根据国家和地区报告新病例的数据,但我们只想预测国家的新病例,因此我们使用 groupby 根据国家对它们进行分组


    总结数据

    执行 groupby 以根据一个国家的新病例来汇总数据,而不是根据地区

    d1=df.groupby(['Country/Region']).sum()


    描述随机选择的国家的累计新病例增长

    1. from numpy.random import seed
    2. plt.plot(F[i], label = RD[i])
    3. plt.show()


    1. # 我们不需要前两列
    2. d1=d1.iloc[:,2:]


    如何用LSTM、GRU、回归和ARIMA预测COVID-19新增病例,实现长尾词时间序列建模?

    1. # # 检查是否有空值
    2.
    3. d1.isnull().sum().any()

    我们可以对每个国家进行预测,也可以对所有国家进行预测,这次我们对所有国家进行预测

    dlycnmdcas.head()

    dalycnfreces.index

    dal_cnre_ces.index = pd.to_datetime(dailyonfrmd_as.index)

    plt.plot(dalnimedases)


    1. ne_ces = daiy_onme_as.diff().dropna().astype(np.int64)
    2. newcaes

    plt.plot(ne_s[1:])

    nw_s.shape

    (153,)

    将数据拆分为训练和测试数据


    1.
    2. ct=0.75
    3. trin_aa,tet_aa = train_test_split(ne_ces, pct)
    (116,)1. plt.plot(tainta)
    2. plt.plot(tesata)

    数据标准化

    scaler = MinMaxScaler()testa.shape(38, 1)

    创建序列

      1.
      2. lentTe = len(ts_data)
      3. for i in range(timmp, lenhTe):
      4. X_st.append(tst_aa[i-tmStap:i])
      5. y_tt.append(tesata[i])
      6.
      7. X_tet=np.array(X_ts)
      8. ytes=np.array(y_tt)
      X_st.shape

      Xtrn.shape


      1.
      2. # 序列的样本
      3. X_trn[0], yran[0]


      为股票价格预测设计RNN模型

      模型:

      1. LSTM
      2. GRU
      model.summary()

      model.summary()


      model.fit(X_trn y_rin, epochs=50, batch_size=200)




      1.
      2. yprd = (mod.predict(X_test))
      3. MSE = mean_squared_error(ytue, y_rd)
      4.
      5. plt.figure(figsize=(14,6))

      1. meRU= Sqtal([
      2. keras.layers.GRU(
      3.
      4.
      5.
      6.
      7. model_GRU.fit(Xtrn, ytin,epochs=50,batch_size=150)


      1. pe_rut = {}
      2.
      3. y_ue = (y_et.reshape(-1,1))
      4. y_prd = (modlGU.predict(X_test))
      5. MSE = mean_squared_error(y_ue, ed)

      用于预测新病例的机器学习算法

      准备数据

      d__in.shape

      moel=LinearRegression(nos=-2)

      ARIMA

      COVID-19 新病例预测的自回归综合移动平均线

      #我们不需要前两列


      1. df1.head()
      2. daly_nfrd_cses = df1.sum(axis=0)
      3. day_cnir_ase.index = pd.to_datetime(da_onieses.index)
      4. new_cs = dacofmecss.diff().dropna().astype(np.int64)
      5.
      6. tri_ta,tet_ata = trintt_it(nw_es, pct)


      1.
      2. ero = men_squred_eror(ts_ar, pricos)


      1. plt.figure(figsize=(12,7))
      2. plt.plot(tanat)