{ "nbformat": 4, "nbformat_minor": 2, "metadata": { "colab": { "name": "Genetic_optimization_example.ipynb", "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3.9.5 64-bit" }, "language_info": { "name": "python", "version": "3.9.5", "mimetype": "text/x-python", "codemirror_mode": { "name": "ipython", "version": 3 }, "pygments_lexer": "ipython3", "nbconvert_exporter": "python", "file_extension": ".py" }, "interpreter": { "hash": "2647ea34e536f865ab67ff9ddee7fd78773d956cec0cab53c79b32cd10da5d83" } }, "cells": [ { "cell_type": "markdown", "source": [ "# Genetic Optimization" ], "metadata": { "id": "qMsrI4WTBcNc" } }, { "cell_type": "markdown", "source": [ "# Installation" ], "metadata": { "id": "rPFycvTBSlFZ" } }, { "cell_type": "code", "execution_count": 1, "source": [ "!pip install zoofs" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Requirement already satisfied: zoofs in c:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (0.1.2)\n", "Requirement already satisfied: scipy in c:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from zoofs) (1.7.1)\n", "Requirement already satisfied: numpy in c:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from zoofs) (1.21.2)\n", "Requirement already satisfied: plotly in c:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from zoofs) (5.3.1)\n", "Requirement already satisfied: pandas in c:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from zoofs) (1.3.2)\n", "Requirement already satisfied: pytz>=2017.3 in c:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from pandas->zoofs) (2021.1)\n", "Requirement already satisfied: python-dateutil>=2.7.3 in c:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from pandas->zoofs) (2.8.2)\n", "Requirement already satisfied: six>=1.5 in c:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from python-dateutil>=2.7.3->pandas->zoofs) (1.16.0)\n", "Requirement already satisfied: tenacity>=6.2.0 in c:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from plotly->zoofs) (8.0.1)\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "WARNING: You are using pip version 21.1.1; however, version 21.2.4 is available.\n", "You should consider upgrading via the 'c:\\users\\user\\appdata\\local\\programs\\python\\python39\\python.exe -m pip install --upgrade pip' command.\n" ] } ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "m11hMUpV8eQQ", "outputId": "4b2a6167-16b8-42f8-b064-e5a0b870e62c" } }, { "cell_type": "markdown", "source": [ "# Load Breast cancer dataset " ], "metadata": { "id": "eIPOmPR2SoFp" } }, { "cell_type": "code", "execution_count": 4, "source": [ "from sklearn.datasets import load_breast_cancer\r\n", "import pandas as pd\r\n", "data = load_breast_cancer()\r\n", "X_train=pd.DataFrame(data['data'],columns=data['feature_names'])\r\n", "y_train=pd.Series(data['target'])\r\n" ], "outputs": [], "metadata": { "id": "50Psmo778jrp" } }, { "cell_type": "markdown", "source": [ "# Importing Zoofs Algo" ], "metadata": { "id": "xbldfYoYJAIv" } }, { "cell_type": "code", "execution_count": 5, "source": [ "from zoofs import GeneticOptimization" ], "outputs": [], "metadata": { "id": "GB3sjzhYSs5P" } }, { "cell_type": "markdown", "source": [ "# Setting up Objective function and fitting algo" ], "metadata": { "id": "M-pTOBAsJDaw" } }, { "cell_type": "code", "execution_count": 6, "source": [ "from sklearn.metrics import log_loss\r\n", "# define your own objective function, make sure the function receives four parameters,\r\n", "# fit your model and return the objective value ! \r\n", "def objective_function_topass(model,X_train, y_train, X_valid, y_valid): \r\n", " model.fit(X_train,y_train) \r\n", " P=log_loss(y_valid,model.predict_proba(X_valid))\r\n", " return P\r\n", " \r\n", "# create object of algorithm\r\n", "algo_object=GeneticOptimization(objective_function_topass,n_iteration=20,\r\n", " population_size=20,selective_pressure=2,elitism=4,\r\n", " mutation_rate=0.1,minimize=True)\r\n", "import lightgbm as lgb\r\n", "lgb_model = lgb.LGBMClassifier() \r\n", "# fit the algorithm\r\n", "algo_object.fit(lgb_model,X_train, y_train, X_train, y_train,verbose=True)\r\n", "#plot your results" ], "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\t\t Best value of metric across iteration \t Best value of metric across population \n", "Iteration 0 \t 0.0005260710188630122 \t\t\t\t\t 0.0005260710188630122 \n", "Iteration 1 \t 0.0005260710188630122 \t\t\t\t\t 0.0005260710188630122 \n", "Iteration 2 \t 0.000510693921051442 \t\t\t\t\t 0.000510693921051442 \n", "Iteration 3 \t 0.000510693921051442 \t\t\t\t\t 0.000510693921051442 \n", "Iteration 4 \t 0.000505133084737278 \t\t\t\t\t 0.000505133084737278 \n", "Iteration 5 \t 0.000505133084737278 \t\t\t\t\t 0.000505133084737278 \n", "Iteration 6 \t 0.0004841698982551718 \t\t\t\t\t 0.0004841698982551718 \n", "Iteration 7 \t 0.0004841698982551718 \t\t\t\t\t 0.0004841698982551718 \n", "Iteration 8 \t 0.0004841698982551718 \t\t\t\t\t 0.0004841698982551718 \n", "Iteration 9 \t 0.0004841698982551718 \t\t\t\t\t 0.0004841698982551718 \n", "Iteration 10 \t 0.0004841698982551718 \t\t\t\t\t 0.0004841698982551718 \n", "Iteration 11 \t 0.0004841698982551718 \t\t\t\t\t 0.0004841698982551718 \n", "Iteration 12 \t 0.0004841698982551718 \t\t\t\t\t 0.0004841698982551718 \n", "Iteration 13 \t 0.0004841698982551718 \t\t\t\t\t 0.0004841698982551718 \n", "Iteration 14 \t 0.0004724901340559412 \t\t\t\t\t 0.0004724901340559412 \n", "Iteration 15 \t 0.0004724901340559412 \t\t\t\t\t 0.0004724901340559412 \n", "Iteration 16 \t 0.0004841698982551718 \t\t\t\t\t 0.0004724901340559412 \n", "Iteration 17 \t 0.0004841698982551718 \t\t\t\t\t 0.0004724901340559412 \n", "Iteration 18 \t 0.0004841698982551718 \t\t\t\t\t 0.0004724901340559412 \n", "Iteration 19 \t 0.0004841698982551718 \t\t\t\t\t 0.0004724901340559412 \n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "['mean radius',\n", " 'mean texture',\n", " 'mean perimeter',\n", " 'mean area',\n", " 'mean smoothness',\n", " 'mean compactness',\n", " 'mean concavity',\n", " 'mean concave points',\n", " 'mean symmetry',\n", " 'mean fractal dimension',\n", " 'radius error',\n", " 'texture error',\n", " 'perimeter error',\n", " 'area error',\n", " 'smoothness error',\n", " 'compactness error',\n", " 'concavity error',\n", " 'concave points error',\n", " 'symmetry error',\n", " 'fractal dimension error',\n", " 'worst radius',\n", " 'worst texture',\n", " 'worst perimeter',\n", " 'worst area',\n", " 'worst smoothness',\n", " 'worst compactness',\n", " 'worst concavity',\n", " 'worst concave points',\n", " 'worst symmetry',\n", " 'worst fractal dimension']" ] }, "metadata": {}, "execution_count": 6 } ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RBewGAguHn10", "outputId": "bb15ff2a-5de1-43b1-b4d5-4d310f1b4c08" } }, { "cell_type": "markdown", "source": [ "# Plotting the Results" ], "metadata": { "id": "2wIhkfa4Jf_e" } }, { "cell_type": "code", "execution_count": 7, "source": [ "algo_object.plot_history()" ], "outputs": [ { "output_type": "display_data", "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "mode": "markers", "name": "objective_score", "type": "scatter", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 ], "y": [ 0.0005260710188630122, 0.0005260710188630122, 0.000510693921051442, 0.000510693921051442, 0.000505133084737278, 0.000505133084737278, 0.0004841698982551718, 0.0004841698982551718, 0.0004841698982551718, 0.0004841698982551718, 0.0004841698982551718, 0.0004841698982551718, 0.0004841698982551718, 0.0004841698982551718, 0.0004724901340559412, 0.0004724901340559412, 0.0004841698982551718, 0.0004841698982551718, 0.0004841698982551718, 0.0004841698982551718 ] }, { "mode": "lines+markers", "name": "best_score", "type": "scatter", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 ], "y": [ 0.0005260710188630122, 0.0005260710188630122, 0.000510693921051442, 0.000510693921051442, 0.000505133084737278, 0.000505133084737278, 0.0004841698982551718, 0.0004841698982551718, 0.0004841698982551718, 0.0004841698982551718, 0.0004841698982551718, 0.0004841698982551718, 0.0004841698982551718, 0.0004841698982551718, 0.0004724901340559412, 0.0004724901340559412, 0.0004724901340559412, 0.0004724901340559412, 0.0004724901340559412, 0.0004724901340559412 ] } ], "layout": { "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "title": { "text": "Optimization History Plot" }, "xaxis": { "title": { "text": "Iteration" } }, "yaxis": { "title": { "text": "objective_score" } } } } }, "metadata": {} } ], "metadata": { "id": "ANgs6iqsHqQk", "colab": { "base_uri": "https://localhost:8080/", "height": 542 }, "outputId": "91c66d72-e1f6-44bd-a2c1-52bcc62468a2" } } ] }