{ "cells": [ { "cell_type": "markdown", "id": "621bc936", "metadata": {}, "source": [ "# AutoPrognosis classification\n", "\n", "Welcome! This tutorial will walk you through the steps of selecting a model for a classification task using AutoPrognosis." ] }, { "cell_type": "markdown", "id": "3a69e2ef", "metadata": {}, "source": [ "### Setup" ] }, { "cell_type": "code", "execution_count": null, "id": "90163d91", "metadata": {}, "outputs": [], "source": [ "# stdlib\n", "import json\n", "import warnings\n", "\n", "# third party\n", "import pandas as pd\n", "from sklearn.datasets import load_breast_cancer\n", "from sklearn.model_selection import train_test_split\n", "\n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "markdown", "id": "e98145ee", "metadata": {}, "source": [ "### Import ClassifierStudy\n", "\n", "ClassifierStudy is the engine that learns an ensemble of pipelines and their hyperparameters automatically." ] }, { "cell_type": "code", "execution_count": null, "id": "822b4467", "metadata": {}, "outputs": [], "source": [ "# autoprognosis absolute\n", "from autoprognosis.studies.classifiers import ClassifierStudy" ] }, { "cell_type": "markdown", "id": "2cce4436", "metadata": {}, "source": [ "### Load the target dataset\n", "\n", "AutoPrognosis expects pandas.DataFrames as input.\n", "\n", "For this example, we will use the [Breast Cancer Wisconsin Dataset](https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+(Diagnostic))." ] }, { "cell_type": "code", "execution_count": null, "id": "f3141611", "metadata": {}, "outputs": [], "source": [ "# stdlib\n", "from pathlib import Path\n", "\n", "X, Y = load_breast_cancer(return_X_y=True, as_frame=True)\n", "\n", "df = X.copy()\n", "df[\"target\"] = Y" ] }, { "cell_type": "markdown", "id": "1ecb0906", "metadata": {}, "source": [ "### Create the classifier\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": "49bea04a", "metadata": {}, "outputs": [], "source": [ "# List the available plugins\n", "\n", "# autoprognosis absolute\n", "from autoprognosis.plugins import Plugins\n", "\n", "print(json.dumps(Plugins().list_available(), indent=2))" ] }, { "cell_type": "markdown", "id": "5382e125", "metadata": {}, "source": [ "We will set a few custom plugins for the pipelines and create the classifier study." ] }, { "cell_type": "code", "execution_count": null, "id": "c3061090", "metadata": {}, "outputs": [], "source": [ "workspace = Path(\"workspace\")\n", "workspace.mkdir(parents=True, exist_ok=True)\n", "\n", "study_name = \"classification_example\"\n", "\n", "study = ClassifierStudy(\n", " study_name=study_name,\n", " dataset=df, # pandas DataFrame\n", " target=\"target\", # the label column in the dataset\n", " num_iter=2, # DELETE THIS LINE FOR BETTER RESULTS. how many trials to do for each candidate. Default: 50\n", " num_study_iter=1, # DELETE THIS LINE FOR BETTER RESULTS. how many outer iterations to do. Default: 5\n", " classifiers=[\n", " \"logistic_regression\",\n", " \"lda\",\n", " \"qda\",\n", " ], # DELETE THIS LINE FOR BETTER RESULTS.\n", " workspace=workspace,\n", ")" ] }, { "cell_type": "markdown", "id": "de1e3907", "metadata": {}, "source": [ "### Search for the optimal ensemble\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b30a99bf", "metadata": {}, "outputs": [], "source": [ "study.run()" ] }, { "cell_type": "code", "execution_count": null, "id": "399fd86c", "metadata": {}, "outputs": [], "source": [ "# stdlib\n", "import pprint\n", "\n", "# autoprognosis absolute\n", "from autoprognosis.utils.serialization import load_model_from_file\n", "from autoprognosis.utils.tester import evaluate_estimator\n", "\n", "output = workspace / study_name / \"model.p\"\n", "\n", "model = load_model_from_file(output)\n", "\n", "metrics = evaluate_estimator(model, X, Y)\n", "\n", "print(f\"Model {model.name()} \")\n", "print(\"Score: \")\n", "\n", "pprint.pprint(metrics)" ] }, { "cell_type": "markdown", "id": "b4e3a83e", "metadata": {}, "source": [ "## Serialization" ] }, { "cell_type": "code", "execution_count": null, "id": "2230c4d2", "metadata": {}, "outputs": [], "source": [ "# autoprognosis absolute\n", "from autoprognosis.utils.serialization import load_from_file, save_to_file\n", "\n", "out = workspace / \"tmp.bkp\"\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": "e2a3ba34", "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.13" } }, "nbformat": 4, "nbformat_minor": 5 }