-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.py
99 lines (68 loc) · 2.82 KB
/
benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
"""benchmark.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1ISuRgSUkVqUq0bratdYVMLZm4PPuW9GJ
"""
import pandas as pd
import numpy as np
"""# Load data"""
covid = pd.read_csv("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owid-covid-data.csv")
covid.head(2)
y = covid[(covid['location']=='World')]
y.index = pd.to_datetime(y['date'])
y = y.asfreq('D')
y = y[['new_cases']]
y.plot()
"""# Distribution focused
[DummyRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.dummy.DummyRegressor.html)
"""
#https://scikit-learn.org/stable/modules/generated/sklearn.dummy.DummyRegressor.html
from sklearn.dummy import DummyRegressor
dummy_regr = DummyRegressor(strategy="mean")
dummy_regr.fit(y, y)
y.mean()
y['mean'] = dummy_regr.predict(y)
y.plot()
"""# Naive
[NaiveForecaster](https://www.sktime.org/en/latest/api_reference/modules/auto_generated/sktime.forecasting.naive.NaiveForecaster.html)
"""
#https://www.sktime.org/en/latest/api_reference/modules/auto_generated/sktime.forecasting.naive.NaiveForecaster.html
!pip install sktime==0.6.1
from sktime.forecasting.naive import NaiveForecaster
#https://www.sktime.org/en/latest/api_reference/modules/auto_generated/sktime.forecasting.base.ForecastingHorizon.html
from sktime.forecasting.base import ForecastingHorizon
from sktime.utils.plotting import plot_series
strategy = 'drift'
forecaster = NaiveForecaster(strategy=strategy, window_length=30, sp=7)
forecaster.fit(y['new_cases'])
h = 30
fh = ForecastingHorizon(pd.date_range(max(y.index), periods=h, freq="d", closed='right'), is_relative=False)
y_pred = forecaster.predict(fh=fh)
import matplotlib.pyplot as plt
plot_series(y['new_cases'], y_pred, labels=["y", "y_pred"])
plt.savefig(f'naive_{strategy}.png')
"""# Expert regressor"""
from sklearn.base import BaseEstimator, RegressorMixin
class ExpertRegressor(BaseEstimator, RegressorMixin):
def __init__(self, expert_value=400000):
self.expert_value = expert_value
def fit(self, X, y):
self.mean = np.mean(X.values)
return self
def predict(self, X):
return min(self.expert_value, self.mean)
# def get_params(self, deep=True):
# return {'expert_value': self.expert_value}
# def set_params(self, **parameters):
# for parameter, value in parameters.items():
# setattr(self, parameter, value)
# return self
er = ExpertRegressor()
er.fit(y[['new_cases']], y['new_cases'])
y['er'] = er.predict(y[['new_cases']])
y[['new_cases', 'er']].plot()
er = ExpertRegressor(expert_value=200000)
er.fit(y[['new_cases']], y['new_cases'])
y['er'] = er.predict(y)
y[['new_cases', 'er']].plot()