Skip to content
Merged
Show file tree
Hide file tree
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
89 changes: 86 additions & 3 deletions examples/40_paper/2018_ida_strang_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,92 @@
~~~~~~~~~~~

| 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

This is currently a placeholder.
"""
import matplotlib.pyplot as plt
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
classifier_family = 'SVM'
flow_ids = [7754, 7756]
measure = 'predictive_accuracy'
meta_features = ['NumberOfInstances', 'NumberOfFeatures']
class_values = ['non-linear better', 'linear better', 'equal']

# Downloads all evaluation records related to this study
evaluations = openml.evaluations.list_evaluations(
measure, flow=flow_ids, study=study_id, 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['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['diff']))
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
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)

# 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)
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()
18 changes: 16 additions & 2 deletions openml/datasets/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -260,12 +265,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
Expand All @@ -285,6 +297,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)


Expand Down
13 changes: 11 additions & 2 deletions openml/evaluations/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -50,6 +51,8 @@ def list_evaluations(

tag : str, optional

study : int, optional

per_fold : bool, optional

sort_order : str, optional
Expand Down Expand Up @@ -84,6 +87,7 @@ def list_evaluations(
flow=flow,
uploader=uploader,
tag=tag,
study=study,
sort_order=sort_order,
per_fold=per_fold_str)

Expand All @@ -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
Expand All @@ -120,6 +125,8 @@ def _list_evaluations(

uploader : list, optional

study : int, optional

kwargs: dict, optional
Legal filter operators: tag, limit, offset.

Expand Down Expand Up @@ -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:
Comment thread
mfeurer marked this conversation as resolved.
Resolved
api_call += "/study/%d" % study
if sort_order is not None:
api_call += "/sort_order/%s" % sort_order

Expand Down Expand Up @@ -196,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'],
Expand All @@ -210,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'],
Expand Down
9 changes: 9 additions & 0 deletions openml/runs/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -857,6 +860,7 @@ def list_runs(
flow=flow,
uploader=uploader,
tag=tag,
study=study,
display_errors=display_errors,
**kwargs)

Expand All @@ -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
Expand All @@ -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).
Expand Down Expand Up @@ -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:
Comment thread
mfeurer marked this conversation as resolved.
Resolved
api_call += "/study/%d" % study
if display_errors:
api_call += "/show_errors/true"
return __list_runs(api_call=api_call, output_format=output_format)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_datasets/test_dataset_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
16 changes: 12 additions & 4 deletions tests/test_study/test_study_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down