|
| 1 | +""" |
| 2 | +================= |
| 3 | +Benchmark studies |
| 4 | +================= |
| 5 | +
|
| 6 | +How to list, download and upload benchmark studies. |
| 7 | +
|
| 8 | +In contrast to `benchmark suites <https://docs.openml.org/benchmark/#benchmarking-suites>`_ which |
| 9 | +hold a list of tasks, studies hold a list of runs. As runs contain all information on flows and |
| 10 | +tasks, all required information about a study can be retrieved. |
| 11 | +""" |
| 12 | +############################################################################ |
| 13 | +import uuid |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +import sklearn.tree |
| 17 | +import sklearn.pipeline |
| 18 | +import sklearn.impute |
| 19 | + |
| 20 | +import openml |
| 21 | + |
| 22 | + |
| 23 | +############################################################################ |
| 24 | +# .. warning:: This example uploads data. For that reason, this example |
| 25 | +# connects to the test server at test.openml.org before doing so. |
| 26 | +# This prevents the crowding of the main server with example datasets, |
| 27 | +# tasks, runs, and so on. |
| 28 | +############################################################################ |
| 29 | + |
| 30 | + |
| 31 | +############################################################################ |
| 32 | +# Listing studies |
| 33 | +# *************** |
| 34 | +# |
| 35 | +# * Use the output_format parameter to select output type |
| 36 | +# * Default gives ``dict``, but we'll use ``dataframe`` to obtain an |
| 37 | +# easier-to-work-with data structure |
| 38 | + |
| 39 | +studies = openml.study.list_studies(output_format='dataframe', status='all') |
| 40 | +print(studies.head(n=10)) |
| 41 | + |
| 42 | + |
| 43 | +############################################################################ |
| 44 | +# Downloading studies |
| 45 | +# =================== |
| 46 | + |
| 47 | +############################################################################ |
| 48 | +# This is done based on the study ID. |
| 49 | +study = openml.study.get_study(123) |
| 50 | +print(study) |
| 51 | + |
| 52 | +############################################################################ |
| 53 | +# Studies also features a description: |
| 54 | +print(study.description) |
| 55 | + |
| 56 | +############################################################################ |
| 57 | +# Studies are a container for runs: |
| 58 | +print(study.runs) |
| 59 | + |
| 60 | +############################################################################ |
| 61 | +# And we can use the evaluation listing functionality to learn more about |
| 62 | +# the evaluations available for the conducted runs: |
| 63 | +evaluations = openml.evaluations.list_evaluations( |
| 64 | + function='predictive_accuracy', |
| 65 | + output_format='dataframe', |
| 66 | + study=study.study_id, |
| 67 | +) |
| 68 | +print(evaluations.head()) |
| 69 | + |
| 70 | +############################################################################ |
| 71 | +# Uploading studies |
| 72 | +# ================= |
| 73 | +# |
| 74 | +# Creating a study is as simple as creating any kind of other OpenML entity. |
| 75 | +# In this examples we'll create a few runs for the OpenML-100 benchmark |
| 76 | +# suite which is available on the OpenML test server. |
| 77 | + |
| 78 | +openml.config.start_using_configuration_for_example() |
| 79 | + |
| 80 | +# Very simple classifier which ignores the feature type |
| 81 | +clf = sklearn.pipeline.Pipeline(steps=[ |
| 82 | + ('imputer', sklearn.impute.SimpleImputer()), |
| 83 | + ('estimator', sklearn.tree.DecisionTreeClassifier(max_depth=5)), |
| 84 | +]) |
| 85 | + |
| 86 | +suite = openml.study.get_suite(1) |
| 87 | +# We'll create a study with one run on three random datasets each |
| 88 | +tasks = np.random.choice(suite.tasks, size=3, replace=False) |
| 89 | +run_ids = [] |
| 90 | +for task_id in tasks: |
| 91 | + task = openml.tasks.get_task(task_id) |
| 92 | + run = openml.runs.run_model_on_task(clf, task) |
| 93 | + run.publish() |
| 94 | + run_ids.append(run.run_id) |
| 95 | + |
| 96 | +# The study needs a machine-readable and unique alias. To obtain this, |
| 97 | +# we simply generate a random uuid. |
| 98 | +alias = uuid.uuid4().hex |
| 99 | + |
| 100 | +new_study = openml.study.create_study( |
| 101 | + name='Test-Study', |
| 102 | + description='Test study for the Python tutorial on studies', |
| 103 | + run_ids=run_ids, |
| 104 | + alias=alias, |
| 105 | + benchmark_suite=suite.study_id, |
| 106 | +) |
| 107 | +new_study.publish() |
| 108 | +print(new_study) |
| 109 | + |
| 110 | + |
| 111 | +############################################################################ |
| 112 | +openml.config.stop_using_configuration_for_example() |
0 commit comments