{ "cells": [ { "cell_type": "markdown", "id": "molecular-moscow", "metadata": {}, "source": [ "# AutoPrognosis regression\n", "\n", "Welcome! This tutorial will walk you through the steps of selecting a model for a regression task using AutoPrognosis." ] }, { "cell_type": "markdown", "id": "auburn-hygiene", "metadata": {}, "source": [ "### Setup" ] }, { "cell_type": "code", "execution_count": null, "id": "wanted-point", "metadata": {}, "outputs": [], "source": [ "# stdlib\n", "import json\n", "import warnings\n", "\n", "# third party\n", "import pandas as pd\n", "from sklearn.model_selection import train_test_split\n", "\n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "markdown", "id": "573e7cfc", "metadata": {}, "source": [ "### Import RegressionStudy\n", "\n", "RegressionStudy is the engine that learns an ensemble of regression pipelines and their hyperparameters automatically." ] }, { "cell_type": "code", "execution_count": null, "id": "c1304c76", "metadata": {}, "outputs": [], "source": [ "# autoprognosis absolute\n", "from autoprognosis.studies.regression import RegressionStudy" ] }, { "cell_type": "markdown", "id": "devoted-console", "metadata": {}, "source": [ "### Load the target dataset\n", "\n", "AutoPrognosis expects pandas.DataFrames as input.\n", "\n", "For this example, we will use the [Airfoil Self-Noise Data Set](https://archive.ics.uci.edu/ml/datasets/airfoil+self-noise)." ] }, { "cell_type": "code", "execution_count": null, "id": "coated-innocent", "metadata": {}, "outputs": [], "source": [ "# third party\n", "import pandas as pd\n", "\n", "df = pd.read_csv(\n", " \"https://archive.ics.uci.edu/ml/machine-learning-databases/00291/airfoil_self_noise.dat\",\n", " header=None,\n", " sep=\"\\\\t\",\n", ")\n", "\n", "\n", "last_col = df.columns[-1]\n", "\n", "y = df[last_col]\n", "X = df.drop(columns=[last_col])\n", "\n", "\n", "df = X.copy()\n", "df[\"target\"] = y\n", "\n", "df" ] }, { "cell_type": "markdown", "id": "refined-booth", "metadata": {}, "source": [ "### Create the regressor\n", "\n", "While AutoPrognosis provides default plugins, it allows the user to customize the plugins for the pipelines.\n", "\n", "You can see the supported plugins below:" ] }, { "cell_type": "code", "execution_count": null, "id": "1def3193", "metadata": {}, "outputs": [], "source": [ "# stdlib\n", "# List the available plugins\n", "import json\n", "\n", "# autoprognosis absolute\n", "from autoprognosis.plugins import Plugins\n", "\n", "print(json.dumps(Plugins().list_available(), indent=2))" ] }, { "cell_type": "markdown", "id": "94bfdf48", "metadata": {}, "source": [ "We will set a few custom plugins for the pipelines and create the classifier study." ] }, { "cell_type": "code", "execution_count": null, "id": "incident-familiar", "metadata": {}, "outputs": [], "source": [ "# stdlib\n", "from pathlib import Path\n", "\n", "workspace = Path(\"workspace\")\n", "workspace.mkdir(parents=True, exist_ok=True)\n", "\n", "study_name = \"regression_example\"\n", "\n", "study = RegressionStudy(\n", " study_name=study_name,\n", " dataset=df, # pandas DataFrame\n", " target=\"target\", # the label column in the dataset\n", " num_iter=10, # DELETE THIS LINE FOR BETTER RESULTS. how many trials to do for each candidate. Default: 50\n", " num_study_iter=2, # DELETE THIS LINE FOR BETTER RESULTS. how many outer iterations to do. Default: 5\n", " regressors=[\n", " \"linear_regression\",\n", " \"xgboost_regressor\",\n", " ], # DELETE THIS LINE FOR BETTER RESULTS.\n", " workspace=workspace,\n", ")" ] }, { "cell_type": "markdown", "id": "dominican-tulsa", "metadata": {}, "source": [ "### Search for the optimal ensemble\n" ] }, { "cell_type": "code", "execution_count": null, "id": "529fd74d", "metadata": {}, "outputs": [], "source": [ "study.run()" ] }, { "cell_type": "code", "execution_count": null, "id": "0677190e", "metadata": {}, "outputs": [], "source": [ "# autoprognosis absolute\n", "from autoprognosis.utils.serialization import load_model_from_file\n", "from autoprognosis.utils.tester import evaluate_regression\n", "\n", "output = workspace / study_name / \"model.p\"\n", "\n", "model = load_model_from_file(output)\n", "\n", "metrics = evaluate_regression(model, X, y)\n", "\n", "f\"Model {model.name()} score: {metrics['raw']}\"" ] }, { "cell_type": "markdown", "id": "3a4295f3", "metadata": {}, "source": [ "## Serialization" ] }, { "cell_type": "code", "execution_count": null, "id": "b7c34fd8", "metadata": {}, "outputs": [], "source": [ "# autoprognosis absolute\n", "from autoprognosis.utils.serialization import load_from_file, save_to_file\n", "\n", "out = workspace / \"tmp.bkp\"\n", "\n", "# Fit the model\n", "model.fit(X, y)\n", "\n", "# Save\n", "save_to_file(out, model)\n", "\n", "# Reload\n", "loaded_model = load_from_file(out)\n", "\n", "print(loaded_model.name())\n", "\n", "assert loaded_model.name() == model.name()\n", "\n", "out.unlink()" ] }, { "cell_type": "markdown", "id": "neural-dutch", "metadata": {}, "source": [ "## Congratulations!\n", "\n", "Congratulations on completing this notebook tutorial! If you enjoyed this and would like to join the movement towards Machine learning and AI for medicine, you can do so in the following ways!\n", "\n", "### Star AutoPrognosis on GitHub\n", "\n", "The easiest way to help our community is just by starring the Repos! This helps raise awareness of the tools we're building.\n", "\n", "- [Star AutoPrognosis](https://github.com/vanderschaarlab/autoprognosis)\n", "- [Star HyperImpute](https://github.com/vanderschaarlab/hyperimpute)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.15" } }, "nbformat": 4, "nbformat_minor": 5 }