{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "051c5726",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from matplotlib import pyplot as plt\n",
    "import numpy as np\n",
    "from wordcloud import WordCloud\n",
    "\n",
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.model_selection import KFold, StratifiedShuffleSplit\n",
    "from sklearn.metrics import confusion_matrix\n",
    "from sklearn.metrics import ConfusionMatrixDisplay\n",
    "\n",
    "import re\n",
    "import string\n",
    "from stop_words import get_stop_words\n",
    "\n",
    "from imblearn.over_sampling import RandomOverSampler\n",
    "\n",
    "import spacy\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "310c031d",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.set_option(\"display.max_colwidth\", 255)\n",
    "pd.set_option('display.max_rows', 255)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "cc6821d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(\"training-nltk-mapping3.xlsx\")\n",
    "cnt = df.groupby(\"CASISTICA_MOTIVAZIONE\").count() \\\n",
    ".rename(columns={\"Unnamed: 0\": \"count\"})[\"count\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "id": "764a318c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CASISTICA_MOTIVAZIONE\n",
       "Doc. sanitaria scaduta                             1\n",
       "Esenzione per ultratrentennale                     1\n",
       "Variazioni tecniche                                1\n",
       "Rientro ecoincentivo                               2\n",
       "Eredi                                              3\n",
       "Rateizzazione                                      3\n",
       "Domiciliazione assente                             3\n",
       "Esenzione successiva                               5\n",
       "Esenzione PH successiva                            6\n",
       "Ecoincentivo                                       7\n",
       "Versamento annullato                               7\n",
       "CRS intestata a soggetto diverso                   8\n",
       "No minivoltura                                    10\n",
       "Esenzione PH eredi                                10\n",
       "Perdita possesso con DS successiva                13\n",
       "Domiciliazione tardiva                            16\n",
       "Rientro in possesso                               18\n",
       "Sequestro non annotato                            24\n",
       "Demolizione                                       30\n",
       "Generica 2_non allegata documentazione            39\n",
       "No esenzione PH                                   43\n",
       "Veicolo diverso                                   46\n",
       "Acquisto nel mese di rinnovo; privato             49\n",
       "Acquisto nel mese di rinnovo; concessionario      53\n",
       "Fermo                                             56\n",
       "Esportazione non annotata                         65\n",
       "Ricevuta non valida                               68\n",
       "Regione diversa                                   71\n",
       "Esenzione PH senza istanza                        91\n",
       "Furto non annotato                               116\n",
       "Vendita tardiva                                  257\n",
       "Periodo diverso                                  831\n",
       "Generica                                        2763\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cnt.sort_values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "1c3d567b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CASISTICA_MOTIVAZIONE                         CASISTICA_LAVORAZIONE                           \n",
       "Acquisto nel mese di rinnovo; concessionario  DEMOLIZIONE/ESPORTAZIONE TARDIVA                       1\n",
       "                                              VERS. PRESENTE/VARIATO/INSERITO CONGRUO                1\n",
       "                                              ATTO DI VENDITA TARDIVO                                2\n",
       "                                              GIACENZA RIVENDITORI                                   2\n",
       "                                              GIACENZA RIVENDITORI CON PARZIALE COPERTURA            2\n",
       "                                              ESENZIONE CONCESSIONARIO TARDIVA                      18\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     27\n",
       "Acquisto nel mese di rinnovo; privato         DEMOLIZIONE/ESPORTAZIONE TARDIVA                       1\n",
       "                                              GIACENZA RIVENDITORI CON PARZIALE COPERTURA            1\n",
       "                                              ATTO DI VENDITA TARDIVO                                7\n",
       "                                              ESENZIONE CONCESSIONARIO TARDIVA                      10\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     30\n",
       "CRS intestata a soggetto diverso              STORICO ESENTE                                         1\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                      3\n",
       "                                              VEICOLO NON RICONOSCIUTO STORICO                       4\n",
       "Demolizione                                   ATTO DI VENDITA INCOMPLETO                             1\n",
       "                                              ATTO DI VENDITA TARDIVO                                1\n",
       "                                              FURTO/SEQUESTRO/CONFISCA/PIGNORAMENTO                  1\n",
       "                                              PERDITA DI POSS. TARDIVA (FURTO, SEQUESTRO, EC.)       2\n",
       "                                              VERSAMENTO NON PRESENTE                                2\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     11\n",
       "                                              DEMOLIZIONE/ESPORTAZIONE TARDIVA                      12\n",
       "Doc. sanitaria scaduta                        ESENZIONE DISABILE RESPINTA                            1\n",
       "Domiciliazione assente                        NESSUNA O INIDONEA DOCUMENTAZIONE                      3\n",
       "Domiciliazione tardiva                        ATTO DI VENDITA TARDIVO                                1\n",
       "                                              VERSAMENTO NON PRESENTE                                2\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     13\n",
       "Ecoincentivo                                  NESSUNA O INIDONEA DOCUMENTAZIONE                      7\n",
       "Eredi                                         ACCETTAZIONE EREDITA'                                  1\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                      2\n",
       "Esenzione PH eredi                            VERSAMENTO NON PRESENTE                                1\n",
       "                                              ESENZIONE DISABILE RESPINTA                            3\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                      6\n",
       "Esenzione PH senza istanza                    PRESA IN CARICO                                        1\n",
       "                                              VERSAMENTO NON PRESENTE                                3\n",
       "                                              ESENZIONE DISABILE RESPINTA                           23\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     64\n",
       "Esenzione PH successiva                       VEICOLO NON RICONOSCIUTO STORICO                       1\n",
       "                                              STORICO ESENTE                                         2\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                      3\n",
       "Esenzione per ultratrentennale                VEICOLO NON RICONOSCIUTO STORICO                       1\n",
       "Esenzione successiva                          NESSUNA O INIDONEA DOCUMENTAZIONE                      1\n",
       "                                              STORICO ESENTE                                         1\n",
       "                                              VEICOLO NON RICONOSCIUTO STORICO                       1\n",
       "                                              ESENZIONE DISABILE RESPINTA                            2\n",
       "Esportazione non annotata                     ATTO DI VENDITA INCOMPLETO                             1\n",
       "                                              DEMOLIZ/ESPORTAZ ALL'ESTERO                            1\n",
       "                                              DEMOLIZIONE/ESPORTAZIONE TARDIVA                       1\n",
       "                                              VERSAMENTO NON PRESENTE                                1\n",
       "                                              DEMOLIZIONE/ESPORTAZIONE NON VALIDA                   11\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     50\n",
       "Fermo                                         ESENZIONE DISABILE RESPINTA                            1\n",
       "                                              FURTO/SEQUESTRO/CONFISCA/PIGNORAMENTO                  1\n",
       "                                              PRESA IN CARICO                                        1\n",
       "                                              VERS. NON PRESENTE ACI/TABACCAIO/AGENZIA               1\n",
       "                                              DEMOLIZIONE/ESPORTAZIONE NON VALIDA                    4\n",
       "                                              VERSAMENTO NON PRESENTE                                6\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     42\n",
       "Furto non annotato                            PRESA IN CARICO                                        1\n",
       "                                              VERSAMENTO NON PRESENTE                                1\n",
       "                                              PERDITA DI POSS. TARDIVA (FURTO, SEQUESTRO, EC.)       4\n",
       "                                              FURTO/SEQUESTRO/CONFISCA/PIGNORAMENTO                  5\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                    105\n",
       "Generica                                      ANNULLAMENTO REGIONALE                                 1\n",
       "                                              FURTO/SEQUESTRO/CONFISCA/PIGNORAMENTO                  1\n",
       "                                              GIACENZA RIVENDITORI CON PARZIALE COPERTURA            1\n",
       "                                              RESIDENTE IN ALTRA REGIONE                             1\n",
       "                                              VERS.VARIATO/INSUFFICIENTE/TARDIVO                     1\n",
       "                                              ATTO DI VENDITA/OMONIMIA/OMOCODIA                      3\n",
       "                                              MAI STATO PROPRIETARIO                                 3\n",
       "                                              PERIODICITA' ERRATA IN ARCHIVIO                        3\n",
       "                                              DATI FISCALI ERRATI                                    4\n",
       "                                              SERVIZI ESENTI                                         4\n",
       "                                              VERS. PRESENTE/VARIATO/INSERITO CONGRUO                4\n",
       "                                              DEMOLIZ/ESPORTAZ ALL'ESTERO                            5\n",
       "                                              DISABILE ESENTE                                        5\n",
       "                                              ESENZIONE CONCESSIONARIO INCOMPLETA                    6\n",
       "                                              GIACENZA RIVENDITORI                                   6\n",
       "                                              PRESA IN CARICO                                        7\n",
       "                                              ACCETTAZIONE EREDITA'                                  8\n",
       "                                              PERDITA DI POSS. PER DICH. SOST. TARDIVA               8\n",
       "                                              STORICO ESENTE                                         8\n",
       "                                              RILIEVO PAGATO                                         9\n",
       "                                              VERSAM PRESENTE NON CONGRUO                           10\n",
       "                                              PERDITA DI POSS. TARDIVA (FURTO, SEQUESTRO, EC.)      11\n",
       "                                              VERS. NON PRESENTE ACI/TABACCAIO/AGENZIA              17\n",
       "                                              ESENZIONE CONCESSIONARIO TARDIVA                      39\n",
       "                                              DEMOLIZIONE/ESPORTAZIONE NON VALIDA                   42\n",
       "                                              DEMOLIZIONE/ESPORTAZIONE TARDIVA                      51\n",
       "                                              ATTO DI VENDITA TARDIVO                               54\n",
       "                                              ESENZIONE DISABILE RESPINTA                           59\n",
       "                                              ATTO DI VENDITA INCOMPLETO                            66\n",
       "                                              VEICOLO NON RICONOSCIUTO STORICO                      96\n",
       "                                              VERSAMENTO NON PRESENTE                              116\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                   2114\n",
       "Generica 2_non allegata documentazione        ATTO DI VENDITA INCOMPLETO                             1\n",
       "                                              ESENZIONE DISABILE RESPINTA                            1\n",
       "                                              VEICOLO NON RICONOSCIUTO STORICO                       1\n",
       "                                              DEMOLIZIONE/ESPORTAZIONE NON VALIDA                    2\n",
       "                                              VERSAMENTO NON PRESENTE                                2\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     32\n",
       "No esenzione PH                               MAI STATO PROPRIETARIO                                 1\n",
       "                                              VERSAMENTO NON PRESENTE                                1\n",
       "                                              DISABILE ESENTE                                        2\n",
       "                                              ESENZIONE DISABILE RESPINTA                           13\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     26\n",
       "No minivoltura                                ATTO DI VENDITA TARDIVO                                1\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                      4\n",
       "                                              ESENZIONE CONCESSIONARIO INCOMPLETA                    5\n",
       "Perdita possesso con DS successiva            VERSAMENTO NON PRESENTE                                1\n",
       "                                              PERDITA DI POSS. PER DICH. SOST. TARDIVA               3\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                      9\n",
       "Periodo diverso                               VERS. PRESENTE/VARIATO/INSERITO CONGRUO                1\n",
       "                                              DEMOLIZIONE/ESPORTAZIONE TARDIVA                       2\n",
       "                                              MAI STATO PROPRIETARIO                                 2\n",
       "                                              VEICOLO NON RICONOSCIUTO STORICO                       3\n",
       "                                              ATTO DI VENDITA TARDIVO                                6\n",
       "                                              VERSAM PRESENTE NON CONGRUO                           27\n",
       "                                              VERS. NON PRESENTE ACI/TABACCAIO/AGENZIA              29\n",
       "                                              VERSAMENTO NON PRESENTE                               73\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                    688\n",
       "Rateizzazione                                 NESSUNA O INIDONEA DOCUMENTAZIONE                      3\n",
       "Regione diversa                               VERSAMENTO NON PRESENTE                                2\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     69\n",
       "Ricevuta non valida                           PRESA IN CARICO                                        1\n",
       "                                              VERSAMENTO NON PRESENTE                                4\n",
       "                                              VERS. NON PRESENTE ACI/TABACCAIO/AGENZIA               6\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     57\n",
       "Rientro ecoincentivo                          NESSUNA O INIDONEA DOCUMENTAZIONE                      2\n",
       "Rientro in possesso                           VERSAMENTO NON PRESENTE                                1\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     17\n",
       "Sequestro non annotato                        ANNULLAMENTO REGIONALE                                 1\n",
       "                                              FURTO/SEQUESTRO/CONFISCA/PIGNORAMENTO                  2\n",
       "                                              PRESA IN CARICO                                        2\n",
       "                                              VERSAMENTO NON PRESENTE                                2\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     17\n",
       "Variazioni tecniche                           NESSUNA O INIDONEA DOCUMENTAZIONE                      1\n",
       "Veicolo diverso                               ATTO DI VENDITA TARDIVO                                1\n",
       "                                              VERS. NON PRESENTE ACI/TABACCAIO/AGENZIA               1\n",
       "                                              VERSAM PRESENTE NON CONGRUO                            3\n",
       "                                              VERSAMENTO NON PRESENTE                                5\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     36\n",
       "Vendita tardiva                               ESENZIONE CONCESSIONARIO TARDIVA                       1\n",
       "                                              ATTO DI VENDITA/OMONIMIA/OMOCODIA                      2\n",
       "                                              ATTO DI VENDITA INCOMPLETO                             5\n",
       "                                              VERS. NON PRESENTE ACI/TABACCAIO/AGENZIA               5\n",
       "                                              VERSAMENTO NON PRESENTE                               31\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                     66\n",
       "                                              ATTO DI VENDITA TARDIVO                              147\n",
       "Versamento annullato                          VERSAM PRESENTE NON CONGRUO                            1\n",
       "                                              NESSUNA O INIDONEA DOCUMENTAZIONE                      6\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby([ \"CASISTICA_MOTIVAZIONE\",\"CASISTICA_LAVORAZIONE\"]).count() \\\n",
    ".sort_values(by=[\"CASISTICA_MOTIVAZIONE\", \"Unnamed: 0\"]) \\\n",
    ".rename(columns={\"Unnamed: 0\": \"count\"})[\"count\"] \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "703a76e6",
   "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>Unnamed: 0</th>\n",
       "      <th>NUMERO_DOCUMENTO</th>\n",
       "      <th>CASISTICA_LAVORAZIONE</th>\n",
       "      <th>NOTE_OPERATORE</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CASISTICA_MOTIVAZIONE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Acquisto nel mese di rinnovo; concessionario</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Acquisto nel mese di rinnovo; privato</th>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Demolizione</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Domiciliazione tardiva</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Generica</th>\n",
       "      <td>54</td>\n",
       "      <td>54</td>\n",
       "      <td>54</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>No minivoltura</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Periodo diverso</th>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Veicolo diverso</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Vendita tardiva</th>\n",
       "      <td>147</td>\n",
       "      <td>147</td>\n",
       "      <td>147</td>\n",
       "      <td>147</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                              Unnamed: 0  NUMERO_DOCUMENTO  \\\n",
       "CASISTICA_MOTIVAZIONE                                                        \n",
       "Acquisto nel mese di rinnovo; concessionario           2                 2   \n",
       "Acquisto nel mese di rinnovo; privato                  7                 7   \n",
       "Demolizione                                            1                 1   \n",
       "Domiciliazione tardiva                                 1                 1   \n",
       "Generica                                              54                54   \n",
       "No minivoltura                                         1                 1   \n",
       "Periodo diverso                                        6                 6   \n",
       "Veicolo diverso                                        1                 1   \n",
       "Vendita tardiva                                      147               147   \n",
       "\n",
       "                                              CASISTICA_LAVORAZIONE  \\\n",
       "CASISTICA_MOTIVAZIONE                                                 \n",
       "Acquisto nel mese di rinnovo; concessionario                      2   \n",
       "Acquisto nel mese di rinnovo; privato                             7   \n",
       "Demolizione                                                       1   \n",
       "Domiciliazione tardiva                                            1   \n",
       "Generica                                                         54   \n",
       "No minivoltura                                                    1   \n",
       "Periodo diverso                                                   6   \n",
       "Veicolo diverso                                                   1   \n",
       "Vendita tardiva                                                 147   \n",
       "\n",
       "                                              NOTE_OPERATORE  \n",
       "CASISTICA_MOTIVAZIONE                                         \n",
       "Acquisto nel mese di rinnovo; concessionario               2  \n",
       "Acquisto nel mese di rinnovo; privato                      7  \n",
       "Demolizione                                                1  \n",
       "Domiciliazione tardiva                                     1  \n",
       "Generica                                                  54  \n",
       "No minivoltura                                             1  \n",
       "Periodo diverso                                            6  \n",
       "Veicolo diverso                                            1  \n",
       "Vendita tardiva                                          147  "
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df[\"CASISTICA_LAVORAZIONE\"]==\"ATTO DI VENDITA TARDIVO\"].groupby(\"CASISTICA_MOTIVAZIONE\").count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "1ecac684",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Axes: >"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 11592 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df['NOTE_OPERATORE'].str.len().hist(range=[0, 255])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "7201abdc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Axes: >"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 10612 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df['NOTE_OPERATORE'].str.split().map(lambda x: len(x)).hist(range=[0, 45])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "f5ac30f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 89268 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "cnt.plot.barh();\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "82de2ab3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Unnamed: 0',\n",
       " 'NUMERO_DOCUMENTO',\n",
       " 'CASISTICA_LAVORAZIONE',\n",
       " 'NOTE_OPERATORE',\n",
       " 'CASISTICA_MOTIVAZIONE']"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns.values.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "59464e01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0                                           Generica\n",
       "9                                    Periodo diverso\n",
       "13                                   Veicolo diverso\n",
       "17                                Furto non annotato\n",
       "19                                             Fermo\n",
       "23                                   Vendita tardiva\n",
       "25                               Ricevuta non valida\n",
       "30                         Esportazione non annotata\n",
       "86             Acquisto nel mese di rinnovo; privato\n",
       "150                                      Demolizione\n",
       "152                           Sequestro non annotato\n",
       "154                Esenzione PH o storica successiva\n",
       "160                                    Furto tardivo\n",
       "161                                     Ecoincentivo\n",
       "176                                  Regione diversa\n",
       "179                       Esenzione PH senza istanza\n",
       "183                                   No minivoltura\n",
       "979                             Versamento annullato\n",
       "1610                                 No esenzione PH\n",
       "1619          Generica 2_non allegata documentazione\n",
       "1621                  Esenzione per ultratrentennale\n",
       "1638    Acquisto nel mese di rinnovo; concessionario\n",
       "1668                          Domiciliazione assente\n",
       "1707                              Esenzione PH eredi\n",
       "1817                          Domiciliazione tardiva\n",
       "2163                         Perdita possesso con DS\n",
       "2393                             Rientro in possesso\n",
       "3336                                   Rateizzazione\n",
       "3582                                         Storico\n",
       "3606                            Rientro ecoincentivo\n",
       "3610                            Esenzione successiva\n",
       "3620                                           EREDI\n",
       "3652                          Doc. sanitaria scaduta\n",
       "3678                             Variazioni tecniche\n",
       "Name: CASISTICA_MOTIVAZIONE, dtype: object"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"CASISTICA_MOTIVAZIONE\"].drop_duplicates()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "83c84464",
   "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>Unnamed: 0</th>\n",
       "      <th>NUMERO_DOCUMENTO</th>\n",
       "      <th>CASISTICA_LAVORAZIONE</th>\n",
       "      <th>NOTE_OPERATORE</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CASISTICA_MOTIVAZIONE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Acquisto nel mese di rinnovo; privato</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Demolizione</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Domiciliazione tardiva</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>EREDI</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Esportazione non annotata</th>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Fermo</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Furto non annotato</th>\n",
       "      <td>44</td>\n",
       "      <td>44</td>\n",
       "      <td>44</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Generica</th>\n",
       "      <td>118</td>\n",
       "      <td>118</td>\n",
       "      <td>118</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Generica 2_non allegata documentazione</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>No minivoltura</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Perdita possesso con DS</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Regione diversa</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Rientro in possesso</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Sequestro non annotato</th>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Vendita tardiva</th>\n",
       "      <td>29</td>\n",
       "      <td>29</td>\n",
       "      <td>29</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        Unnamed: 0  NUMERO_DOCUMENTO  \\\n",
       "CASISTICA_MOTIVAZIONE                                                  \n",
       "Acquisto nel mese di rinnovo; privato            5                 5   \n",
       "Demolizione                                      2                 2   \n",
       "Domiciliazione tardiva                           1                 1   \n",
       "EREDI                                            1                 1   \n",
       "Esportazione non annotata                       21                21   \n",
       "Fermo                                            2                 2   \n",
       "Furto non annotato                              44                44   \n",
       "Generica                                       118               118   \n",
       "Generica 2_non allegata documentazione           3                 3   \n",
       "No minivoltura                                   1                 1   \n",
       "Perdita possesso con DS                          1                 1   \n",
       "Regione diversa                                  1                 1   \n",
       "Rientro in possesso                              2                 2   \n",
       "Sequestro non annotato                           7                 7   \n",
       "Vendita tardiva                                 29                29   \n",
       "\n",
       "                                        CASISTICA_LAVORAZIONE  NOTE_OPERATORE  \n",
       "CASISTICA_MOTIVAZIONE                                                          \n",
       "Acquisto nel mese di rinnovo; privato                       5               5  \n",
       "Demolizione                                                 2               2  \n",
       "Domiciliazione tardiva                                      1               1  \n",
       "EREDI                                                       1               1  \n",
       "Esportazione non annotata                                  21              21  \n",
       "Fermo                                                       2               2  \n",
       "Furto non annotato                                         44              44  \n",
       "Generica                                                  118             118  \n",
       "Generica 2_non allegata documentazione                      3               3  \n",
       "No minivoltura                                              1               1  \n",
       "Perdita possesso con DS                                     1               1  \n",
       "Regione diversa                                             1               1  \n",
       "Rientro in possesso                                         2               2  \n",
       "Sequestro non annotato                                      7               7  \n",
       "Vendita tardiva                                            29              29  "
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df['NOTE_OPERATORE'].str.contains(\"pra \")].groupby(\"CASISTICA_MOTIVAZIONE\").count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "1441fcbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Count: 7\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 163856 characters]",
      "text/plain": [
       "<Figure size 1000x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from wordcloud import WordCloud\n",
    "class_name = \"Versamento annullato\"\n",
    "df2 = df[df[\"CASISTICA_MOTIVAZIONE\"] == class_name]\n",
    "print(\"Count: {}\".format(df2.shape[0]))\n",
    "wordcloud2 = WordCloud().generate(' '.join(df2['NOTE_OPERATORE'].str.lower()))\n",
    "\n",
    "plt.figure(figsize=(10, 5))\n",
    "plt.imshow(wordcloud2, interpolation='bilinear')\n",
    "plt.axis('off')  \n",
    "plt.title(class_name)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "id": "b4a24d73",
   "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>Unnamed: 0</th>\n",
       "      <th>NUMERO_DOCUMENTO</th>\n",
       "      <th>CASISTICA_LAVORAZIONE</th>\n",
       "      <th>NOTE_OPERATORE</th>\n",
       "      <th>CASISTICA_MOTIVAZIONE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>22425</td>\n",
       "      <td>838113553209</td>\n",
       "      <td>NESSUNA O INIDONEA DOCUMENTAZIONE</td>\n",
       "      <td>allegato bollo anno 2017x000d rilavorata esito</td>\n",
       "      <td>Periodo diverso</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>22433</td>\n",
       "      <td>838218932793</td>\n",
       "      <td>VERSAM PRESENTE NON CONGRUO</td>\n",
       "      <td>allega ricevuta annualità successiva VERSAM PRESENTE NON CONGRUO</td>\n",
       "      <td>Periodo diverso</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>22439</td>\n",
       "      <td>68596972278</td>\n",
       "      <td>NESSUNA O INIDONEA DOCUMENTAZIONE</td>\n",
       "      <td>allega versamento tg targa</td>\n",
       "      <td>Veicolo diverso</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>22443</td>\n",
       "      <td>838240147404</td>\n",
       "      <td>NESSUNA O INIDONEA DOCUMENTAZIONE</td>\n",
       "      <td>inviata ricevuta pto periodo</td>\n",
       "      <td>Periodo diverso</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>22447</td>\n",
       "      <td>838083706814</td>\n",
       "      <td>NESSUNA O INIDONEA DOCUMENTAZIONE</td>\n",
       "      <td>presentata pra perdita possesso ds</td>\n",
       "      <td>Furto non annotato</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4701</th>\n",
       "      <td>44688</td>\n",
       "      <td>68299875830</td>\n",
       "      <td>NESSUNA O INIDONEA DOCUMENTAZIONE</td>\n",
       "      <td>versamento allegato anno manca</td>\n",
       "      <td>Periodo diverso</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4702</th>\n",
       "      <td>44690</td>\n",
       "      <td>738203345465</td>\n",
       "      <td>ATTO DI VENDITA TARDIVO</td>\n",
       "      <td>venduta dopo scadenza ATTO DI VENDITA TARDIVO</td>\n",
       "      <td>Vendita tardiva</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4704</th>\n",
       "      <td>44694</td>\n",
       "      <td>68328887621</td>\n",
       "      <td>NESSUNA O INIDONEA DOCUMENTAZIONE</td>\n",
       "      <td>pagamento allegato ricevuta</td>\n",
       "      <td>Ricevuta non valida</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4705</th>\n",
       "      <td>44696</td>\n",
       "      <td>68308840246</td>\n",
       "      <td>NESSUNA O INIDONEA DOCUMENTAZIONE</td>\n",
       "      <td>versamenti allegati manca</td>\n",
       "      <td>Periodo diverso</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4706</th>\n",
       "      <td>44697</td>\n",
       "      <td>68326176065</td>\n",
       "      <td>ATTO DI VENDITA TARDIVO</td>\n",
       "      <td>mezzo venduto dopo scadenza ATTO DI VENDITA TARDIVO</td>\n",
       "      <td>Vendita tardiva</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1953 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Unnamed: 0  NUMERO_DOCUMENTO              CASISTICA_LAVORAZIONE  \\\n",
       "8          22425      838113553209  NESSUNA O INIDONEA DOCUMENTAZIONE   \n",
       "9          22433      838218932793        VERSAM PRESENTE NON CONGRUO   \n",
       "12         22439       68596972278  NESSUNA O INIDONEA DOCUMENTAZIONE   \n",
       "14         22443      838240147404  NESSUNA O INIDONEA DOCUMENTAZIONE   \n",
       "16         22447      838083706814  NESSUNA O INIDONEA DOCUMENTAZIONE   \n",
       "...          ...               ...                                ...   \n",
       "4701       44688       68299875830  NESSUNA O INIDONEA DOCUMENTAZIONE   \n",
       "4702       44690      738203345465            ATTO DI VENDITA TARDIVO   \n",
       "4704       44694       68328887621  NESSUNA O INIDONEA DOCUMENTAZIONE   \n",
       "4705       44696       68308840246  NESSUNA O INIDONEA DOCUMENTAZIONE   \n",
       "4706       44697       68326176065            ATTO DI VENDITA TARDIVO   \n",
       "\n",
       "                                                        NOTE_OPERATORE  \\\n",
       "8                       allegato bollo anno 2017x000d rilavorata esito   \n",
       "9     allega ricevuta annualità successiva VERSAM PRESENTE NON CONGRUO   \n",
       "12                                          allega versamento tg targa   \n",
       "14                                        inviata ricevuta pto periodo   \n",
       "16                                  presentata pra perdita possesso ds   \n",
       "...                                                                ...   \n",
       "4701                                    versamento allegato anno manca   \n",
       "4702                     venduta dopo scadenza ATTO DI VENDITA TARDIVO   \n",
       "4704                                       pagamento allegato ricevuta   \n",
       "4705                                         versamenti allegati manca   \n",
       "4706               mezzo venduto dopo scadenza ATTO DI VENDITA TARDIVO   \n",
       "\n",
       "     CASISTICA_MOTIVAZIONE  \n",
       "8          Periodo diverso  \n",
       "9          Periodo diverso  \n",
       "12         Veicolo diverso  \n",
       "14         Periodo diverso  \n",
       "16      Furto non annotato  \n",
       "...                    ...  \n",
       "4701       Periodo diverso  \n",
       "4702       Vendita tardiva  \n",
       "4704   Ricevuta non valida  \n",
       "4705       Periodo diverso  \n",
       "4706       Vendita tardiva  \n",
       "\n",
       "[1953 rows x 5 columns]"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df[\"CASISTICA_MOTIVAZIONE\"] != \"Generica\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1411257",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "649397c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def replace_punct(text):\n",
    "    text = ''.join([char for char in text if char not in string.punctuation])\n",
    "    return text\n",
    "\n",
    "def remove_stopwords(text):\n",
    "    ita_stop_words = get_stop_words('italian')\n",
    "    pattern = r'\\b(?:' + '|'.join(re.escape(s) for s in ita_stop_words) + r')\\b'\n",
    "    return re.sub(pattern, '', text)\n",
    "\n",
    "\n",
    "def prune_infrequent_classes(input_df: pd.DataFrame):\n",
    "    card_thresh = 10\n",
    "    cnt = input_df[[\"CASISTICA_MOTIVAZIONE\",\"NOTE_OPERATORE\"]].groupby([\"CASISTICA_MOTIVAZIONE\"]).count().rename({\"NOTE_OPERATORE\":\"count\"},axis=1)\n",
    "    res = input_df.merge(cnt, on=\"CASISTICA_MOTIVAZIONE\")\n",
    "    res = res[res[\"count\"] >= card_thresh]\n",
    "    res = res.drop(\"count\", axis=1)\n",
    "    return res\n",
    "\n",
    "nlp = spacy.load(\"it_core_news_sm\")\n",
    "def lemmatize(text):\n",
    "    doc = nlp(text)\n",
    "    lemmi = \" \".join([token.lemma_ for token in doc])\n",
    "    return lemmi\n",
    "\n",
    "\n",
    "#df = prune_infrequent_classes(df)\n",
    "\n",
    "\n",
    "#df[\"C\"] = df.apply(lambda row: row[\"CASISTICA_LAVORAZIONE\"] in row[\"NOTE_OPERATORE\"]   , axis=1)\n",
    "#df[\"X\"] =np.where(df[\"C\"], df[\"NOTE_OPERATORE\"],df[\"NOTE_OPERATORE\"] + \" \" + df[\"CASISTICA_LAVORAZIONE\"])\n",
    "#df[\"X\"] = df[\"NOTE_OPERATORE\"]  if df[\"C\"].bool else df[\"NOTE_OPERATORE\"] + \" \" + df[\"CASISTICA_LAVORAZIONE\"]\n",
    "#df.drop(\"C\", axis=1)\n",
    "\n",
    "\n",
    "df[\"X\"] = df[\"NOTE_OPERATORE\"]\n",
    "df[\"X\"] = df[\"X\"].str.replace(\"x000d\", \" \")\n",
    "#df[\"X\"] = df[\"X\"].apply(lambda x: remove_stopwords(x))\n",
    "#df[\"X\"] = df[\"X\"].apply(lambda x: replace_punct(x))\n",
    "df[\"X\"] = df[\"X\"].apply(lambda x: lemmatize(x))\n",
    "\n",
    "df[\"X\"] = df[\"X\"].str.replace(\"/\", \" \", regex=True) \n",
    "df[\"X\"] = df[\"X\"].str.replace(r\"\\s+\", \" \", regex=True) \n",
    "#df[\"X\"] = df[\"X\"].str.replace({\"à\":\"a\", \"è\":\"e\", \"ì\":\"i\", \"ò\":\"o\", \"ù\":\"u\"}, regex=True) \n",
    "df[\"X\"] = df[\"X\"].str.lower()\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "e5d6d2a8",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "The {'Furto tardivo'} target class is/are not present in the data.",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mValueError\u001b[39m                                Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[153]\u001b[39m\u001b[32m, line 12\u001b[39m\n\u001b[32m      9\u001b[39m oversample = RandomOverSampler(sampling_strategy=class_dict)\n\u001b[32m     11\u001b[39m df_nonGenerica = df[df[\u001b[33m\"\u001b[39m\u001b[33mCASISTICA_MOTIVAZIONE\u001b[39m\u001b[33m\"\u001b[39m] != \u001b[33m\"\u001b[39m\u001b[33mGenerica\u001b[39m\u001b[33m\"\u001b[39m]\n\u001b[32m---> \u001b[39m\u001b[32m12\u001b[39m X_over, y_over = \u001b[43moversample\u001b[49m\u001b[43m.\u001b[49m\u001b[43mfit_resample\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdf_nonGenerica\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mNOTE_OPERATORE\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m.\u001b[49m\u001b[43mto_numpy\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m.\u001b[49m\u001b[43mreshape\u001b[49m\u001b[43m(\u001b[49m\u001b[43m-\u001b[49m\u001b[32;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[32;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdf_nonGenerica\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mCASISTICA_MOTIVAZIONE\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m.\u001b[49m\u001b[43mto_numpy\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m     13\u001b[39m df_over = pd.DataFrame({\u001b[33m\"\u001b[39m\u001b[33mNOTE_OPERATORE\u001b[39m\u001b[33m\"\u001b[39m:X_over[:,\u001b[32m0\u001b[39m], \u001b[33m\"\u001b[39m\u001b[33mCASISTICA_MOTIVAZIONE\u001b[39m\u001b[33m\"\u001b[39m:y_over})\n\u001b[32m     16\u001b[39m sss = StratifiedShuffleSplit(n_splits=\u001b[32m1\u001b[39m, test_size=\u001b[32m0.3\u001b[39m, random_state=\u001b[32m0\u001b[39m)\n",
      "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\a.torchi\\Desktop\\text_classification\\venv\\Lib\\site-packages\\imblearn\\base.py:202\u001b[39m, in \u001b[36mBaseSampler.fit_resample\u001b[39m\u001b[34m(self, X, y, **params)\u001b[39m\n\u001b[32m    181\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mfit_resample\u001b[39m(\u001b[38;5;28mself\u001b[39m, X, y, **params):\n\u001b[32m    182\u001b[39m \u001b[38;5;250m    \u001b[39m\u001b[33;03m\"\"\"Resample the dataset.\u001b[39;00m\n\u001b[32m    183\u001b[39m \n\u001b[32m    184\u001b[39m \u001b[33;03m    Parameters\u001b[39;00m\n\u001b[32m   (...)\u001b[39m\u001b[32m    200\u001b[39m \u001b[33;03m        The corresponding label of `X_resampled`.\u001b[39;00m\n\u001b[32m    201\u001b[39m \u001b[33;03m    \"\"\"\u001b[39;00m\n\u001b[32m--> \u001b[39m\u001b[32m202\u001b[39m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m.\u001b[49m\u001b[43mfit_resample\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mparams\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\a.torchi\\Desktop\\text_classification\\venv\\Lib\\site-packages\\sklearn\\base.py:1389\u001b[39m, in \u001b[36m_fit_context.<locals>.decorator.<locals>.wrapper\u001b[39m\u001b[34m(estimator, *args, **kwargs)\u001b[39m\n\u001b[32m   1382\u001b[39m     estimator._validate_params()\n\u001b[32m   1384\u001b[39m \u001b[38;5;28;01mwith\u001b[39;00m config_context(\n\u001b[32m   1385\u001b[39m     skip_parameter_validation=(\n\u001b[32m   1386\u001b[39m         prefer_skip_nested_validation \u001b[38;5;129;01mor\u001b[39;00m global_skip_validation\n\u001b[32m   1387\u001b[39m     )\n\u001b[32m   1388\u001b[39m ):\n\u001b[32m-> \u001b[39m\u001b[32m1389\u001b[39m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfit_method\u001b[49m\u001b[43m(\u001b[49m\u001b[43mestimator\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\a.torchi\\Desktop\\text_classification\\venv\\Lib\\site-packages\\imblearn\\base.py:101\u001b[39m, in \u001b[36mSamplerMixin.fit_resample\u001b[39m\u001b[34m(self, X, y, **params)\u001b[39m\n\u001b[32m     98\u001b[39m arrays_transformer = ArraysTransformer(X, y)\n\u001b[32m     99\u001b[39m X, y, binarize_y = \u001b[38;5;28mself\u001b[39m._check_X_y(X, y)\n\u001b[32m--> \u001b[39m\u001b[32m101\u001b[39m \u001b[38;5;28mself\u001b[39m.sampling_strategy_ = \u001b[43mcheck_sampling_strategy\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m    102\u001b[39m \u001b[43m    \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43msampling_strategy\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_sampling_type\u001b[49m\n\u001b[32m    103\u001b[39m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m    105\u001b[39m output = \u001b[38;5;28mself\u001b[39m._fit_resample(X, y, **params)\n\u001b[32m    107\u001b[39m y_ = (\n\u001b[32m    108\u001b[39m     label_binarize(output[\u001b[32m1\u001b[39m], classes=np.unique(y)) \u001b[38;5;28;01mif\u001b[39;00m binarize_y \u001b[38;5;28;01melse\u001b[39;00m output[\u001b[32m1\u001b[39m]\n\u001b[32m    109\u001b[39m )\n",
      "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\a.torchi\\Desktop\\text_classification\\venv\\Lib\\site-packages\\imblearn\\utils\\_validation.py:557\u001b[39m, in \u001b[36mcheck_sampling_strategy\u001b[39m\u001b[34m(sampling_strategy, y, sampling_type, **kwargs)\u001b[39m\n\u001b[32m    552\u001b[39m     \u001b[38;5;28;01mreturn\u001b[39;00m OrderedDict(\n\u001b[32m    553\u001b[39m         \u001b[38;5;28msorted\u001b[39m(SAMPLING_TARGET_KIND[sampling_strategy](y, sampling_type).items())\n\u001b[32m    554\u001b[39m     )\n\u001b[32m    555\u001b[39m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(sampling_strategy, \u001b[38;5;28mdict\u001b[39m):\n\u001b[32m    556\u001b[39m     \u001b[38;5;28;01mreturn\u001b[39;00m OrderedDict(\n\u001b[32m--> \u001b[39m\u001b[32m557\u001b[39m         \u001b[38;5;28msorted\u001b[39m(\u001b[43m_sampling_strategy_dict\u001b[49m\u001b[43m(\u001b[49m\u001b[43msampling_strategy\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msampling_type\u001b[49m\u001b[43m)\u001b[49m.items())\n\u001b[32m    558\u001b[39m     )\n\u001b[32m    559\u001b[39m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(sampling_strategy, \u001b[38;5;28mlist\u001b[39m):\n\u001b[32m    560\u001b[39m     \u001b[38;5;28;01mreturn\u001b[39;00m OrderedDict(\n\u001b[32m    561\u001b[39m         \u001b[38;5;28msorted\u001b[39m(_sampling_strategy_list(sampling_strategy, y, sampling_type).items())\n\u001b[32m    562\u001b[39m     )\n",
      "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\a.torchi\\Desktop\\text_classification\\venv\\Lib\\site-packages\\imblearn\\utils\\_validation.py:319\u001b[39m, in \u001b[36m_sampling_strategy_dict\u001b[39m\u001b[34m(sampling_strategy, y, sampling_type)\u001b[39m\n\u001b[32m    315\u001b[39m set_diff_sampling_strategy_target = \u001b[38;5;28mset\u001b[39m(sampling_strategy.keys()) - \u001b[38;5;28mset\u001b[39m(\n\u001b[32m    316\u001b[39m     target_stats.keys()\n\u001b[32m    317\u001b[39m )\n\u001b[32m    318\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(set_diff_sampling_strategy_target) > \u001b[32m0\u001b[39m:\n\u001b[32m--> \u001b[39m\u001b[32m319\u001b[39m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[32m    320\u001b[39m         \u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mThe \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mset_diff_sampling_strategy_target\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m target class is/are not \u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m    321\u001b[39m         \u001b[33m\"\u001b[39m\u001b[33mpresent in the data.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m    322\u001b[39m     )\n\u001b[32m    323\u001b[39m \u001b[38;5;66;03m# check that there is no negative number\u001b[39;00m\n\u001b[32m    324\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28many\u001b[39m(n_samples < \u001b[32m0\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m n_samples \u001b[38;5;129;01min\u001b[39;00m sampling_strategy.values()):\n",
      "\u001b[31mValueError\u001b[39m: The {'Furto tardivo'} target class is/are not present in the data."
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import StratifiedShuffleSplit, StratifiedKFold\n",
    "\n",
    "\n",
    "class_dict = {\"Doc. sanitaria scaduta\" : 2, \n",
    "            \"Esenzione per ultratrentennale\" : 2, \n",
    "            \"Variazioni tecniche\" : 2, \n",
    "            \"Furto tardivo\" : 2\n",
    "            }\n",
    "oversample = RandomOverSampler(sampling_strategy=class_dict)\n",
    "\n",
    "df_nonGenerica = df[df[\"CASISTICA_MOTIVAZIONE\"] != \"Generica\"]\n",
    "X_over, y_over = oversample.fit_resample(df_nonGenerica[\"NOTE_OPERATORE\"].to_numpy().reshape(-1, 1), df_nonGenerica[\"CASISTICA_MOTIVAZIONE\"].to_numpy())\n",
    "df_over = pd.DataFrame({\"NOTE_OPERATORE\":X_over[:,0], \"CASISTICA_MOTIVAZIONE\":y_over})\n",
    "\n",
    "\n",
    "sss = StratifiedShuffleSplit(n_splits=1, test_size=0.3, random_state=0)\n",
    "\n",
    "#sss = StratifiedKFold(n_splits=2, shuffle=True, random_state=0)\n",
    "print(sss.get_n_splits(X_over,  y_over))\n",
    "\n",
    "for i, (train_index, test_index) in enumerate(sss.split(X_over,  y_over)):\n",
    "\n",
    "    print(f\"Fold {i}:\")\n",
    "    test_index_df = df_over.iloc[test_index]\n",
    "    train_index_df = df_over.iloc[train_index]\n",
    "\n",
    "    print(train_index.size)\n",
    "    print(test_index.size)\n",
    "\n",
    "    check_str = \"Rientro ecoincentivo\"\n",
    "    print(\"------TEST------\")\n",
    "    print(test_index_df[test_index_df[\"CASISTICA_MOTIVAZIONE\"] == check_str])\n",
    "    print(\"------TRAIN------\")\n",
    "    print(train_index_df[train_index_df[\"CASISTICA_MOTIVAZIONE\"] == check_str])\n",
    "\n",
    "    #print(df_over.iloc[test_index])\n",
    "    #print(f\"  Train: index={train_index}\")\n",
    "    #print(f\"  Test:  index={test_index}\")\n",
    "\n",
    "X_train = train_index_df[\"NOTE_OPERATORE\"]\n",
    "X_test = test_index_df[\"NOTE_OPERATORE\"]\n",
    "y_train = train_index_df[\"CASISTICA_MOTIVAZIONE\"]\n",
    "y_test = test_index_df[\"CASISTICA_MOTIVAZIONE\"]\n",
    "\n",
    "model_balanced = Pipeline([('tfidf', TfidfVectorizer()), ('clf', LogisticRegression(class_weight=\"balanced\"))])\n",
    "model_unbalanced = Pipeline([('tfidf', TfidfVectorizer()), ('clf', LogisticRegression(class_weight=None))])\n",
    "\n",
    "\n",
    "model_balanced.fit(X_train, y_train)\n",
    "model_unbalanced.fit(X_train, y_train)\n",
    "\n",
    "\n",
    "y_pred_balanced = model_balanced.predict(X_test)\n",
    "y_pred_unbalanced = model_unbalanced.predict(X_test)\n",
    "\n",
    "print(\"--------------------------\")\n",
    "print(\"BAL Accuracy:\", accuracy_score(y_test, y_pred_balanced))\n",
    "print(\"BAL F1-Score:\", f1_score(y_test, y_pred_balanced, average=None))\n",
    "print(\"--------------------------\")\n",
    "print(\"UNBAL Accuracy:\", accuracy_score(y_test, y_pred_unbalanced))\n",
    "print(\"UNBAL F1-Score:\", f1_score(y_test, y_pred_unbalanced, average=None))\n",
    "print(\"--------------------------\")\n",
    "\n",
    "f1_score_balanced = f1_score(y_test, y_pred_balanced, average=None)\n",
    "f1_score_unbalanced = f1_score(y_test, y_pred_unbalanced, average=None)\n",
    "\n",
    "f1_data = np.array([f1_score_balanced.tolist(), f1_score_unbalanced.tolist()])\n",
    "f1_score_df = pd.DataFrame(data=np.float64(f1_data[:,:]), columns=model_balanced.classes_, index = [\"f1_score_balanced\", \"f1_score_unbalanced\"])\n",
    "\n",
    "\n",
    "\n",
    "cm = confusion_matrix(y_test, y_pred_balanced, labels=model_balanced.classes_)\n",
    "\n",
    "disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model_balanced.classes_)\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "disp.plot(cmap=plt.cm.Blues, xticks_rotation=\"vertical\", ax=ax)\n",
    "plt.title('Confusion Matrix model_balanced', fontsize=15, pad=20)\n",
    "plt.xlabel('Prediction', fontsize=11)\n",
    "plt.ylabel('Actual', fontsize=11)\n",
    "plt.gca().xaxis.set_label_position('top')\n",
    "plt.gca().xaxis.tick_bottom()\n",
    "plt.gca().figure.subplots_adjust(bottom=0.2)\n",
    "plt.gca().figure.text(0.5, 0.05, 'Prediction', ha='center', fontsize=13)\n",
    "\n",
    "\n",
    "\n",
    "cm = confusion_matrix(y_test, y_pred_unbalanced, labels=model_unbalanced.classes_)\n",
    "\n",
    "disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model_unbalanced.classes_)\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "disp.plot(cmap=plt.cm.Blues, xticks_rotation=\"vertical\", ax=ax)\n",
    "plt.title('Confusion Matrix model_unbalanced', fontsize=15, pad=20)\n",
    "plt.xlabel('Prediction', fontsize=11)\n",
    "plt.ylabel('Actual', fontsize=11)\n",
    "plt.gca().xaxis.set_label_position('top')\n",
    "plt.gca().xaxis.tick_bottom()\n",
    "plt.gca().figure.subplots_adjust(bottom=0.2)\n",
    "plt.gca().figure.text(0.5, 0.05, 'Prediction', ha='center', fontsize=13)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "b4be4371",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.5588035350101972\n",
      "F1-Score: 0.5793193398671854\n",
      "F1-Score: [0.51162791 0.48275862 0.4137931  0.66666667 0.         0.44444444\n",
      " 0.5        0.8        0.         0.125      0.47169811 0.4\n",
      " 0.         0.47222222 0.8        0.76086957 0.5        0.53344623\n",
      " 0.02777778 0.20289855 0.4        0.32       0.80694981 0.\n",
      " 0.42696629 0.52307692 0.         0.52631579 0.4137931  0.\n",
      " 1.         0.44827586 0.71296296 0.4       ]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Acquisto nel mese di rinnovo; concessionario</th>\n",
       "      <th>Acquisto nel mese di rinnovo; privato</th>\n",
       "      <th>Demolizione</th>\n",
       "      <th>Doc. sanitaria scaduta</th>\n",
       "      <th>Domiciliazione assente</th>\n",
       "      <th>Domiciliazione tardiva</th>\n",
       "      <th>EREDI</th>\n",
       "      <th>Ecoincentivo</th>\n",
       "      <th>Esenzione PH eredi</th>\n",
       "      <th>Esenzione PH o storica successiva</th>\n",
       "      <th>Esenzione PH senza istanza</th>\n",
       "      <th>Esenzione per ultratrentennale</th>\n",
       "      <th>Esenzione successiva</th>\n",
       "      <th>Esportazione non annotata</th>\n",
       "      <th>Fermo</th>\n",
       "      <th>Furto non annotato</th>\n",
       "      <th>Furto tardivo</th>\n",
       "      <th>Generica</th>\n",
       "      <th>Generica 2_non allegata documentazione</th>\n",
       "      <th>No esenzione PH</th>\n",
       "      <th>No minivoltura</th>\n",
       "      <th>Perdita possesso con DS</th>\n",
       "      <th>Periodo diverso</th>\n",
       "      <th>Rateizzazione</th>\n",
       "      <th>Regione diversa</th>\n",
       "      <th>Ricevuta non valida</th>\n",
       "      <th>Rientro ecoincentivo</th>\n",
       "      <th>Rientro in possesso</th>\n",
       "      <th>Sequestro non annotato</th>\n",
       "      <th>Storico</th>\n",
       "      <th>Variazioni tecniche</th>\n",
       "      <th>Veicolo diverso</th>\n",
       "      <th>Vendita tardiva</th>\n",
       "      <th>Versamento annullato</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>f1_score_balanced</th>\n",
       "      <td>0.511628</td>\n",
       "      <td>0.482759</td>\n",
       "      <td>0.413793</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.444444</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.125</td>\n",
       "      <td>0.471698</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.472222</td>\n",
       "      <td>0.800000</td>\n",
       "      <td>0.760870</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.533446</td>\n",
       "      <td>0.027778</td>\n",
       "      <td>0.202899</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.32</td>\n",
       "      <td>0.806950</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.426966</td>\n",
       "      <td>0.523077</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.526316</td>\n",
       "      <td>0.413793</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.448276</td>\n",
       "      <td>0.712963</td>\n",
       "      <td>0.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>f1_score_unbalanced</th>\n",
       "      <td>0.210526</td>\n",
       "      <td>0.461538</td>\n",
       "      <td>0.142857</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.250000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.380952</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.266667</td>\n",
       "      <td>0.758621</td>\n",
       "      <td>0.777778</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.839428</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.835341</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.388889</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.600000</td>\n",
       "      <td>0.736196</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     Acquisto nel mese di rinnovo; concessionario  \\\n",
       "f1_score_balanced                                        0.511628   \n",
       "f1_score_unbalanced                                      0.210526   \n",
       "\n",
       "                     Acquisto nel mese di rinnovo; privato  Demolizione  \\\n",
       "f1_score_balanced                                 0.482759     0.413793   \n",
       "f1_score_unbalanced                               0.461538     0.142857   \n",
       "\n",
       "                     Doc. sanitaria scaduta  Domiciliazione assente  \\\n",
       "f1_score_balanced                  0.666667                     0.0   \n",
       "f1_score_unbalanced                0.000000                     0.0   \n",
       "\n",
       "                     Domiciliazione tardiva  EREDI  Ecoincentivo  \\\n",
       "f1_score_balanced                  0.444444    0.5           0.8   \n",
       "f1_score_unbalanced                0.250000    0.0           0.0   \n",
       "\n",
       "                     Esenzione PH eredi  Esenzione PH o storica successiva  \\\n",
       "f1_score_balanced                   0.0                              0.125   \n",
       "f1_score_unbalanced                 0.0                              0.000   \n",
       "\n",
       "                     Esenzione PH senza istanza  \\\n",
       "f1_score_balanced                      0.471698   \n",
       "f1_score_unbalanced                    0.380952   \n",
       "\n",
       "                     Esenzione per ultratrentennale  Esenzione successiva  \\\n",
       "f1_score_balanced                               0.4                   0.0   \n",
       "f1_score_unbalanced                             0.0                   0.0   \n",
       "\n",
       "                     Esportazione non annotata     Fermo  Furto non annotato  \\\n",
       "f1_score_balanced                     0.472222  0.800000            0.760870   \n",
       "f1_score_unbalanced                   0.266667  0.758621            0.777778   \n",
       "\n",
       "                     Furto tardivo  Generica  \\\n",
       "f1_score_balanced              0.5  0.533446   \n",
       "f1_score_unbalanced            0.0  0.839428   \n",
       "\n",
       "                     Generica 2_non allegata documentazione  No esenzione PH  \\\n",
       "f1_score_balanced                                  0.027778         0.202899   \n",
       "f1_score_unbalanced                                0.000000         0.125000   \n",
       "\n",
       "                     No minivoltura  Perdita possesso con DS  Periodo diverso  \\\n",
       "f1_score_balanced               0.4                     0.32         0.806950   \n",
       "f1_score_unbalanced             0.0                     0.00         0.835341   \n",
       "\n",
       "                     Rateizzazione  Regione diversa  Ricevuta non valida  \\\n",
       "f1_score_balanced              0.0         0.426966             0.523077   \n",
       "f1_score_unbalanced            0.0         0.388889             0.166667   \n",
       "\n",
       "                     Rientro ecoincentivo  Rientro in possesso  \\\n",
       "f1_score_balanced                     0.0             0.526316   \n",
       "f1_score_unbalanced                   0.0             0.000000   \n",
       "\n",
       "                     Sequestro non annotato  Storico  Variazioni tecniche  \\\n",
       "f1_score_balanced                  0.413793      0.0                  1.0   \n",
       "f1_score_unbalanced                0.000000      0.0                  0.0   \n",
       "\n",
       "                     Veicolo diverso  Vendita tardiva  Versamento annullato  \n",
       "f1_score_balanced           0.448276         0.712963                   0.4  \n",
       "f1_score_unbalanced         0.600000         0.736196                   0.0  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "Text(0.5, 0.05, 'Prediction')"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 243700 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 248220 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#X_train, X_test, y_train, y_test = train_test_split(df[\"NOTE_OPERATORE\"], df[\"CASISTICA_MOTIVAZIONE\"], test_size=0.2, random_state=42)\n",
    "\n",
    "\n",
    "class_dict = {\"Doc. sanitaria scaduta\" : 2, \n",
    "            \"Esenzione per ultratrentennale\" : 2, \n",
    "            \"Variazioni tecniche\" : 2, \n",
    "            \"Furto tardivo\" : 2\n",
    "            }\n",
    "oversample = RandomOverSampler(sampling_strategy=class_dict)\n",
    "\n",
    "X_over, y_over = oversample.fit_resample(df[\"NOTE_OPERATORE\"].to_numpy().reshape(-1, 1), df[\"CASISTICA_MOTIVAZIONE\"].to_numpy())\n",
    "df_over = pd.DataFrame({\"NOTE_OPERATORE\":X_over[:,0], \"CASISTICA_MOTIVAZIONE\":y_over})\n",
    "\n",
    "\n",
    "sss = StratifiedShuffleSplit(n_splits=1, test_size=0.3, random_state=0)\n",
    "\n",
    "for i, (train_index, test_index) in enumerate(sss.split(X_over,  y_over)):\n",
    "    test_index_df = df_over.iloc[test_index]\n",
    "    train_index_df = df_over.iloc[train_index]\n",
    "\n",
    "\n",
    "X_train = train_index_df[\"NOTE_OPERATORE\"]\n",
    "X_test = test_index_df[\"NOTE_OPERATORE\"]\n",
    "y_train = train_index_df[\"CASISTICA_MOTIVAZIONE\"]\n",
    "y_test = test_index_df[\"CASISTICA_MOTIVAZIONE\"]\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "model_balanced = Pipeline([('tfidf', TfidfVectorizer()), ('clf', LogisticRegression(class_weight=\"balanced\"))])\n",
    "model_unbalanced = Pipeline([('tfidf', TfidfVectorizer()), ('clf', LogisticRegression(class_weight=None))])\n",
    "\n",
    "\n",
    "model_balanced.fit(X_train, y_train)\n",
    "model_unbalanced.fit(X_train, y_train)\n",
    "\n",
    "\n",
    "y_pred_balanced = model_balanced.predict(X_test)\n",
    "y_pred_unbalanced = model_unbalanced.predict(X_test)\n",
    "\n",
    "print(\"Accuracy:\", accuracy_score(y_test, y_pred_balanced))\n",
    "print(\"F1-Score:\", f1_score(y_test, y_pred_balanced, average=\"weighted\"))\n",
    "print(\"F1-Score:\", f1_score(y_test, y_pred_balanced, average=None))\n",
    "\n",
    "f1_score_balanced = f1_score(y_test, y_pred_balanced, average=None)\n",
    "f1_score_unbalanced = f1_score(y_test, y_pred_unbalanced, average=None)\n",
    "\n",
    "f1_data = np.array([f1_score_balanced.tolist(), f1_score_unbalanced.tolist()])\n",
    "f1_score_df = pd.DataFrame(data=np.float64(f1_data[:,:]), columns=model_balanced.classes_, index = [\"f1_score_balanced\", \"f1_score_unbalanced\"])\n",
    "with pd.option_context('display.max_rows', None, 'display.max_columns', None):\n",
    "   display(f1_score_df)\n",
    "\n",
    "cm = confusion_matrix(y_test, y_pred_unbalanced, labels=model_unbalanced.classes_)\n",
    "\n",
    "disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model_unbalanced.classes_)\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "disp.plot(cmap=plt.cm.Blues, xticks_rotation=\"vertical\", ax=ax)\n",
    "plt.title('Confusion Matrix model_unbalanced', fontsize=15, pad=20)\n",
    "plt.xlabel('Prediction', fontsize=11)\n",
    "plt.ylabel('Actual', fontsize=11)\n",
    "plt.gca().xaxis.set_label_position('top')\n",
    "plt.gca().xaxis.tick_bottom()\n",
    "plt.gca().figure.subplots_adjust(bottom=0.2)\n",
    "plt.gca().figure.text(0.5, 0.05, 'Prediction', ha='center', fontsize=13)\n",
    "\n",
    "\n",
    "cm = confusion_matrix(y_test, y_pred_balanced, labels=model_balanced.classes_)\n",
    "\n",
    "disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model_balanced.classes_)\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "disp.plot(cmap=plt.cm.Blues, xticks_rotation=\"vertical\", ax=ax)\n",
    "plt.title('Confusion Matrix model_balanced', fontsize=15, pad=20)\n",
    "plt.xlabel('Prediction', fontsize=11)\n",
    "plt.ylabel('Actual', fontsize=11)\n",
    "plt.gca().xaxis.set_label_position('top')\n",
    "plt.gca().xaxis.tick_bottom()\n",
    "plt.gca().figure.subplots_adjust(bottom=0.2)\n",
    "plt.gca().figure.text(0.5, 0.05, 'Prediction', ha='center', fontsize=13)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "a0909b53",
   "metadata": {},
   "outputs": [],
   "source": [
    "f1_score_df_nonTransformed = f1_score_df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "d379245b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.5513256288239293\n",
      "F1-Score: 0.5755908999171561\n",
      "F1-Score: [0.52380952 0.47457627 0.42857143 0.66666667 0.         0.48\n",
      " 0.5        0.8        0.16666667 0.17142857 0.48076923 0.4\n",
      " 0.         0.4        0.72727273 0.70967742 0.5        0.53435115\n",
      " 0.02702703 0.21538462 0.375      0.28571429 0.7960396  0.\n",
      " 0.40860215 0.45070423 0.         0.5        0.5        0.\n",
      " 1.         0.45614035 0.73584906 0.        ]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Acquisto nel mese di rinnovo; concessionario</th>\n",
       "      <th>Acquisto nel mese di rinnovo; privato</th>\n",
       "      <th>Demolizione</th>\n",
       "      <th>Doc. sanitaria scaduta</th>\n",
       "      <th>Domiciliazione assente</th>\n",
       "      <th>Domiciliazione tardiva</th>\n",
       "      <th>EREDI</th>\n",
       "      <th>Ecoincentivo</th>\n",
       "      <th>Esenzione PH eredi</th>\n",
       "      <th>Esenzione PH o storica successiva</th>\n",
       "      <th>Esenzione PH senza istanza</th>\n",
       "      <th>Esenzione per ultratrentennale</th>\n",
       "      <th>Esenzione successiva</th>\n",
       "      <th>Esportazione non annotata</th>\n",
       "      <th>Fermo</th>\n",
       "      <th>Furto non annotato</th>\n",
       "      <th>Furto tardivo</th>\n",
       "      <th>Generica</th>\n",
       "      <th>Generica 2_non allegata documentazione</th>\n",
       "      <th>No esenzione PH</th>\n",
       "      <th>No minivoltura</th>\n",
       "      <th>Perdita possesso con DS</th>\n",
       "      <th>Periodo diverso</th>\n",
       "      <th>Rateizzazione</th>\n",
       "      <th>Regione diversa</th>\n",
       "      <th>Ricevuta non valida</th>\n",
       "      <th>Rientro ecoincentivo</th>\n",
       "      <th>Rientro in possesso</th>\n",
       "      <th>Sequestro non annotato</th>\n",
       "      <th>Storico</th>\n",
       "      <th>Variazioni tecniche</th>\n",
       "      <th>Veicolo diverso</th>\n",
       "      <th>Vendita tardiva</th>\n",
       "      <th>Versamento annullato</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>f1_score_balanced</th>\n",
       "      <td>0.52381</td>\n",
       "      <td>0.474576</td>\n",
       "      <td>0.428571</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.48</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.171429</td>\n",
       "      <td>0.480769</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>0.727273</td>\n",
       "      <td>0.709677</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.534351</td>\n",
       "      <td>0.027027</td>\n",
       "      <td>0.215385</td>\n",
       "      <td>0.375</td>\n",
       "      <td>0.285714</td>\n",
       "      <td>0.796040</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.408602</td>\n",
       "      <td>0.450704</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.45614</td>\n",
       "      <td>0.735849</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>f1_score_unbalanced</th>\n",
       "      <td>0.20000</td>\n",
       "      <td>0.518519</td>\n",
       "      <td>0.444444</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.390244</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.275862</td>\n",
       "      <td>0.714286</td>\n",
       "      <td>0.805970</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.841496</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.821643</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.411765</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.743902</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     Acquisto nel mese di rinnovo; concessionario  \\\n",
       "f1_score_balanced                                         0.52381   \n",
       "f1_score_unbalanced                                       0.20000   \n",
       "\n",
       "                     Acquisto nel mese di rinnovo; privato  Demolizione  \\\n",
       "f1_score_balanced                                 0.474576     0.428571   \n",
       "f1_score_unbalanced                               0.518519     0.444444   \n",
       "\n",
       "                     Doc. sanitaria scaduta  Domiciliazione assente  \\\n",
       "f1_score_balanced                  0.666667                     0.0   \n",
       "f1_score_unbalanced                0.000000                     0.0   \n",
       "\n",
       "                     Domiciliazione tardiva  EREDI  Ecoincentivo  \\\n",
       "f1_score_balanced                      0.48    0.5           0.8   \n",
       "f1_score_unbalanced                    0.00    0.0           0.0   \n",
       "\n",
       "                     Esenzione PH eredi  Esenzione PH o storica successiva  \\\n",
       "f1_score_balanced              0.166667                           0.171429   \n",
       "f1_score_unbalanced            0.000000                           0.000000   \n",
       "\n",
       "                     Esenzione PH senza istanza  \\\n",
       "f1_score_balanced                      0.480769   \n",
       "f1_score_unbalanced                    0.390244   \n",
       "\n",
       "                     Esenzione per ultratrentennale  Esenzione successiva  \\\n",
       "f1_score_balanced                               0.4                   0.0   \n",
       "f1_score_unbalanced                             0.0                   0.0   \n",
       "\n",
       "                     Esportazione non annotata     Fermo  Furto non annotato  \\\n",
       "f1_score_balanced                     0.400000  0.727273            0.709677   \n",
       "f1_score_unbalanced                   0.275862  0.714286            0.805970   \n",
       "\n",
       "                     Furto tardivo  Generica  \\\n",
       "f1_score_balanced              0.5  0.534351   \n",
       "f1_score_unbalanced            0.0  0.841496   \n",
       "\n",
       "                     Generica 2_non allegata documentazione  No esenzione PH  \\\n",
       "f1_score_balanced                                  0.027027         0.215385   \n",
       "f1_score_unbalanced                                0.000000         0.125000   \n",
       "\n",
       "                     No minivoltura  Perdita possesso con DS  Periodo diverso  \\\n",
       "f1_score_balanced             0.375                 0.285714         0.796040   \n",
       "f1_score_unbalanced           0.000                 0.000000         0.821643   \n",
       "\n",
       "                     Rateizzazione  Regione diversa  Ricevuta non valida  \\\n",
       "f1_score_balanced              0.0         0.408602             0.450704   \n",
       "f1_score_unbalanced            0.0         0.411765             0.000000   \n",
       "\n",
       "                     Rientro ecoincentivo  Rientro in possesso  \\\n",
       "f1_score_balanced                     0.0                  0.5   \n",
       "f1_score_unbalanced                   0.0                  0.0   \n",
       "\n",
       "                     Sequestro non annotato  Storico  Variazioni tecniche  \\\n",
       "f1_score_balanced                       0.5      0.0                  1.0   \n",
       "f1_score_unbalanced                     0.0      0.0                  0.0   \n",
       "\n",
       "                     Veicolo diverso  Vendita tardiva  Versamento annullato  \n",
       "f1_score_balanced            0.45614         0.735849                   0.0  \n",
       "f1_score_unbalanced          0.00000         0.743902                   0.0  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "Text(0.5, 0.05, 'Prediction')"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 242884 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 249548 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#X_train, X_test, y_train, y_test = train_test_split(df[\"NOTE_OPERATORE\"], df[\"CASISTICA_MOTIVAZIONE\"], test_size=0.2, random_state=42)\n",
    "\n",
    "\n",
    "class_dict = {\"Doc. sanitaria scaduta\" : 2, \n",
    "            \"Esenzione per ultratrentennale\" : 2, \n",
    "            \"Variazioni tecniche\" : 2, \n",
    "            \"Furto tardivo\" : 2\n",
    "            }\n",
    "oversample = RandomOverSampler(sampling_strategy=class_dict)\n",
    "\n",
    "X_over, y_over = oversample.fit_resample(df[\"X\"].to_numpy().reshape(-1, 1), df[\"CASISTICA_MOTIVAZIONE\"].to_numpy())\n",
    "df_over = pd.DataFrame({\"X\":X_over[:,0], \"CASISTICA_MOTIVAZIONE\":y_over})\n",
    "\n",
    "\n",
    "sss = StratifiedShuffleSplit(n_splits=1, test_size=0.3, random_state=0)\n",
    "\n",
    "for i, (train_index, test_index) in enumerate(sss.split(X_over,  y_over)):\n",
    "    test_index_df = df_over.iloc[test_index]\n",
    "    train_index_df = df_over.iloc[train_index]\n",
    "\n",
    "\n",
    "X_train = train_index_df[\"X\"]\n",
    "X_test = test_index_df[\"X\"]\n",
    "y_train = train_index_df[\"CASISTICA_MOTIVAZIONE\"]\n",
    "y_test = test_index_df[\"CASISTICA_MOTIVAZIONE\"]\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "model_balanced = Pipeline([('tfidf', TfidfVectorizer()), ('clf', LogisticRegression(class_weight=\"balanced\"))])\n",
    "model_unbalanced = Pipeline([('tfidf', TfidfVectorizer()), ('clf', LogisticRegression(class_weight=None))])\n",
    "\n",
    "\n",
    "model_balanced.fit(X_train, y_train)\n",
    "model_unbalanced.fit(X_train, y_train)\n",
    "\n",
    "\n",
    "y_pred_balanced = model_balanced.predict(X_test)\n",
    "y_pred_unbalanced = model_unbalanced.predict(X_test)\n",
    "\n",
    "print(\"Accuracy:\", accuracy_score(y_test, y_pred_balanced))\n",
    "print(\"F1-Score:\", f1_score(y_test, y_pred_balanced, average=\"weighted\"))\n",
    "print(\"F1-Score:\", f1_score(y_test, y_pred_balanced, average=None))\n",
    "\n",
    "f1_score_balanced = f1_score(y_test, y_pred_balanced, average=None)\n",
    "f1_score_unbalanced = f1_score(y_test, y_pred_unbalanced, average=None)\n",
    "\n",
    "f1_data = np.array([f1_score_balanced.tolist(), f1_score_unbalanced.tolist()])\n",
    "f1_score_df = pd.DataFrame(data=np.float64(f1_data[:,:]), columns=model_balanced.classes_, index = [\"f1_score_balanced\", \"f1_score_unbalanced\"])\n",
    "with pd.option_context('display.max_rows', None, 'display.max_columns', None):\n",
    "   display(f1_score_df)\n",
    "\n",
    "\n",
    "cm = confusion_matrix(y_test, y_pred_unbalanced, labels=model_unbalanced.classes_)\n",
    "\n",
    "disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model_unbalanced.classes_)\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "disp.plot(cmap=plt.cm.Blues, xticks_rotation=\"vertical\", ax=ax)\n",
    "plt.title('Confusion Matrix model_unbalanced', fontsize=15, pad=20)\n",
    "plt.xlabel('Prediction', fontsize=11)\n",
    "plt.ylabel('Actual', fontsize=11)\n",
    "plt.gca().xaxis.set_label_position('top')\n",
    "plt.gca().xaxis.tick_bottom()\n",
    "plt.gca().figure.subplots_adjust(bottom=0.2)\n",
    "plt.gca().figure.text(0.5, 0.05, 'Prediction', ha='center', fontsize=13)\n",
    "\n",
    "\n",
    "cm = confusion_matrix(y_test, y_pred_balanced, labels=model_balanced.classes_)\n",
    "\n",
    "disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model_balanced.classes_)\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "disp.plot(cmap=plt.cm.Blues, xticks_rotation=\"vertical\", ax=ax)\n",
    "plt.title('Confusion Matrix model_balanced', fontsize=15, pad=20)\n",
    "plt.xlabel('Prediction', fontsize=11)\n",
    "plt.ylabel('Actual', fontsize=11)\n",
    "plt.gca().xaxis.set_label_position('top')\n",
    "plt.gca().xaxis.tick_bottom()\n",
    "plt.gca().figure.subplots_adjust(bottom=0.2)\n",
    "plt.gca().figure.text(0.5, 0.05, 'Prediction', ha='center', fontsize=13)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bed8d9eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "f1_score_df_transformed = f1_score_df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "f9bd5d18",
   "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>Acquisto nel mese di rinnovo; concessionario</th>\n",
       "      <th>Acquisto nel mese di rinnovo; privato</th>\n",
       "      <th>Demolizione</th>\n",
       "      <th>Doc. sanitaria scaduta</th>\n",
       "      <th>Domiciliazione assente</th>\n",
       "      <th>Domiciliazione tardiva</th>\n",
       "      <th>EREDI</th>\n",
       "      <th>Ecoincentivo</th>\n",
       "      <th>Esenzione PH eredi</th>\n",
       "      <th>Esenzione PH o storica successiva</th>\n",
       "      <th>Esenzione PH senza istanza</th>\n",
       "      <th>Esenzione per ultratrentennale</th>\n",
       "      <th>Esenzione successiva</th>\n",
       "      <th>Esportazione non annotata</th>\n",
       "      <th>Fermo</th>\n",
       "      <th>Furto non annotato</th>\n",
       "      <th>Furto tardivo</th>\n",
       "      <th>Generica</th>\n",
       "      <th>Generica 2_non allegata documentazione</th>\n",
       "      <th>No esenzione PH</th>\n",
       "      <th>No minivoltura</th>\n",
       "      <th>Perdita possesso con DS</th>\n",
       "      <th>Periodo diverso</th>\n",
       "      <th>Rateizzazione</th>\n",
       "      <th>Regione diversa</th>\n",
       "      <th>Ricevuta non valida</th>\n",
       "      <th>Rientro ecoincentivo</th>\n",
       "      <th>Rientro in possesso</th>\n",
       "      <th>Sequestro non annotato</th>\n",
       "      <th>Storico</th>\n",
       "      <th>Variazioni tecniche</th>\n",
       "      <th>Veicolo diverso</th>\n",
       "      <th>Vendita tardiva</th>\n",
       "      <th>Versamento annullato</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>f1_score_balanced</th>\n",
       "      <td>0.511628</td>\n",
       "      <td>0.482759</td>\n",
       "      <td>0.413793</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.444444</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.125</td>\n",
       "      <td>0.471698</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.472222</td>\n",
       "      <td>0.800000</td>\n",
       "      <td>0.760870</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.533446</td>\n",
       "      <td>0.027778</td>\n",
       "      <td>0.202899</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.32</td>\n",
       "      <td>0.806950</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.426966</td>\n",
       "      <td>0.523077</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.526316</td>\n",
       "      <td>0.413793</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.448276</td>\n",
       "      <td>0.712963</td>\n",
       "      <td>0.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>f1_score_unbalanced</th>\n",
       "      <td>0.210526</td>\n",
       "      <td>0.461538</td>\n",
       "      <td>0.142857</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.250000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.380952</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.266667</td>\n",
       "      <td>0.758621</td>\n",
       "      <td>0.777778</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.839428</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.835341</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.388889</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.600000</td>\n",
       "      <td>0.736196</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     Acquisto nel mese di rinnovo; concessionario  \\\n",
       "f1_score_balanced                                        0.511628   \n",
       "f1_score_unbalanced                                      0.210526   \n",
       "\n",
       "                     Acquisto nel mese di rinnovo; privato  Demolizione  \\\n",
       "f1_score_balanced                                 0.482759     0.413793   \n",
       "f1_score_unbalanced                               0.461538     0.142857   \n",
       "\n",
       "                     Doc. sanitaria scaduta  Domiciliazione assente  \\\n",
       "f1_score_balanced                  0.666667                     0.0   \n",
       "f1_score_unbalanced                0.000000                     0.0   \n",
       "\n",
       "                     Domiciliazione tardiva  EREDI  Ecoincentivo  \\\n",
       "f1_score_balanced                  0.444444    0.5           0.8   \n",
       "f1_score_unbalanced                0.250000    0.0           0.0   \n",
       "\n",
       "                     Esenzione PH eredi  Esenzione PH o storica successiva  \\\n",
       "f1_score_balanced                   0.0                              0.125   \n",
       "f1_score_unbalanced                 0.0                              0.000   \n",
       "\n",
       "                     Esenzione PH senza istanza  \\\n",
       "f1_score_balanced                      0.471698   \n",
       "f1_score_unbalanced                    0.380952   \n",
       "\n",
       "                     Esenzione per ultratrentennale  Esenzione successiva  \\\n",
       "f1_score_balanced                               0.4                   0.0   \n",
       "f1_score_unbalanced                             0.0                   0.0   \n",
       "\n",
       "                     Esportazione non annotata     Fermo  Furto non annotato  \\\n",
       "f1_score_balanced                     0.472222  0.800000            0.760870   \n",
       "f1_score_unbalanced                   0.266667  0.758621            0.777778   \n",
       "\n",
       "                     Furto tardivo  Generica  \\\n",
       "f1_score_balanced              0.5  0.533446   \n",
       "f1_score_unbalanced            0.0  0.839428   \n",
       "\n",
       "                     Generica 2_non allegata documentazione  No esenzione PH  \\\n",
       "f1_score_balanced                                  0.027778         0.202899   \n",
       "f1_score_unbalanced                                0.000000         0.125000   \n",
       "\n",
       "                     No minivoltura  Perdita possesso con DS  Periodo diverso  \\\n",
       "f1_score_balanced               0.4                     0.32         0.806950   \n",
       "f1_score_unbalanced             0.0                     0.00         0.835341   \n",
       "\n",
       "                     Rateizzazione  Regione diversa  Ricevuta non valida  \\\n",
       "f1_score_balanced              0.0         0.426966             0.523077   \n",
       "f1_score_unbalanced            0.0         0.388889             0.166667   \n",
       "\n",
       "                     Rientro ecoincentivo  Rientro in possesso  \\\n",
       "f1_score_balanced                     0.0             0.526316   \n",
       "f1_score_unbalanced                   0.0             0.000000   \n",
       "\n",
       "                     Sequestro non annotato  Storico  Variazioni tecniche  \\\n",
       "f1_score_balanced                  0.413793      0.0                  1.0   \n",
       "f1_score_unbalanced                0.000000      0.0                  0.0   \n",
       "\n",
       "                     Veicolo diverso  Vendita tardiva  Versamento annullato  \n",
       "f1_score_balanced           0.448276         0.712963                   0.4  \n",
       "f1_score_unbalanced         0.600000         0.736196                   0.0  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "with pd.option_context('display.max_rows', None, 'display.max_columns', None):\n",
    "   display(f1_score_df_nonTransformed)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "f365eb43",
   "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>Acquisto nel mese di rinnovo; concessionario</th>\n",
       "      <th>Acquisto nel mese di rinnovo; privato</th>\n",
       "      <th>Demolizione</th>\n",
       "      <th>Doc. sanitaria scaduta</th>\n",
       "      <th>Domiciliazione assente</th>\n",
       "      <th>Domiciliazione tardiva</th>\n",
       "      <th>EREDI</th>\n",
       "      <th>Ecoincentivo</th>\n",
       "      <th>Esenzione PH eredi</th>\n",
       "      <th>Esenzione PH o storica successiva</th>\n",
       "      <th>Esenzione PH senza istanza</th>\n",
       "      <th>Esenzione per ultratrentennale</th>\n",
       "      <th>Esenzione successiva</th>\n",
       "      <th>Esportazione non annotata</th>\n",
       "      <th>Fermo</th>\n",
       "      <th>Furto non annotato</th>\n",
       "      <th>Furto tardivo</th>\n",
       "      <th>Generica</th>\n",
       "      <th>Generica 2_non allegata documentazione</th>\n",
       "      <th>No esenzione PH</th>\n",
       "      <th>No minivoltura</th>\n",
       "      <th>Perdita possesso con DS</th>\n",
       "      <th>Periodo diverso</th>\n",
       "      <th>Rateizzazione</th>\n",
       "      <th>Regione diversa</th>\n",
       "      <th>Ricevuta non valida</th>\n",
       "      <th>Rientro ecoincentivo</th>\n",
       "      <th>Rientro in possesso</th>\n",
       "      <th>Sequestro non annotato</th>\n",
       "      <th>Storico</th>\n",
       "      <th>Variazioni tecniche</th>\n",
       "      <th>Veicolo diverso</th>\n",
       "      <th>Vendita tardiva</th>\n",
       "      <th>Versamento annullato</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>f1_score_balanced</th>\n",
       "      <td>0.52381</td>\n",
       "      <td>0.474576</td>\n",
       "      <td>0.428571</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.48</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.171429</td>\n",
       "      <td>0.480769</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>0.727273</td>\n",
       "      <td>0.709677</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.534351</td>\n",
       "      <td>0.027027</td>\n",
       "      <td>0.215385</td>\n",
       "      <td>0.375</td>\n",
       "      <td>0.285714</td>\n",
       "      <td>0.796040</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.408602</td>\n",
       "      <td>0.450704</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.45614</td>\n",
       "      <td>0.735849</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>f1_score_unbalanced</th>\n",
       "      <td>0.20000</td>\n",
       "      <td>0.518519</td>\n",
       "      <td>0.444444</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.390244</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.275862</td>\n",
       "      <td>0.714286</td>\n",
       "      <td>0.805970</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.841496</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.821643</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.411765</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.743902</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     Acquisto nel mese di rinnovo; concessionario  \\\n",
       "f1_score_balanced                                         0.52381   \n",
       "f1_score_unbalanced                                       0.20000   \n",
       "\n",
       "                     Acquisto nel mese di rinnovo; privato  Demolizione  \\\n",
       "f1_score_balanced                                 0.474576     0.428571   \n",
       "f1_score_unbalanced                               0.518519     0.444444   \n",
       "\n",
       "                     Doc. sanitaria scaduta  Domiciliazione assente  \\\n",
       "f1_score_balanced                  0.666667                     0.0   \n",
       "f1_score_unbalanced                0.000000                     0.0   \n",
       "\n",
       "                     Domiciliazione tardiva  EREDI  Ecoincentivo  \\\n",
       "f1_score_balanced                      0.48    0.5           0.8   \n",
       "f1_score_unbalanced                    0.00    0.0           0.0   \n",
       "\n",
       "                     Esenzione PH eredi  Esenzione PH o storica successiva  \\\n",
       "f1_score_balanced              0.166667                           0.171429   \n",
       "f1_score_unbalanced            0.000000                           0.000000   \n",
       "\n",
       "                     Esenzione PH senza istanza  \\\n",
       "f1_score_balanced                      0.480769   \n",
       "f1_score_unbalanced                    0.390244   \n",
       "\n",
       "                     Esenzione per ultratrentennale  Esenzione successiva  \\\n",
       "f1_score_balanced                               0.4                   0.0   \n",
       "f1_score_unbalanced                             0.0                   0.0   \n",
       "\n",
       "                     Esportazione non annotata     Fermo  Furto non annotato  \\\n",
       "f1_score_balanced                     0.400000  0.727273            0.709677   \n",
       "f1_score_unbalanced                   0.275862  0.714286            0.805970   \n",
       "\n",
       "                     Furto tardivo  Generica  \\\n",
       "f1_score_balanced              0.5  0.534351   \n",
       "f1_score_unbalanced            0.0  0.841496   \n",
       "\n",
       "                     Generica 2_non allegata documentazione  No esenzione PH  \\\n",
       "f1_score_balanced                                  0.027027         0.215385   \n",
       "f1_score_unbalanced                                0.000000         0.125000   \n",
       "\n",
       "                     No minivoltura  Perdita possesso con DS  Periodo diverso  \\\n",
       "f1_score_balanced             0.375                 0.285714         0.796040   \n",
       "f1_score_unbalanced           0.000                 0.000000         0.821643   \n",
       "\n",
       "                     Rateizzazione  Regione diversa  Ricevuta non valida  \\\n",
       "f1_score_balanced              0.0         0.408602             0.450704   \n",
       "f1_score_unbalanced            0.0         0.411765             0.000000   \n",
       "\n",
       "                     Rientro ecoincentivo  Rientro in possesso  \\\n",
       "f1_score_balanced                     0.0                  0.5   \n",
       "f1_score_unbalanced                   0.0                  0.0   \n",
       "\n",
       "                     Sequestro non annotato  Storico  Variazioni tecniche  \\\n",
       "f1_score_balanced                       0.5      0.0                  1.0   \n",
       "f1_score_unbalanced                     0.0      0.0                  0.0   \n",
       "\n",
       "                     Veicolo diverso  Vendita tardiva  Versamento annullato  \n",
       "f1_score_balanced            0.45614         0.735849                   0.0  \n",
       "f1_score_unbalanced          0.00000         0.743902                   0.0  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "with pd.option_context('display.max_rows', None, 'display.max_columns', None):\n",
    "   display(f1_score_df_transformed)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae4fbf77",
   "metadata": {},
   "outputs": [],
   "source": [
    "f1_score_balanced"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "369c71c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.611740473738414\n",
      "F1-Score: 0.6405037169113871\n",
      "F1-Score: [0.60869565 0.4        0.43243243 0.5        0.         0.\n",
      " 0.24489796 0.31034483 0.66666667 0.71641791 0.61016949 0.04545455\n",
      " 0.42857143 0.54545455 0.4        0.85995086 0.63829787 0.56\n",
      " 0.36363636 0.38095238 0.63157895 0.64661654]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Text(0.5, 0.05, 'Prediction')"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 178028 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 181864 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "df_pruned = prune_infrequent_classes(df)\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_pruned[\"NOTE_OPERATORE\"], df_pruned[\"CASISTICA_MOTIVAZIONE\"], test_size=0.2, random_state=42)\n",
    "\n",
    "model_balanced = Pipeline([('tfidf', TfidfVectorizer()), ('clf', LogisticRegression(class_weight=\"balanced\"))])\n",
    "model_unbalanced = Pipeline([('tfidf', TfidfVectorizer()), ('clf', LogisticRegression(class_weight=None))])\n",
    "\n",
    "\n",
    "model_balanced.fit(X_train, y_train)\n",
    "model_unbalanced.fit(X_train, y_train)\n",
    "\n",
    "\n",
    "y_pred_balanced = model_balanced.predict(X_test)\n",
    "y_pred_unbalanced = model_unbalanced.predict(X_test)\n",
    "\n",
    "print(\"Accuracy:\", accuracy_score(y_test, y_pred_balanced))\n",
    "print(\"F1-Score:\", f1_score(y_test, y_pred_balanced, average=\"weighted\"))\n",
    "\n",
    "print(\"F1-Score:\", f1_score(y_test, y_pred_balanced, average=None))\n",
    "\n",
    "f1_score_balanced = f1_score(y_test, y_pred_balanced, average=None)\n",
    "f1_score_unbalanced = f1_score(y_test, y_pred_unbalanced, average=None)\n",
    "\n",
    "f1_data = np.array([f1_score_balanced.tolist(), f1_score_unbalanced.tolist()])\n",
    "f1_score_df = pd.DataFrame(data=np.float64(f1_data[:,:]), columns=model_balanced.classes_, index = [\"f1_score_balanced\", \"f1_score_unbalanced\"])\n",
    "\n",
    "\n",
    "\n",
    "cm = confusion_matrix(y_test, y_pred_unbalanced, labels=model_unbalanced.classes_)\n",
    "\n",
    "disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model_unbalanced.classes_)\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "disp.plot(cmap=plt.cm.Blues, xticks_rotation=\"vertical\", ax=ax)\n",
    "plt.title('Confusion Matrix model_unbalanced', fontsize=15, pad=20)\n",
    "plt.xlabel('Prediction', fontsize=11)\n",
    "plt.ylabel('Actual', fontsize=11)\n",
    "plt.gca().xaxis.set_label_position('top')\n",
    "plt.gca().xaxis.tick_bottom()\n",
    "plt.gca().figure.subplots_adjust(bottom=0.2)\n",
    "plt.gca().figure.text(0.5, 0.05, 'Prediction', ha='center', fontsize=13)\n",
    "\n",
    "\n",
    "cm = confusion_matrix(y_test, y_pred_balanced, labels=model_balanced.classes_)\n",
    "\n",
    "disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model_balanced.classes_)\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "disp.plot(cmap=plt.cm.Blues, xticks_rotation=\"vertical\", ax=ax)\n",
    "plt.title('Confusion Matrix model_balanced', fontsize=15, pad=20)\n",
    "plt.xlabel('Prediction', fontsize=11)\n",
    "plt.ylabel('Actual', fontsize=11)\n",
    "plt.gca().xaxis.set_label_position('top')\n",
    "plt.gca().xaxis.tick_bottom()\n",
    "plt.gca().figure.subplots_adjust(bottom=0.2)\n",
    "plt.gca().figure.text(0.5, 0.05, 'Prediction', ha='center', fontsize=13)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "f0e80e79",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.7817745803357314\n",
      "F1-Score: 0.7876875800962205\n",
      "F1-Score: [0.71428571 0.71428571 0.76190476 0.         0.         0.85714286\n",
      " 0.         0.4        0.72222222 0.         0.74285714 0.95238095\n",
      " 0.89795918 0.         0.21052632 0.73684211 0.88888889 0.57142857\n",
      " 0.88501742 0.87179487 0.69387755 0.         0.88888889 0.25\n",
      " 0.28571429 0.8        0.83333333 0.66666667]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Text(0.5, 0.05, 'Prediction')"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 232464 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 232080 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df_nonGenerica = df[df[\"CASISTICA_MOTIVAZIONE\"] != \"Generica\"]\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_nonGenerica[\"NOTE_OPERATORE\"], df_nonGenerica[\"CASISTICA_MOTIVAZIONE\"], test_size=0.2, random_state=42)\n",
    "\n",
    "\n",
    "model_balanced = Pipeline([('tfidf', TfidfVectorizer()), ('clf', LogisticRegression(class_weight=\"balanced\"))])\n",
    "model_unbalanced = Pipeline([('tfidf', TfidfVectorizer()), ('clf', LogisticRegression(class_weight=None))])\n",
    "\n",
    "\n",
    "model_balanced.fit(X_train, y_train)\n",
    "model_unbalanced.fit(X_train, y_train)\n",
    "\n",
    "\n",
    "y_pred_balanced = model_balanced.predict(X_test)\n",
    "y_pred_unbalanced = model_unbalanced.predict(X_test)\n",
    "\n",
    "print(\"Accuracy:\", accuracy_score(y_test, y_pred_balanced))\n",
    "print(\"F1-Score:\", f1_score(y_test, y_pred_balanced, average=\"weighted\"))\n",
    "print(\"F1-Score:\", f1_score(y_test, y_pred_balanced, average=None))\n",
    "\n",
    "f1_score_balanced = f1_score(y_test, y_pred_balanced, average=None, zero_division=0.0)\n",
    "f1_score_unbalanced = f1_score(y_test, y_pred_unbalanced, average=None, zero_division=0.0)\n",
    "\n",
    "#f1_data = np.array([f1_score_balanced.tolist(), f1_score_unbalanced.tolist()])\n",
    "#f1_score_df = pd.DataFrame(data=np.float64(f1_data[:,:]), columns=model_balanced.classes_, index = [\"f1_score_balanced\", \"f1_score_unbalanced\"])\n",
    "\n",
    "cm = confusion_matrix(y_test, y_pred_unbalanced, labels=model_unbalanced.classes_)\n",
    "\n",
    "disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model_unbalanced.classes_)\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "disp.plot(cmap=plt.cm.Blues, xticks_rotation=\"vertical\", ax=ax)\n",
    "plt.title('Confusion Matrix model_unbalanced', fontsize=15, pad=20)\n",
    "plt.xlabel('Prediction', fontsize=11)\n",
    "plt.ylabel('Actual', fontsize=11)\n",
    "plt.gca().xaxis.set_label_position('top')\n",
    "plt.gca().xaxis.tick_bottom()\n",
    "plt.gca().figure.subplots_adjust(bottom=0.2)\n",
    "plt.gca().figure.text(0.5, 0.05, 'Prediction', ha='center', fontsize=13)\n",
    "\n",
    "\n",
    "cm = confusion_matrix(y_test, y_pred_balanced, labels=model_balanced.classes_)\n",
    "\n",
    "disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model_balanced.classes_)\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "disp.plot(cmap=plt.cm.Blues, xticks_rotation=\"vertical\", ax=ax)\n",
    "plt.title('Confusion Matrix model_balanced', fontsize=15, pad=20)\n",
    "plt.xlabel('Prediction', fontsize=11)\n",
    "plt.ylabel('Actual', fontsize=11)\n",
    "plt.gca().xaxis.set_label_position('top')\n",
    "plt.gca().xaxis.tick_bottom()\n",
    "plt.gca().figure.subplots_adjust(bottom=0.2)\n",
    "plt.gca().figure.text(0.5, 0.05, 'Prediction', ha='center', fontsize=13)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 323,
   "id": "579fecb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.66666667, 0.66666667, 0.66666667, 0.66666667, 0.        ,\n",
       "       0.84210526, 0.66666667, 0.9       , 0.94339623, 0.        ,\n",
       "       0.57142857, 1.        , 0.4       , 0.88452088, 0.93333333,\n",
       "       0.22222222, 1.        , 0.66666667, 0.44444444, 0.94117647])"
      ]
     },
     "execution_count": 323,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f1_score_unbalanced"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "10ed8269",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['09620x000d' '1003291435x000d' '1003297047x000d' ... 'voltura' 'vuole'\n",
      " 'x000d']\n",
      "(1667, 1114)\n",
      "[[0. 0. 0. ... 0. 0. 0.]\n",
      " [0. 0. 0. ... 0. 0. 0.]\n",
      " [0. 0. 0. ... 0. 0. 0.]\n",
      " ...\n",
      " [0. 0. 0. ... 0. 0. 0.]\n",
      " [0. 0. 0. ... 0. 0. 0.]\n",
      " [0. 0. 0. ... 0. 0. 0.]]\n",
      "Accuracy: 0.7529976019184652\n",
      "F1-Score: 0.709454742723374\n"
     ]
    }
   ],
   "source": [
    "df_nonGenerica = df[df[\"CASISTICA_MOTIVAZIONE\"] != \"Generica\"]\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_nonGenerica[\"NOTE_OPERATORE\"], df_nonGenerica[\"CASISTICA_MOTIVAZIONE\"], test_size=0.2, random_state=42)\n",
    "\n",
    "vectorizer = TfidfVectorizer()\n",
    "X = vectorizer.fit_transform(X_train)\n",
    "\n",
    "print(vectorizer.get_feature_names_out())\n",
    "print(X.shape)\n",
    "print(X.todense())\n",
    "\n",
    "model = LogisticRegression()\n",
    "\n",
    "# Training the model\n",
    "model.fit(X, y_train)\n",
    "\n",
    "\n",
    "# Predicting on the test set\n",
    "y_pred = model.predict(vectorizer.transform(X_test))\n",
    "dec_fun = model.decision_function(vectorizer.transform(X_test))\n",
    "pred_prob = model.predict_proba(vectorizer.transform(X_test))\n",
    "\n",
    "print(\"Accuracy:\", accuracy_score(y_test, y_pred))\n",
    "print(\"F1-Score:\", f1_score(y_test, y_pred, average=\"weighted\"))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8cafd94",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "df_generica = df[df[\"CASISTICA_MOTIVAZIONE\"] == \"Generica\"].reset_index()\n",
    "X_test_gen, y_test_gen = df_generica[\"NOTE_OPERATORE\"], df_generica[\"CASISTICA_MOTIVAZIONE\"]\n",
    "y_pred_gen = model.predict(vectorizer.transform(X_test_gen))\n",
    "dec_fun_gen = model.decision_function(vectorizer.transform(X_test_gen))\n",
    "pred_prob_gen = model.predict_proba(vectorizer.transform(X_test_gen))\n",
    "\n",
    "df_res_gen = pd.DataFrame(data=np.int_(pred_prob_gen[:,:]), columns=model.classes_)\n",
    "\n",
    "print(X_test_gen[1])\n",
    "print(model.classes_)\n",
    "print(pred_prob_gen[1])\n",
    "print(np.argmax(pred_prob_gen[1]))\n",
    "print(model.classes_[np.argmax(pred_prob_gen[1])])\n",
    "print(max(pred_prob_gen[1]))\n",
    "print(pred_prob_gen[1][np.argmax(pred_prob_gen[1])])\n",
    "\n",
    "\n",
    "thresh = .5\n",
    "df_res_gen = pd.DataFrame(data=np.float64(pred_prob_gen[:,:]), columns=model.classes_)\n",
    "\n",
    "df_res_gen_above_thresh = df_res_gen[df_res_gen.max(axis=1)>thresh]\n",
    "df_res_gen_above_thresh\n",
    "#with pd.option_context('display.max_rows', None, 'display.max_columns', None):\n",
    "#    display(df_res_gen_above_thresh)#display(df_generica.iloc[df_res_gen_above_thresh.index])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "d8c06a5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\a.torchi\\AppData\\Local\\Temp\\ipykernel_34736\\531835673.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df_generica_classified[\"predicted_class\"] = max_prob_classes\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>NUMERO_DOCUMENTO</th>\n",
       "      <th>CASISTICA_LAVORAZIONE</th>\n",
       "      <th>NOTE_OPERATORE</th>\n",
       "      <th>CASISTICA_MOTIVAZIONE</th>\n",
       "      <th>X</th>\n",
       "      <th>predicted_class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>22401</td>\n",
       "      <td>838165467407</td>\n",
       "      <td>NESSUNA O INIDONEA DOCUMENTAZIONE</td>\n",
       "      <td>allega atto vendita illeggibile autenticato giugno</td>\n",
       "      <td>Generica</td>\n",
       "      <td>allegare atto vendita illeggibile autenticare giugno</td>\n",
       "      <td>Vendita tardiva</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>22402</td>\n",
       "      <td>838225044100</td>\n",
       "      <td>NESSUNA O INIDONEA DOCUMENTAZIONE</td>\n",
       "      <td>dichiara vendita avvenuta ottobre</td>\n",
       "      <td>Generica</td>\n",
       "      <td>dichiarare vendita avvenire ottobre</td>\n",
       "      <td>Vendita tardiva</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>22404</td>\n",
       "      <td>68229236386</td>\n",
       "      <td>NESSUNA O INIDONEA DOCUMENTAZIONE</td>\n",
       "      <td>allega denuncia furto rilasciata novembre</td>\n",
       "      <td>Generica</td>\n",
       "      <td>allegare denuncia furto rilasciare novembre</td>\n",
       "      <td>Furto non annotato</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>28</td>\n",
       "      <td>22479</td>\n",
       "      <td>838140324094</td>\n",
       "      <td>ATTO DI VENDITA INCOMPLETO</td>\n",
       "      <td>manca allegato autentica firma</td>\n",
       "      <td>Generica</td>\n",
       "      <td>mancare allegato autentico firmare</td>\n",
       "      <td>Periodo diverso</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>36</td>\n",
       "      <td>22502</td>\n",
       "      <td>838229064041</td>\n",
       "      <td>NESSUNA O INIDONEA DOCUMENTAZIONE</td>\n",
       "      <td>cliente accenna denuncia nessuna annotazione</td>\n",
       "      <td>Generica</td>\n",
       "      <td>cliente accenna denunciare nessuno annotazione</td>\n",
       "      <td>Furto non annotato</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",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2784</th>\n",
       "      <td>4844</td>\n",
       "      <td>44582</td>\n",
       "      <td>738025855471</td>\n",
       "      <td>PERDITA DI POSS. TARDIVA (FURTO, SEQUESTRO, EC.)</td>\n",
       "      <td>denuncia querela successiva trascritta pra</td>\n",
       "      <td>Generica</td>\n",
       "      <td>denuncia querela successivo trascrittare pra</td>\n",
       "      <td>Furto non annotato</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2785</th>\n",
       "      <td>4847</td>\n",
       "      <td>44598</td>\n",
       "      <td>68356228483</td>\n",
       "      <td>ESENZIONE DISABILE RESPINTA</td>\n",
       "      <td>esenzione prevista motoveicoli</td>\n",
       "      <td>Generica</td>\n",
       "      <td>esenzione prevedere motoveicoli</td>\n",
       "      <td>Esenzione PH senza istanza</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2790</th>\n",
       "      <td>4858</td>\n",
       "      <td>44650</td>\n",
       "      <td>738000098638</td>\n",
       "      <td>NESSUNA O INIDONEA DOCUMENTAZIONE</td>\n",
       "      <td>cliente allega documentazione esenzione presente nessuna esenzione</td>\n",
       "      <td>Generica</td>\n",
       "      <td>cliente allegare documentazione esenzione presente nessuno esenzione</td>\n",
       "      <td>Esenzione PH senza istanza</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2795</th>\n",
       "      <td>4866</td>\n",
       "      <td>44661</td>\n",
       "      <td>838167592717</td>\n",
       "      <td>NESSUNA O INIDONEA DOCUMENTAZIONE</td>\n",
       "      <td>nessun versamento</td>\n",
       "      <td>Generica</td>\n",
       "      <td>nessuno versamento</td>\n",
       "      <td>Periodo diverso</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2811</th>\n",
       "      <td>4895</td>\n",
       "      <td>44719</td>\n",
       "      <td>68412289534</td>\n",
       "      <td>NESSUNA O INIDONEA DOCUMENTAZIONE</td>\n",
       "      <td>atto vendita illeggibile</td>\n",
       "      <td>Generica</td>\n",
       "      <td>atto venditare illeggibile</td>\n",
       "      <td>Vendita tardiva</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>585 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      index  Unnamed: 0  NUMERO_DOCUMENTO  \\\n",
       "0         0       22401      838165467407   \n",
       "1         1       22402      838225044100   \n",
       "3         3       22404       68229236386   \n",
       "18       28       22479      838140324094   \n",
       "24       36       22502      838229064041   \n",
       "...     ...         ...               ...   \n",
       "2784   4844       44582      738025855471   \n",
       "2785   4847       44598       68356228483   \n",
       "2790   4858       44650      738000098638   \n",
       "2795   4866       44661      838167592717   \n",
       "2811   4895       44719       68412289534   \n",
       "\n",
       "                                 CASISTICA_LAVORAZIONE  \\\n",
       "0                    NESSUNA O INIDONEA DOCUMENTAZIONE   \n",
       "1                    NESSUNA O INIDONEA DOCUMENTAZIONE   \n",
       "3                    NESSUNA O INIDONEA DOCUMENTAZIONE   \n",
       "18                          ATTO DI VENDITA INCOMPLETO   \n",
       "24                   NESSUNA O INIDONEA DOCUMENTAZIONE   \n",
       "...                                                ...   \n",
       "2784  PERDITA DI POSS. TARDIVA (FURTO, SEQUESTRO, EC.)   \n",
       "2785                       ESENZIONE DISABILE RESPINTA   \n",
       "2790                 NESSUNA O INIDONEA DOCUMENTAZIONE   \n",
       "2795                 NESSUNA O INIDONEA DOCUMENTAZIONE   \n",
       "2811                 NESSUNA O INIDONEA DOCUMENTAZIONE   \n",
       "\n",
       "                                                          NOTE_OPERATORE  \\\n",
       "0                     allega atto vendita illeggibile autenticato giugno   \n",
       "1                                      dichiara vendita avvenuta ottobre   \n",
       "3                              allega denuncia furto rilasciata novembre   \n",
       "18                                        manca allegato autentica firma   \n",
       "24                          cliente accenna denuncia nessuna annotazione   \n",
       "...                                                                  ...   \n",
       "2784                          denuncia querela successiva trascritta pra   \n",
       "2785                                      esenzione prevista motoveicoli   \n",
       "2790  cliente allega documentazione esenzione presente nessuna esenzione   \n",
       "2795                                                   nessun versamento   \n",
       "2811                                            atto vendita illeggibile   \n",
       "\n",
       "     CASISTICA_MOTIVAZIONE  \\\n",
       "0                 Generica   \n",
       "1                 Generica   \n",
       "3                 Generica   \n",
       "18                Generica   \n",
       "24                Generica   \n",
       "...                    ...   \n",
       "2784              Generica   \n",
       "2785              Generica   \n",
       "2790              Generica   \n",
       "2795              Generica   \n",
       "2811              Generica   \n",
       "\n",
       "                                                                         X  \\\n",
       "0                     allegare atto vendita illeggibile autenticare giugno   \n",
       "1                                      dichiarare vendita avvenire ottobre   \n",
       "3                              allegare denuncia furto rilasciare novembre   \n",
       "18                                      mancare allegato autentico firmare   \n",
       "24                          cliente accenna denunciare nessuno annotazione   \n",
       "...                                                                    ...   \n",
       "2784                          denuncia querela successivo trascrittare pra   \n",
       "2785                                       esenzione prevedere motoveicoli   \n",
       "2790  cliente allegare documentazione esenzione presente nessuno esenzione   \n",
       "2795                                                    nessuno versamento   \n",
       "2811                                            atto venditare illeggibile   \n",
       "\n",
       "                 predicted_class  \n",
       "0                Vendita tardiva  \n",
       "1                Vendita tardiva  \n",
       "3             Furto non annotato  \n",
       "18               Periodo diverso  \n",
       "24            Furto non annotato  \n",
       "...                          ...  \n",
       "2784          Furto non annotato  \n",
       "2785  Esenzione PH senza istanza  \n",
       "2790  Esenzione PH senza istanza  \n",
       "2795             Periodo diverso  \n",
       "2811             Vendita tardiva  \n",
       "\n",
       "[585 rows x 8 columns]"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max_prob_classes = df_res_gen_above_thresh.idxmax(axis=1)\n",
    "df_generica_classified = df_generica.iloc[max_prob_classes.index]\n",
    "df_generica_classified[\"predicted_class\"] = max_prob_classes\n",
    "df_generica_classified"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "50ed4947",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.7410071942446043\n",
      "F1-Score: 0.6937810217431234\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\a.torchi\\Desktop\\text_classification\\venv\\Lib\\site-packages\\sklearn\\linear_model\\_logistic.py:1264: FutureWarning: 'multi_class' was deprecated in version 1.5 and will be removed in 1.7. From then on, it will always use 'multinomial'. Leave it to its default value to avoid this warning.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "df_nonGenerica = df[df[\"CASISTICA_MOTIVAZIONE\"] != \"Generica\"]\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_nonGenerica[\"X\"], df_nonGenerica[\"CASISTICA_MOTIVAZIONE\"], test_size=0.2, random_state=42)\n",
    "\n",
    "model = Pipeline([('tfidf', TfidfVectorizer()), ('clf', LogisticRegression(penalty=\"l2\", multi_class='multinomial'))])\n",
    "\n",
    "# Training the model\n",
    "model.fit(X_train, y_train)\n",
    "\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n",
    "\n",
    "# Predicting on the test set\n",
    "y_pred = model.predict(X_test)\n",
    "\n",
    "print(\"Accuracy:\", accuracy_score(y_test, y_pred))\n",
    "print(\"F1-Score:\", f1_score(y_test, y_pred, average=\"weighted\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "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.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
