2023-03-23 18:59:13 -07:00
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Objective functions can optionally take in step, budget, and generations.\n",
"\n",
2023-04-18 14:07:54 -07:00
"step - The same objective function will be run for #evaluation_early_stop_steps, the current step will be passed into the function as an interger. (This is useful for getting a single fold of cross validation for example).\n",
2023-03-23 18:59:13 -07:00
"\n",
"budget - A parameter that varies over the course of the generations. Gets passed into the objective function as a float between 0 and 1. If the budget of the previous evaluation is less than the current budget, it will get re-evaluated. Useful for using smaller datasets earlier in training.\n",
"\n",
"generations - an int corresponding to the current generation number.\n"
]
},
{
"cell_type": "code",
2024-09-28 15:54:13 -07:00
"execution_count": 1,
2023-03-23 18:59:13 -07:00
"metadata": {},
"outputs": [
{
2023-06-14 11:52:03 -07:00
"name": "stderr",
"output_type": "stream",
"text": [
2025-02-21 23:22:48 -08:00
"/opt/anaconda3/envs/tpotenv/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n",
"Generation: 100%|██████████| 100/100 [01:43<00:00, 1.03s/it]\n"
2023-06-14 11:52:03 -07:00
]
2023-03-23 18:59:13 -07:00
}
],
"source": [
"#knapsack problem\n",
"import numpy as np\n",
2024-12-23 11:11:12 -08:00
"import tpot\n",
2023-03-23 18:59:13 -07:00
"import random\n",
"import matplotlib.pyplot as plt\n",
2023-04-11 12:37:32 -07:00
"from dask.distributed import Client, LocalCluster\n",
2023-03-23 18:59:13 -07:00
"\n",
2024-12-23 11:11:12 -08:00
"class SubsetSelector(tpot.individual.BaseIndividual):\n",
2023-03-23 18:59:13 -07:00
" def __init__( self,\n",
" values,\n",
" initial_set = None,\n",
" k=1, #step size for shuffling\n",
" ):\n",
"\n",
" if isinstance(values, int):\n",
" self.values = set(range(0,values))\n",
" else:\n",
" self.values = set(values)\n",
"\n",
"\n",
" if initial_set is None:\n",
" self.subsets = set(random.choices(values, k=k))\n",
" else:\n",
" self.subsets = set(initial_set)\n",
"\n",
" self.k = k\n",
"\n",
" self.mutation_list = [self._mutate_add, self._mutate_remove]\n",
" self.crossover_list = [self._crossover_swap]\n",
" \n",
2023-04-11 12:37:32 -07:00
"\n",
2024-06-28 19:21:30 -07:00
" def mutate(self, rng=None):\n",
2023-04-11 12:37:32 -07:00
" mutation_list_copy = self.mutation_list.copy()\n",
" random.shuffle(mutation_list_copy)\n",
" for func in mutation_list_copy:\n",
" if func():\n",
" return True\n",
" return False\n",
"\n",
2024-06-28 19:21:30 -07:00
" def crossover(self, ind2, rng=None):\n",
2023-04-11 12:37:32 -07:00
" crossover_list_copy = self.crossover_list.copy()\n",
" random.shuffle(crossover_list_copy)\n",
" for func in crossover_list_copy:\n",
" if func(ind2):\n",
" return True\n",
" return False\n",
"\n",
2023-03-23 18:59:13 -07:00
" def _mutate_add(self,):\n",
" not_included = list(self.values.difference(self.subsets))\n",
" if len(not_included) > 1:\n",
" self.subsets.update(random.sample(not_included, k=min(self.k, len(not_included))))\n",
" return True\n",
" else:\n",
" return False\n",
"\n",
" def _mutate_remove(self,):\n",
" if len(self.subsets) > 1:\n",
" self.subsets = self.subsets - set(random.sample(list(self.subsets), k=min(self.k, len(self.subsets)-1) ))\n",
"\n",
" def _crossover_swap(self, ss2):\n",
" diffs = self.subsets.symmetric_difference(ss2.subsets)\n",
"\n",
" if len(diffs) == 0:\n",
" return False\n",
" for v in diffs:\n",
" self.subsets.discard(v)\n",
" ss2.subsets.discard(v)\n",
" random.choice([self.subsets, ss2.subsets]).add(v)\n",
" \n",
" return True\n",
"\n",
" def unique_id(self):\n",
" return str(tuple(sorted(self.subsets)))\n",
"\n",
"def individual_generator():\n",
" while True:\n",
" yield SubsetSelector(values=np.arange(len(values)))\n",
"\n",
"\n",
"values = np.random.randint(200,size=100)\n",
"weights = np.random.random(200)*10\n",
"max_weight = 50\n",
"\n",
"def simple_objective(ind, **kwargs):\n",
" subset = np.array(list(ind.subsets))\n",
" if len(subset) == 0:\n",
" return 0, 0\n",
"\n",
" total_weight = np.sum(weights[subset])\n",
" total_value = np.sum(values[subset])\n",
"\n",
" if total_weight > max_weight:\n",
" total_value = 0\n",
"\n",
" return total_value, total_weight\n",
"\n",
"objective_names = [\"Value\", \"Weight\"]\n",
"objective_function_weights = [1,-1]\n",
"\n",
2023-04-11 12:37:32 -07:00
"\n",
"\n",
2024-12-23 11:11:12 -08:00
"evolver = tpot.evolvers.BaseEvolver( individual_generator=individual_generator(), \n",
2023-03-23 18:59:13 -07:00
" objective_functions=[simple_objective],\n",
" objective_function_weights = objective_function_weights,\n",
" bigger_is_better = True,\n",
" population_size= 100,\n",
" objective_names = objective_names,\n",
" generations= 100,\n",
2024-09-28 15:54:13 -07:00
" n_jobs=32,\n",
2023-03-23 18:59:13 -07:00
" verbose = 1,\n",
"\n",
")\n",
"\n",
"evolver.optimize()"
]
},
{
"cell_type": "code",
2024-09-28 15:54:13 -07:00
"execution_count": 2,
2023-03-23 18:59:13 -07:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2025-02-21 23:22:48 -08:00
"best subset {1, 8, 9, 16, 17, 22, 23, 24, 28, 29, 31, 42, 43, 48, 50, 61, 62, 68, 80, 89, 91, 97, 98}\n",
"Best value 3070.0, weight 49.01985602703945\n",
2023-03-23 18:59:13 -07:00
"\n",
"All results\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Selected Index</th>\n",
" <th>Value</th>\n",
" <th>Weight</th>\n",
" <th>Parents</th>\n",
" <th>Variation_Function</th>\n",
" <th>Individual</th>\n",
" <th>Generation</th>\n",
2024-02-16 14:09:31 -08:00
" <th>Submitted Timestamp</th>\n",
" <th>Completed Timestamp</th>\n",
2024-06-28 19:21:30 -07:00
" <th>Eval Error</th>\n",
2023-06-14 11:52:03 -07:00
" <th>Pareto_Front</th>\n",
2023-03-23 18:59:13 -07:00
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
2025-02-21 23:22:48 -08:00
" <td>(40,)</td>\n",
" <td>89.0</td>\n",
" <td>9.883465</td>\n",
2023-03-23 18:59:13 -07:00
" <td>NaN</td>\n",
" <td>NaN</td>\n",
2025-02-21 23:22:48 -08:00
" <td><__main__.SubsetSelector object at 0x32aa80eb0></td>\n",
2023-03-23 18:59:13 -07:00
" <td>0.0</td>\n",
2025-02-21 23:22:48 -08:00
" <td>1.740209e+09</td>\n",
" <td>1.740209e+09</td>\n",
2024-06-28 19:21:30 -07:00
" <td>None</td>\n",
2023-06-14 11:52:03 -07:00
" <td>NaN</td>\n",
2023-03-23 18:59:13 -07:00
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
2025-02-21 23:22:48 -08:00
" <td>(45,)</td>\n",
" <td>116.0</td>\n",
" <td>6.643557</td>\n",
2023-03-23 18:59:13 -07:00
" <td>NaN</td>\n",
" <td>NaN</td>\n",
2025-02-21 23:22:48 -08:00
" <td><__main__.SubsetSelector object at 0x32aa83b50></td>\n",
2023-03-23 18:59:13 -07:00
" <td>0.0</td>\n",
2025-02-21 23:22:48 -08:00
" <td>1.740209e+09</td>\n",
" <td>1.740209e+09</td>\n",
2024-06-28 19:21:30 -07:00
" <td>None</td>\n",
2023-06-14 11:52:03 -07:00
" <td>NaN</td>\n",
2023-03-23 18:59:13 -07:00
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
2025-02-21 23:22:48 -08:00
" <td>(52,)</td>\n",
" <td>172.0</td>\n",
" <td>9.273163</td>\n",
2023-03-23 18:59:13 -07:00
" <td>NaN</td>\n",
" <td>NaN</td>\n",
2025-02-21 23:22:48 -08:00
" <td><__main__.SubsetSelector object at 0x32aa81210></td>\n",
2023-03-23 18:59:13 -07:00
" <td>0.0</td>\n",
2025-02-21 23:22:48 -08:00
" <td>1.740209e+09</td>\n",
" <td>1.740209e+09</td>\n",
2024-06-28 19:21:30 -07:00
" <td>None</td>\n",
2023-06-14 11:52:03 -07:00
" <td>NaN</td>\n",
2023-03-23 18:59:13 -07:00
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
2025-02-21 23:22:48 -08:00
" <td>(33,)</td>\n",
" <td>112.0</td>\n",
" <td>1.594347</td>\n",
2023-03-23 18:59:13 -07:00
" <td>NaN</td>\n",
" <td>NaN</td>\n",
2025-02-21 23:22:48 -08:00
" <td><__main__.SubsetSelector object at 0x32aa838e0></td>\n",
2023-03-23 18:59:13 -07:00
" <td>0.0</td>\n",
2025-02-21 23:22:48 -08:00
" <td>1.740209e+09</td>\n",
" <td>1.740209e+09</td>\n",
2024-06-28 19:21:30 -07:00
" <td>None</td>\n",
2023-06-14 11:52:03 -07:00
" <td>NaN</td>\n",
2023-03-23 18:59:13 -07:00
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
2025-02-21 23:22:48 -08:00
" <td>(37,)</td>\n",
" <td>90.0</td>\n",
" <td>3.273826</td>\n",
2023-03-23 18:59:13 -07:00
" <td>NaN</td>\n",
" <td>NaN</td>\n",
2025-02-21 23:22:48 -08:00
" <td><__main__.SubsetSelector object at 0x32aa83e50></td>\n",
2023-03-23 18:59:13 -07:00
" <td>0.0</td>\n",
2025-02-21 23:22:48 -08:00
" <td>1.740209e+09</td>\n",
" <td>1.740209e+09</td>\n",
2024-06-28 19:21:30 -07:00
" <td>None</td>\n",
2023-06-14 11:52:03 -07:00
" <td>NaN</td>\n",
2023-03-23 18:59:13 -07:00
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
2023-06-14 11:52:03 -07:00
" <td>...</td>\n",
2024-02-16 14:09:31 -08:00
" <td>...</td>\n",
" <td>...</td>\n",
2024-06-28 19:21:30 -07:00
" <td>...</td>\n",
2023-03-23 18:59:13 -07:00
" </tr>\n",
" <tr>\n",
" <th>9995</th>\n",
2025-02-21 23:22:48 -08:00
" <td>(1, 9, 16, 23, 24, 31, 77, 79)</td>\n",
" <td>998.0</td>\n",
" <td>11.622582</td>\n",
" <td>((1, 9, 16, 17, 23, 24, 31, 77), (1, 9, 16, 17...</td>\n",
2024-02-16 14:09:31 -08:00
" <td>ind_mutate</td>\n",
2025-02-21 23:22:48 -08:00
" <td><__main__.SubsetSelector object at 0x3a739b010></td>\n",
2023-03-23 18:59:13 -07:00
" <td>99.0</td>\n",
2025-02-21 23:22:48 -08:00
" <td>1.740209e+09</td>\n",
" <td>1.740209e+09</td>\n",
2024-06-28 19:21:30 -07:00
" <td>None</td>\n",
" <td>NaN</td>\n",
2023-03-23 18:59:13 -07:00
" </tr>\n",
" <tr>\n",
" <th>9996</th>\n",
2025-02-21 23:22:48 -08:00
" <td>(1, 8, 9, 16, 22, 23, 24, 28, 29, 31, 48, 49, ...</td>\n",
" <td>0.0</td>\n",
" <td>51.400433</td>\n",
" <td>((1, 8, 9, 16, 17, 22, 23, 24, 28, 29, 31, 48,...</td>\n",
2024-06-28 19:21:30 -07:00
" <td>ind_mutate</td>\n",
2025-02-21 23:22:48 -08:00
" <td><__main__.SubsetSelector object at 0x3af9a4460></td>\n",
2023-03-23 18:59:13 -07:00
" <td>99.0</td>\n",
2025-02-21 23:22:48 -08:00
" <td>1.740209e+09</td>\n",
" <td>1.740209e+09</td>\n",
2024-06-28 19:21:30 -07:00
" <td>None</td>\n",
2023-06-14 11:52:03 -07:00
" <td>NaN</td>\n",
2023-03-23 18:59:13 -07:00
" </tr>\n",
" <tr>\n",
" <th>9997</th>\n",
2025-02-21 23:22:48 -08:00
" <td>(1, 4, 8, 9, 16, 17, 23, 24, 31, 49, 68, 77, 8...</td>\n",
" <td>1728.0</td>\n",
" <td>15.997430</td>\n",
" <td>((1, 4, 8, 9, 16, 17, 23, 24, 31, 68, 77, 88, ...</td>\n",
2024-02-16 14:09:31 -08:00
" <td>ind_mutate</td>\n",
2025-02-21 23:22:48 -08:00
" <td><__main__.SubsetSelector object at 0x3aa303430></td>\n",
2023-03-23 18:59:13 -07:00
" <td>99.0</td>\n",
2025-02-21 23:22:48 -08:00
" <td>1.740209e+09</td>\n",
" <td>1.740209e+09</td>\n",
2024-06-28 19:21:30 -07:00
" <td>None</td>\n",
2025-02-21 23:22:48 -08:00
" <td>1.0</td>\n",
2023-03-23 18:59:13 -07:00
" </tr>\n",
" <tr>\n",
" <th>9998</th>\n",
2025-02-21 23:22:48 -08:00
" <td>(8, 9, 17, 23, 24, 25, 31, 51, 77)</td>\n",
" <td>972.0</td>\n",
" <td>11.991547</td>\n",
" <td>((8, 9, 17, 23, 24, 31, 77, 88), (8, 9, 17, 23...</td>\n",
2024-02-16 14:09:31 -08:00
" <td>ind_mutate</td>\n",
2025-02-21 23:22:48 -08:00
" <td><__main__.SubsetSelector object at 0x3a7399600></td>\n",
2023-03-23 18:59:13 -07:00
" <td>99.0</td>\n",
2025-02-21 23:22:48 -08:00
" <td>1.740209e+09</td>\n",
" <td>1.740209e+09</td>\n",
2024-06-28 19:21:30 -07:00
" <td>None</td>\n",
2023-06-14 11:52:03 -07:00
" <td>NaN</td>\n",
2023-03-23 18:59:13 -07:00
" </tr>\n",
" <tr>\n",
" <th>9999</th>\n",
2025-02-21 23:22:48 -08:00
" <td>(8, 23, 24, 73, 79)</td>\n",
" <td>648.0</td>\n",
" <td>12.109013</td>\n",
" <td>((8, 16, 17, 23, 24), (8, 16, 17, 23, 24))</td>\n",
2024-06-28 19:21:30 -07:00
" <td>ind_mutate</td>\n",
2025-02-21 23:22:48 -08:00
" <td><__main__.SubsetSelector object at 0x3a88d4430></td>\n",
2023-03-23 18:59:13 -07:00
" <td>99.0</td>\n",
2025-02-21 23:22:48 -08:00
" <td>1.740209e+09</td>\n",
" <td>1.740209e+09</td>\n",
2024-06-28 19:21:30 -07:00
" <td>None</td>\n",
2023-06-14 11:52:03 -07:00
" <td>NaN</td>\n",
2023-03-23 18:59:13 -07:00
" </tr>\n",
" </tbody>\n",
"</table>\n",
2024-06-28 19:21:30 -07:00
"<p>10000 rows × 11 columns</p>\n",
2023-03-23 18:59:13 -07:00
"</div>"
],
"text/plain": [
2025-02-21 23:22:48 -08:00
" Selected Index Value Weight \\\n",
"0 (40,) 89.0 9.883465 \n",
"1 (45,) 116.0 6.643557 \n",
"2 (52,) 172.0 9.273163 \n",
"3 (33,) 112.0 1.594347 \n",
"4 (37,) 90.0 3.273826 \n",
"... ... ... ... \n",
"9995 (1, 9, 16, 23, 24, 31, 77, 79) 998.0 11.622582 \n",
"9996 (1, 8, 9, 16, 22, 23, 24, 28, 29, 31, 48, 49, ... 0.0 51.400433 \n",
"9997 (1, 4, 8, 9, 16, 17, 23, 24, 31, 49, 68, 77, 8... 1728.0 15.997430 \n",
"9998 (8, 9, 17, 23, 24, 25, 31, 51, 77) 972.0 11.991547 \n",
"9999 (8, 23, 24, 73, 79) 648.0 12.109013 \n",
2023-06-14 11:52:03 -07:00
"\n",
2024-06-28 19:21:30 -07:00
" Parents Variation_Function \\\n",
"0 NaN NaN \n",
"1 NaN NaN \n",
"2 NaN NaN \n",
"3 NaN NaN \n",
"4 NaN NaN \n",
"... ... ... \n",
2025-02-21 23:22:48 -08:00
"9995 ((1, 9, 16, 17, 23, 24, 31, 77), (1, 9, 16, 17... ind_mutate \n",
"9996 ((1, 8, 9, 16, 17, 22, 23, 24, 28, 29, 31, 48,... ind_mutate \n",
"9997 ((1, 4, 8, 9, 16, 17, 23, 24, 31, 68, 77, 88, ... ind_mutate \n",
"9998 ((8, 9, 17, 23, 24, 31, 77, 88), (8, 9, 17, 23... ind_mutate \n",
"9999 ((8, 16, 17, 23, 24), (8, 16, 17, 23, 24)) ind_mutate \n",
2023-03-23 18:59:13 -07:00
"\n",
2025-02-21 23:22:48 -08:00
" Individual Generation \\\n",
"0 <__main__.SubsetSelector object at 0x32aa80eb0> 0.0 \n",
"1 <__main__.SubsetSelector object at 0x32aa83b50> 0.0 \n",
"2 <__main__.SubsetSelector object at 0x32aa81210> 0.0 \n",
"3 <__main__.SubsetSelector object at 0x32aa838e0> 0.0 \n",
"4 <__main__.SubsetSelector object at 0x32aa83e50> 0.0 \n",
"... ... ... \n",
"9995 <__main__.SubsetSelector object at 0x3a739b010> 99.0 \n",
"9996 <__main__.SubsetSelector object at 0x3af9a4460> 99.0 \n",
"9997 <__main__.SubsetSelector object at 0x3aa303430> 99.0 \n",
"9998 <__main__.SubsetSelector object at 0x3a7399600> 99.0 \n",
"9999 <__main__.SubsetSelector object at 0x3a88d4430> 99.0 \n",
2023-03-23 18:59:13 -07:00
"\n",
2024-06-28 19:21:30 -07:00
" Submitted Timestamp Completed Timestamp Eval Error Pareto_Front \n",
2025-02-21 23:22:48 -08:00
"0 1.740209e+09 1.740209e+09 None NaN \n",
"1 1.740209e+09 1.740209e+09 None NaN \n",
"2 1.740209e+09 1.740209e+09 None NaN \n",
"3 1.740209e+09 1.740209e+09 None NaN \n",
"4 1.740209e+09 1.740209e+09 None NaN \n",
2024-06-28 19:21:30 -07:00
"... ... ... ... ... \n",
2025-02-21 23:22:48 -08:00
"9995 1.740209e+09 1.740209e+09 None NaN \n",
"9996 1.740209e+09 1.740209e+09 None NaN \n",
"9997 1.740209e+09 1.740209e+09 None 1.0 \n",
"9998 1.740209e+09 1.740209e+09 None NaN \n",
"9999 1.740209e+09 1.740209e+09 None NaN \n",
2023-03-23 18:59:13 -07:00
"\n",
2024-06-28 19:21:30 -07:00
"[10000 rows x 11 columns]"
2023-03-23 18:59:13 -07:00
]
},
2024-09-28 15:54:13 -07:00
"execution_count": 2,
2023-03-23 18:59:13 -07:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"final_population_results = evolver.population.evaluated_individuals\n",
"final_population_results.reset_index(inplace=True)\n",
"final_population_results = final_population_results.rename(columns = {'index':'Selected Index'})\n",
"\n",
"best_idx = final_population_results[\"Value\"].idxmax()\n",
"best_individual = final_population_results.loc[best_idx]['Individual']\n",
"print(\"best subset\", best_individual.subsets)\n",
"print(\"Best value {0}, weight {1}\".format(final_population_results.loc[best_idx, \"Value\"],final_population_results.loc[best_idx, \"Weight\"]))\n",
"print()\n",
"\n",
"print(\"All results\")\n",
"final_population_results"
]
},
{
"cell_type": "code",
2024-09-28 15:54:13 -07:00
"execution_count": 3,
2023-03-23 18:59:13 -07:00
"metadata": {},
"outputs": [
{
"data": {
2025-02-21 23:22:48 -08:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAicAAAGGCAYAAACg+CELAAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjAsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvlHJYcgAAAAlwSFlzAAAPYQAAD2EBqD+naQAAbK5JREFUeJzt3Qd8FEX7B/BJTyAFCB1C6L0jIEV6RwTxVVF6VZo0QUB6EUSlC6goRUEEBRSkh96L9BI6ofcEElLv9v95hv/t7Fxy4fbIJZfc7/t+7s3c7d7e3ubMPcwzz4yLoigKAwAAAHAQrml9AgAAAABaCE4AAADAoSA4AQAAAIeC4AQAAAAcCoITAAAAcCgITgAAAMChIDgBAAAAh4LgBAAAABwKghMAAABwKAhOwCYuLi5s3LhxzJHt3LmTnyf9BEhJ9NmnzxYA2AeCE+AWL17M/9hqbzlz5mT169dnGzduZM5i3rx5/L1Xr149rU/FIRmNRrZ06VLWuHFjlj17dubh4cE/J02aNGE//vgji42NZRnFixcveBCC4BYg9bmnwWuCA5swYQIrVKgQoyWX7t+/z4OWFi1asHXr1rG3335b3S86Opq5u2e8j8+yZctYwYIF2eHDh9nly5dZ0aJF0/qUHAb9zt999122efNmVrNmTfb555+zXLlys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a6E9X5o8jo6rReOWTMd9FfoMEipb1jL1nlF5OgBYDz0nIKEvH8rDm/L3lEKgfw3SbJjaMSOpYerUqXzQJI3
2023-03-23 18:59:13 -07:00
"text/plain": [
"<Figure size 1200x400 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from scipy.stats import binned_statistic_2d\n",
"\n",
"y = final_population_results[\"Value\"]\n",
"x = final_population_results[\"Weight\"]\n",
"c = final_population_results[\"Generation\"]\n",
"\n",
"x_bins = np.linspace(0, 100, 100)\n",
"y_bins = np.linspace(0, 3000, 100)\n",
"\n",
"ret = binned_statistic_2d(x, y, c, statistic=np.mean, bins=[x_bins, y_bins])\n",
"\n",
"fig, ax1 = plt.subplots(1, 1, figsize=(12, 4))\n",
"\n",
"im = ax1.imshow(ret.statistic.T, origin='lower', extent=(0,100,0,3000), vmin=0, vmax=100, aspect=.03)\n",
"ax1.set_xlabel(\"Weight\")\n",
"ax1.set_ylabel(\"Value\")\n",
"ax1.set_title(\"Binned Average Generation\")\n",
"\n",
"cbar = fig.colorbar(im,)\n",
"cbar.set_label('Generation')\n",
"plt.tight_layout()"
]
}
],
"metadata": {
"kernelspec": {
2025-02-21 23:22:48 -08:00
"display_name": "tpotenv",
2023-03-23 18:59:13 -07:00
"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",
2025-02-21 23:22:48 -08:00
"version": "3.10.16"
2023-03-23 18:59:13 -07:00
},
2025-02-21 23:22:48 -08:00
"orig_nbformat": 4
2023-03-23 18:59:13 -07:00
},
"nbformat": 4,
"nbformat_minor": 2
}