python-time-series-forecasting/main.py

110 lines
2.9 KiB
Python
Raw Normal View History

2023-04-28 11:53:13 +04:00
import matplotlib.pyplot as plt
import pandas as pd
from statsmodels.tsa.holtwinters import ExponentialSmoothing
import requests
import json
# ts = pd.read_csv("d:\\files\\Features count.csv")
# ts.plot()
# plt.grid()
# plt.show()
# moving_avg = ts.rolling(4).mean()
# ts.plot()
# plt.plot(moving_avg, color='green', label='Moving average')
# plt.legend()
# plt.grid()
# model = ExponentialSmoothing(ts, trend="additive", seasonal="additive", seasonal_periods=5)
# fit1 = model.fit()
# pred1 = fit1.forecast(5)
# plt.plot(ts, label='Features count')
# plt.plot(pred1, color='red', label='Holt-Winters forecast')
# plt.legend()
# plt.grid()
ts = pd.read_csv("d:\\files\\Testing time.csv")
model = ExponentialSmoothing(ts, trend="additive", seasonal="additive", seasonal_periods=7)
fit1 = model.fit()
pred1 = fit1.forecast(10)
plt.plot(ts, label='Testing time')
plt.plot(pred1, color='red', label='Holt-Winters forecast')
plt.legend()
plt.grid()
plt.show()
2023-04-28 11:54:32 +04:00
# Использовать внешний сервис прогнозирования
2023-04-28 11:53:13 +04:00
time_series = {
"originalTimeSeries": {
"values": [
{
"date": "2023-04-16T06:29:50.668476",
"value": 1
},
{
"date": "2023-04-17T06:29:50.668476",
"value": 2
},
{
"date": "2023-04-18T06:29:50.668476",
"value": 3
},
{
"date": "2023-04-19T06:29:50.668476",
"value": 4
},
{
"date": "2023-04-20T06:29:50.668476",
"value": 5
},
{
"date": "2023-04-21T06:29:50.668476",
"value": 6
},
{
"date": "2023-04-22T06:29:50.668476",
"value": 5
},
{
"date": "2023-04-23T06:29:50.668476",
"value": 4
},
{
"date": "2023-04-24T06:29:50.668476",
"value": 3
},
{
"date": "2023-04-25T06:29:50.668476",
"value": 2
},
{
"date": "2023-04-26T06:29:50.668476",
"value": 1
},
{
"date": "2023-04-27T06:29:50.668476",
"value": 2
}
],
"name": 'test'
},
"countForecast": 5
}
source = []
for v in time_series['originalTimeSeries']['values']:
source.append(v['value'])
url = "http://time-series.athene.tech/api/1.0/getForecast"
headers = {'Content-type': 'application/json'}
r = requests.post(url, data=json.dumps(time_series), headers=headers)
2023-04-28 15:07:15 +04:00
forecast = [item['value'] for item in r.json()['timeSeries']['values']]
2023-04-28 11:53:13 +04:00
plt.plot(range(1, 1+len(source)), source)
plt.plot(range(len(source), len(source)+len(forecast)), forecast)
plt.grid()
plt.show()