Note
Go to the end to download the full example code.
Flows and Runs¶
A simple tutorial on how to train/run a model and how to upload the results.
# License: BSD 3-Clause
import openml
from sklearn import ensemble, neighbors
Warning
This example uploads data. For that reason, this example connects to the test server at test.openml.org. This prevents the main server from crowding with example datasets, tasks, runs, and so on. The use of this test server can affect behaviour and performance of the OpenML-Python API.
openml.config.start_using_configuration_for_example()
/home/runner/work/openml-python/openml-python/examples/20_basic/simple_flows_and_runs_tutorial.py:17: UserWarning: Switching to the test server https://test.openml.org/api/v1/xml to not upload results to the live server. Using the test server may result in reduced performance of the API!
openml.config.start_using_configuration_for_example()
Train a machine learning model¶
# NOTE: We are using dataset "diabetes" from the test server: https://test.openml.org/d/20
dataset = openml.datasets.get_dataset(dataset_id="diabetes", version=1)
X, y, categorical_indicator, attribute_names = dataset.get_data(
target=dataset.default_target_attribute
)
clf = neighbors.KNeighborsClassifier(n_neighbors=3)
clf.fit(X, y)
Running a model on a task¶
task = openml.tasks.get_task(119)
clf = ensemble.RandomForestClassifier()
run = openml.runs.run_model_on_task(clf, task)
print(run)
OpenML Run
==========
Uploader Name...................: None
Metric..........................: None
Local Result - Accuracy (+- STD): 0.7589 +- 0.0000
Local Runtime - ms (+- STD).....: 142.9040 +- 0.0000
Run ID..........................: None
Task ID.........................: 119
Task Type.......................: None
Task URL........................: https://test.openml.org/t/119
Flow ID.........................: 16
Flow Name.......................: sklearn.ensemble._forest.RandomForestClassifier
Flow URL........................: https://test.openml.org/f/16
Setup ID........................: None
Setup String....................: Python_3.8.18. Sklearn_1.3.2. NumPy_1.24.4. SciPy_1.10.1.
Dataset ID......................: 20
Dataset URL.....................: https://test.openml.org/d/20
Publishing the run¶
myrun = run.publish()
print(f"Run was uploaded to {myrun.openml_url}")
print(f"The flow can be found at {myrun.flow.openml_url}")
Run was uploaded to https://test.openml.org/r/3814
The flow can be found at https://test.openml.org/f/16
openml.config.stop_using_configuration_for_example()
Total running time of the script: (0 minutes 8.061 seconds)