{ "nbformat": 4, "nbformat_minor": 2, "metadata": { "colab": { "name": "Particle_swarm_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": [ "# Particle Swarm Optimization" ], "metadata": {} }, { "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: 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: numpy in c:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from zoofs) (1.21.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: 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": "ffeda484-d453-4052-8ba7-b06ea557899b" } }, { "cell_type": "markdown", "source": [ "# Load Breast cancer dataset " ], "metadata": { "id": "eIPOmPR2SoFp" } }, { "cell_type": "code", "execution_count": 2, "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": 3, "source": [ "from zoofs import ParticleSwarmOptimization" ], "outputs": [], "metadata": { "id": "GB3sjzhYSs5P" } }, { "cell_type": "markdown", "source": [ "# Setting up Objective function and fitting algo" ], "metadata": { "id": "M-pTOBAsJDaw" } }, { "cell_type": "code", "execution_count": 4, "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=ParticleSwarmOptimization(objective_function_topass,n_iteration=25,\r\n", " population_size=20,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.0005296967375056357 \t\t\t\t\t 0.0005296967375056357 \n", "Iteration 1 \t 0.0005350875802894996 \t\t\t\t\t 0.0005296967375056357 \n", "Iteration 2 \t 0.0004900565872079092 \t\t\t\t\t 0.0004900565872079092 \n", "Iteration 3 \t 0.00048332067099894437 \t\t\t\t\t 0.00048332067099894437 \n", "Iteration 4 \t 0.00045637897622823905 \t\t\t\t\t 0.00045637897622823905 \n", "Iteration 5 \t 0.000466775244080281 \t\t\t\t\t 0.00045637897622823905 \n", "Iteration 6 \t 0.0004918998489345662 \t\t\t\t\t 0.00045637897622823905 \n", "Iteration 7 \t 0.0004921178797640744 \t\t\t\t\t 0.00045637897622823905 \n", "Iteration 8 \t 0.00042340845980268525 \t\t\t\t\t 0.00042340845980268525 \n", "Iteration 9 \t 0.00042474680152597216 \t\t\t\t\t 0.00042340845980268525 \n", "Iteration 10 \t 0.0003983627188019335 \t\t\t\t\t 0.0003983627188019335 \n", "Iteration 11 \t 0.0004249661758924971 \t\t\t\t\t 0.0003983627188019335 \n", "Iteration 12 \t 0.0004264664684658149 \t\t\t\t\t 0.0003983627188019335 \n", "Iteration 13 \t 0.00046151713507627454 \t\t\t\t\t 0.0003983627188019335 \n", "Iteration 14 \t 0.0004123962375142004 \t\t\t\t\t 0.0003983627188019335 \n", "Iteration 15 \t 0.00043259241757591187 \t\t\t\t\t 0.0003983627188019335 \n", "Iteration 16 \t 0.00039191564861833434 \t\t\t\t\t 0.00039191564861833434 \n", "Iteration 17 \t 0.0004517608567341095 \t\t\t\t\t 0.00039191564861833434 \n", "Iteration 18 \t 0.00040691262960006647 \t\t\t\t\t 0.00039191564861833434 \n", "Iteration 19 \t 0.0004248262025278297 \t\t\t\t\t 0.00039191564861833434 \n", "Iteration 20 \t 0.0004102547126767602 \t\t\t\t\t 0.00039191564861833434 \n", "Iteration 21 \t 0.00041051318455042024 \t\t\t\t\t 0.00039191564861833434 \n", "Iteration 22 \t 0.00037769760489395467 \t\t\t\t\t 0.00037769760489395467 \n", "Iteration 23 \t 0.0004249105077075491 \t\t\t\t\t 0.00037769760489395467 \n", "Iteration 24 \t 0.000432562624556134 \t\t\t\t\t 0.00037769760489395467 \n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "['mean perimeter',\n", " 'mean area',\n", " 'mean smoothness',\n", " 'mean concavity',\n", " 'mean concave points',\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 smoothness',\n", " 'worst compactness',\n", " 'worst symmetry',\n", " 'worst fractal dimension']" ] }, "metadata": {}, "execution_count": 4 } ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RBewGAguHn10", "outputId": "9f17f017-f77c-4cfe-8e38-03e563a61884" } }, { "cell_type": "markdown", "source": [ "# Plotting the Results" ], "metadata": { "id": "2wIhkfa4Jf_e" } }, { "cell_type": "code", "execution_count": 5, "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, 20, 21, 22, 23, 24 ], "y": [ 0.0005296967375056357, 0.0005350875802894996, 0.0004900565872079092, 0.00048332067099894437, 0.00045637897622823905, 0.000466775244080281, 0.0004918998489345662, 0.0004921178797640744, 0.00042340845980268525, 0.00042474680152597216, 0.0003983627188019335, 0.0004249661758924971, 0.0004264664684658149, 0.00046151713507627454, 0.0004123962375142004, 0.00043259241757591187, 0.00039191564861833434, 0.0004517608567341095, 0.00040691262960006647, 0.0004248262025278297, 0.0004102547126767602, 0.00041051318455042024, 0.00037769760489395467, 0.0004249105077075491, 0.000432562624556134 ] }, { "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, 20, 21, 22, 23, 24 ], "y": [ 0.0005296967375056357, 0.0005296967375056357, 0.0004900565872079092, 0.00048332067099894437, 0.00045637897622823905, 0.00045637897622823905, 0.00045637897622823905, 0.00045637897622823905, 0.00042340845980268525, 0.00042340845980268525, 0.0003983627188019335, 0.0003983627188019335, 0.0003983627188019335, 0.0003983627188019335, 0.0003983627188019335, 0.0003983627188019335, 0.00039191564861833434, 0.00039191564861833434, 0.00039191564861833434, 0.00039191564861833434, 0.00039191564861833434, 0.00039191564861833434, 0.00037769760489395467, 0.00037769760489395467, 0.00037769760489395467 ] } ], "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": "c8aa7e99-6659-430c-c779-eb35476b6d0f" } } ] }