0

## Arima 預測空氣品質

temx是上面顯示的格式

froce iT邦大師 1 級 ‧ 2020-02-16 23:42:27 檢舉

fillano iT邦超人 1 級 ‧ 2020-02-17 06:15:48 檢舉

fillano iT邦超人 1 級 ‧ 2020-02-17 10:14:15 檢舉

lingwu iT邦新手 5 級 ‧ 2020-02-17 14:10:11 檢舉

fillano iT邦超人 1 級 ‧ 2020-02-18 16:59:45 檢舉

### 1 個回答

0
fillano
iT邦超人 1 級 ‧ 2020-02-19 09:52:59

expect是ans.csv提供的正確答案，predict是跑出來的預測

2020-02-19 13:54 補充

``````myorder = sm.tsa.arma_order_select_ic(v, max_ar=5, max_ma=4, ic='aic')
lag_ar = myorder.aic_min_order[0]
lag_ma = myorder.aic_min_order[1]
lmodel = tsa.arima_model.ARIMA(v, order=(lag_ar, 0, lag_ma))
``````

lingwu iT邦新手 5 級 ‧ 2020-02-19 21:24:21 檢舉

fillano iT邦超人 1 級 ‧ 2020-02-20 09:30:04 檢舉

lingwu iT邦新手 5 級 ‧ 2020-02-20 14:21:01 檢舉

``````pred = result.predict(start=str('9'),end=str('9'),dynamic=False)
``````

fillano iT邦超人 1 級 ‧ 2020-02-21 09:28:17 檢舉

lingwu iT邦新手 5 級 ‧ 2020-02-23 15:03:35 檢舉

lingwu iT邦新手 5 級 ‧ 2020-02-24 16:04:28 檢舉

fillano iT邦超人 1 級 ‧ 2020-02-24 17:17:08 檢舉
``````import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
import statsmodels.tsa as tsa
import numpy as np
import statsmodels.graphics.tsaplots as ts
from sklearn.utils.testing import ignore_warnings
from sklearn.exceptions import ConvergenceWarning
from sklearn.metrics import mean_squared_error

@ignore_warnings(category=ConvergenceWarning)
def resolve(data, test25, ans):
result_predict = list()
for i in range(len(test25.values)):
for j in range(9):
data.append(float(test25.values[i][j + 2]))
v = np.array(data)
#myorder = sm.tsa.arma_order_select_ic(v, max_ar=5, max_ma=4, ic='aic')
#lag_ar = myorder.aic_min_order[0]
#lag_ma = myorder.aic_min_order[1]
lag_ar = 1
lag_ma = 1
lmodel = tsa.arima_model.ARIMA(v, order=(lag_ar, 0, lag_ma))
lfitted = lmodel.fit(display=-1)
pre = lfitted.predict(len(v) + 1, len(v) + 1)
result_predict.append(pre[0])

pm25 = df[df['測項'].str.contains('PM2.5')]

data = list()
for x in pm25.values:
for y in range(24):
data.append(float(x[y + 3]))

test.columns = ['id', 'type', '0', '1', '2', '3', '4', '5', '6', '7', '8']
test25 = test[test['type'].str.contains('PM2.5')]

ans.columns = ['id', 'val']
ans.set_index('id', inplace=True)

predict, expect = resolve(data, test25, ans.values)

mse = mean_squared_error(expect, predict)
print('mean square error: %.3f' % mse)

final = pd.DataFrame()

final['original'] = expect
final['predict'] = predict
final.plot(figsize=(20, 10))
plt.show()
``````

lingwu iT邦新手 5 級 ‧ 2020-02-25 21:40:25 檢舉