{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "XhthOTRd1VMP" }, "source": [ "# Install Zoofs" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ucgdfcwHx9sm", "outputId": "654ad478-779d-4eae-bace-ad77a8dfbe43" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: zoofs in /usr/local/lib/python3.7/dist-packages (0.1.3)\n", "Requirement already satisfied: plotly in /usr/local/lib/python3.7/dist-packages (from zoofs) (4.4.1)\n", "Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from zoofs) (1.4.1)\n", "Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from zoofs) (1.1.5)\n", "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from zoofs) (1.19.5)\n", "Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas->zoofs) (2.8.2)\n", "Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas->zoofs) (2018.9)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7.3->pandas->zoofs) (1.15.0)\n", "Requirement already satisfied: retrying>=1.3.3 in /usr/local/lib/python3.7/dist-packages (from plotly->zoofs) (1.3.3)\n" ] } ], "source": [ "!pip install zoofs" ] }, { "cell_type": "markdown", "metadata": { "id": "K6VUxBdu1bsY" }, "source": [ "# Prepare data" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "id": "MpkGAS7ryJcM" }, "outputs": [], "source": [ "from sklearn.datasets import load_breast_cancer\n", "import pandas as pd\n", "data = load_breast_cancer()" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "id": "TvmrCKncyceR" }, "outputs": [], "source": [ "X_train=pd.DataFrame(data['data'],columns=data['feature_names'])\n", "y_train=pd.Series(data['target'])" ] }, { "cell_type": "markdown", "metadata": { "id": "BB7ECvCZ1koj" }, "source": [ "# Load an algo" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "id": "QD6fAoHg1au9" }, "outputs": [], "source": [ "from zoofs import HarrisHawkOptimization" ] }, { "cell_type": "markdown", "metadata": { "id": "r8CuTiNu1n_Y" }, "source": [ "# Prepare an objective function, and run the algo" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "k5Hcf_7Ox_vL", "outputId": "36a176ab-c76e-4200-b9be-bea54b76ec10" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\t\t Best value of metric across iteration \t Best value of metric across population \n", "Iteration 0 \t 0.000834507195418593 \t\t\t\t\t 0.000834507195418593 \n", "Iteration 1 \t 0.0006783272710830701 \t\t\t\t\t 0.0006783272710830701 \n", "Iteration 2 \t 0.0006783272710830701 \t\t\t\t\t 0.0006783272710830701 \n", "Iteration 3 \t 0.0007444757214734708 \t\t\t\t\t 0.0006783272710830701 \n", "Iteration 4 \t 0.000698228318389392 \t\t\t\t\t 0.0006783272710830701 \n", "Iteration 5 \t 0.0007875445320993873 \t\t\t\t\t 0.0006783272710830701 \n", "Iteration 6 \t 0.0006751479047728708 \t\t\t\t\t 0.0006751479047728708 \n", "Iteration 7 \t 0.0006751479047728708 \t\t\t\t\t 0.0006751479047728708 \n", "Iteration 8 \t 0.0006933743957114641 \t\t\t\t\t 0.0006751479047728708 \n", "Iteration 9 \t 0.0006933743957114641 \t\t\t\t\t 0.0006751479047728708 \n", "Iteration 10 \t 0.000685477824521282 \t\t\t\t\t 0.0006751479047728708 \n", "Iteration 11 \t 0.0006563435418546247 \t\t\t\t\t 0.0006563435418546247 \n", "Iteration 12 \t 0.0006933743957114641 \t\t\t\t\t 0.0006563435418546247 \n", "Iteration 13 \t 0.0006933743957114641 \t\t\t\t\t 0.0006563435418546247 \n", "Iteration 14 \t 0.0006965898526582949 \t\t\t\t\t 0.0006563435418546247 \n", "Iteration 15 \t 0.0006261969431135148 \t\t\t\t\t 0.0006261969431135148 \n", "Iteration 16 \t 0.0006261969431135148 \t\t\t\t\t 0.0006261969431135148 \n", "Iteration 17 \t 0.0006261969431135148 \t\t\t\t\t 0.0006261969431135148 \n", "Iteration 18 \t 0.0006261969431135148 \t\t\t\t\t 0.0006261969431135148 \n", "Iteration 19 \t 0.0006308051888594318 \t\t\t\t\t 0.0006261969431135148 \n" ] }, { "data": { "text/plain": [ "['mean radius',\n", " 'mean texture',\n", " 'mean perimeter',\n", " 'mean area',\n", " 'mean smoothness',\n", " 'mean concavity',\n", " 'mean concave points',\n", " 'mean symmetry',\n", " 'mean fractal dimension',\n", " 'radius error',\n", " 'area error',\n", " 'smoothness error',\n", " 'symmetry error',\n", " 'worst radius',\n", " 'worst perimeter',\n", " 'worst area',\n", " 'worst smoothness',\n", " 'worst compactness',\n", " 'worst concave points',\n", " 'worst symmetry']" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.metrics import log_loss\n", "# define your own objective function, make sure the function receives four parameters,\n", "# fit your model and return the objective value ! \n", "def objective_function_topass(model,X_train, y_train, X_valid, y_valid): \n", " model.fit(X_train,y_train) \n", " P=log_loss(y_valid,model.predict_proba(X_valid))\n", " return P\n", " \n", "# import an algorithm ! \n", "from zoofs import HarrisHawkOptimization\n", "# create object of algorithm\n", "algo_object=HarrisHawkOptimization(objective_function_topass,n_iteration=20,\n", " population_size=20,minimize=True)\n", "import lightgbm as lgb\n", "lgb_model = lgb.LGBMClassifier() \n", "# fit the algorithm\n", "algo_object.fit(lgb_model,X_train, y_train, X_train, y_train,verbose=True)\n", "#plot your results" ] }, { "cell_type": "markdown", "metadata": { "id": "6z5R4X111w2m" }, "source": [ "# Plot objective history plot" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 542 }, "id": "uZjsGBWwzOMs", "outputId": "df171698-b2bd-44b0-8480-9424e13b091e" }, "outputs": [ { "data": { "text/html": [ "<html>\n", "<head><meta charset=\"utf-8\" /></head>\n", "<body>\n", " <div>\n", " <script src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_SVG\"></script><script type=\"text/javascript\">if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script>\n", " <script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script>\n", " <script src=\"https://cdn.plot.ly/plotly-latest.min.js\"></script> \n", " <div id=\"2bf88d57-1600-4cd0-87cd-179e32437b03\" class=\"plotly-graph-div\" style=\"height:525px; width:100%;\"></div>\n", " <script type=\"text/javascript\">\n", " \n", " window.PLOTLYENV=window.PLOTLYENV || {};\n", " \n", " if (document.getElementById(\"2bf88d57-1600-4cd0-87cd-179e32437b03\")) {\n", " Plotly.newPlot(\n", " '2bf88d57-1600-4cd0-87cd-179e32437b03',\n", " [{\"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.000834507195418593, 0.0006783272710830701, 0.0006783272710830701, 0.0007444757214734708, 0.000698228318389392, 0.0007875445320993873, 0.0006751479047728708, 0.0006751479047728708, 0.0006933743957114641, 0.0006933743957114641, 0.000685477824521282, 0.0006563435418546247, 0.0006933743957114641, 0.0006933743957114641, 0.0006965898526582949, 0.0006261969431135148, 0.0006261969431135148, 0.0006261969431135148, 0.0006261969431135148, 0.0006308051888594318]}, {\"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.000834507195418593, 0.0006783272710830701, 0.0006783272710830701, 0.0006783272710830701, 0.0006783272710830701, 0.0006783272710830701, 0.0006751479047728708, 0.0006751479047728708, 0.0006751479047728708, 0.0006751479047728708, 0.0006751479047728708, 0.0006563435418546247, 0.0006563435418546247, 0.0006563435418546247, 0.0006563435418546247, 0.0006261969431135148, 0.0006261969431135148, 0.0006261969431135148, 0.0006261969431135148, 0.0006261969431135148]}],\n", " {\"template\": {\"data\": {\"bar\": [{\"error_x\": {\"color\": \"#2a3f5f\"}, \"error_y\": {\"color\": \"#2a3f5f\"}, \"marker\": {\"line\": {\"color\": \"#E5ECF6\", \"width\": 0.5}}, \"type\": \"bar\"}], \"barpolar\": [{\"marker\": {\"line\": {\"color\": \"#E5ECF6\", \"width\": 0.5}}, \"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.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.0, \"#f0f921\"]], \"type\": \"contour\"}], \"contourcarpet\": [{\"colorbar\": {\"outlinewidth\": 0, \"ticks\": \"\"}, \"type\": \"contourcarpet\"}], \"heatmap\": [{\"colorbar\": {\"outlinewidth\": 0, \"ticks\": \"\"}, \"colorscale\": [[0.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.0, \"#f0f921\"]], \"type\": \"heatmap\"}], \"heatmapgl\": [{\"colorbar\": {\"outlinewidth\": 0, \"ticks\": \"\"}, \"colorscale\": [[0.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.0, \"#f0f921\"]], \"type\": \"heatmapgl\"}], \"histogram\": [{\"marker\": {\"colorbar\": {\"outlinewidth\": 0, \"ticks\": \"\"}}, \"type\": \"histogram\"}], \"histogram2d\": [{\"colorbar\": {\"outlinewidth\": 0, \"ticks\": \"\"}, \"colorscale\": [[0.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.0, \"#f0f921\"]], \"type\": \"histogram2d\"}], \"histogram2dcontour\": [{\"colorbar\": {\"outlinewidth\": 0, \"ticks\": \"\"}, \"colorscale\": [[0.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.0, \"#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.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.0, \"#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}, \"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.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.0, \"#f0f921\"]], \"sequentialminus\": [[0.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.0, \"#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\"}}},\n", " {\"responsive\": true}\n", " ).then(function(){\n", " \n", "var gd = document.getElementById('2bf88d57-1600-4cd0-87cd-179e32437b03');\n", "var x = new MutationObserver(function (mutations, observer) {{\n", " var display = window.getComputedStyle(gd).display;\n", " if (!display || display === 'none') {{\n", " console.log([gd, 'removed!']);\n", " Plotly.purge(gd);\n", " observer.disconnect();\n", " }}\n", "}});\n", "\n", "// Listen for the removal of the full notebook cells\n", "var notebookContainer = gd.closest('#notebook-container');\n", "if (notebookContainer) {{\n", " x.observe(notebookContainer, {childList: true});\n", "}}\n", "\n", "// Listen for the clearing of the current output cell\n", "var outputEl = gd.closest('.output');\n", "if (outputEl) {{\n", " x.observe(outputEl, {childList: true});\n", "}}\n", "\n", " })\n", " };\n", " \n", " </script>\n", " </div>\n", "</body>\n", "</html>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "algo_object.plot_history()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "ldvKM9Gn1wYl" }, "outputs": [], "source": [] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "HarrisHawkOptimization.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 0 }