{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "66148399-693d-4d63-9be0-5716dcc9f334",
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "\"\"\"\n",
    "plot a pretty confusion matrix with seaborn\n",
    "Created on Mon Jun 25 14:17:37 2018\n",
    "@author: Wagner Cipriano - wagnerbhbr - gmail - CEFETMG / MMC\n",
    "REFerences:\n",
    "  https://www.mathworks.com/help/nnet/ref/plotconfusion.html\n",
    "  https://stackoverflow.com/questions/28200786/how-to-plot-scikit-learn-classification-report\n",
    "  https://stackoverflow.com/questions/5821125/how-to-plot-confusion-matrix-with-string-axis-rather-than-integer-in-python\n",
    "  https://www.programcreek.com/python/example/96197/seaborn.heatmap\n",
    "  https://stackoverflow.com/questions/19233771/sklearn-plot-confusion-matrix-with-labels/31720054\n",
    "  http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html#sphx-glr-auto-examples-model-selection-plot-confusion-matrix-py\n",
    "\"\"\"\n",
    "import sys\n",
    "sys.path.append('../') ##accrocchio\n",
    "from src.utils import retrive_data,split\n",
    "import pickle\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "from optuna.visualization import plot_param_importances,plot_edf,plot_optimization_history,plot_intermediate_values,plot_parallel_coordinate,plot_edf\n",
    "\n",
    "import matplotlib.font_manager as fm\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import seaborn as sn\n",
    "from matplotlib.collections import QuadMesh\n",
    "import xgboost as xgb\n",
    "from sklearn.metrics import accuracy_score, matthews_corrcoef,confusion_matrix\n",
    "\n",
    "def get_new_fig(fn, figsize=[9, 9]):\n",
    "    \"\"\"Init graphics\"\"\"\n",
    "    fig1 = plt.figure(fn, figsize)\n",
    "    ax1 = fig1.gca()  # Get Current Axis\n",
    "    ax1.cla()  # clear existing plot\n",
    "    return fig1, ax1\n",
    "\n",
    "\n",
    "def configcell_text_and_colors(\n",
    "    array_df, lin, col, oText, facecolors, posi, fz, fmt, show_null_values=0\n",
    "):\n",
    "    \"\"\"\n",
    "    config cell text and colors\n",
    "    and return text elements to add and to dell\n",
    "    @TODO: use fmt\n",
    "    \"\"\"\n",
    "    text_add = []\n",
    "    text_del = []\n",
    "    cell_val = array_df[lin][col]\n",
    "    tot_all = array_df[-1][-1]\n",
    "    per = (float(cell_val) / tot_all) * 100\n",
    "    curr_column = array_df[:, col]\n",
    "    ccl = len(curr_column)\n",
    "\n",
    "    # last line  and/or last column\n",
    "    if (col == (ccl - 1)) or (lin == (ccl - 1)):\n",
    "        # tots and percents\n",
    "        if cell_val != 0:\n",
    "            if (col == ccl - 1) and (lin == ccl - 1):\n",
    "                tot_rig = 0\n",
    "                for i in range(array_df.shape[0] - 1):\n",
    "                    tot_rig += array_df[i][i]\n",
    "                per_ok = (float(tot_rig) / cell_val) * 100\n",
    "            elif col == ccl - 1:\n",
    "                tot_rig = array_df[lin][lin]\n",
    "                per_ok = (float(tot_rig) / cell_val) * 100\n",
    "            elif lin == ccl - 1:\n",
    "                tot_rig = array_df[col][col]\n",
    "                per_ok = (float(tot_rig) / cell_val) * 100\n",
    "            per_err = 100 - per_ok\n",
    "        else:\n",
    "            per_ok = per_err = 0\n",
    "\n",
    "        per_ok_s = [\"%.2f%%\" % (per_ok), \"100%\"][per_ok == 100]\n",
    "\n",
    "        # text to DEL\n",
    "        text_del.append(oText)\n",
    "\n",
    "        # text to ADD\n",
    "        font_prop = fm.FontProperties(weight=\"bold\", size=fz)\n",
    "        text_kwargs = dict(\n",
    "            color=\"w\",\n",
    "            ha=\"center\",\n",
    "            va=\"center\",\n",
    "            gid=\"sum\",\n",
    "            fontproperties=font_prop,\n",
    "        )\n",
    "        lis_txt = [\"%d\" % (cell_val), per_ok_s, \"%.2f%%\" % (per_err)]\n",
    "        lis_kwa = [text_kwargs]\n",
    "        dic = text_kwargs.copy()\n",
    "        dic[\"color\"] = \"g\"\n",
    "        lis_kwa.append(dic)\n",
    "        dic = text_kwargs.copy()\n",
    "        dic[\"color\"] = \"r\"\n",
    "        lis_kwa.append(dic)\n",
    "        lis_pos = [\n",
    "            (oText._x, oText._y - 0.3),\n",
    "            (oText._x, oText._y),\n",
    "            (oText._x, oText._y + 0.3),\n",
    "        ]\n",
    "        for i in range(len(lis_txt)):\n",
    "            newText = dict(\n",
    "                x=lis_pos[i][0],\n",
    "                y=lis_pos[i][1],\n",
    "                text=lis_txt[i],\n",
    "                kw=lis_kwa[i],\n",
    "            )\n",
    "            text_add.append(newText)\n",
    "\n",
    "        # set background color for sum cells (last line and last column)\n",
    "        carr = [0.27, 0.30, 0.27, 1.0]\n",
    "        if (col == ccl - 1) and (lin == ccl - 1):\n",
    "            carr = [0.17, 0.20, 0.17, 1.0]\n",
    "        facecolors[posi] = carr\n",
    "\n",
    "    else:\n",
    "        if per > 0:\n",
    "            txt = \"%s\\n%.2f%%\" % (cell_val, per)\n",
    "        else:\n",
    "            if show_null_values == 0:\n",
    "                txt = \"\"\n",
    "            elif show_null_values == 1:\n",
    "                txt = \"0\"\n",
    "            else:\n",
    "                txt = \"0\\n0.0%\"\n",
    "        oText.set_text(txt)\n",
    "\n",
    "        # main diagonal\n",
    "        if col == lin:\n",
    "            # set color of the textin the diagonal to white\n",
    "            oText.set_color(\"w\")\n",
    "            # set background color in the diagonal to blue\n",
    "            facecolors[posi] = [0.35, 0.8, 0.55, 1.0]\n",
    "        else:\n",
    "            oText.set_color(\"r\")\n",
    "\n",
    "    return text_add, text_del\n",
    "\n",
    "\n",
    "def insert_totals(df_cm):\n",
    "    \"\"\"insert total column and line (the last ones)\"\"\"\n",
    "    sum_col = []\n",
    "    for c in df_cm.columns:\n",
    "        sum_col.append(df_cm[c].sum())\n",
    "    sum_lin = []\n",
    "    for item_line in df_cm.iterrows():\n",
    "        sum_lin.append(item_line[1].sum())\n",
    "    df_cm[\"sum_lin\"] = sum_lin\n",
    "    sum_col.append(np.sum(sum_lin))\n",
    "    df_cm.loc[\"sum_col\"] = sum_col\n",
    "\n",
    "\n",
    "def pp_matrix(\n",
    "    df_cm,\n",
    "    annot=True,\n",
    "    cmap=\"Oranges\",\n",
    "    fmt=\".2f\",\n",
    "    fz=11,\n",
    "    lw=0.5,\n",
    "    cbar=False,\n",
    "    figsize=[8, 8],\n",
    "    show_null_values=0,\n",
    "    pred_val_axis=\"y\",\n",
    "):\n",
    "    \"\"\"\n",
    "    print conf matrix with default layout (like matlab)\n",
    "    params:\n",
    "      df_cm          dataframe (pandas) without totals\n",
    "      annot          print text in each cell\n",
    "      cmap           Oranges,Oranges_r,YlGnBu,Blues,RdBu, ... see:\n",
    "      fz             fontsize\n",
    "      lw             linewidth\n",
    "      pred_val_axis  where to show the prediction values (x or y axis)\n",
    "                      'col' or 'x': show predicted values in columns (x axis) instead lines\n",
    "                      'lin' or 'y': show predicted values in lines   (y axis)\n",
    "    \"\"\"\n",
    "    if pred_val_axis in (\"col\", \"x\"):\n",
    "        xlbl = \"Predicted\"\n",
    "        ylbl = \"Actual\"\n",
    "    else:\n",
    "        xlbl = \"Actual\"\n",
    "        ylbl = \"Predicted\"\n",
    "        df_cm = df_cm.T\n",
    "\n",
    "    # create \"Total\" column\n",
    "    insert_totals(df_cm)\n",
    "\n",
    "    # this is for print allways in the same window\n",
    "    fig, ax1 = get_new_fig(\"Conf matrix default\", figsize)\n",
    "\n",
    "    ax = sn.heatmap(\n",
    "        df_cm,\n",
    "        annot=annot,\n",
    "        annot_kws={\"size\": fz},\n",
    "        linewidths=lw,\n",
    "        ax=ax1,\n",
    "        cbar=cbar,\n",
    "        cmap=cmap,\n",
    "        linecolor=\"w\",\n",
    "        fmt=fmt,\n",
    "    )\n",
    "\n",
    "    # set ticklabels rotation\n",
    "    ax.set_xticklabels(ax.get_xticklabels(), rotation=45, fontsize=10)\n",
    "    ax.set_yticklabels(ax.get_yticklabels(), rotation=25, fontsize=10)\n",
    "\n",
    "    # Turn off all the ticks\n",
    "    for t in ax.xaxis.get_major_ticks():\n",
    "        t.tick1On = False\n",
    "        t.tick2On = False\n",
    "    for t in ax.yaxis.get_major_ticks():\n",
    "        t.tick1On = False\n",
    "        t.tick2On = False\n",
    "\n",
    "    # face colors list\n",
    "    quadmesh = ax.findobj(QuadMesh)[0]\n",
    "    facecolors = quadmesh.get_facecolors()\n",
    "\n",
    "    # iter in text elements\n",
    "    array_df = np.array(df_cm.to_records(index=False).tolist())\n",
    "    text_add = []\n",
    "    text_del = []\n",
    "    posi = -1  # from left to right, bottom to top.\n",
    "    for t in ax.collections[0].axes.texts:  # ax.texts:\n",
    "        pos = np.array(t.get_position()) - [0.5, 0.5]\n",
    "        lin = int(pos[1])\n",
    "        col = int(pos[0])\n",
    "        posi += 1\n",
    "\n",
    "        # set text\n",
    "        txt_res = configcell_text_and_colors(\n",
    "            array_df, lin, col, t, facecolors, posi, fz, fmt, show_null_values\n",
    "        )\n",
    "\n",
    "        text_add.extend(txt_res[0])\n",
    "        text_del.extend(txt_res[1])\n",
    "\n",
    "    # remove the old ones\n",
    "    for item in text_del:\n",
    "        item.remove()\n",
    "    # append the new ones\n",
    "    for item in text_add:\n",
    "        ax.text(item[\"x\"], item[\"y\"], item[\"text\"], **item[\"kw\"])\n",
    "\n",
    "    # titles and legends\n",
    "    ax.set_title(\"Confusion matrix\")\n",
    "    ax.set_xlabel(xlbl)\n",
    "    ax.set_ylabel(ylbl)\n",
    "    plt.tight_layout()  # set layout slim\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "def pp_matrix_from_data(\n",
    "    y_test,\n",
    "    predictions,\n",
    "    columns=None,\n",
    "    annot=True,\n",
    "    cmap=\"Oranges\",\n",
    "    fmt=\".2f\",\n",
    "    fz=11,\n",
    "    lw=0.5,\n",
    "    cbar=False,\n",
    "    figsize=[8, 8],\n",
    "    show_null_values=0,\n",
    "    pred_val_axis=\"lin\",\n",
    "):\n",
    "    \"\"\"\n",
    "    plot confusion matrix function with y_test (actual values) and predictions (predic),\n",
    "    whitout a confusion matrix yet\n",
    "    \"\"\"\n",
    "    from pandas import DataFrame\n",
    "    from sklearn.metrics import confusion_matrix\n",
    "\n",
    "    # data\n",
    "    if not columns:\n",
    "        from string import ascii_uppercase\n",
    "\n",
    "        columns = [\n",
    "            \"class %s\" % (i)\n",
    "            for i in list(ascii_uppercase)[0 : len(np.unique(y_test))]\n",
    "        ]\n",
    "\n",
    "    confm = confusion_matrix(y_test, predictions)\n",
    "    df_cm = DataFrame(confm, index=columns, columns=columns)\n",
    "    pp_matrix(\n",
    "        df_cm,\n",
    "        fz=fz,\n",
    "        cmap=cmap,\n",
    "        figsize=figsize,\n",
    "        show_null_values=show_null_values,\n",
    "        pred_val_axis=pred_val_axis,\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d6e04c2-37bb-4d9c-8ab6-63e93ec29fb1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f779a954-7c90-48ad-badc-402f65be389d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   class          p\n",
      "0      0   0.203424\n",
      "1      1   0.013596\n",
      "2      2   0.056109\n",
      "3      3   1.130661\n",
      "4      4  16.206137\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "#you can put these parameters in the args but here I keep it simpler\n",
    "num_boost_round = 600\n",
    "SKI_AREA_TEST= 'Klausberg'\n",
    "SEASON_TEST_SKIAREA = 'Kronplatz'\n",
    "SEASON_TEST_YEAR= 2023\n",
    "weight_type = 'sqrt' \n",
    "## get the data\n",
    "labeled,labeled_small,to_remove,evacuations = retrive_data(reload_data=False,threshold_under_represented=0.5,path='/home/agobbi/Projects/PID/datanalytics/PID/src')\n",
    "\n",
    "\n",
    "#split the data\n",
    "dataset,dataset_test = split(labeled_small  ,\n",
    "                            SKI_AREA_TEST= SKI_AREA_TEST,\n",
    "                            SEASON_TEST_SKIAREA = SEASON_TEST_SKIAREA,\n",
    "                            SEASON_TEST_YEAR= SEASON_TEST_YEAR,\n",
    "                            use_smote = False,\n",
    "                            weight_type = weight_type )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "654af94f-e6e8-4dce-baab-fbf63c426c89",
   "metadata": {},
   "outputs": [],
   "source": [
    "aa = labeled.groupby('india').age.count().reset_index().rename(columns={'age':'Nsamples'})\n",
    "\n",
    "matplotlib.rcParams.update({'font.size': 18})\n",
    "\n",
    "plt.bar(aa.india, aa.Nsamples,log=True)\n",
    "plt.xlabel('India')\n",
    "plt.ylabel('Counts (log)')\n",
    "plt.tight_layout()\n",
    "plt.savefig('counts.png')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "92dff3bc-68ac-4765-83ed-8ef449e3af80",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/agobbi/miniconda3/envs/pid/lib/python3.11/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"
     ]
    }
   ],
   "source": [
    "#plot_edf(study)\n",
    "#plot_optimization_history(study)\n",
    "with open('../src/best_params.pkl','rb') as f:\n",
    "    params_final,feat_imp,best_model,study = pickle.load(f)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e270ae7a-5a18-4452-949a-61694b8bfee1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "KeyboardInterrupt\n",
      "\n"
     ]
    }
   ],
   "source": [
    "plot_param_importances(study)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d00b19ca-4466-4239-98c3-4cd155b4965a",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_parallel_coordinate(study)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d8e7c975-6cd3-4c8f-909a-12ba49037015",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('../src/best_params_and_final_model.pkl','rb') as f:\n",
    "    tot,bst_FS,FS = pickle.load(f)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "960c03f9-0bb2-4881-8d9a-549454e07f97",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(15.2, 0.7068054959209963, 'ACC:0.64\\nMCC:0.38')"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 33468 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "plt.plot(tot.FS, tot.acc,'o-', label='accuracy')\n",
    "plt.plot(tot.FS, tot.mcc,'o-', label='MCC')\n",
    "plt.xlabel('N features')\n",
    "plt.ylabel('Score')\n",
    "bacc = tot.acc[tot.FS==FS].values[0]\n",
    "bmcc = tot.mcc[tot.FS==FS].values[0]\n",
    "plt.vlines(x=FS, ymin=0, ymax=tot.acc.max()*1.2,color='gray',linestyles='dotted')\n",
    "plt.text(x=FS+0.2, y=tot.acc.max()*1.1, s=f'ACC:{round(bacc,2)}\\nMCC:{round(bmcc,2)}',fontdict={'size': 12})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5dfefae5-2a8c-4d06-9ae7-330ffc1c5aea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(22,)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feat_imp.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "600a9d2a-e278-45a7-936a-b675bbc2aded",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18],\n",
       " [Text(0, 0, 'age'),\n",
       "  Text(1, 0, 'diagnosis'),\n",
       "  Text(2, 0, 'injury_general_location'),\n",
       "  Text(3, 0, 'country'),\n",
       "  Text(4, 0, 'difficulty'),\n",
       "  Text(5, 0, 'destination'),\n",
       "  Text(6, 0, 'cause'),\n",
       "  Text(7, 0, 'injury_side'),\n",
       "  Text(8, 0, 'gender'),\n",
       "  Text(9, 0, 'snowmobile'),\n",
       "  Text(10, 0, 'ambulance'),\n",
       "  Text(11, 0, 'helicopter'),\n",
       "  Text(12, 0, 'equipment'),\n",
       "  Text(13, 0, 'indipendently'),\n",
       "  Text(14, 0, 'akja'),\n",
       "  Text(15, 0, 'ski_lift'),\n",
       "  Text(16, 0, 'snowmobile_sled'),\n",
       "  Text(17, 0, 'helmet'),\n",
       "  Text(18, 0, 'skiarea_ambulance')])"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 117320 characters]",
      "text/plain": [
       "<Figure size 1200x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize= (12,5))\n",
    "plt.bar(feat_imp.index,feat_imp.values,)\n",
    "plt.xticks(rotation=45, ha='right')\n",
    "#plt.vlines(x=FS, ymin=0, ymax=300,color='gray',linestyles='dotted')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7ff1fe4f-970a-4484-a0f6-731c0128d52a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RESULT ON THE TEST SKI AREA mcc=0.38970728651001324, acc=0.7561455260570304, \n",
      "cm=array([[  39,   26,    2,    0,    0],\n",
      "       [  34, 1278,  245,    0,    0],\n",
      "       [   0,  170,  220,    6,    0],\n",
      "       [   0,    0,   12,    1,    0],\n",
      "       [   0,    0,    1,    0,    0]])\n",
      "RESULT ON THE TEST SKI SEASON mcc=0.46928939502481826, acc=0.7517178195144297, cm=array([[  50,   15,    3,    0,    1],\n",
      "       [  21, 1268,  375,    1,    1],\n",
      "       [   0,  102,  320,   12,    0],\n",
      "       [   0,    0,   11,    2,    0],\n",
      "       [   0,    0,    0,    0,    1]])\n"
     ]
    }
   ],
   "source": [
    "\n",
    "dtest_FS = xgb.DMatrix(dataset_test.X_test_area[bst_FS.feature_names],dataset_test.y_test_area,enable_categorical=True,)\n",
    "dtest_season_FS = xgb.DMatrix(dataset_test.X_test_season[bst_FS.feature_names],dataset_test.y_test_season,enable_categorical=True,)\n",
    "preds_class_test = bst_FS.predict(dtest_FS)\n",
    "preds_class_test_season = bst_FS.predict(dtest_season_FS)\n",
    "\n",
    "mcc = matthews_corrcoef(dataset_test.y_test_area,preds_class_test.argmax(1))\n",
    "acc = accuracy_score(dataset_test.y_test_area,preds_class_test.argmax(1))\n",
    "cm = confusion_matrix(dataset_test.y_test_area,preds_class_test.argmax(1))\n",
    "\n",
    "print(f'RESULT ON THE TEST SKI AREA {mcc=}, {acc=}, \\n{cm=}')\n",
    "mcc = matthews_corrcoef(dataset_test.y_test_season,preds_class_test_season.argmax(1))\n",
    "acc = accuracy_score(dataset_test.y_test_season,preds_class_test_season.argmax(1))\n",
    "cm = confusion_matrix(dataset_test.y_test_season,preds_class_test_season.argmax(1))\n",
    "\n",
    "print(f'RESULT ON THE TEST SKI SEASON {mcc=}, {acc=}, {cm=}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "422dc553-950c-4af9-a5b7-e482badb7755",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RESULT ON THE TEST SKI AREA mcc=0.36210533676802603, acc=0.7404129793510325, \n",
      "cm=array([[  41,   25,    1,    0,    0],\n",
      "       [  62, 1257,  236,    1,    1],\n",
      "       [   1,  179,  203,   13,    0],\n",
      "       [   0,    0,    8,    5,    0],\n",
      "       [   0,    0,    1,    0,    0]])\n",
      "RESULT ON THE TEST SKI SEASON mcc=0.4349525919582487, acc=0.7329363261566652, cm=array([[  53,   13,    3,    0,    0],\n",
      "       [  43, 1246,  373,    2,    2],\n",
      "       [   0,  112,  298,   24,    0],\n",
      "       [   0,    0,   11,    2,    0],\n",
      "       [   0,    0,    0,    0,    1]])\n"
     ]
    }
   ],
   "source": [
    "dtest_FS = xgb.DMatrix(dataset_test.X_test_area[bst_FS.feature_names],dataset_test.y_test_area,enable_categorical=True,)\n",
    "dtest_season_FS = xgb.DMatrix(dataset_test.X_test_season[bst_FS.feature_names],dataset_test.y_test_season,enable_categorical=True,)\n",
    "preds_class_test = bst_FS.predict(dtest_FS)\n",
    "preds_class_test_season = bst_FS.predict(dtest_season_FS)\n",
    "\n",
    "mcc = matthews_corrcoef(dataset_test.y_test_area,preds_class_test.argmax(1))\n",
    "acc = accuracy_score(dataset_test.y_test_area,preds_class_test.argmax(1))\n",
    "cm = confusion_matrix(dataset_test.y_test_area,preds_class_test.argmax(1))\n",
    "\n",
    "print(f'RESULT ON THE TEST SKI AREA {mcc=}, {acc=}, \\n{cm=}')\n",
    "mcc = matthews_corrcoef(dataset_test.y_test_season,preds_class_test_season.argmax(1))\n",
    "acc = accuracy_score(dataset_test.y_test_season,preds_class_test_season.argmax(1))\n",
    "cm = confusion_matrix(dataset_test.y_test_season,preds_class_test_season.argmax(1))\n",
    "\n",
    "print(f'RESULT ON THE TEST SKI SEASON {mcc=}, {acc=}, {cm=}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "6e2cac2f-b414-45ed-8e26-3fb9786e73aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([1032, 1503, 2070]),)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(preds_class_test_season.argmax(1)==4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "585102a0-0942-48b1-a4d1-ff84393fd70f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2bb3ef03-c2fa-4102-ae70-fb11dae3e042",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_106563/989477338.py:65: DeprecationWarning: In future, it will be an error for 'np.bool_' scalars to be interpreted as an index\n",
      "  per_ok_s = [\"%.2f%%\" % (per_ok), \"100%\"][per_ok == 100]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 110396 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "pp_matrix_from_data(dataset_test.y_test_area,preds_class_test.argmax(1),cmap = 'cividis', columns=[f'india{i}' for i in range(5)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "73463369-7ffa-4481-a1cf-b8c6bc7142ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_106563/989477338.py:65: DeprecationWarning: In future, it will be an error for 'np.bool_' scalars to be interpreted as an index\n",
      "  per_ok_s = [\"%.2f%%\" % (per_ok), \"100%\"][per_ok == 100]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 114896 characters]",
      "text/plain": [
       "<Figure size 800x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pp_matrix_from_data(dataset_test.y_test_season,preds_class_test_season.argmax(1),cmap = 'cividis', columns=[f'india{i}' for i in range(5)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "33ad200c-1064-4152-bfeb-c679f7966772",
   "metadata": {},
   "outputs": [
    {
     "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>skiarea_name</th>\n",
       "      <th>season</th>\n",
       "      <th>difficulty</th>\n",
       "      <th>cause</th>\n",
       "      <th>gender</th>\n",
       "      <th>equipment</th>\n",
       "      <th>helmet</th>\n",
       "      <th>destination</th>\n",
       "      <th>diagnosis</th>\n",
       "      <th>india</th>\n",
       "      <th>...</th>\n",
       "      <th>indipendently</th>\n",
       "      <th>snowmobile_sled</th>\n",
       "      <th>akja</th>\n",
       "      <th>privat_helicopter</th>\n",
       "      <th>snowmobile</th>\n",
       "      <th>skiarea_ambulance</th>\n",
       "      <th>helicopter</th>\n",
       "      <th>offroad_vehicle</th>\n",
       "      <th>other</th>\n",
       "      <th>car</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>6362</th>\n",
       "      <td>Kronplatz</td>\n",
       "      <td>2023</td>\n",
       "      <td>advanced</td>\n",
       "      <td>other</td>\n",
       "      <td>M</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>other</td>\n",
       "      <td>None</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9030</th>\n",
       "      <td>Kronplatz</td>\n",
       "      <td>2024</td>\n",
       "      <td>easy</td>\n",
       "      <td>illness</td>\n",
       "      <td>M</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>other</td>\n",
       "      <td>other</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18060</th>\n",
       "      <td>Kronplatz</td>\n",
       "      <td>2024</td>\n",
       "      <td>advanced</td>\n",
       "      <td>other</td>\n",
       "      <td>M</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>other</td>\n",
       "      <td>other</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      skiarea_name  season difficulty    cause gender equipment helmet  \\\n",
       "6362     Kronplatz    2023   advanced    other      M       ski   True   \n",
       "9030     Kronplatz    2024       easy  illness      M      None   True   \n",
       "18060    Kronplatz    2024   advanced    other      M       ski   True   \n",
       "\n",
       "      destination diagnosis  india  ...  indipendently snowmobile_sled   akja  \\\n",
       "6362        other      None      0  ...          False           False   True   \n",
       "9030        other     other      1  ...          False           False  False   \n",
       "18060       other     other      4  ...          False           False   True   \n",
       "\n",
       "      privat_helicopter snowmobile skiarea_ambulance helicopter  \\\n",
       "6362              False      False             False      False   \n",
       "9030              False       True             False      False   \n",
       "18060             False      False             False       True   \n",
       "\n",
       "      offroad_vehicle  other    car  \n",
       "6362            False  False  False  \n",
       "9030            False  False  False  \n",
       "18060           False   True  False  \n",
       "\n",
       "[3 rows x 27 columns]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset_test.X_test_season.iloc[np.where(preds_class_test_season.argmax(1)==4)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ae3d4c40-3b60-4895-964d-8856d02cf645",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 184, 1032, 2070])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(preds_class_test_season.argmax(1)==4)[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "fa91b66b-2094-4469-895f-9c59adad7eae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6362     0\n",
       "9030     1\n",
       "18060    4\n",
       "Name: india, dtype: int64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset_test.y_test_season.iloc[np.where(preds_class_test_season.argmax(1)==4)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "013a5151-06ab-48af-abb7-4c02d0065bdf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "skiarea_name               Kronplatz\n",
       "season                          2024\n",
       "difficulty                      easy\n",
       "cause                        illness\n",
       "gender                             M\n",
       "equipment                       None\n",
       "helmet                          True\n",
       "destination                    other\n",
       "diagnosis                      other\n",
       "india                              1\n",
       "age                             64.0\n",
       "country                       Italia\n",
       "injury_side                     None\n",
       "injury_general_location         None\n",
       "ambulance                      False\n",
       "ski_lift                        True\n",
       "quad                           False\n",
       "indipendently                  False\n",
       "snowmobile_sled                False\n",
       "akja                           False\n",
       "privat_helicopter              False\n",
       "snowmobile                      True\n",
       "skiarea_ambulance              False\n",
       "helicopter                     False\n",
       "offroad_vehicle                False\n",
       "other                          False\n",
       "car                            False\n",
       "Name: 9030, dtype: object"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset_test.X_test_season.iloc[np.where(preds_class_test_season.argmax(1)==4)].iloc[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "60c9ebdc-943c-4461-a0ee-9196227d1d8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import shap\n",
    "import xgboost as xgb\n",
    "import numpy as np\n",
    "\n",
    "explainer = shap.TreeExplainer(bst_FS,feature_names=bst_FS.feature_names)\n",
    "shap_values = explainer.shap_values(dtest_season_FS)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "fe1306f0-83b8-4438-87e4-08e7a63b6e83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[42.0, 0.8, 0.2, 0.0, 57.0]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(np.round(preds_class_test_season[184]*100,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f438ab81-aa6b-40ef-ba9f-72f2419c151f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "5c6e71ae-5dc4-49ee-a3a4-f7ae55a51750",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<div id='i7Q925GXM6RKWK7NUZJWP'>\n",
       "<div style='color: #900; text-align: center;'>\n",
       "  <b>Visualization omitted, Javascript library not loaded!</b><br>\n",
       "  Have you run `initjs()` in this notebook? If this notebook was from another\n",
       "  user you must also trust this notebook (File -> Trust notebook). If you are viewing\n",
       "  this notebook on github the Javascript has been stripped for security. If you are using\n",
       "  JupyterLab this error is because a JupyterLab extension has not yet been written.\n",
       "</div></div>\n",
       " <script>\n",
       "   if (window.SHAP) SHAP.ReactDom.render(\n",
       "    SHAP.React.createElement(SHAP.AdditiveForceVisualizer, {\"outNames\": [\"f(x)\"], \"baseValue\": -1.1636028289794922, \"outValue\": 0.4295194149017334, \"link\": \"identity\", \"featureNames\": [\"age\", \"diagnosis\", \"country\", \"difficulty\", \"injury_general_location\", \"cause\", \"destination\", \"equipment\", \"injury_side\", \"snowmobile\", \"gender\", \"indipendently\", \"helmet\", \"akja\", \"ski_lift\", \"ambulance\", \"snowmobile_sled\", \"helicopter\", \"other\", \"skiarea_ambulance\"], \"features\": {\"0\": {\"effect\": 0.4104311168193817, \"value\": \"\"}, \"1\": {\"effect\": 0.8114210963249207, \"value\": \"\"}, \"2\": {\"effect\": 0.06270141154527664, \"value\": \"\"}, \"3\": {\"effect\": -1.5315330028533936, \"value\": \"\"}, \"4\": {\"effect\": -0.022503014653921127, \"value\": \"\"}, \"5\": {\"effect\": -0.20787297189235687, \"value\": \"\"}, \"6\": {\"effect\": 2.2462408542633057, \"value\": \"\"}, \"7\": {\"effect\": 0.2167215347290039, \"value\": \"\"}, \"8\": {\"effect\": 0.030627980828285217, \"value\": \"\"}, \"9\": {\"effect\": -0.4790402352809906, \"value\": \"\"}, \"10\": {\"effect\": 0.07920225709676743, \"value\": \"\"}, \"11\": {\"effect\": 0.11538670212030411, \"value\": \"\"}, \"12\": {\"effect\": 0.03300907835364342, \"value\": \"\"}, \"13\": {\"effect\": -0.0023419498465955257, \"value\": \"\"}, \"14\": {\"effect\": 0.010712329298257828, \"value\": \"\"}, \"15\": {\"effect\": 0.002213317435234785, \"value\": \"\"}, \"16\": {\"effect\": -0.0884682834148407, \"value\": \"\"}, \"17\": {\"effect\": -0.08531501144170761, \"value\": \"\"}, \"18\": {\"effect\": 0.0022041318006813526, \"value\": \"\"}, \"19\": {\"effect\": -0.010675014927983284, \"value\": \"\"}}, \"plot_cmap\": \"RdBu\", \"labelMargin\": 20}),\n",
       "    document.getElementById('i7Q925GXM6RKWK7NUZJWP')\n",
       "  );\n",
       "</script>"
      ],
      "text/plain": [
       "<shap.plots._force.AdditiveForceVisualizer at 0x7f1e8624ca10>"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i=4\n",
    "shap.plots.force(explainer.expected_value[i], shap_values[i][1032],feature_names=bst_FS.feature_names)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "e65a1cd7-8f29-4709-86f9-cb9eacfa4e60",
   "metadata": {},
   "outputs": [],
   "source": [
    "xx = dataset.X_train\n",
    "xx['india'] = dataset.y_train.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "16ef4703-3d5f-480a-bf1d-15c96cba0f64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['other']\n",
       "Categories (7, object): ['None', 'domicile', 'hospital_emergency_room', 'other', 'private_clinic', 'public_clinic', 'traumacenter']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx.destination[xx.india==4].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "45c04ea6-8ebe-4b9c-938a-fb13647aaa38",
   "metadata": {},
   "outputs": [
    {
     "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>difficulty</th>\n",
       "      <th>cause</th>\n",
       "      <th>gender</th>\n",
       "      <th>equipment</th>\n",
       "      <th>helmet</th>\n",
       "      <th>destination</th>\n",
       "      <th>diagnosis</th>\n",
       "      <th>age</th>\n",
       "      <th>country</th>\n",
       "      <th>injury_side</th>\n",
       "      <th>...</th>\n",
       "      <th>snowmobile_sled</th>\n",
       "      <th>akja</th>\n",
       "      <th>privat_helicopter</th>\n",
       "      <th>snowmobile</th>\n",
       "      <th>skiarea_ambulance</th>\n",
       "      <th>helicopter</th>\n",
       "      <th>offroad_vehicle</th>\n",
       "      <th>other</th>\n",
       "      <th>car</th>\n",
       "      <th>india</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2473</th>\n",
       "      <td>intermediate</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>M</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>other</td>\n",
       "      <td>other</td>\n",
       "      <td>75.0</td>\n",
       "      <td>Italia</td>\n",
       "      <td>None</td>\n",
       "      <td>...</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2517</th>\n",
       "      <td>intermediate</td>\n",
       "      <td>collision_person</td>\n",
       "      <td>M</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>other</td>\n",
       "      <td>None</td>\n",
       "      <td>47.0</td>\n",
       "      <td>Slovenia</td>\n",
       "      <td>None</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15824</th>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>M</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>other</td>\n",
       "      <td>None</td>\n",
       "      <td>62.0</td>\n",
       "      <td>Italia</td>\n",
       "      <td>None</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3159</th>\n",
       "      <td>intermediate</td>\n",
       "      <td>illness</td>\n",
       "      <td>M</td>\n",
       "      <td>ski</td>\n",
       "      <td>False</td>\n",
       "      <td>other</td>\n",
       "      <td>other</td>\n",
       "      <td>68.0</td>\n",
       "      <td>Repubblica Ceca</td>\n",
       "      <td>None</td>\n",
       "      <td>...</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10356</th>\n",
       "      <td>None</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>M</td>\n",
       "      <td>None</td>\n",
       "      <td>True</td>\n",
       "      <td>other</td>\n",
       "      <td>other</td>\n",
       "      <td>21.0</td>\n",
       "      <td>Italia</td>\n",
       "      <td>None</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11525</th>\n",
       "      <td>advanced</td>\n",
       "      <td>other</td>\n",
       "      <td>M</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>other</td>\n",
       "      <td>other</td>\n",
       "      <td>81.0</td>\n",
       "      <td>Italia</td>\n",
       "      <td>None</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6 rows × 25 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         difficulty             cause gender equipment helmet destination  \\\n",
       "2473   intermediate        fall_alone      M       ski   True       other   \n",
       "2517   intermediate  collision_person      M       ski   True       other   \n",
       "15824          None              None      M      None   None       other   \n",
       "3159   intermediate           illness      M       ski  False       other   \n",
       "10356          None        fall_alone      M      None   True       other   \n",
       "11525      advanced             other      M       ski   True       other   \n",
       "\n",
       "      diagnosis   age          country injury_side  ... snowmobile_sled  \\\n",
       "2473      other  75.0           Italia        None  ...            True   \n",
       "2517       None  47.0         Slovenia        None  ...           False   \n",
       "15824      None  62.0           Italia        None  ...           False   \n",
       "3159      other  68.0  Repubblica Ceca        None  ...            True   \n",
       "10356     other  21.0           Italia        None  ...           False   \n",
       "11525     other  81.0           Italia        None  ...           False   \n",
       "\n",
       "        akja privat_helicopter snowmobile skiarea_ambulance helicopter  \\\n",
       "2473   False             False      False              True      False   \n",
       "2517   False             False       True             False      False   \n",
       "15824  False             False      False             False       True   \n",
       "3159   False             False      False             False      False   \n",
       "10356  False             False       True             False       True   \n",
       "11525   True             False      False             False      False   \n",
       "\n",
       "      offroad_vehicle  other    car india  \n",
       "2473            False  False  False     4  \n",
       "2517            False  False  False     4  \n",
       "15824           False  False  False     4  \n",
       "3159            False  False  False     4  \n",
       "10356           False  False  False     4  \n",
       "11525           False  False  False     4  \n",
       "\n",
       "[6 rows x 25 columns]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx[xx.india==4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ceb5b308-1260-4426-ad05-4251bb45c5a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#############Check inference part\n",
    "import os\n",
    "with open('../src/data.pkl','rb') as f:\n",
    "    df = pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f35925ae-f963-4028-b949-9eceffedb9be",
   "metadata": {},
   "outputs": [
    {
     "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>dateandtime</th>\n",
       "      <th>skiarea_id</th>\n",
       "      <th>skiarea_name</th>\n",
       "      <th>day_of_year</th>\n",
       "      <th>minute_of_day</th>\n",
       "      <th>year</th>\n",
       "      <th>season</th>\n",
       "      <th>difficulty</th>\n",
       "      <th>cause</th>\n",
       "      <th>town</th>\n",
       "      <th>...</th>\n",
       "      <th>equipment</th>\n",
       "      <th>helmet</th>\n",
       "      <th>destination</th>\n",
       "      <th>diagnosis</th>\n",
       "      <th>india</th>\n",
       "      <th>age</th>\n",
       "      <th>country</th>\n",
       "      <th>injury_side</th>\n",
       "      <th>injury_general_location</th>\n",
       "      <th>evacuation_vehicles</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2009-01-12 10:02:00+00:00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Pampeago</td>\n",
       "      <td>12</td>\n",
       "      <td>602</td>\n",
       "      <td>2009</td>\n",
       "      <td>2009</td>\n",
       "      <td>novice</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>SIKLOS</td>\n",
       "      <td>...</td>\n",
       "      <td>ski</td>\n",
       "      <td>None</td>\n",
       "      <td>hospital_emergency_room</td>\n",
       "      <td>distortion</td>\n",
       "      <td>None</td>\n",
       "      <td>32.0</td>\n",
       "      <td>Ungheria</td>\n",
       "      <td>L</td>\n",
       "      <td>lower_limbs</td>\n",
       "      <td>[akja]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2009-01-12 11:02:00+00:00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Pampeago</td>\n",
       "      <td>12</td>\n",
       "      <td>662</td>\n",
       "      <td>2009</td>\n",
       "      <td>2009</td>\n",
       "      <td>advanced</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>MALMO</td>\n",
       "      <td>...</td>\n",
       "      <td>ski</td>\n",
       "      <td>None</td>\n",
       "      <td>hospital_emergency_room</td>\n",
       "      <td>bruise</td>\n",
       "      <td>None</td>\n",
       "      <td>32.0</td>\n",
       "      <td>Svezia</td>\n",
       "      <td>R</td>\n",
       "      <td>skull_or_face</td>\n",
       "      <td>[akja]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2009-01-14 14:15:00+00:00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Pampeago</td>\n",
       "      <td>14</td>\n",
       "      <td>855</td>\n",
       "      <td>2009</td>\n",
       "      <td>2009</td>\n",
       "      <td>advanced</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>CALDARO</td>\n",
       "      <td>...</td>\n",
       "      <td>ski</td>\n",
       "      <td>None</td>\n",
       "      <td>domicile</td>\n",
       "      <td>other</td>\n",
       "      <td>None</td>\n",
       "      <td>12.0</td>\n",
       "      <td>Italia</td>\n",
       "      <td>R</td>\n",
       "      <td>None</td>\n",
       "      <td>[snowmobile]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2009-01-16 10:00:00+00:00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Pampeago</td>\n",
       "      <td>16</td>\n",
       "      <td>600</td>\n",
       "      <td>2009</td>\n",
       "      <td>2009</td>\n",
       "      <td>advanced</td>\n",
       "      <td>collision_person</td>\n",
       "      <td>LINZ</td>\n",
       "      <td>...</td>\n",
       "      <td>ski</td>\n",
       "      <td>None</td>\n",
       "      <td>hospital_emergency_room</td>\n",
       "      <td>bruise</td>\n",
       "      <td>None</td>\n",
       "      <td>58.0</td>\n",
       "      <td>Austria</td>\n",
       "      <td>R</td>\n",
       "      <td>lower_limbs</td>\n",
       "      <td>[snowmobile]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2009-01-16 10:00:00+00:00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Pampeago</td>\n",
       "      <td>16</td>\n",
       "      <td>600</td>\n",
       "      <td>2009</td>\n",
       "      <td>2009</td>\n",
       "      <td>advanced</td>\n",
       "      <td>collision_person</td>\n",
       "      <td>RUSAVA</td>\n",
       "      <td>...</td>\n",
       "      <td>ski</td>\n",
       "      <td>None</td>\n",
       "      <td>other</td>\n",
       "      <td>bruise</td>\n",
       "      <td>None</td>\n",
       "      <td>25.0</td>\n",
       "      <td>Repubblica Ceca</td>\n",
       "      <td>L</td>\n",
       "      <td>lower_limbs</td>\n",
       "      <td>[other]</td>\n",
       "    </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",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>150102</th>\n",
       "      <td>2024-02-04 13:35:00+00:00</td>\n",
       "      <td>76.0</td>\n",
       "      <td>Speikboden</td>\n",
       "      <td>35</td>\n",
       "      <td>815</td>\n",
       "      <td>2024</td>\n",
       "      <td>2024</td>\n",
       "      <td>intermediate</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>Dettmannsdorf OT Dettmannsdorf - Kölzow</td>\n",
       "      <td>...</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>hospital_emergency_room</td>\n",
       "      <td>other</td>\n",
       "      <td>i2</td>\n",
       "      <td>37.0</td>\n",
       "      <td>Germania</td>\n",
       "      <td>L</td>\n",
       "      <td>lower_limbs</td>\n",
       "      <td>[snowmobile_sled, helicopter]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>150103</th>\n",
       "      <td>2024-01-27 10:50:00+00:00</td>\n",
       "      <td>84.0</td>\n",
       "      <td>Bardonecchia</td>\n",
       "      <td>27</td>\n",
       "      <td>650</td>\n",
       "      <td>2024</td>\n",
       "      <td>2024</td>\n",
       "      <td>easy</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>Milano</td>\n",
       "      <td>...</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>None</td>\n",
       "      <td>wound</td>\n",
       "      <td>None</td>\n",
       "      <td>33.0</td>\n",
       "      <td>Italia</td>\n",
       "      <td>L</td>\n",
       "      <td>upper_limbs</td>\n",
       "      <td>[indipendently]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>150104</th>\n",
       "      <td>2024-02-04 14:52:00+00:00</td>\n",
       "      <td>77.0</td>\n",
       "      <td>Klausberg</td>\n",
       "      <td>35</td>\n",
       "      <td>892</td>\n",
       "      <td>2024</td>\n",
       "      <td>2024</td>\n",
       "      <td>easy</td>\n",
       "      <td>collision_person</td>\n",
       "      <td>Düsseldorf</td>\n",
       "      <td>...</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>traumacenter</td>\n",
       "      <td>other</td>\n",
       "      <td>i2</td>\n",
       "      <td>66.0</td>\n",
       "      <td>Germania</td>\n",
       "      <td>L</td>\n",
       "      <td>upper_limbs</td>\n",
       "      <td>[car, ski_lift, snowmobile]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>150105</th>\n",
       "      <td>2024-02-04 08:04:00+00:00</td>\n",
       "      <td>61.0</td>\n",
       "      <td>Moena Lusia</td>\n",
       "      <td>35</td>\n",
       "      <td>484</td>\n",
       "      <td>2024</td>\n",
       "      <td>2024</td>\n",
       "      <td>intermediate</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>Rimini</td>\n",
       "      <td>...</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>domicile</td>\n",
       "      <td>bruise</td>\n",
       "      <td>None</td>\n",
       "      <td>14.0</td>\n",
       "      <td>Italia</td>\n",
       "      <td>R</td>\n",
       "      <td>lower_limbs</td>\n",
       "      <td>[ski_lift, snowmobile]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>150106</th>\n",
       "      <td>2024-02-04 15:40:00+00:00</td>\n",
       "      <td>65.0</td>\n",
       "      <td>Porta Vescovo - Arabba</td>\n",
       "      <td>35</td>\n",
       "      <td>940</td>\n",
       "      <td>2024</td>\n",
       "      <td>2024</td>\n",
       "      <td>intermediate</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>Busto Garolfo</td>\n",
       "      <td>...</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>domicile</td>\n",
       "      <td>distortion</td>\n",
       "      <td>None</td>\n",
       "      <td>58.0</td>\n",
       "      <td>Italia</td>\n",
       "      <td>L</td>\n",
       "      <td>lower_limbs</td>\n",
       "      <td>[akja]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150107 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                      dateandtime  skiarea_id            skiarea_name  \\\n",
       "0       2009-01-12 10:02:00+00:00         NaN                Pampeago   \n",
       "1       2009-01-12 11:02:00+00:00         NaN                Pampeago   \n",
       "2       2009-01-14 14:15:00+00:00         NaN                Pampeago   \n",
       "3       2009-01-16 10:00:00+00:00         NaN                Pampeago   \n",
       "4       2009-01-16 10:00:00+00:00         NaN                Pampeago   \n",
       "...                           ...         ...                     ...   \n",
       "150102  2024-02-04 13:35:00+00:00        76.0              Speikboden   \n",
       "150103  2024-01-27 10:50:00+00:00        84.0            Bardonecchia   \n",
       "150104  2024-02-04 14:52:00+00:00        77.0               Klausberg   \n",
       "150105  2024-02-04 08:04:00+00:00        61.0             Moena Lusia   \n",
       "150106  2024-02-04 15:40:00+00:00        65.0  Porta Vescovo - Arabba   \n",
       "\n",
       "        day_of_year  minute_of_day  year  season    difficulty  \\\n",
       "0                12            602  2009    2009        novice   \n",
       "1                12            662  2009    2009      advanced   \n",
       "2                14            855  2009    2009      advanced   \n",
       "3                16            600  2009    2009      advanced   \n",
       "4                16            600  2009    2009      advanced   \n",
       "...             ...            ...   ...     ...           ...   \n",
       "150102           35            815  2024    2024  intermediate   \n",
       "150103           27            650  2024    2024          easy   \n",
       "150104           35            892  2024    2024          easy   \n",
       "150105           35            484  2024    2024  intermediate   \n",
       "150106           35            940  2024    2024  intermediate   \n",
       "\n",
       "                   cause                                     town  ...  \\\n",
       "0             fall_alone                                   SIKLOS  ...   \n",
       "1             fall_alone                                    MALMO  ...   \n",
       "2             fall_alone                                  CALDARO  ...   \n",
       "3       collision_person                                     LINZ  ...   \n",
       "4       collision_person                                   RUSAVA  ...   \n",
       "...                  ...                                      ...  ...   \n",
       "150102        fall_alone  Dettmannsdorf OT Dettmannsdorf - Kölzow  ...   \n",
       "150103        fall_alone                                   Milano  ...   \n",
       "150104  collision_person                               Düsseldorf  ...   \n",
       "150105        fall_alone                                   Rimini  ...   \n",
       "150106        fall_alone                            Busto Garolfo  ...   \n",
       "\n",
       "       equipment helmet              destination   diagnosis india   age  \\\n",
       "0            ski   None  hospital_emergency_room  distortion  None  32.0   \n",
       "1            ski   None  hospital_emergency_room      bruise  None  32.0   \n",
       "2            ski   None                 domicile       other  None  12.0   \n",
       "3            ski   None  hospital_emergency_room      bruise  None  58.0   \n",
       "4            ski   None                    other      bruise  None  25.0   \n",
       "...          ...    ...                      ...         ...   ...   ...   \n",
       "150102       ski   True  hospital_emergency_room       other    i2  37.0   \n",
       "150103       ski   True                     None       wound  None  33.0   \n",
       "150104       ski   True             traumacenter       other    i2  66.0   \n",
       "150105       ski   True                 domicile      bruise  None  14.0   \n",
       "150106       ski   True                 domicile  distortion  None  58.0   \n",
       "\n",
       "                country  injury_side injury_general_location  \\\n",
       "0              Ungheria            L             lower_limbs   \n",
       "1                Svezia            R           skull_or_face   \n",
       "2                Italia            R                    None   \n",
       "3               Austria            R             lower_limbs   \n",
       "4       Repubblica Ceca            L             lower_limbs   \n",
       "...                 ...          ...                     ...   \n",
       "150102         Germania            L             lower_limbs   \n",
       "150103           Italia            L             upper_limbs   \n",
       "150104         Germania            L             upper_limbs   \n",
       "150105           Italia            R             lower_limbs   \n",
       "150106           Italia            L             lower_limbs   \n",
       "\n",
       "                  evacuation_vehicles  \n",
       "0                              [akja]  \n",
       "1                              [akja]  \n",
       "2                        [snowmobile]  \n",
       "3                        [snowmobile]  \n",
       "4                             [other]  \n",
       "...                               ...  \n",
       "150102  [snowmobile_sled, helicopter]  \n",
       "150103                [indipendently]  \n",
       "150104    [car, ski_lift, snowmobile]  \n",
       "150105         [ski_lift, snowmobile]  \n",
       "150106                         [akja]  \n",
       "\n",
       "[150107 rows x 22 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "9e564cb2-e87c-4579-a8c3-4ec8c78c80ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fd68dd8dbb0>]"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 16972 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(np.power(dataset.weight_train,2.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "20c8703e-d784-48cb-804a-030a652c4c3a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['akja']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.evacuation_vehicles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "773bedfc-2dee-4e12-ba76-2e59d2afd909",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "skiarea_name                    Kronplatz\n",
       "season                               2018\n",
       "difficulty                           easy\n",
       "cause                          fall_alone\n",
       "town                            Piottello\n",
       "province                           Milano\n",
       "gender                                  F\n",
       "equipment                             ski\n",
       "helmet                               True\n",
       "destination                private_clinic\n",
       "diagnosis                      distortion\n",
       "india                                  i1\n",
       "age                                  45.0\n",
       "country                            Italia\n",
       "injury_side                             R\n",
       "injury_general_location       lower_limbs\n",
       "evacuation_vehicles          [snowmobile]\n",
       "Name: 13031, dtype: object"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[~pd.isnull(df.india)].iloc[123]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "03aa0bd6-19fd-4c79-bcd2-5576fa346be5",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "with open('../service/app/metadata.pkl','rb') as f:\n",
    "    to_remove,use_small,evacuations,encoders = pickle.load(f)\n",
    "    \n",
    "    \n",
    "i=123\n",
    "df.drop(columns=['dateandtime','skiarea_id','day_of_year','minute_of_day','year'], inplace=True, errors='ignore')\n",
    "data = df[~pd.isnull(df.india)].iloc[i:i+1]\n",
    "evacuation = data.evacuation_vehicles.values[0]\n",
    "if isinstance(evacuation,str):\n",
    "    evacuation = [evacuation]\n",
    "for c in evacuations:\n",
    "    data[c] = False\n",
    "for c in evacuation:\n",
    "    data[c] = True\n",
    "\n",
    "for c in evacuation:\n",
    "    if c not in evacuations:\n",
    "        data['other'] = True\n",
    "        break\n",
    "\n",
    "data.drop(columns=['town','province','evacuation_vehicles'],inplace=True, errors='ignore')\n",
    "\n",
    "\n",
    "data['age'] =  data['age'].astype(np.float32)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "for c in data.columns:\n",
    "    if c not in ['india','age','season','skiarea_name','destination']:\n",
    "        data[c] = data[c].astype('str')  \n",
    "if use_small:\n",
    "    for c in to_remove.keys():\n",
    "        for k in to_remove[c]:\n",
    "            data.loc[data[c]==k,c] = 'other'\n",
    "for c in data.columns:\n",
    "    if c not in ['age','season','skiarea_name','india']:\n",
    "        data[c] =  data[c].fillna('None')\n",
    "        if use_small:\n",
    "            data[c] = pd.Categorical( encoders['small'][c].transform(data[c]), categories=list(range(len(encoders['small'][c].classes_))), ordered=False)\n",
    "        else:\n",
    "            data[c] = pd.Categorical( encoders['normal'][c].transform(data[c]), categories=list(range(len(encoders['normal'][c].classes_))), ordered=False)\n",
    "         \n",
    "\n",
    "bst_FS = xgb.Booster()\n",
    "bst_FS.load_model(\"../service/app/model.json\")\n",
    "with open('../service/app/best_params_and_final_model.pkl','rb') as f:\n",
    "    tot,bst_FS,FS = pickle.load(f)  \n",
    "    \n",
    "dtest_FS = xgb.DMatrix(data[bst_FS.feature_names],enable_categorical=True)\n",
    "preds = bst_FS.predict(dtest_FS)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "8f64bbfe-9b66-45a0-abcb-dbe5d7782334",
   "metadata": {},
   "outputs": [
    {
     "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>skiarea_name</th>\n",
       "      <th>season</th>\n",
       "      <th>difficulty</th>\n",
       "      <th>cause</th>\n",
       "      <th>gender</th>\n",
       "      <th>equipment</th>\n",
       "      <th>helmet</th>\n",
       "      <th>destination</th>\n",
       "      <th>diagnosis</th>\n",
       "      <th>india</th>\n",
       "      <th>...</th>\n",
       "      <th>ski_lift</th>\n",
       "      <th>ambulance</th>\n",
       "      <th>indipendently</th>\n",
       "      <th>snowmobile</th>\n",
       "      <th>akja</th>\n",
       "      <th>offroad_vehicle</th>\n",
       "      <th>other</th>\n",
       "      <th>snowmobile_sled</th>\n",
       "      <th>car</th>\n",
       "      <th>quad</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>13031</th>\n",
       "      <td>Kronplatz</td>\n",
       "      <td>2018</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>9</td>\n",
       "      <td>i1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      skiarea_name  season difficulty cause gender equipment helmet  \\\n",
       "13031    Kronplatz    2018          2     3      1         2      2   \n",
       "\n",
       "      destination diagnosis india  ...  ski_lift ambulance indipendently  \\\n",
       "13031           6         9    i1  ...         0         0             0   \n",
       "\n",
       "      snowmobile akja offroad_vehicle other snowmobile_sled car quad  \n",
       "13031          1    0               0     0               0   0    0  \n",
       "\n",
       "[1 rows x 27 columns]"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "02e77ff0-546a-4de7-891c-7fe809e007ff",
   "metadata": {},
   "outputs": [
    {
     "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>age</th>\n",
       "      <th>diagnosis</th>\n",
       "      <th>injury_general_location</th>\n",
       "      <th>difficulty</th>\n",
       "      <th>country</th>\n",
       "      <th>destination</th>\n",
       "      <th>cause</th>\n",
       "      <th>helicopter</th>\n",
       "      <th>injury_side</th>\n",
       "      <th>equipment</th>\n",
       "      <th>gender</th>\n",
       "      <th>ambulance</th>\n",
       "      <th>snowmobile</th>\n",
       "      <th>akja</th>\n",
       "      <th>indipendently</th>\n",
       "      <th>snowmobile_sled</th>\n",
       "      <th>helmet</th>\n",
       "      <th>ski_lift</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>13031</th>\n",
       "      <td>45.0</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        age diagnosis injury_general_location difficulty country destination  \\\n",
       "13031  45.0         9                       1          2       7           6   \n",
       "\n",
       "      cause helicopter injury_side equipment gender ambulance snowmobile akja  \\\n",
       "13031     3          0           2         2      1         0          1    0   \n",
       "\n",
       "      indipendently snowmobile_sled helmet ski_lift  \n",
       "13031             0               0      2        0  "
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[bst_FS.feature_names]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "5856226f-6776-42c4-a618-5460eb1211eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "skiarea_name                    Kronplatz\n",
       "season                               2018\n",
       "difficulty                           easy\n",
       "cause                          fall_alone\n",
       "town                            Piottello\n",
       "province                           Milano\n",
       "gender                                  F\n",
       "equipment                             ski\n",
       "helmet                               True\n",
       "destination                private_clinic\n",
       "diagnosis                      distortion\n",
       "india                                  i1\n",
       "age                                  45.0\n",
       "country                            Italia\n",
       "injury_side                             R\n",
       "injury_general_location       lower_limbs\n",
       "evacuation_vehicles          [snowmobile]\n",
       "Name: 13031, dtype: object"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " df[~pd.isnull(df.india)].iloc[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "ea0a39c9-06ba-43e4-96c0-cb76713a0bae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "skiarea_name               Kronplatz\n",
       "season                          2018\n",
       "difficulty                         2\n",
       "cause                              3\n",
       "gender                             1\n",
       "equipment                          2\n",
       "helmet                             2\n",
       "destination                        6\n",
       "diagnosis                          9\n",
       "india                             i1\n",
       "age                             45.0\n",
       "country                            7\n",
       "injury_side                        2\n",
       "injury_general_location            1\n",
       "privat_helicopter                  0\n",
       "helicopter                         0\n",
       "skiarea_ambulance                  0\n",
       "ski_lift                           0\n",
       "ambulance                          0\n",
       "indipendently                      0\n",
       "snowmobile                         1\n",
       "akja                               0\n",
       "offroad_vehicle                    0\n",
       "other                              0\n",
       "snowmobile_sled                    0\n",
       "car                                0\n",
       "quad                               0\n",
       "Name: 13031, dtype: object"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.iloc[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "57be1bf4-4716-44db-b5c2-ab97efe0f92e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.00761119, 0.718736  , 0.2629358 , 0.00976286, 0.00095411]],\n",
       "      dtype=float32)"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "da8d0b45-0b61-4c78-b9cb-06e17ca155b3",
   "metadata": {},
   "outputs": [
    {
     "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>skiarea_name</th>\n",
       "      <th>season</th>\n",
       "      <th>difficulty</th>\n",
       "      <th>cause</th>\n",
       "      <th>gender</th>\n",
       "      <th>equipment</th>\n",
       "      <th>helmet</th>\n",
       "      <th>destination</th>\n",
       "      <th>diagnosis</th>\n",
       "      <th>india</th>\n",
       "      <th>...</th>\n",
       "      <th>skiarea_ambulance</th>\n",
       "      <th>privat_helicopter</th>\n",
       "      <th>other</th>\n",
       "      <th>ski_lift</th>\n",
       "      <th>akja</th>\n",
       "      <th>snowmobile_sled</th>\n",
       "      <th>car</th>\n",
       "      <th>quad</th>\n",
       "      <th>snowmobile</th>\n",
       "      <th>offroad_vehicle</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Kronplatz</td>\n",
       "      <td>2018</td>\n",
       "      <td>None</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>F</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>hospital_emergency_room</td>\n",
       "      <td>distortion</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Kronplatz</td>\n",
       "      <td>2018</td>\n",
       "      <td>easy</td>\n",
       "      <td>illness</td>\n",
       "      <td>M</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>domicile</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Kronplatz</td>\n",
       "      <td>2018</td>\n",
       "      <td>advanced</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>M</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>hospital_emergency_room</td>\n",
       "      <td>wound</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Kronplatz</td>\n",
       "      <td>2018</td>\n",
       "      <td>advanced</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>M</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>hospital_emergency_room</td>\n",
       "      <td>fracture</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Kronplatz</td>\n",
       "      <td>2018</td>\n",
       "      <td>intermediate</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>M</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>hospital_emergency_room</td>\n",
       "      <td>other</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </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",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18414</th>\n",
       "      <td>Drei Zinnen</td>\n",
       "      <td>2024</td>\n",
       "      <td>intermediate</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>M</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>hospital_emergency_room</td>\n",
       "      <td>dislocation</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18415</th>\n",
       "      <td>Obereggen</td>\n",
       "      <td>2024</td>\n",
       "      <td>intermediate</td>\n",
       "      <td>collision_person</td>\n",
       "      <td>M</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>hospital_emergency_room</td>\n",
       "      <td>trauma_crane</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18416</th>\n",
       "      <td>Speikboden</td>\n",
       "      <td>2024</td>\n",
       "      <td>intermediate</td>\n",
       "      <td>collision_person</td>\n",
       "      <td>M</td>\n",
       "      <td>snowboard</td>\n",
       "      <td>True</td>\n",
       "      <td>hospital_emergency_room</td>\n",
       "      <td>concussion</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18417</th>\n",
       "      <td>Speikboden</td>\n",
       "      <td>2024</td>\n",
       "      <td>intermediate</td>\n",
       "      <td>fall_alone</td>\n",
       "      <td>F</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>hospital_emergency_room</td>\n",
       "      <td>other</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18418</th>\n",
       "      <td>Klausberg</td>\n",
       "      <td>2024</td>\n",
       "      <td>easy</td>\n",
       "      <td>collision_person</td>\n",
       "      <td>F</td>\n",
       "      <td>ski</td>\n",
       "      <td>True</td>\n",
       "      <td>traumacenter</td>\n",
       "      <td>other</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>18330 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      skiarea_name  season    difficulty             cause gender  equipment  \\\n",
       "0        Kronplatz    2018          None        fall_alone      F        ski   \n",
       "1        Kronplatz    2018          easy           illness      M        ski   \n",
       "2        Kronplatz    2018      advanced        fall_alone      M        ski   \n",
       "3        Kronplatz    2018      advanced        fall_alone      M        ski   \n",
       "4        Kronplatz    2018  intermediate        fall_alone      M        ski   \n",
       "...            ...     ...           ...               ...    ...        ...   \n",
       "18414  Drei Zinnen    2024  intermediate        fall_alone      M        ski   \n",
       "18415    Obereggen    2024  intermediate  collision_person      M        ski   \n",
       "18416   Speikboden    2024  intermediate  collision_person      M  snowboard   \n",
       "18417   Speikboden    2024  intermediate        fall_alone      F        ski   \n",
       "18418    Klausberg    2024          easy  collision_person      F        ski   \n",
       "\n",
       "      helmet              destination     diagnosis  india  ...  \\\n",
       "0       True  hospital_emergency_room    distortion      1  ...   \n",
       "1       True                 domicile         other      0  ...   \n",
       "2       True  hospital_emergency_room         wound      1  ...   \n",
       "3       True  hospital_emergency_room      fracture      1  ...   \n",
       "4       True  hospital_emergency_room         other      1  ...   \n",
       "...      ...                      ...           ...    ...  ...   \n",
       "18414   True  hospital_emergency_room   dislocation      2  ...   \n",
       "18415   True  hospital_emergency_room  trauma_crane      1  ...   \n",
       "18416   True  hospital_emergency_room    concussion      2  ...   \n",
       "18417   True  hospital_emergency_room         other      2  ...   \n",
       "18418   True             traumacenter         other      2  ...   \n",
       "\n",
       "       skiarea_ambulance privat_helicopter  other ski_lift   akja  \\\n",
       "0                  False             False  False    False  False   \n",
       "1                  False             False  False     True  False   \n",
       "2                  False             False  False    False   True   \n",
       "3                  False             False  False    False   True   \n",
       "4                  False             False  False    False   True   \n",
       "...                  ...               ...    ...      ...    ...   \n",
       "18414              False             False  False    False  False   \n",
       "18415              False             False  False    False  False   \n",
       "18416              False             False  False    False  False   \n",
       "18417              False             False  False    False  False   \n",
       "18418              False             False  False     True  False   \n",
       "\n",
       "      snowmobile_sled    car   quad snowmobile offroad_vehicle  \n",
       "0               False  False  False       True           False  \n",
       "1               False  False  False      False           False  \n",
       "2               False  False  False      False           False  \n",
       "3               False  False  False      False           False  \n",
       "4               False  False  False      False           False  \n",
       "...               ...    ...    ...        ...             ...  \n",
       "18414            True  False  False      False           False  \n",
       "18415           False  False  False       True           False  \n",
       "18416           False  False  False      False           False  \n",
       "18417            True  False  False      False           False  \n",
       "18418           False   True  False       True           False  \n",
       "\n",
       "[18330 rows x 27 columns]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "labeled"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "edcd37a4-50de-4e43-a97d-c2ec1cf13c29",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import LabelEncoder\n",
    "le = LabelEncoder().fit([1,2,3,'a',1,2,3,'a'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "1b8dd296-ae02-4034-a583-ed1ea02b9ded",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.DataFrame({'a':[1,2,3,'a',1,2,3,'a']})\n",
    "data['a'] = data['a'].astype('str')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "c140eb7c-69aa-4aca-995a-606b0033b966",
   "metadata": {},
   "outputs": [
    {
     "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>a</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a\n",
       "0  1\n",
       "1  2\n",
       "2  3\n",
       "3  a\n",
       "4  1\n",
       "5  2\n",
       "6  3\n",
       "7  a"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "c05dd944-999e-4b63-bb8a-1225077ca33a",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "985a74fa-ba86-4319-b5cc-0bb41c66d8d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = pd.DataFrame(le.transform(data.a)).astype('category')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "df27a1b8-f549-45ef-a5c9-bd4d572062f7",
   "metadata": {},
   "outputs": [
    {
     "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>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0\n",
       "0  0\n",
       "1  1\n",
       "2  2\n",
       "3  3\n",
       "4  0\n",
       "5  1\n",
       "6  2\n",
       "7  3"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "961e9514-c225-4c05-b09a-e685125cd8c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 0, 1: 1, 2: 2, 3: 3}"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict( enumerate(a[0].cat.categories ) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "04696091-f61e-4206-8f85-d1adeb8d9c7d",
   "metadata": {},
   "outputs": [
    {
     "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>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0\n",
       "0  3\n",
       "1  0"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = pd.DataFrame({'a':['a',1]})\n",
    "c = pd.DataFrame(le.transform(b.a.astype(str))).astype('category')\n",
    "c\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "eb00f49c-1ca2-414e-beef-a91f618ce5ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 0, 1: 3}"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict( enumerate(c[0].cat.categories ) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "67fdc3ff-3e49-4489-8c89-3050af9036b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 0]\n",
       "Categories (4, int64): [0, 1, 2, 3]"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Categorical(\n",
    "    le.transform(b.a), categories=[0,1,2,3], ordered=False\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "301e8ae3-0cbe-47f0-b67b-a90cd974a30c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['1', '2', '3', 'a'], dtype='<U21')"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "le.classes_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "943ced39-9bc1-4202-9441-87222232e9a5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
