Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeseries: test Stack learner #273

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions orangecontrib/timeseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import platform
from unittest.mock import patch

from Orange.data import Table
from Orange.data import Table, ContinuousVariable, Domain, TimeVariable, \
DiscreteVariable
from Orange.classification import TreeLearner, KNNLearner
from Orange.ensembles import StackedFitter
from Orange.evaluation import CrossValidation, MSE

from orangecontrib.timeseries import Timeseries
from orangecontrib.timeseries.functions import timestamp, fromtimestamp
Expand All @@ -19,7 +23,8 @@ def test_create_time_variable(self):
self.assertNotEqual(id_1, id(time_series.attributes))

def test_make_timeseries_from_continuous_var(self):
table = Table.from_url("http://file.biolab.si/datasets/slovenian-national-assembly-eng.tab")
table = Table.from_url(
"http://file.biolab.si/datasets/slovenian-national-assembly-eng.tab")
time_series = Timeseries.make_timeseries_from_continuous_var(table,
'date of birth')
self.assertEqual(time_series.time_variable.name, 'date of birth')
Expand All @@ -29,7 +34,7 @@ def test_time_var_removed(self):
ts_with_tv = Timeseries.from_file('airpassengers')
# select columns without time variable
ts_without_tv = Timeseries.from_data_table(ts_with_tv[:,
ts_with_tv.domain.class_var])
ts_with_tv.domain.class_var])
self.assertTrue(ts_with_tv.time_variable)
# make sure the Timeseries without time variable in domain has
# time_variable set to None
Expand Down Expand Up @@ -66,6 +71,7 @@ def test_timestamp_windows(self):
with hardcoded correct timestamps. It can be only tested with UTC
since otherwise timestamp would be machine local time dependent
"""

class T(datetime):
def timestamp(self):
nonlocal was_hit
Expand Down Expand Up @@ -102,6 +108,28 @@ def fromtimestamp(cls, *args, **kwargs):
self.assertEqual(fromtimestamp(TS, tz=timezone.utc), expected)
self.assertTrue(was_hit)

def test_stacking(self):
domain = Domain([TimeVariable("a"), ContinuousVariable("b"),
ContinuousVariable("c")],
class_vars=DiscreteVariable("cls", values=["1", "0"]))
ts = Table.from_numpy(domain, [(585167768, 0.88224325, 0.87219962),
(1402820096, 0.30777631, 0.44907067),
(899898806, 0.79237373, 0.42574664),
(1270258083, 0.99060523, 0.05312487),
(393049941, 0.41741731, 0.36743904),
(701125886, 0.77428077, 0.12416262),
(18750041, 0.91150353, 0.80794697),
(815292377, 0.99979289, 0.82930241),
(161861359, 0.06679552, 0.70782449),
(408412451, 0.37535755, 0.94807882)],
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
sf = StackedFitter([TreeLearner(), KNNLearner()])
cv = CrossValidation(k=3, random_state=0)
results = cv(ts, [sf, KNNLearner(), TreeLearner()])
mse = MSE()(results)
self.assertLess(mse[0], mse[1])
self.assertLess(mse[0], mse[2])


if __name__ == "__main__":
unittest.main()
Loading