From 0297023c00006aee549e31f31314449f5f27063e Mon Sep 17 00:00:00 2001 From: janvanrijn Date: Tue, 1 Oct 2019 19:53:37 +0200 Subject: [PATCH 1/6] added example strang, and more filter options --- examples/40_paper/2018_ida_strang_example.py | 59 +++++++++++++++++++- openml/datasets/functions.py | 11 +++- openml/evaluations/functions.py | 9 +++ openml/runs/functions.py | 9 +++ tests/test_study/test_study_functions.py | 16 ++++-- 5 files changed, 97 insertions(+), 7 deletions(-) diff --git a/examples/40_paper/2018_ida_strang_example.py b/examples/40_paper/2018_ida_strang_example.py index 21165a8ffb..2a98a8b152 100644 --- a/examples/40_paper/2018_ida_strang_example.py +++ b/examples/40_paper/2018_ida_strang_example.py @@ -12,6 +12,61 @@ | Benjamin Strang, Pieter Putten, Jan van Rijn and Frank Hutter | In *Advances in Intelligent Data Analysis XVII 17th International Symposium*, 2018 | Available at https://link.springer.com/chapter/10.1007%2F978-3-030-01768-2_25 - -This is currently a placeholder. """ +import matplotlib.pyplot as plt +import openml +import pandas as pd + +study_id = 123 +# for comparing svms: flow_ids = [7754, 7756] +# for comparing nns: flow_ids = [7722, 7729] +# for comparing dts: flow_ids = [7725], differentiate on hyper-parameter value instead +classifier_family = 'SVM' +flow_ids = [7754, 7756] +measure = 'predictive_accuracy' +meta_features = ['NumberOfInstances', 'NumberOfFeatures'] + + +evaluations = openml.evaluations.list_evaluations(measure, flow=flow_ids, study=study_id, output_format='dataframe') +evaluations['data_id'] = evaluations['data_id'].apply(pd.to_numeric) +evaluations = evaluations.pivot(index='data_id', columns='flow_id', values='value').dropna() +data_qualities = openml.datasets.list_datasets(data_id=list(evaluations.index.values), output_format='dataframe') +data_qualities = data_qualities[meta_features] +evaluations = evaluations.join(data_qualities, how='inner') + +# adds column that indicates the difference between the two classifiers +evaluations['difference'] = evaluations[flow_ids[0]] - evaluations[flow_ids[1]] + +fig_splot, ax_splot = plt.subplots() +ax_splot.plot(range(len(evaluations)), sorted(evaluations['difference'])) +ax_splot.set_title(classifier_family) +ax_splot.set_xlabel('Dataset (sorted)') +ax_splot.set_ylabel('difference between linear and non-linear classifier') +ax_splot.grid(linestyle='--', axis='y') +plt.show() + + +# adds column that indicates the difference between the two classifiers +class_values = ['non-linear better', 'linear better', 'equal'] +def determine_class(val_lin, val_nonlin): + if val_lin < val_nonlin: + return class_values[0] + elif val_nonlin < val_lin: + return class_values[1] + else: + return class_values[2] + + +evaluations['class'] = evaluations.apply(lambda row: determine_class(row[flow_ids[0]], row[flow_ids[1]]), axis=1) + +fig_scatter, ax_scatter = plt.subplots() +for class_val in class_values: + df_class = evaluations[evaluations['class'] == class_val] + plt.scatter(df_class[meta_features[0]], df_class[meta_features[1]], label=class_val) +ax_scatter.set_title(classifier_family) +ax_scatter.set_xlabel(meta_features[0]) +ax_scatter.set_ylabel(meta_features[1]) +ax_scatter.legend() +ax_scatter.set_xscale('log') +ax_scatter.set_yscale('log') +plt.show() diff --git a/openml/datasets/functions.py b/openml/datasets/functions.py index bd33592208..f2eb00a6f5 100644 --- a/openml/datasets/functions.py +++ b/openml/datasets/functions.py @@ -260,12 +260,19 @@ def list_datasets( **kwargs) -def _list_datasets(output_format='dict', **kwargs): +def _list_datasets(data_id: Optional[List] = None, output_format='dict', **kwargs): """ Perform api call to return a list of all datasets. Parameters ---------- + The arguments that are lists are separated from the single value + ones which are put into the kwargs. + display_errors is also separated from the kwargs since it has a + default value. + + data_id : list, optional + output_format: str, optional (default='dict') The parameter decides the format of the output. - If 'dict' the output is a dict of dict @@ -285,6 +292,8 @@ def _list_datasets(output_format='dict', **kwargs): if kwargs is not None: for operator, value in kwargs.items(): api_call += "/%s/%s" % (operator, value) + if data_id is not None: + api_call += "/data_id/%s" % ','.join([str(int(i)) for i in data_id]) return __list_datasets(api_call=api_call, output_format=output_format) diff --git a/openml/evaluations/functions.py b/openml/evaluations/functions.py index 6e67623fda..5972638a36 100644 --- a/openml/evaluations/functions.py +++ b/openml/evaluations/functions.py @@ -21,6 +21,7 @@ def list_evaluations( flow: Optional[List] = None, uploader: Optional[List] = None, tag: Optional[str] = None, + study: Optional[int] = None, per_fold: Optional[bool] = None, sort_order: Optional[str] = None, output_format: str = 'object' @@ -50,6 +51,8 @@ def list_evaluations( tag : str, optional + study : int, optional + per_fold : bool, optional sort_order : str, optional @@ -84,6 +87,7 @@ def list_evaluations( flow=flow, uploader=uploader, tag=tag, + study=study, sort_order=sort_order, per_fold=per_fold_str) @@ -95,6 +99,7 @@ def _list_evaluations( setup: Optional[List] = None, flow: Optional[List] = None, uploader: Optional[List] = None, + study: Optional[int] = None, sort_order: Optional[str] = None, output_format: str = 'object', **kwargs @@ -120,6 +125,8 @@ def _list_evaluations( uploader : list, optional + study : int, optional + kwargs: dict, optional Legal filter operators: tag, limit, offset. @@ -153,6 +160,8 @@ def _list_evaluations( api_call += "/flow/%s" % ','.join([str(int(i)) for i in flow]) if uploader is not None: api_call += "/uploader/%s" % ','.join([str(int(i)) for i in uploader]) + if study is not None: + api_call += "/study/%d" % study if sort_order is not None: api_call += "/sort_order/%s" % sort_order diff --git a/openml/runs/functions.py b/openml/runs/functions.py index 623a2544e9..95407d5177 100644 --- a/openml/runs/functions.py +++ b/openml/runs/functions.py @@ -789,6 +789,7 @@ def list_runs( flow: Optional[List] = None, uploader: Optional[List] = None, tag: Optional[str] = None, + study: Optional[int] = None, display_errors: bool = False, output_format: str = 'dict', **kwargs @@ -816,6 +817,8 @@ def list_runs( tag : str, optional + study : int, optional + display_errors : bool, optional (default=None) Whether to list runs which have an error (for example a missing prediction file). @@ -857,6 +860,7 @@ def list_runs( flow=flow, uploader=uploader, tag=tag, + study=study, display_errors=display_errors, **kwargs) @@ -867,6 +871,7 @@ def _list_runs( setup: Optional[List] = None, flow: Optional[List] = None, uploader: Optional[List] = None, + study: Optional[int] = None, display_errors: bool = False, output_format: str = 'dict', **kwargs @@ -892,6 +897,8 @@ def _list_runs( uploader : list, optional + study : int, optional + display_errors : bool, optional (default=None) Whether to list runs which have an error (for example a missing prediction file). @@ -924,6 +931,8 @@ def _list_runs( api_call += "/flow/%s" % ','.join([str(int(i)) for i in flow]) if uploader is not None: api_call += "/uploader/%s" % ','.join([str(int(i)) for i in uploader]) + if study is not None: + api_call += "/study/%d" % study if display_errors: api_call += "/show_errors/true" return __list_runs(api_call=api_call, output_format=output_format) diff --git a/tests/test_study/test_study_functions.py b/tests/test_study/test_study_functions.py index 2c4574da3a..0194c5b0fb 100644 --- a/tests/test_study/test_study_functions.py +++ b/tests/test_study/test_study_functions.py @@ -117,15 +117,15 @@ def test_publish_benchmark_suite(self): def test_publish_study(self): # get some random runs to attach - run_list = openml.runs.list_runs(size=10) + run_list = openml.evaluations.list_evaluations('predictive_accuracy', size=10) self.assertEqual(len(run_list), 10) fixt_alias = None fixt_name = 'unit tested study' fixt_descr = 'bla' - fixt_flow_ids = set([run['flow_id'] for run in run_list.values()]) - fixt_task_ids = set([run['task_id'] for run in run_list.values()]) - fixt_setup_ids = set([run['setup_id']for run in run_list.values()]) + fixt_flow_ids = set([evaluation.flow_id for evaluation in run_list.values()]) + fixt_task_ids = set([evaluation.task_id for evaluation in run_list.values()]) + fixt_setup_ids = set([evaluation.setup_id for evaluation in run_list.values()]) study = openml.study.create_study( alias=fixt_alias, @@ -149,6 +149,14 @@ def test_publish_study(self): self.assertSetEqual(set(study_downloaded.flows), set(fixt_flow_ids)) self.assertSetEqual(set(study_downloaded.tasks), set(fixt_task_ids)) + # test whether the list run function also handles study data fine + run_ids = openml.runs.list_runs(study=study_id) + self.assertSetEqual(set(run_ids), set(study_downloaded.runs)) + + # test whether the list evaluation function also handles study data fine + run_ids = openml.evaluations.list_evaluations('predictive_accuracy', study=study_id) + self.assertSetEqual(set(run_ids), set(study_downloaded.runs)) + # attach more runs run_list_additional = openml.runs.list_runs(size=10, offset=10) openml.study.attach_to_study(study_id, From a36804b9eecc76a17d093a83972269887c4f692d Mon Sep 17 00:00:00 2001 From: janvanrijn Date: Tue, 1 Oct 2019 20:09:57 +0200 Subject: [PATCH 2/6] added comments --- examples/40_paper/2018_ida_strang_example.py | 51 ++++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/examples/40_paper/2018_ida_strang_example.py b/examples/40_paper/2018_ida_strang_example.py index 2a98a8b152..fe7a9ed9d3 100644 --- a/examples/40_paper/2018_ida_strang_example.py +++ b/examples/40_paper/2018_ida_strang_example.py @@ -17,28 +17,56 @@ import openml import pandas as pd +############################################################################## +# A basic step for each data-mining or machine learning task is to determine +# which model to choose based on the problem and the data at hand. In this +# work we investigate when non-linear classifiers outperform linear +# classifiers by means of a large scale experiment. +# +# The paper is accompanied with a study object, containing all relevant tasks +# and runs (study id = 123). The paper features three experiment classes: +# Support Vector Machines (SVM), Neural Networks (NN) and Decision Trees (DT). +# This example demonstrates how to reproduce the plots, comparing two +# classifiers given the OpenML flow ids. Note that this allows us to reproduce +# the SVM and NN experiment, but not the DT experiment, as this requires a bit +# more effort to distinguish the same flow with different hyperparameter +# values. +############################################################################## + study_id = 123 # for comparing svms: flow_ids = [7754, 7756] # for comparing nns: flow_ids = [7722, 7729] -# for comparing dts: flow_ids = [7725], differentiate on hyper-parameter value instead +# for comparing dts: flow_ids = [7725], differentiate on hyper-parameter value classifier_family = 'SVM' flow_ids = [7754, 7756] measure = 'predictive_accuracy' meta_features = ['NumberOfInstances', 'NumberOfFeatures'] +class_values = ['non-linear better', 'linear better', 'equal'] - -evaluations = openml.evaluations.list_evaluations(measure, flow=flow_ids, study=study_id, output_format='dataframe') +# Downloads all evaluation records related to this study +evaluations = openml.evaluations.list_evaluations( + measure, flow=flow_ids, study=study_id, output_format='dataframe') +# cast to integer (required to do a join to meta-features) evaluations['data_id'] = evaluations['data_id'].apply(pd.to_numeric) -evaluations = evaluations.pivot(index='data_id', columns='flow_id', values='value').dropna() -data_qualities = openml.datasets.list_datasets(data_id=list(evaluations.index.values), output_format='dataframe') +# gives us a table with columns data_id, flow1_value, flow2_value +evaluations = evaluations.pivot( + index='data_id', columns='flow_id', values='value').dropna() +# downloads all data qualities (for scatter plot) +data_qualities = openml.datasets.list_datasets( + data_id=list(evaluations.index.values), output_format='dataframe') +# removes irrelevant data qualities data_qualities = data_qualities[meta_features] +# makes a join between evaluation table and data qualities table, +# now we have columns data_id, flow1_value, flow2_value, meta_feature_1, +# meta_feature_2 evaluations = evaluations.join(data_qualities, how='inner') # adds column that indicates the difference between the two classifiers -evaluations['difference'] = evaluations[flow_ids[0]] - evaluations[flow_ids[1]] +evaluations['diff'] = evaluations[flow_ids[0]] - evaluations[flow_ids[1]] +# makes the s-plot fig_splot, ax_splot = plt.subplots() -ax_splot.plot(range(len(evaluations)), sorted(evaluations['difference'])) +ax_splot.plot(range(len(evaluations)), sorted(evaluations['diff'])) ax_splot.set_title(classifier_family) ax_splot.set_xlabel('Dataset (sorted)') ax_splot.set_ylabel('difference between linear and non-linear classifier') @@ -47,7 +75,6 @@ # adds column that indicates the difference between the two classifiers -class_values = ['non-linear better', 'linear better', 'equal'] def determine_class(val_lin, val_nonlin): if val_lin < val_nonlin: return class_values[0] @@ -57,12 +84,16 @@ def determine_class(val_lin, val_nonlin): return class_values[2] -evaluations['class'] = evaluations.apply(lambda row: determine_class(row[flow_ids[0]], row[flow_ids[1]]), axis=1) +evaluations['class'] = evaluations.apply( + lambda row: determine_class(row[flow_ids[0]], row[flow_ids[1]]), axis=1) +# makes the scatter plot fig_scatter, ax_scatter = plt.subplots() for class_val in class_values: df_class = evaluations[evaluations['class'] == class_val] - plt.scatter(df_class[meta_features[0]], df_class[meta_features[1]], label=class_val) + plt.scatter(df_class[meta_features[0]], + df_class[meta_features[1]], + label=class_val) ax_scatter.set_title(classifier_family) ax_scatter.set_xlabel(meta_features[0]) ax_scatter.set_ylabel(meta_features[1]) From 2a07a69424cdfe9e8e55346bbcf38d81f48a079f Mon Sep 17 00:00:00 2001 From: janvanrijn Date: Tue, 1 Oct 2019 23:05:41 +0200 Subject: [PATCH 3/6] updated data id from list function --- openml/datasets/functions.py | 7 ++++++- tests/test_datasets/test_dataset_functions.py | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/openml/datasets/functions.py b/openml/datasets/functions.py index f2eb00a6f5..0f8b486cd4 100644 --- a/openml/datasets/functions.py +++ b/openml/datasets/functions.py @@ -190,6 +190,7 @@ def list_qualities() -> List[str]: def list_datasets( + data_id: Optional[List[int]] = None, offset: Optional[int] = None, size: Optional[int] = None, status: Optional[str] = None, @@ -204,6 +205,9 @@ def list_datasets( Parameters ---------- + data_id : list, optional + A list of data ids, to specify which datasets should be + listed offset : int, optional The number of datasets to skip, starting from the first. size : int, optional @@ -251,7 +255,8 @@ def list_datasets( raise ValueError("Invalid output format selected. " "Only 'dict' or 'dataframe' applicable.") - return openml.utils._list_all(output_format=output_format, + return openml.utils._list_all(data_id=data_id, + output_format=output_format, listing_call=_list_datasets, offset=offset, size=size, diff --git a/tests/test_datasets/test_dataset_functions.py b/tests/test_datasets/test_dataset_functions.py index 5726d2442a..3453644576 100644 --- a/tests/test_datasets/test_dataset_functions.py +++ b/tests/test_datasets/test_dataset_functions.py @@ -525,23 +525,23 @@ def test_data_status(self): openml.datasets.status_update(did, 'active') # need to use listing fn, as this is immune to cache - result = openml.datasets.list_datasets(data_id=did, status='all') + result = openml.datasets.list_datasets(data_id=[did], status='all') self.assertEqual(len(result), 1) self.assertEqual(result[did]['status'], 'active') openml.datasets.status_update(did, 'deactivated') # need to use listing fn, as this is immune to cache - result = openml.datasets.list_datasets(data_id=did, status='all') + result = openml.datasets.list_datasets(data_id=[did], status='all') self.assertEqual(len(result), 1) self.assertEqual(result[did]['status'], 'deactivated') openml.datasets.status_update(did, 'active') # need to use listing fn, as this is immune to cache - result = openml.datasets.list_datasets(data_id=did, status='all') + result = openml.datasets.list_datasets(data_id=[did], status='all') self.assertEqual(len(result), 1) self.assertEqual(result[did]['status'], 'active') with self.assertRaises(ValueError): openml.datasets.status_update(did, 'in_preparation') # need to use listing fn, as this is immune to cache - result = openml.datasets.list_datasets(data_id=did, status='all') + result = openml.datasets.list_datasets(data_id=[did], status='all') self.assertEqual(len(result), 1) self.assertEqual(result[did]['status'], 'active') From 4063646689789a664e240b4521ec8cae0ebbce1f Mon Sep 17 00:00:00 2001 From: janvanrijn Date: Tue, 1 Oct 2019 23:22:42 +0200 Subject: [PATCH 4/6] typo fix --- examples/40_paper/2018_ida_strang_example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/40_paper/2018_ida_strang_example.py b/examples/40_paper/2018_ida_strang_example.py index fe7a9ed9d3..49700c861a 100644 --- a/examples/40_paper/2018_ida_strang_example.py +++ b/examples/40_paper/2018_ida_strang_example.py @@ -9,7 +9,7 @@ ~~~~~~~~~~~ | Don't Rule Out Simple Models Prematurely: A Large Scale Benchmark Comparing Linear and Non-linear Classifiers in OpenML -| Benjamin Strang, Pieter Putten, Jan van Rijn and Frank Hutter +| Benjamin Strang, Peter van der Putten, Jan N. van Rijn and Frank Hutter | In *Advances in Intelligent Data Analysis XVII 17th International Symposium*, 2018 | Available at https://link.springer.com/chapter/10.1007%2F978-3-030-01768-2_25 """ From 45299d3bc61a14596026dad433d1c446a6730276 Mon Sep 17 00:00:00 2001 From: janvanrijn Date: Wed, 2 Oct 2019 15:47:35 +0200 Subject: [PATCH 5/6] improved comments by Matthias F. --- examples/40_paper/2018_ida_strang_example.py | 3 --- openml/evaluations/functions.py | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/examples/40_paper/2018_ida_strang_example.py b/examples/40_paper/2018_ida_strang_example.py index 49700c861a..5f4abce1d8 100644 --- a/examples/40_paper/2018_ida_strang_example.py +++ b/examples/40_paper/2018_ida_strang_example.py @@ -31,7 +31,6 @@ # the SVM and NN experiment, but not the DT experiment, as this requires a bit # more effort to distinguish the same flow with different hyperparameter # values. -############################################################################## study_id = 123 # for comparing svms: flow_ids = [7754, 7756] @@ -46,8 +45,6 @@ # Downloads all evaluation records related to this study evaluations = openml.evaluations.list_evaluations( measure, flow=flow_ids, study=study_id, output_format='dataframe') -# cast to integer (required to do a join to meta-features) -evaluations['data_id'] = evaluations['data_id'].apply(pd.to_numeric) # gives us a table with columns data_id, flow1_value, flow2_value evaluations = evaluations.pivot( index='data_id', columns='flow_id', values='value').dropna() diff --git a/openml/evaluations/functions.py b/openml/evaluations/functions.py index 5972638a36..38c31ad12e 100644 --- a/openml/evaluations/functions.py +++ b/openml/evaluations/functions.py @@ -205,7 +205,7 @@ def __list_evaluations(api_call, output_format='object'): int(eval_['oml:setup_id']), int(eval_['oml:flow_id']), eval_['oml:flow_name'], - eval_['oml:data_id'], + int(eval_['oml:data_id']), eval_['oml:data_name'], eval_['oml:function'], eval_['oml:upload_time'], @@ -219,7 +219,7 @@ def __list_evaluations(api_call, output_format='object'): 'setup_id': int(eval_['oml:setup_id']), 'flow_id': int(eval_['oml:flow_id']), 'flow_name': eval_['oml:flow_name'], - 'data_id': eval_['oml:data_id'], + 'data_id': int(eval_['oml:data_id']), 'data_name': eval_['oml:data_name'], 'function': eval_['oml:function'], 'upload_time': eval_['oml:upload_time'], From c6eb05f260b4502eb71bd7f52d6fa165867c812f Mon Sep 17 00:00:00 2001 From: janvanrijn Date: Wed, 2 Oct 2019 15:49:37 +0200 Subject: [PATCH 6/6] style update --- examples/40_paper/2018_ida_strang_example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/40_paper/2018_ida_strang_example.py b/examples/40_paper/2018_ida_strang_example.py index 5f4abce1d8..70ed51ba23 100644 --- a/examples/40_paper/2018_ida_strang_example.py +++ b/examples/40_paper/2018_ida_strang_example.py @@ -24,7 +24,7 @@ # classifiers by means of a large scale experiment. # # The paper is accompanied with a study object, containing all relevant tasks -# and runs (study id = 123). The paper features three experiment classes: +# and runs (``study_id=123``). The paper features three experiment classes: # Support Vector Machines (SVM), Neural Networks (NN) and Decision Trees (DT). # This example demonstrates how to reproduce the plots, comparing two # classifiers given the OpenML flow ids. Note that this allows us to reproduce