{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 필요한 모듈 import"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.pipeline import FeatureUnion\n",
    "from imblearn.pipeline import make_pipeline, Pipeline\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.model_selection import StratifiedKFold, GridSearchCV, RandomizedSearchCV, train_test_split\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.discriminant_analysis import LinearDiscriminantAnalysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_selection import VarianceThreshold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "APCFP = pd.read_csv('finalfingerprint_sub_apc.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "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>target_name_bin</th>\n",
       "      <th>target_name_ter</th>\n",
       "      <th>target_name_quater</th>\n",
       "      <th>smiles</th>\n",
       "      <th>APC2D1_C_C</th>\n",
       "      <th>APC2D1_C_N</th>\n",
       "      <th>APC2D1_C_O</th>\n",
       "      <th>APC2D1_C_S</th>\n",
       "      <th>APC2D1_C_P</th>\n",
       "      <th>APC2D1_C_F</th>\n",
       "      <th>...</th>\n",
       "      <th>SubFPC303</th>\n",
       "      <th>SubFPC304</th>\n",
       "      <th>SubFPC305</th>\n",
       "      <th>SubFPC306</th>\n",
       "      <th>SubFPC307</th>\n",
       "      <th>bond:C=S_carbonyl_thio_generic</th>\n",
       "      <th>bond:CN_amine_pri-NH2_aromatic</th>\n",
       "      <th>bond:CS_sulfide</th>\n",
       "      <th>bond:S=O_sulfonyl_generic</th>\n",
       "      <th>ring:hetero_[6_6]_O_benzopyrone_(1_4-)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "      <td>A1</td>\n",
       "      <td>Nc1ccccc1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "      <td>A1</td>\n",
       "      <td>COc1ccc(N)cc1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "      <td>A1</td>\n",
       "      <td>COP(=S)(OC)SCn1nnc2ccccc2c1=O</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "      <td>A1</td>\n",
       "      <td>CC(C)(c1ccc(O)cc1)c1ccc(O)cc1</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "      <td>A1</td>\n",
       "      <td>CC(C)(C)c1cc(O)ccc1O</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>582</td>\n",
       "      <td>B</td>\n",
       "      <td>C</td>\n",
       "      <td>C</td>\n",
       "      <td>CSCCCCN=C=S</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>583</td>\n",
       "      <td>B</td>\n",
       "      <td>C</td>\n",
       "      <td>C</td>\n",
       "      <td>CCCN=C=S</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>584</td>\n",
       "      <td>B</td>\n",
       "      <td>C</td>\n",
       "      <td>C</td>\n",
       "      <td>CS(=O)CCCCC#N</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>585</td>\n",
       "      <td>B</td>\n",
       "      <td>C</td>\n",
       "      <td>C</td>\n",
       "      <td>CSCCCCC#N</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>586</td>\n",
       "      <td>B</td>\n",
       "      <td>C</td>\n",
       "      <td>C</td>\n",
       "      <td>C=CCSC#N</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>587 rows × 1096 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    target_name_bin target_name_ter target_name_quater  \\\n",
       "0                 A               A                 A1   \n",
       "1                 A               A                 A1   \n",
       "2                 A               A                 A1   \n",
       "3                 A               A                 A1   \n",
       "4                 A               A                 A1   \n",
       "..              ...             ...                ...   \n",
       "582               B               C                  C   \n",
       "583               B               C                  C   \n",
       "584               B               C                  C   \n",
       "585               B               C                  C   \n",
       "586               B               C                  C   \n",
       "\n",
       "                            smiles  APC2D1_C_C  APC2D1_C_N  APC2D1_C_O  \\\n",
       "0                        Nc1ccccc1           6           1           0   \n",
       "1                    COc1ccc(N)cc1           6           1           2   \n",
       "2    COP(=S)(OC)SCn1nnc2ccccc2c1=O           7           3           3   \n",
       "3    CC(C)(c1ccc(O)cc1)c1ccc(O)cc1          16           0           2   \n",
       "4             CC(C)(C)c1cc(O)ccc1O          10           0           2   \n",
       "..                             ...         ...         ...         ...   \n",
       "582                    CSCCCCN=C=S           3           2           0   \n",
       "583                       CCCN=C=S           2           2           0   \n",
       "584                  CS(=O)CCCCC#N           4           1           0   \n",
       "585                      CSCCCCC#N           4           1           0   \n",
       "586                       C=CCSC#N           2           1           0   \n",
       "\n",
       "     APC2D1_C_S  APC2D1_C_P  APC2D1_C_F  ...  SubFPC303  SubFPC304  SubFPC305  \\\n",
       "0             0           0           0  ...          0          0          0   \n",
       "1             0           0           0  ...          0          0          0   \n",
       "2             1           0           0  ...          0          0          0   \n",
       "3             0           0           0  ...          0          0          0   \n",
       "4             0           0           0  ...          0          0          0   \n",
       "..          ...         ...         ...  ...        ...        ...        ...   \n",
       "582           3           0           0  ...          0          0          0   \n",
       "583           1           0           0  ...          0          0          0   \n",
       "584           2           0           0  ...          0          0          0   \n",
       "585           2           0           0  ...          0          0          0   \n",
       "586           2           0           0  ...          0          0          0   \n",
       "\n",
       "     SubFPC306  SubFPC307  bond:C=S_carbonyl_thio_generic  \\\n",
       "0            0          6                               0   \n",
       "1            0          6                               0   \n",
       "2            0          9                               0   \n",
       "3            0         13                               0   \n",
       "4            0          7                               0   \n",
       "..         ...        ...                             ...   \n",
       "582          0          0                               1   \n",
       "583          0          0                               1   \n",
       "584          0          1                               0   \n",
       "585          0          0                               0   \n",
       "586          0          1                               0   \n",
       "\n",
       "     bond:CN_amine_pri-NH2_aromatic  bond:CS_sulfide  \\\n",
       "0                                 1                0   \n",
       "1                                 1                0   \n",
       "2                                 0                1   \n",
       "3                                 0                0   \n",
       "4                                 0                0   \n",
       "..                              ...              ...   \n",
       "582                               0                1   \n",
       "583                               0                0   \n",
       "584                               0                0   \n",
       "585                               0                1   \n",
       "586                               0                1   \n",
       "\n",
       "     bond:S=O_sulfonyl_generic  ring:hetero_[6_6]_O_benzopyrone_(1_4-)  \n",
       "0                            0                                       0  \n",
       "1                            0                                       0  \n",
       "2                            0                                       0  \n",
       "3                            0                                       0  \n",
       "4                            0                                       0  \n",
       "..                         ...                                     ...  \n",
       "582                          0                                       0  \n",
       "583                          0                                       0  \n",
       "584                          0                                       0  \n",
       "585                          0                                       0  \n",
       "586                          0                                       0  \n",
       "\n",
       "[587 rows x 1096 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "APCFP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "target_bin = APCFP.loc[:, 'target_name_bin']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "target_bin = pd.DataFrame(target_bin)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "target_ter = APCFP.loc[:, 'target_name_ter']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "target_ter = pd.DataFrame(target_ter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "target_quater = APCFP.loc[:, 'target_name_quater']\n",
    "target_quater = pd.DataFrame(target_quater)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "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>target_name_ter</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>582</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>583</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>584</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>585</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>586</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>587 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    target_name_ter\n",
       "0                 A\n",
       "1                 A\n",
       "2                 A\n",
       "3                 A\n",
       "4                 A\n",
       "..              ...\n",
       "582               C\n",
       "583               C\n",
       "584               C\n",
       "585               C\n",
       "586               C\n",
       "\n",
       "[587 rows x 1 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target_ter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "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>target_name_quater</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>A1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>A1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>A1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>A1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>A1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>582</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>583</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>584</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>585</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>586</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>587 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    target_name_quater\n",
       "0                   A1\n",
       "1                   A1\n",
       "2                   A1\n",
       "3                   A1\n",
       "4                   A1\n",
       "..                 ...\n",
       "582                  C\n",
       "583                  C\n",
       "584                  C\n",
       "585                  C\n",
       "586                  C\n",
       "\n",
       "[587 rows x 1 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target_quater"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "features = APCFP.loc[:, 'APC2D1_C_C':'ring:hetero_[6_6]_O_benzopyrone_(1_4-)']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "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>APC2D1_C_C</th>\n",
       "      <th>APC2D1_C_N</th>\n",
       "      <th>APC2D1_C_O</th>\n",
       "      <th>APC2D1_C_S</th>\n",
       "      <th>APC2D1_C_P</th>\n",
       "      <th>APC2D1_C_F</th>\n",
       "      <th>APC2D1_C_Cl</th>\n",
       "      <th>APC2D1_C_Br</th>\n",
       "      <th>APC2D1_C_I</th>\n",
       "      <th>APC2D1_C_B</th>\n",
       "      <th>...</th>\n",
       "      <th>SubFPC303</th>\n",
       "      <th>SubFPC304</th>\n",
       "      <th>SubFPC305</th>\n",
       "      <th>SubFPC306</th>\n",
       "      <th>SubFPC307</th>\n",
       "      <th>bond:C=S_carbonyl_thio_generic</th>\n",
       "      <th>bond:CN_amine_pri-NH2_aromatic</th>\n",
       "      <th>bond:CS_sulfide</th>\n",
       "      <th>bond:S=O_sulfonyl_generic</th>\n",
       "      <th>ring:hetero_[6_6]_O_benzopyrone_(1_4-)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>582</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>583</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>584</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>585</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>586</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>587 rows × 1092 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     APC2D1_C_C  APC2D1_C_N  APC2D1_C_O  APC2D1_C_S  APC2D1_C_P  APC2D1_C_F  \\\n",
       "0             6           1           0           0           0           0   \n",
       "1             6           1           2           0           0           0   \n",
       "2             7           3           3           1           0           0   \n",
       "3            16           0           2           0           0           0   \n",
       "4            10           0           2           0           0           0   \n",
       "..          ...         ...         ...         ...         ...         ...   \n",
       "582           3           2           0           3           0           0   \n",
       "583           2           2           0           1           0           0   \n",
       "584           4           1           0           2           0           0   \n",
       "585           4           1           0           2           0           0   \n",
       "586           2           1           0           2           0           0   \n",
       "\n",
       "     APC2D1_C_Cl  APC2D1_C_Br  APC2D1_C_I  APC2D1_C_B  ...  SubFPC303  \\\n",
       "0              0            0           0           0  ...          0   \n",
       "1              0            0           0           0  ...          0   \n",
       "2              0            0           0           0  ...          0   \n",
       "3              0            0           0           0  ...          0   \n",
       "4              0            0           0           0  ...          0   \n",
       "..           ...          ...         ...         ...  ...        ...   \n",
       "582            0            0           0           0  ...          0   \n",
       "583            0            0           0           0  ...          0   \n",
       "584            0            0           0           0  ...          0   \n",
       "585            0            0           0           0  ...          0   \n",
       "586            0            0           0           0  ...          0   \n",
       "\n",
       "     SubFPC304  SubFPC305  SubFPC306  SubFPC307  \\\n",
       "0            0          0          0          6   \n",
       "1            0          0          0          6   \n",
       "2            0          0          0          9   \n",
       "3            0          0          0         13   \n",
       "4            0          0          0          7   \n",
       "..         ...        ...        ...        ...   \n",
       "582          0          0          0          0   \n",
       "583          0          0          0          0   \n",
       "584          0          0          0          1   \n",
       "585          0          0          0          0   \n",
       "586          0          0          0          1   \n",
       "\n",
       "     bond:C=S_carbonyl_thio_generic  bond:CN_amine_pri-NH2_aromatic  \\\n",
       "0                                 0                               1   \n",
       "1                                 0                               1   \n",
       "2                                 0                               0   \n",
       "3                                 0                               0   \n",
       "4                                 0                               0   \n",
       "..                              ...                             ...   \n",
       "582                               1                               0   \n",
       "583                               1                               0   \n",
       "584                               0                               0   \n",
       "585                               0                               0   \n",
       "586                               0                               0   \n",
       "\n",
       "     bond:CS_sulfide  bond:S=O_sulfonyl_generic  \\\n",
       "0                  0                          0   \n",
       "1                  0                          0   \n",
       "2                  1                          0   \n",
       "3                  0                          0   \n",
       "4                  0                          0   \n",
       "..               ...                        ...   \n",
       "582                1                          0   \n",
       "583                0                          0   \n",
       "584                0                          0   \n",
       "585                1                          0   \n",
       "586                1                          0   \n",
       "\n",
       "     ring:hetero_[6_6]_O_benzopyrone_(1_4-)  \n",
       "0                                         0  \n",
       "1                                         0  \n",
       "2                                         0  \n",
       "3                                         0  \n",
       "4                                         0  \n",
       "..                                      ...  \n",
       "582                                       0  \n",
       "583                                       0  \n",
       "584                                       0  \n",
       "585                                       0  \n",
       "586                                       0  \n",
       "\n",
       "[587 rows x 1092 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "from imblearn.over_sampling import *\n",
    "from sklearn.ensemble import AdaBoostClassifier"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AtomPairCount_Binary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "seed=28\n",
    "X_train_bin, X_test_bin, y_train_bin, y_test_bin = train_test_split(features, target_bin, test_size=0.2, random_state=seed, stratify=target_bin)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe_ada_pca = Pipeline([(\"std\", MinMaxScaler()),\n",
    "                        (\"thresholder\", VarianceThreshold(threshold=0.01)),\n",
    "                        (\"PCA\", PCA()),\n",
    "                        (\"smote\", SMOTE(random_state=seed)),\n",
    "                        (\"classifier\", AdaBoostClassifier(random_state=seed))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "random_grid_pca = [{\"PCA__n_components\": [1, 2, 3],\n",
    "                   \"classifier__n_estimators\": [200, 400, 800, 1600, 3200],\n",
    "                   \"classifier__learning_rate\": [0.2, 0.4, 0.6, 0.8, 1.0]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "cv = StratifiedKFold(n_splits=5, random_state=seed, shuffle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "rand_ada_pca = RandomizedSearchCV(pipe_ada_pca, random_grid_pca, n_iter=50, cv=cv, random_state=seed, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 50 candidates, totalling 250 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:   16.2s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:   53.5s\n",
      "[Parallel(n_jobs=-1)]: Done 250 out of 250 | elapsed:  1.4min finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "RandomizedSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "                   error_score=nan,\n",
       "                   estimator=Pipeline(memory=None,\n",
       "                                      steps=[('std',\n",
       "                                              MinMaxScaler(copy=True,\n",
       "                                                           feature_range=(0,\n",
       "                                                                          1))),\n",
       "                                             ('thresholder',\n",
       "                                              VarianceThreshold(threshold=0.01)),\n",
       "                                             ('PCA',\n",
       "                                              PCA(copy=True,\n",
       "                                                  iterated_power='auto',\n",
       "                                                  n_components=None,\n",
       "                                                  random_state=None,\n",
       "                                                  svd_solver='auto', tol=0.0,\n",
       "                                                  whiten=Fal...\n",
       "                                                                 learning_rate=1.0,\n",
       "                                                                 n_estimators=50,\n",
       "                                                                 random_state=28))],\n",
       "                                      verbose=False),\n",
       "                   iid='deprecated', n_iter=50, n_jobs=-1,\n",
       "                   param_distributions=[{'PCA__n_components': [1, 2, 3],\n",
       "                                         'classifier__learning_rate': [0.2, 0.4,\n",
       "                                                                       0.6, 0.8,\n",
       "                                                                       1.0],\n",
       "                                         'classifier__n_estimators': [200, 400,\n",
       "                                                                      800, 1600,\n",
       "                                                                      3200]}],\n",
       "                   pre_dispatch='2*n_jobs', random_state=28, refit=True,\n",
       "                   return_train_score=False, scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_pca.fit(X_train_bin, y_train_bin.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5308820037439792"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_pca.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'classifier__n_estimators': 200,\n",
       " 'classifier__learning_rate': 0.6,\n",
       " 'PCA__n_components': 3}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_pca.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_grid_pca_bin = [{\"PCA__n_components\": [3],\n",
    "                       \"classifier__n_estimators\": [50, 100, 200, 300],\n",
    "                       \"classifier__learning_rate\": [0.5, 0.6, 0.7]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf_ada_pca_bin = GridSearchCV(pipe_ada_pca, param_grid_pca_bin, cv=cv, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 12 candidates, totalling 60 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:    1.4s\n",
      "[Parallel(n_jobs=-1)]: Done  60 out of  60 | elapsed:    3.6s finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "             error_score=nan,\n",
       "             estimator=Pipeline(memory=None,\n",
       "                                steps=[('std',\n",
       "                                        MinMaxScaler(copy=True,\n",
       "                                                     feature_range=(0, 1))),\n",
       "                                       ('thresholder',\n",
       "                                        VarianceThreshold(threshold=0.01)),\n",
       "                                       ('PCA',\n",
       "                                        PCA(copy=True, iterated_power='auto',\n",
       "                                            n_components=None,\n",
       "                                            random_state=None,\n",
       "                                            svd_solver='auto', tol=0.0,\n",
       "                                            whiten=False)),\n",
       "                                       (...\n",
       "                                        AdaBoostClassifier(algorithm='SAMME.R',\n",
       "                                                           base_estimator=None,\n",
       "                                                           learning_rate=1.0,\n",
       "                                                           n_estimators=50,\n",
       "                                                           random_state=28))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'PCA__n_components': [3],\n",
       "                          'classifier__learning_rate': [0.5, 0.6, 0.7],\n",
       "                          'classifier__n_estimators': [50, 100, 200, 300]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_pca_bin.fit(X_train_bin, y_train_bin.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'PCA__n_components': 3,\n",
       " 'classifier__learning_rate': 0.7,\n",
       " 'classifier__n_estimators': 300}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_pca_bin.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5318919125976331"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_pca_bin.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.5689\n"
     ]
    }
   ],
   "source": [
    "from sklearn import metrics\n",
    "bin_pca_predict = clf_ada_pca_bin.predict(X_test_bin)\n",
    "testscore_bin_pca = metrics.f1_score(y_test_bin, bin_pca_predict, average='macro')\n",
    "\n",
    "print(\"f1 score (test): {:.4f}\".format(testscore_bin_pca))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[68 29]\n",
      " [10 11]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_bin, bin_pca_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Binary_APC_AdaB_PCA.pkl']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import joblib\n",
    "joblib.dump(clf_ada_pca_bin, \"Binary_APC_AdaB_PCA.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LDA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe_ada_lda = Pipeline([(\"std\", MinMaxScaler()),\n",
    "                        (\"thresholder\", VarianceThreshold(threshold=0.01)),\n",
    "                        (\"LDA\", LinearDiscriminantAnalysis()),\n",
    "                        (\"smote\", SMOTE(random_state=seed)),\n",
    "                        (\"classifier\", AdaBoostClassifier(random_state=seed))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "random_grid_lda_bin = [{\"LDA__n_components\": [1],\n",
    "                        \"classifier__n_estimators\": [200, 400, 800, 1600, 3200],\n",
    "                        \"classifier__learning_rate\": [0.2, 0.4, 0.6, 0.8, 1.0]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "rand_ada_lda_bin = RandomizedSearchCV(pipe_ada_lda, random_grid_lda_bin, n_iter=50, cv=cv, random_state=seed, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\model_selection\\_search.py:281: UserWarning: The total space of parameters 25 is smaller than n_iter=50. Running 25 iterations. For exhaustive searches, use GridSearchCV.\n",
      "  % (grid_size, self.n_iter, grid_size), UserWarning)\n",
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 25 candidates, totalling 125 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:    6.5s\n",
      "[Parallel(n_jobs=-1)]: Done 125 out of 125 | elapsed:   42.4s finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "RandomizedSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "                   error_score=nan,\n",
       "                   estimator=Pipeline(memory=None,\n",
       "                                      steps=[('std',\n",
       "                                              MinMaxScaler(copy=True,\n",
       "                                                           feature_range=(0,\n",
       "                                                                          1))),\n",
       "                                             ('thresholder',\n",
       "                                              VarianceThreshold(threshold=0.01)),\n",
       "                                             ('LDA',\n",
       "                                              LinearDiscriminantAnalysis(n_components=None,\n",
       "                                                                         priors=None,\n",
       "                                                                         shrinkage=None,\n",
       "                                                                         solver='svd',\n",
       "                                                                         store_covariance=False,...\n",
       "                                                                 learning_rate=1.0,\n",
       "                                                                 n_estimators=50,\n",
       "                                                                 random_state=28))],\n",
       "                                      verbose=False),\n",
       "                   iid='deprecated', n_iter=50, n_jobs=-1,\n",
       "                   param_distributions=[{'LDA__n_components': [1],\n",
       "                                         'classifier__learning_rate': [0.2, 0.4,\n",
       "                                                                       0.6, 0.8,\n",
       "                                                                       1.0],\n",
       "                                         'classifier__n_estimators': [200, 400,\n",
       "                                                                      800, 1600,\n",
       "                                                                      3200]}],\n",
       "                   pre_dispatch='2*n_jobs', random_state=28, refit=True,\n",
       "                   return_train_score=False, scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_lda_bin.fit(X_train_bin, y_train_bin.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6199672416647858"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_lda_bin.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'classifier__n_estimators': 800,\n",
       " 'classifier__learning_rate': 0.2,\n",
       " 'LDA__n_components': 1}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_lda_bin.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_grid_lda_bin = [{\"LDA__n_components\": [1],\n",
    "                       \"classifier__n_estimators\": [600, 800, 1000, 1200, 1400],\n",
    "                       \"classifier__learning_rate\": [0.1, 0.2, 0.3]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf_ada_lda_bin = GridSearchCV(pipe_ada_lda, param_grid_lda_bin, cv=cv, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 15 candidates, totalling 75 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:   18.8s\n",
      "[Parallel(n_jobs=-1)]: Done  75 out of  75 | elapsed:   32.1s finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "             error_score=nan,\n",
       "             estimator=Pipeline(memory=None,\n",
       "                                steps=[('std',\n",
       "                                        MinMaxScaler(copy=True,\n",
       "                                                     feature_range=(0, 1))),\n",
       "                                       ('thresholder',\n",
       "                                        VarianceThreshold(threshold=0.01)),\n",
       "                                       ('LDA',\n",
       "                                        LinearDiscriminantAnalysis(n_components=None,\n",
       "                                                                   priors=None,\n",
       "                                                                   shrinkage=None,\n",
       "                                                                   solver='svd',\n",
       "                                                                   store_covariance=False,\n",
       "                                                                   tol=0....\n",
       "                                        AdaBoostClassifier(algorithm='SAMME.R',\n",
       "                                                           base_estimator=None,\n",
       "                                                           learning_rate=1.0,\n",
       "                                                           n_estimators=50,\n",
       "                                                           random_state=28))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'LDA__n_components': [1],\n",
       "                          'classifier__learning_rate': [0.1, 0.2, 0.3],\n",
       "                          'classifier__n_estimators': [600, 800, 1000, 1200,\n",
       "                                                       1400]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_lda_bin.fit(X_train_bin, y_train_bin.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'LDA__n_components': 1,\n",
       " 'classifier__learning_rate': 0.2,\n",
       " 'classifier__n_estimators': 800}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_lda_bin.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6199672416647858"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_lda_bin.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.5851\n"
     ]
    }
   ],
   "source": [
    "bin_lda_predict = clf_ada_lda_bin.predict(X_test_bin)\n",
    "testscore_bin_lda = metrics.f1_score(y_test_bin, bin_lda_predict, average='macro')\n",
    "\n",
    "print(\"f1 score (test): {:.4f}\".format(testscore_bin_lda))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[73 24]\n",
      " [11 10]]\n"
     ]
    }
   ],
   "source": [
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_bin, bin_lda_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Binary_APC_AdaB_LDA.pkl']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_ada_lda_bin, \"Binary_APC_AdaB_LDA.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AtomPairCount_Ternary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_ter, X_test_ter, y_train_ter, y_test_ter = train_test_split(features, target_ter, test_size=0.2, random_state=seed, stratify=target_ter)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 50 candidates, totalling 250 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:   10.1s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:   50.1s\n",
      "[Parallel(n_jobs=-1)]: Done 250 out of 250 | elapsed:  1.4min finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "RandomizedSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "                   error_score=nan,\n",
       "                   estimator=Pipeline(memory=None,\n",
       "                                      steps=[('std',\n",
       "                                              MinMaxScaler(copy=True,\n",
       "                                                           feature_range=(0,\n",
       "                                                                          1))),\n",
       "                                             ('thresholder',\n",
       "                                              VarianceThreshold(threshold=0.01)),\n",
       "                                             ('PCA',\n",
       "                                              PCA(copy=True,\n",
       "                                                  iterated_power='auto',\n",
       "                                                  n_components=None,\n",
       "                                                  random_state=None,\n",
       "                                                  svd_solver='auto', tol=0.0,\n",
       "                                                  whiten=Fal...\n",
       "                                                                 learning_rate=1.0,\n",
       "                                                                 n_estimators=50,\n",
       "                                                                 random_state=28))],\n",
       "                                      verbose=False),\n",
       "                   iid='deprecated', n_iter=50, n_jobs=-1,\n",
       "                   param_distributions=[{'PCA__n_components': [1, 2, 3],\n",
       "                                         'classifier__learning_rate': [0.2, 0.4,\n",
       "                                                                       0.6, 0.8,\n",
       "                                                                       1.0],\n",
       "                                         'classifier__n_estimators': [200, 400,\n",
       "                                                                      800, 1600,\n",
       "                                                                      3200]}],\n",
       "                   pre_dispatch='2*n_jobs', random_state=28, refit=True,\n",
       "                   return_train_score=False, scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_pca.fit(X_train_ter, y_train_ter.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3820138280191391"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_pca.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'classifier__n_estimators': 200,\n",
       " 'classifier__learning_rate': 1.0,\n",
       " 'PCA__n_components': 3}"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_pca.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_grid_pca_ter = [{\"PCA__n_components\": [3],\n",
    "                       \"classifier__n_estimators\": [50, 100, 200, 300],\n",
    "                       \"classifier__learning_rate\": [0.9, 1.0]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf_ada_pca_ter = GridSearchCV(pipe_ada_pca, param_grid_pca_ter, cv=cv, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 8 candidates, totalling 40 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:    1.5s\n",
      "[Parallel(n_jobs=-1)]: Done  40 out of  40 | elapsed:    2.5s finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "             error_score=nan,\n",
       "             estimator=Pipeline(memory=None,\n",
       "                                steps=[('std',\n",
       "                                        MinMaxScaler(copy=True,\n",
       "                                                     feature_range=(0, 1))),\n",
       "                                       ('thresholder',\n",
       "                                        VarianceThreshold(threshold=0.01)),\n",
       "                                       ('PCA',\n",
       "                                        PCA(copy=True, iterated_power='auto',\n",
       "                                            n_components=None,\n",
       "                                            random_state=None,\n",
       "                                            svd_solver='auto', tol=0.0,\n",
       "                                            whiten=False)),\n",
       "                                       (...\n",
       "                                        AdaBoostClassifier(algorithm='SAMME.R',\n",
       "                                                           base_estimator=None,\n",
       "                                                           learning_rate=1.0,\n",
       "                                                           n_estimators=50,\n",
       "                                                           random_state=28))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'PCA__n_components': [3],\n",
       "                          'classifier__learning_rate': [0.9, 1.0],\n",
       "                          'classifier__n_estimators': [50, 100, 200, 300]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_pca_ter.fit(X_train_ter, y_train_ter.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'PCA__n_components': 3,\n",
       " 'classifier__learning_rate': 1.0,\n",
       " 'classifier__n_estimators': 50}"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_pca_ter.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.39323965955360646"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_pca_ter.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.3881\n"
     ]
    }
   ],
   "source": [
    "ter_pca_predict = clf_ada_pca_ter.predict(X_test_ter)\n",
    "testscore_ter_pca = metrics.f1_score(y_test_ter, ter_pca_predict, average='macro')\n",
    "\n",
    "print(\"f1 score (test): {:.4f}\".format(testscore_ter_pca))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[34 23 14]\n",
      " [11  8  7]\n",
      " [ 6  6  9]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_ter, ter_pca_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Ternary_APC_AdaB_PCA.pkl']"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_ada_pca_ter, 'Ternary_APC_AdaB_PCA.pkl')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LDA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "random_grid_lda_ter = [{\"LDA__n_components\": [1, 2],\n",
    "                        \"classifier__n_estimators\": [200, 400, 800, 1600, 3200],\n",
    "                        \"classifier__learning_rate\": [0.2, 0.4, 0.6, 0.8, 1.0]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "rand_ada_lda_ter = RandomizedSearchCV(pipe_ada_lda, random_grid_lda_ter, n_iter=100, cv=cv, random_state=seed, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\model_selection\\_search.py:281: UserWarning: The total space of parameters 50 is smaller than n_iter=100. Running 50 iterations. For exhaustive searches, use GridSearchCV.\n",
      "  % (grid_size, self.n_iter, grid_size), UserWarning)\n",
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 50 candidates, totalling 250 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:    7.3s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:   50.3s\n",
      "[Parallel(n_jobs=-1)]: Done 250 out of 250 | elapsed:  1.5min finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "RandomizedSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "                   error_score=nan,\n",
       "                   estimator=Pipeline(memory=None,\n",
       "                                      steps=[('std',\n",
       "                                              MinMaxScaler(copy=True,\n",
       "                                                           feature_range=(0,\n",
       "                                                                          1))),\n",
       "                                             ('thresholder',\n",
       "                                              VarianceThreshold(threshold=0.01)),\n",
       "                                             ('LDA',\n",
       "                                              LinearDiscriminantAnalysis(n_components=None,\n",
       "                                                                         priors=None,\n",
       "                                                                         shrinkage=None,\n",
       "                                                                         solver='svd',\n",
       "                                                                         store_covariance=False,...\n",
       "                                                                 learning_rate=1.0,\n",
       "                                                                 n_estimators=50,\n",
       "                                                                 random_state=28))],\n",
       "                                      verbose=False),\n",
       "                   iid='deprecated', n_iter=100, n_jobs=-1,\n",
       "                   param_distributions=[{'LDA__n_components': [1, 2],\n",
       "                                         'classifier__learning_rate': [0.2, 0.4,\n",
       "                                                                       0.6, 0.8,\n",
       "                                                                       1.0],\n",
       "                                         'classifier__n_estimators': [200, 400,\n",
       "                                                                      800, 1600,\n",
       "                                                                      3200]}],\n",
       "                   pre_dispatch='2*n_jobs', random_state=28, refit=True,\n",
       "                   return_train_score=False, scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_lda_ter.fit(X_train_ter, y_train_ter.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4715210460226621"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_lda_ter.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'classifier__n_estimators': 200,\n",
       " 'classifier__learning_rate': 0.2,\n",
       " 'LDA__n_components': 2}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_lda_ter.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_grid_lda_ter = [{\"LDA__n_components\": [2],\n",
    "                       \"classifier__n_estimators\": [50, 100, 200, 300],\n",
    "                       \"classifier__learning_rate\": [0.1, 0.2, 0.3]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf_ada_lda_ter = GridSearchCV(pipe_ada_lda, param_grid_lda_ter, cv=cv, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 12 candidates, totalling 60 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:    1.4s\n",
      "[Parallel(n_jobs=-1)]: Done  60 out of  60 | elapsed:    3.8s finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "             error_score=nan,\n",
       "             estimator=Pipeline(memory=None,\n",
       "                                steps=[('std',\n",
       "                                        MinMaxScaler(copy=True,\n",
       "                                                     feature_range=(0, 1))),\n",
       "                                       ('thresholder',\n",
       "                                        VarianceThreshold(threshold=0.01)),\n",
       "                                       ('LDA',\n",
       "                                        LinearDiscriminantAnalysis(n_components=None,\n",
       "                                                                   priors=None,\n",
       "                                                                   shrinkage=None,\n",
       "                                                                   solver='svd',\n",
       "                                                                   store_covariance=False,\n",
       "                                                                   tol=0....\n",
       "                                        AdaBoostClassifier(algorithm='SAMME.R',\n",
       "                                                           base_estimator=None,\n",
       "                                                           learning_rate=1.0,\n",
       "                                                           n_estimators=50,\n",
       "                                                           random_state=28))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'LDA__n_components': [2],\n",
       "                          'classifier__learning_rate': [0.1, 0.2, 0.3],\n",
       "                          'classifier__n_estimators': [50, 100, 200, 300]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_lda_ter.fit(X_train_ter, y_train_ter.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'LDA__n_components': 2,\n",
       " 'classifier__learning_rate': 0.1,\n",
       " 'classifier__n_estimators': 200}"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_lda_ter.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.48105004504960985"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_lda_ter.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.4167\n"
     ]
    }
   ],
   "source": [
    "from sklearn import metrics\n",
    "ter_lda_predict = clf_ada_lda_ter.predict(X_test_ter)\n",
    "testscore_ter_lda = metrics.f1_score(y_test_ter, ter_lda_predict, average='macro')\n",
    "\n",
    "print(\"f1 score (test): {:.4f}\".format(testscore_ter_lda))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[46 15 10]\n",
      " [13  6  7]\n",
      " [ 3 10  8]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_ter, ter_lda_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Ternary_APC_AdaB_LDA.pkl']"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import joblib\n",
    "joblib.dump(clf_ada_lda_ter, 'Ternary_APC_AdaB_LDA.pkl')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Quaternary(w/o SMOTE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_quater, X_test_quater, y_train_quater, y_test_quater = train_test_split(features, target_quater, test_size=0.2, random_state=seed, stratify=target_quater)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe_ada_pca_smotx = Pipeline([(\"std\", MinMaxScaler()),\n",
    "                               (\"thresholder\", VarianceThreshold(threshold=0.01)),\n",
    "                               (\"PCA\", PCA()),\n",
    "                               (\"classifier\", AdaBoostClassifier(random_state=seed))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "rand_ada_pca_smotx = RandomizedSearchCV(pipe_ada_pca_smotx, random_grid_pca, n_iter=100, cv=cv, random_state=seed, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\model_selection\\_search.py:281: UserWarning: The total space of parameters 75 is smaller than n_iter=100. Running 75 iterations. For exhaustive searches, use GridSearchCV.\n",
      "  % (grid_size, self.n_iter, grid_size), UserWarning)\n",
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 75 candidates, totalling 375 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:    5.9s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:   42.0s\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  1.7min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  1.9min finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "RandomizedSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "                   error_score=nan,\n",
       "                   estimator=Pipeline(memory=None,\n",
       "                                      steps=[('std',\n",
       "                                              MinMaxScaler(copy=True,\n",
       "                                                           feature_range=(0,\n",
       "                                                                          1))),\n",
       "                                             ('thresholder',\n",
       "                                              VarianceThreshold(threshold=0.01)),\n",
       "                                             ('PCA',\n",
       "                                              PCA(copy=True,\n",
       "                                                  iterated_power='auto',\n",
       "                                                  n_components=None,\n",
       "                                                  random_state=None,\n",
       "                                                  svd_solver='auto', tol=0.0,\n",
       "                                                  whiten=Fal...\n",
       "                                                                 learning_rate=1.0,\n",
       "                                                                 n_estimators=50,\n",
       "                                                                 random_state=28))],\n",
       "                                      verbose=False),\n",
       "                   iid='deprecated', n_iter=100, n_jobs=-1,\n",
       "                   param_distributions=[{'PCA__n_components': [1, 2, 3],\n",
       "                                         'classifier__learning_rate': [0.2, 0.4,\n",
       "                                                                       0.6, 0.8,\n",
       "                                                                       1.0],\n",
       "                                         'classifier__n_estimators': [200, 400,\n",
       "                                                                      800, 1600,\n",
       "                                                                      3200]}],\n",
       "                   pre_dispatch='2*n_jobs', random_state=28, refit=True,\n",
       "                   return_train_score=False, scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_pca_smotx.fit(X_train_quater, y_train_quater.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3121702948564985"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_pca_smotx.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'classifier__n_estimators': 200,\n",
       " 'classifier__learning_rate': 1.0,\n",
       " 'PCA__n_components': 3}"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_pca_smotx.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_grid_pca_quater = [{\"PCA__n_components\": [3],\n",
    "                       \"classifier__n_estimators\": [50, 100, 200, 300],\n",
    "                       \"classifier__learning_rate\": [0.9, 1.0]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf_ada_pca_smotx = GridSearchCV(pipe_ada_pca_smotx, param_grid_pca_quater, cv=cv, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 8 candidates, totalling 40 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:    1.3s\n",
      "[Parallel(n_jobs=-1)]: Done  40 out of  40 | elapsed:    2.2s finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "             error_score=nan,\n",
       "             estimator=Pipeline(memory=None,\n",
       "                                steps=[('std',\n",
       "                                        MinMaxScaler(copy=True,\n",
       "                                                     feature_range=(0, 1))),\n",
       "                                       ('thresholder',\n",
       "                                        VarianceThreshold(threshold=0.01)),\n",
       "                                       ('PCA',\n",
       "                                        PCA(copy=True, iterated_power='auto',\n",
       "                                            n_components=None,\n",
       "                                            random_state=None,\n",
       "                                            svd_solver='auto', tol=0.0,\n",
       "                                            whiten=False)),\n",
       "                                       (...\n",
       "                                        AdaBoostClassifier(algorithm='SAMME.R',\n",
       "                                                           base_estimator=None,\n",
       "                                                           learning_rate=1.0,\n",
       "                                                           n_estimators=50,\n",
       "                                                           random_state=28))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'PCA__n_components': [3],\n",
       "                          'classifier__learning_rate': [0.9, 1.0],\n",
       "                          'classifier__n_estimators': [50, 100, 200, 300]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_pca_smotx.fit(X_train_quater, y_train_quater.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'PCA__n_components': 3,\n",
       " 'classifier__learning_rate': 0.9,\n",
       " 'classifier__n_estimators': 50}"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_pca_smotx.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.32215983649989444"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_pca_smotx.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.3580\n"
     ]
    }
   ],
   "source": [
    "quater_pca_predict = clf_ada_pca_smotx.predict(X_test_quater)\n",
    "testscore_quater_pca = metrics.f1_score(y_test_quater, quater_pca_predict, average='macro')\n",
    "\n",
    "print(\"f1 score (test): {:.4f}\".format(testscore_quater_pca))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[ 7 12  5  1]\n",
      " [ 7 25 10  4]\n",
      " [ 2 13  9  2]\n",
      " [ 2  9  5  5]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_quater, quater_pca_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Quaternary_APC_AdaB_PCA_smotx.pkl']"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_ada_pca_smotx, 'Quaternary_APC_AdaB_PCA_smotx.pkl')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LDA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "random_grid_lda_quater = [{\"LDA__n_components\": [1, 2, 3],\n",
    "                          \"classifier__n_estimators\": [200, 400, 800, 1600, 3200],\n",
    "                          \"classifier__learning_rate\": [0.2, 0.4, 0.6, 0.8, 1.0]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe_ada_lda_smotx = Pipeline([(\"std\", MinMaxScaler()),\n",
    "                              (\"thresholder\", VarianceThreshold(threshold=0.01)),\n",
    "                              (\"LDA\", LinearDiscriminantAnalysis()),\n",
    "                              (\"classifier\", AdaBoostClassifier(random_state=seed))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "rand_ada_lda_smotx = RandomizedSearchCV(pipe_ada_lda_smotx, random_grid_lda_quater, n_iter=100, cv=cv, random_state=seed, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\model_selection\\_search.py:281: UserWarning: The total space of parameters 75 is smaller than n_iter=100. Running 75 iterations. For exhaustive searches, use GridSearchCV.\n",
      "  % (grid_size, self.n_iter, grid_size), UserWarning)\n",
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 75 candidates, totalling 375 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:    5.8s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:   42.0s\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  1.7min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  1.9min finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "RandomizedSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "                   error_score=nan,\n",
       "                   estimator=Pipeline(memory=None,\n",
       "                                      steps=[('std',\n",
       "                                              MinMaxScaler(copy=True,\n",
       "                                                           feature_range=(0,\n",
       "                                                                          1))),\n",
       "                                             ('thresholder',\n",
       "                                              VarianceThreshold(threshold=0.01)),\n",
       "                                             ('LDA',\n",
       "                                              LinearDiscriminantAnalysis(n_components=None,\n",
       "                                                                         priors=None,\n",
       "                                                                         shrinkage=None,\n",
       "                                                                         solver='svd',\n",
       "                                                                         store_covariance=False,...\n",
       "                                                                 learning_rate=1.0,\n",
       "                                                                 n_estimators=50,\n",
       "                                                                 random_state=28))],\n",
       "                                      verbose=False),\n",
       "                   iid='deprecated', n_iter=100, n_jobs=-1,\n",
       "                   param_distributions=[{'LDA__n_components': [1, 2, 3],\n",
       "                                         'classifier__learning_rate': [0.2, 0.4,\n",
       "                                                                       0.6, 0.8,\n",
       "                                                                       1.0],\n",
       "                                         'classifier__n_estimators': [200, 400,\n",
       "                                                                      800, 1600,\n",
       "                                                                      3200]}],\n",
       "                   pre_dispatch='2*n_jobs', random_state=28, refit=True,\n",
       "                   return_train_score=False, scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_lda_smotx.fit(X_train_quater, y_train_quater.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3693152404348332"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_lda_smotx.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'classifier__n_estimators': 200,\n",
       " 'classifier__learning_rate': 0.2,\n",
       " 'LDA__n_components': 2}"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_lda_smotx.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_grid_lda_quater = [{\"LDA__n_components\": [2],\n",
    "                       \"classifier__n_estimators\": [50, 100, 200, 300],\n",
    "                       \"classifier__learning_rate\": [0.1, 0.2, 0.3]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf_ada_lda_smotx = GridSearchCV(pipe_ada_lda_smotx, param_grid_lda_quater, cv=cv, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 12 candidates, totalling 60 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:    1.4s\n",
      "[Parallel(n_jobs=-1)]: Done  60 out of  60 | elapsed:    3.6s finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "             error_score=nan,\n",
       "             estimator=Pipeline(memory=None,\n",
       "                                steps=[('std',\n",
       "                                        MinMaxScaler(copy=True,\n",
       "                                                     feature_range=(0, 1))),\n",
       "                                       ('thresholder',\n",
       "                                        VarianceThreshold(threshold=0.01)),\n",
       "                                       ('LDA',\n",
       "                                        LinearDiscriminantAnalysis(n_components=None,\n",
       "                                                                   priors=None,\n",
       "                                                                   shrinkage=None,\n",
       "                                                                   solver='svd',\n",
       "                                                                   store_covariance=False,\n",
       "                                                                   tol=0....\n",
       "                                        AdaBoostClassifier(algorithm='SAMME.R',\n",
       "                                                           base_estimator=None,\n",
       "                                                           learning_rate=1.0,\n",
       "                                                           n_estimators=50,\n",
       "                                                           random_state=28))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'LDA__n_components': [2],\n",
       "                          'classifier__learning_rate': [0.1, 0.2, 0.3],\n",
       "                          'classifier__n_estimators': [50, 100, 200, 300]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_lda_smotx.fit(X_train_quater, y_train_quater.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'LDA__n_components': 2,\n",
       " 'classifier__learning_rate': 0.3,\n",
       " 'classifier__n_estimators': 100}"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_lda_smotx.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.39118734459450377"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_lda_smotx.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.4013\n"
     ]
    }
   ],
   "source": [
    "quater_pca_predict = clf_ada_lda_smotx.predict(X_test_quater)\n",
    "testscore_quater_pca = metrics.f1_score(y_test_quater, quater_pca_predict, average='macro')\n",
    "\n",
    "print(\"f1 score (test): {:.4f}\".format(testscore_quater_pca))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[18  5  2  0]\n",
      " [10 20  8  8]\n",
      " [ 4 13  3  6]\n",
      " [ 2  5  5  9]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_quater, quater_pca_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Quaternary_APC_AdaB_LDA_smotx.pkl']"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_ada_lda_smotx, 'Quaternary_APC_AdaB_LDA_smotx.pkl')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Quaternary (w/ SMOTE)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 50 candidates, totalling 250 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:   10.3s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:   55.4s\n",
      "[Parallel(n_jobs=-1)]: Done 250 out of 250 | elapsed:  1.5min finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "RandomizedSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "                   error_score=nan,\n",
       "                   estimator=Pipeline(memory=None,\n",
       "                                      steps=[('std',\n",
       "                                              MinMaxScaler(copy=True,\n",
       "                                                           feature_range=(0,\n",
       "                                                                          1))),\n",
       "                                             ('thresholder',\n",
       "                                              VarianceThreshold(threshold=0.01)),\n",
       "                                             ('PCA',\n",
       "                                              PCA(copy=True,\n",
       "                                                  iterated_power='auto',\n",
       "                                                  n_components=None,\n",
       "                                                  random_state=None,\n",
       "                                                  svd_solver='auto', tol=0.0,\n",
       "                                                  whiten=Fal...\n",
       "                                                                 learning_rate=1.0,\n",
       "                                                                 n_estimators=50,\n",
       "                                                                 random_state=28))],\n",
       "                                      verbose=False),\n",
       "                   iid='deprecated', n_iter=50, n_jobs=-1,\n",
       "                   param_distributions=[{'PCA__n_components': [1, 2, 3],\n",
       "                                         'classifier__learning_rate': [0.2, 0.4,\n",
       "                                                                       0.6, 0.8,\n",
       "                                                                       1.0],\n",
       "                                         'classifier__n_estimators': [200, 400,\n",
       "                                                                      800, 1600,\n",
       "                                                                      3200]}],\n",
       "                   pre_dispatch='2*n_jobs', random_state=28, refit=True,\n",
       "                   return_train_score=False, scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_pca.fit(X_train_quater, y_train_quater.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3178850027046884"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_pca.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'classifier__n_estimators': 3200,\n",
       " 'classifier__learning_rate': 0.4,\n",
       " 'PCA__n_components': 3}"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_pca.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_grid_pca_quater_smot = [{\"PCA__n_components\": [3],\n",
    "                               \"classifier__n_estimators\": [1800, 2000, 2200, 2400, 2600, 2800, 3000, 3200, 3500],\n",
    "                               \"classifier__learning_rate\": [0.3, 0.4, 0.5]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf_ada_pca_quater_smot = GridSearchCV(pipe_ada_pca, param_grid_pca_quater_smot, cv=cv, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 27 candidates, totalling 135 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:   21.6s\n",
      "[Parallel(n_jobs=-1)]: Done 135 out of 135 | elapsed:  1.8min finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "             error_score=nan,\n",
       "             estimator=Pipeline(memory=None,\n",
       "                                steps=[('std',\n",
       "                                        MinMaxScaler(copy=True,\n",
       "                                                     feature_range=(0, 1))),\n",
       "                                       ('thresholder',\n",
       "                                        VarianceThreshold(threshold=0.01)),\n",
       "                                       ('PCA',\n",
       "                                        PCA(copy=True, iterated_power='auto',\n",
       "                                            n_components=None,\n",
       "                                            random_state=None,\n",
       "                                            svd_solver='auto', tol=0.0,\n",
       "                                            whiten=False)),\n",
       "                                       (...\n",
       "                                                           base_estimator=None,\n",
       "                                                           learning_rate=1.0,\n",
       "                                                           n_estimators=50,\n",
       "                                                           random_state=28))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'PCA__n_components': [3],\n",
       "                          'classifier__learning_rate': [0.3, 0.4, 0.5],\n",
       "                          'classifier__n_estimators': [1800, 2000, 2200, 2400,\n",
       "                                                       2600, 2800, 3000, 3200,\n",
       "                                                       3500]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_pca_quater_smot.fit(X_train_quater, y_train_quater.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'PCA__n_components': 3,\n",
       " 'classifier__learning_rate': 0.4,\n",
       " 'classifier__n_estimators': 3500}"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_pca_quater_smot.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3186674356753133"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_pca_quater_smot.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.3800\n"
     ]
    }
   ],
   "source": [
    "quater_smot_pca_predict = clf_ada_pca_quater_smot.predict(X_test_quater)\n",
    "testscore_quater_smot_pca = metrics.f1_score(y_test_quater, quater_smot_pca_predict, average='macro')\n",
    "\n",
    "print(\"f1 score (test): {:.4f}\".format(testscore_quater_smot_pca))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[13  8  3  1]\n",
      " [12 14 12  8]\n",
      " [ 3 12  7  4]\n",
      " [ 4  5  2 10]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_quater, quater_smot_pca_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Quaternary_APC_AdaB_PCA_smot.pkl']"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_ada_pca_quater_smot, 'Quaternary_APC_AdaB_PCA_smot.pkl')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LDA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [],
   "source": [
    "rand_ada_lda_quater = RandomizedSearchCV(pipe_ada_lda, random_grid_lda_quater, n_iter=100, cv=cv, random_state=seed, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\model_selection\\_search.py:281: UserWarning: The total space of parameters 75 is smaller than n_iter=100. Running 75 iterations. For exhaustive searches, use GridSearchCV.\n",
      "  % (grid_size, self.n_iter, grid_size), UserWarning)\n",
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 75 candidates, totalling 375 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:   13.3s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:   55.1s\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  2.3min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  2.5min finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "RandomizedSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "                   error_score=nan,\n",
       "                   estimator=Pipeline(memory=None,\n",
       "                                      steps=[('std',\n",
       "                                              MinMaxScaler(copy=True,\n",
       "                                                           feature_range=(0,\n",
       "                                                                          1))),\n",
       "                                             ('thresholder',\n",
       "                                              VarianceThreshold(threshold=0.01)),\n",
       "                                             ('LDA',\n",
       "                                              LinearDiscriminantAnalysis(n_components=None,\n",
       "                                                                         priors=None,\n",
       "                                                                         shrinkage=None,\n",
       "                                                                         solver='svd',\n",
       "                                                                         store_covariance=False,...\n",
       "                                                                 learning_rate=1.0,\n",
       "                                                                 n_estimators=50,\n",
       "                                                                 random_state=28))],\n",
       "                                      verbose=False),\n",
       "                   iid='deprecated', n_iter=100, n_jobs=-1,\n",
       "                   param_distributions=[{'LDA__n_components': [1, 2, 3],\n",
       "                                         'classifier__learning_rate': [0.2, 0.4,\n",
       "                                                                       0.6, 0.8,\n",
       "                                                                       1.0],\n",
       "                                         'classifier__n_estimators': [200, 400,\n",
       "                                                                      800, 1600,\n",
       "                                                                      3200]}],\n",
       "                   pre_dispatch='2*n_jobs', random_state=28, refit=True,\n",
       "                   return_train_score=False, scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_lda_quater.fit(X_train_quater, y_train_quater.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3802628461880527"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_lda_quater.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'classifier__n_estimators': 200,\n",
       " 'classifier__learning_rate': 0.2,\n",
       " 'LDA__n_components': 3}"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_ada_lda_quater.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_grid_lda_quater_smot = [{\"LDA__n_components\": [3],\n",
    "                               \"classifier__n_estimators\": [50, 100, 200, 300],\n",
    "                               \"classifier__learning_rate\": [0.1, 0.2, 0.3]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf_ada_lda_quater_smot = GridSearchCV(pipe_ada_lda, param_grid_lda_quater_smot, cv=cv, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 12 candidates, totalling 60 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:    9.1s\n",
      "[Parallel(n_jobs=-1)]: Done  60 out of  60 | elapsed:   11.3s finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=StratifiedKFold(n_splits=5, random_state=28, shuffle=True),\n",
       "             error_score=nan,\n",
       "             estimator=Pipeline(memory=None,\n",
       "                                steps=[('std',\n",
       "                                        MinMaxScaler(copy=True,\n",
       "                                                     feature_range=(0, 1))),\n",
       "                                       ('thresholder',\n",
       "                                        VarianceThreshold(threshold=0.01)),\n",
       "                                       ('LDA',\n",
       "                                        LinearDiscriminantAnalysis(n_components=None,\n",
       "                                                                   priors=None,\n",
       "                                                                   shrinkage=None,\n",
       "                                                                   solver='svd',\n",
       "                                                                   store_covariance=False,\n",
       "                                                                   tol=0....\n",
       "                                        AdaBoostClassifier(algorithm='SAMME.R',\n",
       "                                                           base_estimator=None,\n",
       "                                                           learning_rate=1.0,\n",
       "                                                           n_estimators=50,\n",
       "                                                           random_state=28))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'LDA__n_components': [3],\n",
       "                          'classifier__learning_rate': [0.1, 0.2, 0.3],\n",
       "                          'classifier__n_estimators': [50, 100, 200, 300]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_lda_quater_smot.fit(X_train_quater, y_train_quater.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'LDA__n_components': 3,\n",
       " 'classifier__learning_rate': 0.1,\n",
       " 'classifier__n_estimators': 100}"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_lda_quater_smot.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.38368499383433113"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_ada_lda_quater_smot.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.3908\n"
     ]
    }
   ],
   "source": [
    "quater_smot_lda_predict = clf_ada_lda_quater_smot.predict(X_test_quater)\n",
    "testscore_quater_smot_lda = metrics.f1_score(y_test_quater, quater_smot_lda_predict, average='macro')\n",
    "\n",
    "print(\"f1 score (test): {:.4f}\".format(testscore_quater_smot_lda))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[17  5  3  0]\n",
      " [13 14  6 13]\n",
      " [ 5  6  6  9]\n",
      " [ 1  3  7 10]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_quater, quater_smot_lda_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Quaternary_APC_AdaB_LDA_smot.pkl']"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_ada_lda_quater_smot, 'Quaternary_APC_AdaB_LDA_smot.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
