{
 "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, 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",
       "      <th>0</th>\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",
       "      <th>1</th>\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",
       "      <th>2</th>\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",
       "      <th>3</th>\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",
       "      <th>4</th>\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",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>582</th>\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",
       "      <th>583</th>\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",
       "      <th>584</th>\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",
       "      <th>585</th>\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",
       "      <th>586</th>\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']\n",
    "target_ter = apcFP.loc[:, 'target_name_ter']\n",
    "target_quater = apcFP.loc[:, 'target_name_quater']"
   ]
  },
  {
   "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 = pd.DataFrame(target_ter)\n",
    "target_quater = pd.DataFrame(target_quater)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "features = apcFP.loc[:, 'APC2D1_C_C':'ring:hetero_[6_6]_O_benzopyrone_(1_4-)']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "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",
       "      <th>0</th>\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",
       "      <th>1</th>\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",
       "      <th>2</th>\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",
       "      <th>3</th>\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",
       "      <th>4</th>\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",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>582</th>\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",
       "      <th>583</th>\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",
       "      <th>584</th>\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",
       "      <th>585</th>\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",
       "      <th>586</th>\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": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from imblearn.over_sampling import *\n",
    "from sklearn.ensemble import RandomForestClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "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": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe_rf_pca = Pipeline([(\"std\", MinMaxScaler()),\n",
    "                        (\"thresholder\", VarianceThreshold(threshold=0.01)),\n",
    "                        (\"PCA\", PCA()),\n",
    "                        (\"smote\", SMOTE(random_state=seed)),\n",
    "                        (\"classifier\", RandomForestClassifier(random_state=seed))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_grid_pca = [{\"PCA__n_components\": [1, 2, 3],\n",
    "                   \"classifier__n_estimators\": [200, 400, 800, 1600, 3200],\n",
    "                   \"classifier__max_depth\": [5, 10, 20, 40, 80]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "cv = StratifiedKFold(n_splits=5, random_state=seed, shuffle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf_rf_pca = GridSearchCV(pipe_rf_pca, param_grid_pca, cv=cv, 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 75 candidates, totalling 375 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:   50.3s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:  2.6min\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  5.7min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  6.3min 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",
       "                                                               n_estimators=100,\n",
       "                                                               n_jobs=None,\n",
       "                                                               oob_score=False,\n",
       "                                                               random_state=28,\n",
       "                                                               verbose=0,\n",
       "                                                               warm_start=False))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'PCA__n_components': [1, 2, 3],\n",
       "                          'classifier__max_depth': [5, 10, 20, 40, 80],\n",
       "                          'classifier__n_estimators': [200, 400, 800, 1600,\n",
       "                                                       3200]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca.fit(X_train_bin, y_train_bin.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'PCA__n_components': 3,\n",
       " 'classifier__max_depth': 40,\n",
       " 'classifier__n_estimators': 1600}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5894137051161377"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.6373\n"
     ]
    }
   ],
   "source": [
    "from sklearn import metrics\n",
    "bin_pca_predict = clf_rf_pca.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": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[78 19]\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": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Binary_APC_RF_PCA.pkl']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import joblib\n",
    "joblib.dump(clf_rf_pca, \"Binary_APC_RF_PCA.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LDA "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe_rf_lda = Pipeline([(\"std\", MinMaxScaler()),\n",
    "                        (\"thresholder\", VarianceThreshold(threshold=0.01)),\n",
    "                        (\"LDA\", LinearDiscriminantAnalysis(solver='eigen', shrinkage='auto')),\n",
    "                        (\"smote\", SMOTE(random_state=seed)),\n",
    "                        (\"classifier\", RandomForestClassifier(random_state=seed))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_grid_lda = [{\"LDA__n_components\": [1, 2, 3],\n",
    "                   \"classifier__n_estimators\": [200, 400, 800, 1600, 3200],\n",
    "                   \"classifier__max_depth\": [5, 10, 20, 40, 80]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf_rf_lda = GridSearchCV(pipe_rf_lda, param_grid_lda, cv=cv, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "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)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:   11.2s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:   40.2s\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  1.6min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  1.7min 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='auto',\n",
       "                                                                   solver='eigen',\n",
       "                                                                   store_covariance=False,\n",
       "                                                                   to...\n",
       "                                                               n_estimators=100,\n",
       "                                                               n_jobs=None,\n",
       "                                                               oob_score=False,\n",
       "                                                               random_state=28,\n",
       "                                                               verbose=0,\n",
       "                                                               warm_start=False))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'LDA__n_components': [1, 2, 3],\n",
       "                          'classifier__max_depth': [5, 10, 20, 40, 80],\n",
       "                          'classifier__n_estimators': [200, 400, 800, 1600,\n",
       "                                                       3200]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.fit(X_train_bin, y_train_bin.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'LDA__n_components': 1,\n",
       " 'classifier__max_depth': 5,\n",
       " 'classifier__n_estimators': 400}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6179828179451061"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.5851\n"
     ]
    }
   ],
   "source": [
    "bin_lda_predict = clf_rf_lda.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": 33,
   "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": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Binary_APC_RF_LDA.pkl']"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_rf_lda, \"Binary_APC_RF_LDA.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AtomPairCount_Binary_smotx"
   ]
  },
  {
   "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_rf_pca = Pipeline([(\"std\", MinMaxScaler()),\n",
    "                        (\"thresholder\", VarianceThreshold(threshold=0.01)),\n",
    "                        (\"PCA\", PCA()),\n",
    "                        (\"classifier\", RandomForestClassifier(random_state=seed))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_grid_pca = [{\"PCA__n_components\": [1, 2, 3],\n",
    "                   \"classifier__n_estimators\": [200, 400, 800, 1600, 3200],\n",
    "                   \"classifier__max_depth\": [5, 10, 20, 40, 80]}]"
   ]
  },
  {
   "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": [
    "clf_rf_pca = GridSearchCV(pipe_rf_pca, param_grid_pca, cv=cv, 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 75 candidates, totalling 375 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.2s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:   36.5s\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  1.3min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  1.4min 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",
       "                                                               n_estimators=100,\n",
       "                                                               n_jobs=None,\n",
       "                                                               oob_score=False,\n",
       "                                                               random_state=28,\n",
       "                                                               verbose=0,\n",
       "                                                               warm_start=False))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'PCA__n_components': [1, 2, 3],\n",
       "                          'classifier__max_depth': [5, 10, 20, 40, 80],\n",
       "                          'classifier__n_estimators': [200, 400, 800, 1600,\n",
       "                                                       3200]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca.fit(X_train_bin, y_train_bin.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'PCA__n_components': 3,\n",
       " 'classifier__max_depth': 20,\n",
       " 'classifier__n_estimators': 200}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5698515684547165"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.5609\n"
     ]
    }
   ],
   "source": [
    "from sklearn import metrics\n",
    "bin_pca_predict = clf_rf_pca.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": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[94  3]\n",
      " [18  3]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_bin, bin_pca_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Binary_APC_RF_PCA_smotx.pkl']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import joblib\n",
    "joblib.dump(clf_rf_pca, \"Binary_APC_RF_PCA_smotx.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LDA "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe_rf_lda = Pipeline([(\"std\", MinMaxScaler()),\n",
    "                        (\"thresholder\", VarianceThreshold(threshold=0.01)),\n",
    "                        (\"LDA\", LinearDiscriminantAnalysis()),\n",
    "                        (\"classifier\", RandomForestClassifier(random_state=seed))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_grid_lda = [{\"LDA__n_components\": [1, 2, 3],\n",
    "                   \"classifier__n_estimators\": [200, 400, 800, 1600, 3200],\n",
    "                   \"classifier__max_depth\": [5, 10, 20, 40, 80]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf_rf_lda = GridSearchCV(pipe_rf_lda, param_grid_lda, cv=cv, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "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)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:    4.1s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:   28.6s\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  1.2min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  1.3min 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",
       "                                                               n_estimators=100,\n",
       "                                                               n_jobs=None,\n",
       "                                                               oob_score=False,\n",
       "                                                               random_state=28,\n",
       "                                                               verbose=0,\n",
       "                                                               warm_start=False))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'LDA__n_components': [1, 2, 3],\n",
       "                          'classifier__max_depth': [5, 10, 20, 40, 80],\n",
       "                          'classifier__n_estimators': [200, 400, 800, 1600,\n",
       "                                                       3200]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.fit(X_train_bin, y_train_bin.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'LDA__n_components': 1,\n",
       " 'classifier__max_depth': 10,\n",
       " 'classifier__n_estimators': 800}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5709218702004106"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.5469\n"
     ]
    }
   ],
   "source": [
    "bin_lda_predict = clf_rf_lda.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": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[79 18]\n",
      " [15  6]]\n"
     ]
    }
   ],
   "source": [
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_bin, bin_lda_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Binary_APC_RF_LDA_smotx.pkl']"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_rf_lda, \"Binary_APC_RF_LDA_smotx.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AtomPairCount_Ternary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "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": 36,
   "metadata": {},
   "outputs": [
    {
     "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)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:   17.4s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:  2.4min\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  5.3min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  5.7min 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",
       "                                                               n_estimators=100,\n",
       "                                                               n_jobs=None,\n",
       "                                                               oob_score=False,\n",
       "                                                               random_state=28,\n",
       "                                                               verbose=0,\n",
       "                                                               warm_start=False))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'PCA__n_components': [1, 2, 3],\n",
       "                          'classifier__max_depth': [5, 10, 20, 40, 80],\n",
       "                          'classifier__n_estimators': [200, 400, 800, 1600,\n",
       "                                                       3200]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca.fit(X_train_ter, y_train_ter.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'PCA__n_components': 3,\n",
       " 'classifier__max_depth': 10,\n",
       " 'classifier__n_estimators': 400}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.42110064642759176"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.4122\n"
     ]
    }
   ],
   "source": [
    "ter_pca_predict = clf_rf_pca.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": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[43 16 12]\n",
      " [14  5  7]\n",
      " [ 4  7 10]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_ter, ter_pca_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Ternary_APC_RF_PCA.pkl']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_rf_pca, \"Ternary_APC_RF_PCA.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LDA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "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)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:   12.6s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:  1.7min\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  5.3min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  5.9min 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",
       "                                                               n_estimators=100,\n",
       "                                                               n_jobs=None,\n",
       "                                                               oob_score=False,\n",
       "                                                               random_state=28,\n",
       "                                                               verbose=0,\n",
       "                                                               warm_start=False))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'LDA__n_components': [1, 2, 3],\n",
       "                          'classifier__max_depth': [5, 10, 20, 40, 80],\n",
       "                          'classifier__n_estimators': [200, 400, 800, 1600,\n",
       "                                                       3200]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.fit(X_train_ter, y_train_ter.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'LDA__n_components': 2,\n",
       " 'classifier__max_depth': 10,\n",
       " 'classifier__n_estimators': 200}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4775083550460565"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.4266\n"
     ]
    }
   ],
   "source": [
    "ter_lda_predict = clf_rf_lda.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": 46,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[41 19 11]\n",
      " [11  9  6]\n",
      " [ 3 10  8]]\n"
     ]
    }
   ],
   "source": [
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_ter, ter_lda_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Ternary_APC_RF_LDA.pkl']"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_rf_lda, \"Ternary_APC_RF_LDA.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AtomPairCount_Ternary_smotx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "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": 36,
   "metadata": {},
   "outputs": [
    {
     "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)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:    4.0s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:   31.7s\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  1.3min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  1.4min 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",
       "                                                               n_estimators=100,\n",
       "                                                               n_jobs=None,\n",
       "                                                               oob_score=False,\n",
       "                                                               random_state=28,\n",
       "                                                               verbose=0,\n",
       "                                                               warm_start=False))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'PCA__n_components': [1, 2, 3],\n",
       "                          'classifier__max_depth': [5, 10, 20, 40, 80],\n",
       "                          'classifier__n_estimators': [200, 400, 800, 1600,\n",
       "                                                       3200]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca.fit(X_train_ter, y_train_ter.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'PCA__n_components': 3,\n",
       " 'classifier__max_depth': 40,\n",
       " 'classifier__n_estimators': 400}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.43385303939202335"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.4679\n"
     ]
    }
   ],
   "source": [
    "ter_pca_predict = clf_rf_pca.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": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[62  7  2]\n",
      " [21  4  1]\n",
      " [12  2  7]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_ter, ter_pca_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Ternary_APC_RF_PCA_smotx.pkl']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_rf_pca, \"Ternary_APC_RF_PCA_smotx.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LDA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "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)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:    3.9s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:   29.6s\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  1.2min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  1.3min 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",
       "                                                               n_estimators=100,\n",
       "                                                               n_jobs=None,\n",
       "                                                               oob_score=False,\n",
       "                                                               random_state=28,\n",
       "                                                               verbose=0,\n",
       "                                                               warm_start=False))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'LDA__n_components': [1, 2, 3],\n",
       "                          'classifier__max_depth': [5, 10, 20, 40, 80],\n",
       "                          'classifier__n_estimators': [200, 400, 800, 1600,\n",
       "                                                       3200]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.fit(X_train_ter, y_train_ter.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'LDA__n_components': 2,\n",
       " 'classifier__max_depth': 5,\n",
       " 'classifier__n_estimators': 3200}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4798112320569735"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.4392\n"
     ]
    }
   ],
   "source": [
    "ter_lda_predict = clf_rf_lda.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": 46,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[50 14  7]\n",
      " [15  7  4]\n",
      " [ 6  8  7]]\n"
     ]
    }
   ],
   "source": [
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_ter, ter_lda_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Ternary_APC_RF_LDA_smotx.pkl']"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_rf_lda, \"Ternary_APC_RF_LDA_smotx.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Quaternary(w/o SMOTE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "seed=42\n",
    "X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=seed, stratify=target)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe_rf_pca = Pipeline([(\"std\", MinMaxScaler()),\n",
    "                        (\"thresholder\", VarianceThreshold(threshold=0.01)),\n",
    "                        (\"PCA\", PCA()),\n",
    "                        (\"classifier\", RandomForestClassifier(random_state=seed))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf_rf_pca_smotx = GridSearchCV(pipe_rf_pca_smotx, param_grid_pca, cv=cv, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "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)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:   21.0s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:  1.9min\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  4.3min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  4.7min 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",
       "                                                               n_estimators=100,\n",
       "                                                               n_jobs=None,\n",
       "                                                               oob_score=False,\n",
       "                                                               random_state=28,\n",
       "                                                               verbose=0,\n",
       "                                                               warm_start=False))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'PCA__n_components': [1, 2, 3],\n",
       "                          'classifier__max_depth': [5, 10, 20, 40, 80],\n",
       "                          'classifier__n_estimators': [200, 400, 800, 1600,\n",
       "                                                       3200]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca_smotx.fit(X_train_quater, y_train_quater.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'PCA__n_components': 3,\n",
       " 'classifier__max_depth': 40,\n",
       " 'classifier__n_estimators': 200}"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca_smotx.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3872162012014563"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca_smotx.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.4400\n"
     ]
    }
   ],
   "source": [
    "quater_pca_predict = clf_rf_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": 55,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[12 11  1  1]\n",
      " [ 5 30  8  3]\n",
      " [ 2 16  6  2]\n",
      " [ 0 13  1  7]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_quater, quater_pca_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Quaternary_APC_RF_PCA_smotx.pkl']"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_rf_pca_smotx, \"Quaternary_APC_RF_PCA_smotx.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LDA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe_rf_lda_smotx = Pipeline([(\"std\", MinMaxScaler()),\n",
    "                              (\"thresholder\", VarianceThreshold(threshold=0.01)),\n",
    "                              (\"LDA\", LinearDiscriminantAnalysis()),\n",
    "                              (\"classifier\", RandomForestClassifier(random_state=seed))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf_rf_lda_smotx = GridSearchCV(pipe_rf_lda_smotx, param_grid_lda, cv=cv, scoring='f1_macro', verbose=2, n_jobs=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "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)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:   18.0s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:  1.3min\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  3.8min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  4.2min 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",
       "                                                               n_estimators=100,\n",
       "                                                               n_jobs=None,\n",
       "                                                               oob_score=False,\n",
       "                                                               random_state=28,\n",
       "                                                               verbose=0,\n",
       "                                                               warm_start=False))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'LDA__n_components': [1, 2, 3],\n",
       "                          'classifier__max_depth': [5, 10, 20, 40, 80],\n",
       "                          'classifier__n_estimators': [200, 400, 800, 1600,\n",
       "                                                       3200]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda_smotx.fit(X_train_quater, y_train_quater.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'LDA__n_components': 2,\n",
       " 'classifier__max_depth': 5,\n",
       " 'classifier__n_estimators': 200}"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda_smotx.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.41243676011223673"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda_smotx.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.3953\n"
     ]
    }
   ],
   "source": [
    "quater_lda_predict = clf_rf_lda_smotx.predict(X_test_quater)\n",
    "testscore_quater_lda = metrics.f1_score(y_test_quater, quater_lda_predict, average='macro')\n",
    "\n",
    "print(\"f1 score (test): {:.4f}\".format(testscore_quater_lda))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[17  8  0  0]\n",
      " [11 20  4 11]\n",
      " [ 4 15  2  5]\n",
      " [ 1  5  4 11]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_quater, quater_lda_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Quaternary_APC_RF_LDA_smotx.pkl']"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_rf_lda_smotx, \"Quaternary_APC_RF_LDA_smotx.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Quaternary (w/ SMOTE)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "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)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:   20.3s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:  2.1min\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  5.1min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  5.5min 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",
       "                                                               n_estimators=100,\n",
       "                                                               n_jobs=None,\n",
       "                                                               oob_score=False,\n",
       "                                                               random_state=28,\n",
       "                                                               verbose=0,\n",
       "                                                               warm_start=False))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'PCA__n_components': [1, 2, 3],\n",
       "                          'classifier__max_depth': [5, 10, 20, 40, 80],\n",
       "                          'classifier__n_estimators': [200, 400, 800, 1600,\n",
       "                                                       3200]}],\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_rf_pca.fit(X_train_quater, y_train_quater.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'PCA__n_components': 3,\n",
       " 'classifier__max_depth': 10,\n",
       " 'classifier__n_estimators': 1600}"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.38241835802914825"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_pca.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.4420\n"
     ]
    }
   ],
   "source": [
    "quater_smot_pca_predict = clf_rf_pca.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": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[12 10  2  1]\n",
      " [ 5 18 13 10]\n",
      " [ 3 11  8  4]\n",
      " [ 1  6  1 13]]\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": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Quaternary_APC_RF_PCA_smot.pkl']"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_rf_pca, \"Quaternary_APC_RF_PCA_smot.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LDA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "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)]: Using backend LokyBackend with 8 concurrent workers.\n",
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:   21.5s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:  1.8min\n",
      "[Parallel(n_jobs=-1)]: Done 349 tasks      | elapsed:  2.9min\n",
      "[Parallel(n_jobs=-1)]: Done 375 out of 375 | elapsed:  3.1min 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",
       "                                                               n_estimators=100,\n",
       "                                                               n_jobs=None,\n",
       "                                                               oob_score=False,\n",
       "                                                               random_state=28,\n",
       "                                                               verbose=0,\n",
       "                                                               warm_start=False))],\n",
       "                                verbose=False),\n",
       "             iid='deprecated', n_jobs=-1,\n",
       "             param_grid=[{'LDA__n_components': [1, 2, 3],\n",
       "                          'classifier__max_depth': [5, 10, 20, 40, 80],\n",
       "                          'classifier__n_estimators': [200, 400, 800, 1600,\n",
       "                                                       3200]}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='f1_macro', verbose=2)"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.fit(X_train_quater, y_train_quater.values.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'LDA__n_components': 3,\n",
       " 'classifier__max_depth': 10,\n",
       " 'classifier__n_estimators': 400}"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.41292303367912425"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf_rf_lda.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1 score (test): 0.4205\n"
     ]
    }
   ],
   "source": [
    "quater_smot_lda_predict = clf_rf_lda.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": 75,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬: \n",
      " [[17  6  1  1]\n",
      " [11 15  6 14]\n",
      " [ 3  7  7  9]\n",
      " [ 1  2  7 11]]\n"
     ]
    }
   ],
   "source": [
    "print(\"오차 행렬: \\n\", confusion_matrix(y_test_quater, quater_smot_lda_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Quaternary_APC_RF_LDA_smot.pkl']"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(clf_rf_lda, \"Quaternary_APC_RF_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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
