{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from lale.grammar import Grammar\n", "from lale.operators import make_choice\n", "from lale import wrap_imported_operators\n", "from lale.settings import set_disable_hyperparams_schema_validation\n", "set_disable_hyperparams_schema_validation(True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple: First example" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_9\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_0\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_1\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_1\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_2\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_2\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_3\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_3\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_4\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_4\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_5\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_6\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_7\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_8\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "\n", "no_op_0\n", "\n", "\n", "No-\n", "Op\n", "\n", "\n", "\n", "\n", "\n", "no_op_1\n", "\n", "\n", "No-\n", "Op\n", "\n", "\n", "\n", "\n", "\n", "no_op_2\n", "\n", "\n", "No-\n", "Op\n", "\n", "\n", "\n", "\n", "\n", "no_op_3\n", "\n", "\n", "No-\n", "Op\n", "\n", "\n", "\n", "\n", "\n", "no_op_4\n", "\n", "\n", "No-\n", "Op\n", "\n", "\n", "\n", "\n", "\n", "pca_0\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "no_op_4->pca_0\n", "\n", "\n", "\n", "\n", "\n", "pca_1\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_0->pca_1\n", "\n", "\n", "\n", "\n", "\n", "scaler_0\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_2\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_1->pca_2\n", "\n", "\n", "\n", "\n", "\n", "scaler_1\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_3\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_2->pca_3\n", "\n", "\n", "\n", "\n", "\n", "scaler_2\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "lr\n", "\n", "\n", "LR\n", "\n", "\n", "\n", "\n", "\n", "pca_3->lr\n", "\n", "\n", "\n", "\n", "\n", "scaler_3\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "knn\n", "\n", "\n", "KNN\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from sklearn.linear_model import LogisticRegression as LR\n", "from sklearn.neighbors import KNeighborsClassifier as KNN\n", "from sklearn.decomposition import PCA\n", "from sklearn.preprocessing import StandardScaler as Scaler\n", "from lale.lib.lale import NoOp\n", "wrap_imported_operators()\n", "\n", "g = Grammar()\n", "\n", "g.start = g.estimator\n", "g.estimator = (NoOp | g.transformer) >> g.prim_est\n", "g.transformer = (NoOp | g.transformer) >> g.prim_tfm\n", "\n", "g.prim_est = LR | KNN\n", "g.prim_tfm = PCA | Scaler\n", "\n", "generated = g.unfold(6)\n", "generated.visualize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Training" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100%|██████████| 6/6 [00:04<00:00, 1.40trial/s, best loss: -0.9261575551782683]\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "no_op\n", "\n", "\n", "No-\n", "Op\n", "\n", "\n", "\n", "\n", "\n", "lr\n", "\n", "\n", "LR\n", "\n", "\n", "\n", "\n", "\n", "no_op->lr\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lale.lib.lale import Hyperopt\n", "import lale.datasets\n", "(train_X, train_y), (test_X, test_y) = lale.datasets.load_iris_df()\n", "\n", "trainer = Hyperopt(estimator=generated, cv=2, max_evals=6)\n", "trained = trainer.fit(train_X, train_y)\n", "trained.get_pipeline().visualize()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "no_op\n", "\n", "\n", "No-\n", "Op\n", "\n", "\n", "\n", "\n", "\n", "knn\n", "\n", "\n", "KNN\n", "\n", "\n", "\n", "\n", "\n", "no_op->knn\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "no_op\n", "\n", "\n", "No-\n", "Op\n", "\n", "\n", "\n", "\n", "\n", "scaler\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "no_op->scaler\n", "\n", "\n", "\n", "\n", "\n", "pca\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "scaler->pca\n", "\n", "\n", "\n", "\n", "\n", "knn\n", "\n", "\n", "KNN\n", "\n", "\n", "\n", "\n", "\n", "pca->knn\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "no_op\n", "\n", "\n", "No-\n", "Op\n", "\n", "\n", "\n", "\n", "\n", "knn\n", "\n", "\n", "KNN\n", "\n", "\n", "\n", "\n", "\n", "no_op->knn\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "no_op\n", "\n", "\n", "No-\n", "Op\n", "\n", "\n", "\n", "\n", "\n", "lr\n", "\n", "\n", "LR\n", "\n", "\n", "\n", "\n", "\n", "no_op->lr\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "no_op\n", "\n", "\n", "No-\n", "Op\n", "\n", "\n", "\n", "\n", "\n", "pca\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "no_op->pca\n", "\n", "\n", "\n", "\n", "\n", "scaler\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca->scaler\n", "\n", "\n", "\n", "\n", "\n", "knn\n", "\n", "\n", "KNN\n", "\n", "\n", "\n", "\n", "\n", "scaler->knn\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pool = {g.sample(10) for _ in range(5)}\n", "for tree in pool:\n", " tree.visualize()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100%|██████████| 6/6 [00:03<00:00, 1.93trial/s, best loss: -0.9383575551782682]\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "no_op\n", "\n", "\n", "No-\n", "Op\n", "\n", "\n", "\n", "\n", "\n", "lr\n", "\n", "\n", "LR\n", "\n", "\n", "\n", "\n", "\n", "no_op->lr\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "generated = make_choice(*pool)\n", "\n", "trainer = Hyperopt(estimator=generated, cv=2, max_evals=6)\n", "trained = trainer.fit(train_X, train_y)\n", "trained.get_pipeline().visualize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Grammar that exercices all combinators" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_0\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_1\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_1\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_2\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_2\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_3\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_3\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_4\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_4\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_5\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_5\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_6\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_7\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_8\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_6\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_9\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_7\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_10\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_8\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_11\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_12\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_9\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_13\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_10\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_14\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_15\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_16\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_17\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_11\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_18\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_12\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_19\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_13\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_20\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_21\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_14\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_22\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_23\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_24\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "\n", "lr_0\n", "\n", "\n", "LR\n", "\n", "\n", "\n", "\n", "\n", "knn_0\n", "\n", "\n", "KNN\n", "\n", "\n", "\n", "\n", "\n", "pca_0\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "scaler_0\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_1\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "scaler_1\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_2\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_3\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_2->pca_3\n", "\n", "\n", "\n", "\n", "\n", "scaler_2\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "concat_0\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "pca_3->concat_0\n", "\n", "\n", "\n", "\n", "\n", "scaler_3\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_4\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "scaler_4\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_5\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_6\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_5->pca_6\n", "\n", "\n", "\n", "\n", "\n", "scaler_5\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "scaler_6\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_7\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "scaler_7\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_8\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "scaler_8\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_9\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "scaler_9\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_10\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_11\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_10->pca_11\n", "\n", "\n", "\n", "\n", "\n", "scaler_10\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "concat_1\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "pca_11->concat_1\n", "\n", "\n", "\n", "\n", "\n", "scaler_11\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_12\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "scaler_12\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_13\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_14\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_13->pca_14\n", "\n", "\n", "\n", "\n", "\n", "scaler_13\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "scaler_14\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_15\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "scaler_15\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_16\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "concat_1->pca_16\n", "\n", "\n", "\n", "\n", "\n", "scaler_16\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_17\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "concat_2\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "pca_17->concat_2\n", "\n", "\n", "\n", "\n", "\n", "scaler_17\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_18\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "scaler_18\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_19\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "concat_3\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "pca_19->concat_3\n", "\n", "\n", "\n", "\n", "\n", "scaler_19\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_20\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "concat_3->pca_20\n", "\n", "\n", "\n", "\n", "\n", "scaler_20\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_21\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_22\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_21->pca_22\n", "\n", "\n", "\n", "\n", "\n", "scaler_21\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "lr_1\n", "\n", "\n", "LR\n", "\n", "\n", "\n", "\n", "\n", "pca_22->lr_1\n", "\n", "\n", "\n", "\n", "\n", "scaler_22\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "knn_1\n", "\n", "\n", "KNN\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from sklearn.linear_model import LogisticRegression as LR\n", "from sklearn.neighbors import KNeighborsClassifier as KNN\n", "from sklearn.decomposition import PCA\n", "from sklearn.preprocessing import StandardScaler as Scaler\n", "from sklearn.ensemble import AdaBoostClassifier as Boost\n", "from lale.lib.lale import ConcatFeatures as Concat\n", "wrap_imported_operators()\n", "\n", "g = Grammar()\n", "\n", "g.start = g.estimator\n", "g.estimator = g.term_est | g.transformer >> g.term_est\n", "g.term_est = g.prim_est #| g.ensemble\n", "#g.ensemble = Boost ( base_estimator = LR )\n", "g.transformer = g.union_tfm | g.union_tfm >> g.transformer\n", "g.union_tfm = g.prim_tfm | g.union_body >> Concat\n", "g.union_body = g.transformer | g.transformer & g.union_body\n", "\n", "g.prim_est = LR | KNN\n", "g.prim_tfm = PCA | Scaler\n", "g.ensembler = Boost\n", "\n", "generated = g.unfold(7)\n", "generated.visualize()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100%|██████████| 3/3 [00:01<00:00, 1.58trial/s, best loss: -0.8889151103565365]\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "knn\n", "\n", "\n", "KNN\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "trainer = Hyperopt(estimator=generated, cv=2, max_evals=3)\n", "trained = trainer.fit(train_X, train_y)\n", "trained.get_pipeline().visualize()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "lr\n", "\n", "\n", "LR\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "(root)\n", "\n", "\n", "No-\n", "Op\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "knn\n", "\n", "\n", "KNN\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "lr\n", "\n", "\n", "LR\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "pca\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "scaler\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca->scaler\n", "\n", "\n", "\n", "\n", "\n", "lr\n", "\n", "\n", "LR\n", "\n", "\n", "\n", "\n", "\n", "scaler->lr\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pool = {g.sample(20) for _ in range(5)}\n", "for tree in pool:\n", " tree.visualize()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100%|██████████| 6/6 [00:02<00:00, 2.80trial/s, best loss: -0.9505575551782683]\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "lr\n", "\n", "\n", "LR\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "generated = make_choice(*pool)\n", "\n", "trainer = Hyperopt(estimator=generated, cv=2, max_evals=6)\n", "trained = trainer.fit(train_X, train_y)\n", "trained.get_pipeline().visualize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# recipe: grammar from this [paper](https://link.springer.com/chapter/10.1007/978-3-319-55696-3_16)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_0\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_1\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_1\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_2\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_2\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_3\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_3\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_4\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_4\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_5\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "\n", "gauss_nb_0\n", "\n", "\n", "Gauss-\n", "NB\n", "\n", "\n", "\n", "\n", "\n", "multin_nb_0\n", "\n", "\n", "Multin-\n", "NB\n", "\n", "\n", "\n", "\n", "\n", "bernou_nb_0\n", "\n", "\n", "Bernou-\n", "NB\n", "\n", "\n", "\n", "\n", "\n", "tree_0\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "forest_0\n", "\n", "\n", "Forest\n", "\n", "\n", "\n", "\n", "\n", "imputer\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "pca_0\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "imputer->pca_0\n", "\n", "\n", "\n", "\n", "\n", "poly_feat_0\n", "\n", "\n", "Poly-\n", "Feat\n", "\n", "\n", "\n", "\n", "\n", "pca_0->poly_feat_0\n", "\n", "\n", "\n", "\n", "\n", "feat_aggl_0\n", "\n", "\n", "Feat-\n", "Aggl\n", "\n", "\n", "\n", "\n", "\n", "select_k_best_0\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "gauss_nb_1\n", "\n", "\n", "Gauss-\n", "NB\n", "\n", "\n", "\n", "\n", "\n", "poly_feat_0->gauss_nb_1\n", "\n", "\n", "\n", "\n", "\n", "pca_1\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "feat_aggl_1\n", "\n", "\n", "Feat-\n", "Aggl\n", "\n", "\n", "\n", "\n", "\n", "select_k_best_1\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "poly_feat_1\n", "\n", "\n", "Poly-\n", "Feat\n", "\n", "\n", "\n", "\n", "\n", "pca_2\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "poly_feat_2\n", "\n", "\n", "Poly-\n", "Feat\n", "\n", "\n", "\n", "\n", "\n", "pca_2->poly_feat_2\n", "\n", "\n", "\n", "\n", "\n", "feat_aggl_2\n", "\n", "\n", "Feat-\n", "Aggl\n", "\n", "\n", "\n", "\n", "\n", "select_k_best_2\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "pca_3\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "feat_aggl_3\n", "\n", "\n", "Feat-\n", "Aggl\n", "\n", "\n", "\n", "\n", "\n", "select_k_best_3\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "poly_feat_3\n", "\n", "\n", "Poly-\n", "Feat\n", "\n", "\n", "\n", "\n", "\n", "multin_nb_1\n", "\n", "\n", "Multin-\n", "NB\n", "\n", "\n", "\n", "\n", "\n", "bernou_nb_1\n", "\n", "\n", "Bernou-\n", "NB\n", "\n", "\n", "\n", "\n", "\n", "tree_1\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "forest_1\n", "\n", "\n", "Forest\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from sklearn.decomposition import PCA\n", "from sklearn.cluster import FeatureAgglomeration as FeatAggl\n", "from sklearn.preprocessing import PolynomialFeatures as PolyFeat\n", "from sklearn.naive_bayes import GaussianNB as GaussNB\n", "from sklearn.naive_bayes import MultinomialNB as MultinNB\n", "from sklearn.naive_bayes import BernoulliNB as BernouNB\n", "from sklearn.impute import SimpleImputer as Imputer\n", "from sklearn.ensemble import RandomForestClassifier as Forest\n", "from sklearn.tree import DecisionTreeClassifier as Tree\n", "from sklearn.feature_selection import SelectKBest\n", "wrap_imported_operators()\n", "\n", "g = Grammar()\n", "\n", "g.start = g.algorithm | g.preprocessing >> g.algorithm\n", "g.preprocessing = g.imputation >> g.dimensionality_definition | g.dimensionality_definition\n", "g.dimensionality_definition = g.feature_selection >> g.feature_construction | g.feature_selection | g.feature_construction\n", "g.feature_selection = g.unsupervised | g.supervised \n", "g.algorithm = g.naive_bayes | g.trees\n", "\n", "g.imputation = Imputer\n", "g.supervised = SelectKBest\n", "g.unsupervised = PCA | FeatAggl\n", "g.feature_construction = PolyFeat\n", "g.naive_bayes = GaussNB | MultinNB | BernouNB\n", "g.trees = Tree | Forest\n", "\n", "\n", "generated = g.unfold(5)\n", "generated.visualize()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100%|██████████| 3/3 [00:02<00:00, 1.37trial/s, best loss: -0.9139575551782683]\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "gauss_nb\n", "\n", "\n", "Gauss-\n", "NB\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "trainer = Hyperopt(estimator=generated, cv=2, max_evals=3)\n", "trained = trainer.fit(train_X, train_y)\n", "trained.get_pipeline().visualize()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "pca\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "multin_nb\n", "\n", "\n", "Multin-\n", "NB\n", "\n", "\n", "\n", "\n", "\n", "pca->multin_nb\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "select_k_best\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "poly_feat\n", "\n", "\n", "Poly-\n", "Feat\n", "\n", "\n", "\n", "\n", "\n", "select_k_best->poly_feat\n", "\n", "\n", "\n", "\n", "\n", "tree\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "poly_feat->tree\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "pca\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "poly_feat\n", "\n", "\n", "Poly-\n", "Feat\n", "\n", "\n", "\n", "\n", "\n", "pca->poly_feat\n", "\n", "\n", "\n", "\n", "\n", "bernou_nb\n", "\n", "\n", "Bernou-\n", "NB\n", "\n", "\n", "\n", "\n", "\n", "poly_feat->bernou_nb\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "gauss_nb\n", "\n", "\n", "Gauss-\n", "NB\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "imputer\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "feat_aggl\n", "\n", "\n", "Feat-\n", "Aggl\n", "\n", "\n", "\n", "\n", "\n", "imputer->feat_aggl\n", "\n", "\n", "\n", "\n", "\n", "bernou_nb\n", "\n", "\n", "Bernou-\n", "NB\n", "\n", "\n", "\n", "\n", "\n", "feat_aggl->bernou_nb\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import sys\n", "pool = {g.sample(30) for _ in range(5)}\n", "for tree in pool:\n", " if tree is None:\n", " print('None', file=sys.stderr)\n", " else:\n", " tree.visualize()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100%|██████████| 6/6 [00:02<00:00, 2.24trial/s, best loss: -0.9139575551782683]\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "gauss_nb\n", "\n", "\n", "Gauss-\n", "NB\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "generated = make_choice(*pool)\n", "\n", "trainer = Hyperopt(estimator=generated, cv=2, max_evals=6)\n", "trained = trainer.fit(train_X, train_y)\n", "trained.get_pipeline().visualize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# alphad3m: Grammar from this [paper](https://www.automl.org/wp-content/uploads/2019/06/automlws2019_Paper34.pdf)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_0\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_1\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_1\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_2\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_2\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_3\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_3\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_4\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_5\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_4\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_6\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_7\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_5\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_8\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_9\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_10\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_6\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_11\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_7\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_12\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_13\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_8\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_14\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_15\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_9\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_16\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_17\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_10\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_18\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_19\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_20\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_11\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_21\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_12\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_22\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_23\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_13\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_24\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_25\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_14\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_26\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_27\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_15\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_28\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_29\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_30\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_16\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_31\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_32\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_17\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_33\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_34\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_18\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_35\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_36\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:pipeline_19\n", "\n", "\n", "\n", "\n", "\n", "\n", "cluster:choice_37\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_38\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "cluster:choice_39\n", "\n", "\n", "Choice\n", "\n", "\n", "\n", "\n", "\n", "gauss_nb_0\n", "\n", "\n", "Gauss-\n", "NB\n", "\n", "\n", "\n", "\n", "\n", "ridge_0\n", "\n", "\n", "Ridge\n", "\n", "\n", "\n", "\n", "\n", "linear_svc_0\n", "\n", "\n", "Linear-\n", "SVC\n", "\n", "\n", "\n", "\n", "\n", "sgd_classifier_0\n", "\n", "\n", "SGD-\n", "Classifier\n", "\n", "\n", "\n", "\n", "\n", "imputer_0\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "imputer_1\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "imputer_0->imputer_1\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_0\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_2\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "imputer_1->imputer_2\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_1\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_3\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "imputer_2->imputer_3\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_2\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_4\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "imputer_3->imputer_4\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_3\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "gauss_nb_1\n", "\n", "\n", "Gauss-\n", "NB\n", "\n", "\n", "\n", "\n", "\n", "imputer_4->gauss_nb_1\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_4\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_5\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_5\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_6\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_6\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_7\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_7\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_8\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_8\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "ridge_1\n", "\n", "\n", "Ridge\n", "\n", "\n", "\n", "\n", "\n", "linear_svc_1\n", "\n", "\n", "Linear-\n", "SVC\n", "\n", "\n", "\n", "\n", "\n", "sgd_classifier_1\n", "\n", "\n", "SGD-\n", "Classifier\n", "\n", "\n", "\n", "\n", "\n", "pca_0\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_1\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_0->pca_1\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_0\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_0\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "pca_2\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_1->pca_2\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_1\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_1\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "pca_3\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_2->pca_3\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_2\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_2\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "pca_4\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_3->pca_4\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_3\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_3\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "gauss_nb_2\n", "\n", "\n", "Gauss-\n", "NB\n", "\n", "\n", "\n", "\n", "\n", "pca_4->gauss_nb_2\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_4\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_4\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "pca_5\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_5\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_5\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "pca_6\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_6\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_6\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "pca_7\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_7\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_7\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "pca_8\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_8\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_8\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "ridge_2\n", "\n", "\n", "Ridge\n", "\n", "\n", "\n", "\n", "\n", "linear_svc_2\n", "\n", "\n", "Linear-\n", "SVC\n", "\n", "\n", "\n", "\n", "\n", "sgd_classifier_2\n", "\n", "\n", "SGD-\n", "Classifier\n", "\n", "\n", "\n", "\n", "\n", "imputer_9\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "imputer_10\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "imputer_9->imputer_10\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_9\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_11\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "imputer_10->imputer_11\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_10\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_12\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "imputer_11->imputer_12\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_11\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_13\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "imputer_12->imputer_13\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_12\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "pca_9\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "imputer_13->pca_9\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_13\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_14\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_14\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_15\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_15\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_16\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_16\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_17\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_17\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "pca_10\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_9->pca_10\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_9\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_9\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "pca_11\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_10->pca_11\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_10\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_10\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "pca_12\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_11->pca_12\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_11\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_11\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "pca_13\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_12->pca_13\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_12\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_12\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "gauss_nb_3\n", "\n", "\n", "Gauss-\n", "NB\n", "\n", "\n", "\n", "\n", "\n", "pca_13->gauss_nb_3\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_13\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_13\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "pca_14\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_14\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_14\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "pca_15\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_15\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_15\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "pca_16\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_16\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_16\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "pca_17\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder_17\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "one_hot_17\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "ridge_3\n", "\n", "\n", "Ridge\n", "\n", "\n", "\n", "\n", "\n", "linear_svc_3\n", "\n", "\n", "Linear-\n", "SVC\n", "\n", "\n", "\n", "\n", "\n", "sgd_classifier_3\n", "\n", "\n", "SGD-\n", "Classifier\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from sklearn.impute import SimpleImputer as Imputer\n", "from sklearn.impute import MissingIndicator\n", "from sklearn.decomposition import PCA\n", "from sklearn.naive_bayes import GaussianNB as GaussNB\n", "from sklearn.svm import LinearSVC\n", "from sklearn.linear_model import Ridge\n", "from sklearn.linear_model import SGDClassifier\n", "from sklearn.preprocessing import OneHotEncoder as OneHot\n", "from sklearn.preprocessing import OrdinalEncoder\n", "wrap_imported_operators()\n", "\n", "g = Grammar()\n", "\n", "g.start = g.est | g.clean >> g.est | g.tfm >> g.est | g.clean >> g.tfm >> g.est\n", "g.clean = g.clean1 >> g.clean | g.clean1\n", "g.tfm = g.tfm1 >> g.tfm | g.tfm1\n", "\n", "g.clean1 = Imputer | MissingIndicator\n", "g.tfm1 = PCA | OrdinalEncoder | OneHot(handle_unknown='ignore')\n", "g.est = GaussNB | Ridge | LinearSVC | SGDClassifier\n", "\n", "generated = g.unfold(6)\n", "generated.visualize()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100%|██████████| 3/3 [00:04<00:00, 1.66s/trial, best loss: -0.45581188455008487]\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "sgd_classifier\n", "\n", "\n", "SGD-\n", "Classifier\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "trainer = Hyperopt(estimator=generated, cv=2, max_evals=3)\n", "trained = trainer.fit(train_X, train_y)\n", "if trained.get_pipeline() is None:\n", " print('None', file=sys.stderr)\n", "else:\n", " trained.get_pipeline().visualize()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "imputer\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "pca_0\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "imputer->pca_0\n", "\n", "\n", "\n", "\n", "\n", "pca_1\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_0->pca_1\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "pca_1->ordinal_encoder\n", "\n", "\n", "\n", "\n", "\n", "ridge\n", "\n", "\n", "Ridge\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder->ridge\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "pca\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "pca->ordinal_encoder\n", "\n", "\n", "\n", "\n", "\n", "sgd_classifier\n", "\n", "\n", "SGD-\n", "Classifier\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder->sgd_classifier\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_0\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_1\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_0->missing_indicator_1\n", "\n", "\n", "\n", "\n", "\n", "ridge\n", "\n", "\n", "Ridge\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator_1->ridge\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "pca\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "ridge\n", "\n", "\n", "Ridge\n", "\n", "\n", "\n", "\n", "\n", "pca->ridge\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "pca_0\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_1\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_0->pca_1\n", "\n", "\n", "\n", "\n", "\n", "ridge\n", "\n", "\n", "Ridge\n", "\n", "\n", "\n", "\n", "\n", "pca_1->ridge\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "imputer_0\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator\n", "\n", "\n", "Missing-\n", "Indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_0->missing_indicator\n", "\n", "\n", "\n", "\n", "\n", "imputer_1\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "missing_indicator->imputer_1\n", "\n", "\n", "\n", "\n", "\n", "ridge\n", "\n", "\n", "Ridge\n", "\n", "\n", "\n", "\n", "\n", "imputer_1->ridge\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "imputer\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "linear_svc\n", "\n", "\n", "Linear-\n", "SVC\n", "\n", "\n", "\n", "\n", "\n", "imputer->linear_svc\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "sgd_classifier\n", "\n", "\n", "SGD-\n", "Classifier\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "imputer_0\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "imputer_1\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "imputer_0->imputer_1\n", "\n", "\n", "\n", "\n", "\n", "imputer_2\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "imputer_1->imputer_2\n", "\n", "\n", "\n", "\n", "\n", "imputer_3\n", "\n", "\n", "Imputer\n", "\n", "\n", "\n", "\n", "\n", "imputer_2->imputer_3\n", "\n", "\n", "\n", "\n", "\n", "one_hot\n", "\n", "\n", "One-\n", "Hot\n", "\n", "\n", "\n", "\n", "\n", "imputer_3->one_hot\n", "\n", "\n", "\n", "\n", "\n", "gauss_nb\n", "\n", "\n", "Gauss-\n", "NB\n", "\n", "\n", "\n", "\n", "\n", "one_hot->gauss_nb\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "pca\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder\n", "\n", "\n", "Ordinal-\n", "Encoder\n", "\n", "\n", "\n", "\n", "\n", "pca->ordinal_encoder\n", "\n", "\n", "\n", "\n", "\n", "ridge\n", "\n", "\n", "Ridge\n", "\n", "\n", "\n", "\n", "\n", "ordinal_encoder->ridge\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pool = {g.sample(10) for _ in range(10)}\n", "for tree in pool:\n", " tree.visualize()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100%|██████████| 10/10 [00:09<00:00, 1.04trial/s, best loss: -0.9045959105402828]\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "pca\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "ridge\n", "\n", "\n", "Ridge\n", "\n", "\n", "\n", "\n", "\n", "pca->ridge\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "generated = make_choice(*pool)\n", "\n", "trainer = Hyperopt(estimator=generated, cv=2, max_evals=10)\n", "trained = trainer.fit(train_X, train_y)\n", "trained.get_pipeline().visualize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# TPot: Grammar inferred from this [paper](https://dl.acm.org/doi/pdf/10.1145/2908812.2908918)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "# from sklearn.preprocessing import StandardScaler as SScaler\n", "# from sklearn.preprocessing import RobustScaler as RScaler\n", "# from sklearn.preprocessing import PolynomialFeatures as PolyFeat\n", "# from sklearn.decomposition import PCA\n", "# from sklearn.feature_selection import SelectKBest\n", "# from sklearn.feature_selection import RFE\n", "# from sklearn.feature_selection import SelectPercentile\n", "# from sklearn.feature_selection import VarianceThreshold\n", "# from sklearn.tree import DecisionTreeClassifier as Tree\n", "# from sklearn.ensemble import RandomForestClassifier as Forest\n", "# from sklearn.ensemble import GradientBoostingClassifier as Gradient\n", "# from sklearn.svm import SVC as SVM\n", "# from sklearn.linear_model import LogisticRegression as LR\n", "# from sklearn.neighbors import KNeighborsClassifier as KNN\n", "# from lale.lib.lale import ConcatFeatures as Concat\n", "\n", "from sklearn.preprocessing import StandardScaler as PreProcess\n", "from sklearn.decomposition import PCA as Decomposition\n", "from sklearn.feature_selection import SelectKBest as Feature\n", "from sklearn.tree import DecisionTreeClassifier as Model\n", "from lale.lib.lale import ConcatFeatures as Concat\n", "from lale.lib.lale import NoOp\n", "\n", "wrap_imported_operators()\n", "\n", "g = Grammar()\n", "\n", "g.start = g.tree >> g.model\n", "g.tree = g.node | g.tree >> g.node | (g.tree & g.tree) >> Concat\n", "g.node = g.preprocessing | g.decomposition | g.feature_selection | g.model \\\n", "# | (g.node & g.node) >> Concat \\\n", "# | g.node >> g.node\n", "\n", "\n", "g.preprocessing = PreProcess\n", "g.decomposition = Decomposition\n", "g.feature_selection = Feature\n", "g.model = Model\n", "\n", "# g.preprocessing = SScaler | RScaler | PolyFeat\n", "# g.decomposition = PCA\n", "# g.feature_selection = SelectKBest | RFE | SelectPercentile | VarianceThreshold\n", "# g.model = Tree | Forest | Gradient | SVM | LR | KNN\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "tree_0\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "scaler_0\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "tree_0->scaler_0\n", "\n", "\n", "\n", "\n", "\n", "concat_0\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "scaler_0->concat_0\n", "\n", "\n", "\n", "\n", "\n", "select_k_best_0\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "pca_0\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "select_k_best_0->pca_0\n", "\n", "\n", "\n", "\n", "\n", "pca_0->concat_0\n", "\n", "\n", "\n", "\n", "\n", "scaler_1\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "concat_0->scaler_1\n", "\n", "\n", "\n", "\n", "\n", "scaler_2\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "scaler_1->scaler_2\n", "\n", "\n", "\n", "\n", "\n", "pca_1\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "scaler_2->pca_1\n", "\n", "\n", "\n", "\n", "\n", "concat_9\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "pca_1->concat_9\n", "\n", "\n", "\n", "\n", "\n", "tree_1\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "concat_1\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "tree_1->concat_1\n", "\n", "\n", "\n", "\n", "\n", "scaler_3\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "scaler_4\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "scaler_3->scaler_4\n", "\n", "\n", "\n", "\n", "\n", "scaler_4->concat_1\n", "\n", "\n", "\n", "\n", "\n", "tree_2\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "concat_1->tree_2\n", "\n", "\n", "\n", "\n", "\n", "concat_2\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "tree_2->concat_2\n", "\n", "\n", "\n", "\n", "\n", "select_k_best_1\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "select_k_best_1->concat_2\n", "\n", "\n", "\n", "\n", "\n", "concat_8\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "concat_2->concat_8\n", "\n", "\n", "\n", "\n", "\n", "tree_3\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "concat_7\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "tree_3->concat_7\n", "\n", "\n", "\n", "\n", "\n", "scaler_5\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "scaler_6\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "scaler_5->scaler_6\n", "\n", "\n", "\n", "\n", "\n", "concat_5\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "scaler_6->concat_5\n", "\n", "\n", "\n", "\n", "\n", "select_k_best_2\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "scaler_7\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "select_k_best_2->scaler_7\n", "\n", "\n", "\n", "\n", "\n", "concat_4\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "scaler_7->concat_4\n", "\n", "\n", "\n", "\n", "\n", "tree_4\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "concat_3\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "tree_4->concat_3\n", "\n", "\n", "\n", "\n", "\n", "pca_2\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_2->concat_3\n", "\n", "\n", "\n", "\n", "\n", "select_k_best_3\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "concat_3->select_k_best_3\n", "\n", "\n", "\n", "\n", "\n", "select_k_best_3->concat_4\n", "\n", "\n", "\n", "\n", "\n", "concat_4->concat_5\n", "\n", "\n", "\n", "\n", "\n", "concat_6\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "concat_5->concat_6\n", "\n", "\n", "\n", "\n", "\n", "select_k_best_4\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "select_k_best_4->concat_6\n", "\n", "\n", "\n", "\n", "\n", "concat_6->concat_7\n", "\n", "\n", "\n", "\n", "\n", "pca_3\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "concat_7->pca_3\n", "\n", "\n", "\n", "\n", "\n", "pca_3->concat_8\n", "\n", "\n", "\n", "\n", "\n", "concat_8->concat_9\n", "\n", "\n", "\n", "\n", "\n", "tree_5\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "concat_9->tree_5\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "scaler\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "tree\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "scaler->tree\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "select_k_best\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "concat\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "select_k_best->concat\n", "\n", "\n", "\n", "\n", "\n", "pca\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "scaler\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca->scaler\n", "\n", "\n", "\n", "\n", "\n", "scaler->concat\n", "\n", "\n", "\n", "\n", "\n", "tree\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "concat->tree\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "\n", "(root)\n", "\n", "\n", "No-\n", "Op\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "pca\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "select_k_best\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "pca->select_k_best\n", "\n", "\n", "\n", "\n", "\n", "tree\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "select_k_best->tree\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "select_k_best\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "tree\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "select_k_best->tree\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "select_k_best\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "pca_0\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "select_k_best->pca_0\n", "\n", "\n", "\n", "\n", "\n", "pca_1\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_0->pca_1\n", "\n", "\n", "\n", "\n", "\n", "scaler\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_1->scaler\n", "\n", "\n", "\n", "\n", "\n", "tree\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "scaler->tree\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "tree_0\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "scaler_0\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "tree_0->scaler_0\n", "\n", "\n", "\n", "\n", "\n", "concat_0\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "scaler_0->concat_0\n", "\n", "\n", "\n", "\n", "\n", "scaler_1\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "scaler_1->concat_0\n", "\n", "\n", "\n", "\n", "\n", "concat_3\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "concat_0->concat_3\n", "\n", "\n", "\n", "\n", "\n", "pca_0\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "concat_1\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "pca_0->concat_1\n", "\n", "\n", "\n", "\n", "\n", "pca_1\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "select_k_best\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "pca_1->select_k_best\n", "\n", "\n", "\n", "\n", "\n", "select_k_best->concat_1\n", "\n", "\n", "\n", "\n", "\n", "concat_2\n", "\n", "\n", "Concat\n", "\n", "\n", "\n", "\n", "\n", "concat_1->concat_2\n", "\n", "\n", "\n", "\n", "\n", "pca_2\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_2->concat_2\n", "\n", "\n", "\n", "\n", "\n", "scaler_2\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "concat_2->scaler_2\n", "\n", "\n", "\n", "\n", "\n", "pca_3\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "scaler_2->pca_3\n", "\n", "\n", "\n", "\n", "\n", "pca_3->concat_3\n", "\n", "\n", "\n", "\n", "\n", "tree_1\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "concat_3->tree_1\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pool = {g.sample(15) for _ in range(10)}\n", "for tree in pool:\n", " tree.visualize()" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100%|██████████| 6/6 [00:10<00:00, 1.82s/trial, best loss: 1.4095161290322582]\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "cluster:(root)\n", "\n", "\n", "\n", "\n", "\n", "\n", "select_k_best\n", "\n", "\n", "Select-\n", "K-\n", "Best\n", "\n", "\n", "\n", "\n", "\n", "pca_0\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "select_k_best->pca_0\n", "\n", "\n", "\n", "\n", "\n", "pca_1\n", "\n", "\n", "PCA\n", "\n", "\n", "\n", "\n", "\n", "pca_0->pca_1\n", "\n", "\n", "\n", "\n", "\n", "scaler\n", "\n", "\n", "Scaler\n", "\n", "\n", "\n", "\n", "\n", "pca_1->scaler\n", "\n", "\n", "\n", "\n", "\n", "tree\n", "\n", "\n", "Tree\n", "\n", "\n", "\n", "\n", "\n", "scaler->tree\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "generated = make_choice(*pool)\n", "\n", "trainer = Hyperopt(estimator=generated, cv=2, max_evals=6)\n", "trained = trainer.fit(train_X, train_y)\n", "trained.get_pipeline().visualize()" ] } ], "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.7.9" } }, "nbformat": 4, "nbformat_minor": 2 }