{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# OPTaaS SubsetParameter\n", "\n", "### Note: To run this notebook, you need an API Key. You can get one here.\n", "\n", "A new feature in the latest version of OPTaaS is the `SubsetParameter`. It's similar to the existing `CategoricalParameter`, but instead of choosing one value from a set of allowed values, we can choose a subset (zero or more values).\n", "\n", "For example, we may want to know whether winsorizing any of the features in our dataset has a positive impact:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "{'id': 'winsorize', 'name': 'winsorize', 'type': 'subset', 'default': ['feature_A', 'feature_C'], 'allowedValues': ['feature_A', 'feature_B', 'feature_C', 'feature_D', 'feature_E']}" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from mindfoundry.optaas.client.parameter import SubsetParameter, BoolParameter\n", "\n", "winsorize = SubsetParameter('winsorize', id='winsorize', values=['feature_A', 'feature_B', 'feature_C', 'feature_D', 'feature_E'],\n", " default=['feature_A', 'feature_C'])\n", "\n", "display(winsorize)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Constraints\n", "\n", "We can use constraints with SubsetParameters:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "if #winsorize_all == true then #winsorize == ['feature_A', 'feature_B', 'feature_C', 'feature_D', 'feature_E']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindfoundry.optaas.client.constraint import Constraint\n", "\n", "winsorize_all = BoolParameter('winsorize_all', id='winsorize_all')\n", "\n", "# Equals\n", "Constraint(when=winsorize_all == True, then=winsorize == ['feature_A', 'feature_B', 'feature_C', 'feature_D', 'feature_E'])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "#winsorize != []" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Not equals\n", "Constraint(winsorize != [])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And if the parameter is optional:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "#winsorize is_present" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Constraint(winsorize.is_present())" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "#winsorize is_absent" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Constraint(winsorize.is_absent())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use it in a Task" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "[{'type': 'default', 'values': {'winsorize': ['feature_A', 'feature_C']}},\n", " { 'type': 'exploration',\n", " 'values': {'winsorize': ['feature_B', 'feature_D', 'feature_E']}},\n", " { 'type': 'exploration',\n", " 'values': {'winsorize': ['feature_A', 'feature_B', 'feature_C', 'feature_D']}},\n", " {'type': 'exploration', 'values': {'winsorize': ['feature_B', 'feature_E']}},\n", " {'type': 'exploration', 'values': {'winsorize': ['feature_A', 'feature_C']}},\n", " {'type': 'exploration', 'values': {'winsorize': ['feature_E']}},\n", " {'type': 'exploration', 'values': {'winsorize': ['feature_D']}},\n", " { 'type': 'exploration',\n", " 'values': {'winsorize': ['feature_A', 'feature_B', 'feature_D', 'feature_E']}},\n", " {'type': 'exploration', 'values': {'winsorize': ['feature_A', 'feature_C']}},\n", " {'type': 'exploration', 'values': {'winsorize': ['feature_E']}}]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mindfoundry.optaas.client.client import OPTaaSClient\n", "\n", "client = OPTaaSClient('https://optaas.mindfoundry.ai', '')\n", "\n", "task = client.create_task(\n", " title='Winsorizing', \n", " parameters=[winsorize],\n", " constraints=[Constraint(winsorize != [])],\n", ")\n", "\n", "task.generate_configurations(10)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.5" }, "nav_menu": {}, "toc": { "navigate_menu": true, "number_sections": false, "sideBar": true, "threshold": 6, "toc_cell": false, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }