PyCaret（+ MLflow）を使ったタイタニックの生存者予測

# PyCaret で行うTitanicの生存者予測

PyCaret（+MLflow）の使い方を調べたので備忘録を残します。  
題材としてはKaggleのチュートリアルでも使われるTitanicの生存者予測を行います。  


# 参考資料
本投稿は以下記事を参考にしています。  
- [PyCaret公式ドキュメント](https://pycaret.gitbook.io/docs/)
- [PyCaretとMLflowで機械学習の実験を簡単に実行・記録する](https://data-campus.ai/announcements/k9grrqa6ljedngeu?type=text)


# データ

データは以下よりダウンロードし、実行場所と同階層に`train.csv`と`test.csv`を配置してください。  
- [Titanic - Machine Learning from Disaster](https://www.kaggle.com/competitions/titanic/data)

# ライブラリのインストール

事前にMLFlowとPyCaretをインストールしておいてください。

```shell
pip install mlflow
pip install pycaret
```

# ライブラリのインポート


```python
import pandas as pd
from pycaret.classification import *
```

# データ読み込み


```python
train = pd.read_csv("train.csv")
test = pd.read_csv("test.csv")
train.head()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>PassengerId</th>
      <th>Survived</th>
      <th>Pclass</th>
      <th>Name</th>
      <th>Sex</th>
      <th>Age</th>
      <th>SibSp</th>
      <th>Parch</th>
      <th>Ticket</th>
      <th>Fare</th>
      <th>Cabin</th>
      <th>Embarked</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>0</td>
      <td>3</td>
      <td>Braund, Mr. Owen Harris</td>
      <td>male</td>
      <td>22.0</td>
      <td>1</td>
      <td>0</td>
      <td>A/5 21171</td>
      <td>7.2500</td>
      <td>NaN</td>
      <td>S</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2</td>
      <td>1</td>
      <td>1</td>
      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>
      <td>female</td>
      <td>38.0</td>
      <td>1</td>
      <td>0</td>
      <td>PC 17599</td>
      <td>71.2833</td>
      <td>C85</td>
      <td>C</td>
    </tr>
    <tr>
      <th>2</th>
      <td>3</td>
      <td>1</td>
      <td>3</td>
      <td>Heikkinen, Miss. Laina</td>
      <td>female</td>
      <td>26.0</td>
      <td>0</td>
      <td>0</td>
      <td>STON/O2. 3101282</td>
      <td>7.9250</td>
      <td>NaN</td>
      <td>S</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
      <td>1</td>
      <td>1</td>
      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>
      <td>female</td>
      <td>35.0</td>
      <td>1</td>
      <td>0</td>
      <td>113803</td>
      <td>53.1000</td>
      <td>C123</td>
      <td>S</td>
    </tr>
    <tr>
      <th>4</th>
      <td>5</td>
      <td>0</td>
      <td>3</td>
      <td>Allen, Mr. William Henry</td>
      <td>male</td>
      <td>35.0</td>
      <td>0</td>
      <td>0</td>
      <td>373450</td>
      <td>8.0500</td>
      <td>NaN</td>
      <td>S</td>
    </tr>
  </tbody>
</table>
</div>



# PyCaretにおける実験環境の設定
PyCaretでは`setup`という関数を実行し、実験環境の設定を行う必要があります。  
具体的に何をしているかというと、以下になります。  
- 目的変数の指定
- 入力変数の型推定（必要に応じて明示的に指定することも可能）
- 乱数シードの指定

また、簡単な前処理などはこの時点で行ってくれます。  
**※不要な列削除や特徴量作成などはこの前に自分で行う必要あり。**

今回は目的変数に`Survived`を指定し、乱数シードに0を指定します。  


更に、次の2つの引数を指定することで、MLflowでの実験記録の保存ができます。  
- log_experiment: 実験記録の保存有無
- experiment_name: 実験記録名



```python
s = setup(
    data=train, # データセット
    target="Survived", # 目的変数
    log_experiment=True, # 実験記録の保存
    experiment_name="Titanic", # 実験記録の名前
    categorical_features=[], # カテゴリカル変数列を明示的に指定する場合は設定
    numeric_features=[], # 数値変数列を明示的に指定する場合は設定
    session_id=0, # 乱数シード（今回は0）
    silent=False, # 結果確認の有無（Trueの場合、実験環境設定の結果表示を省略）
)
```

実行後に以下メッセージと自動推定された各変数の型が表示されます。

```
Following data types have been inferred automatically, if they are correct press enter to continue or type 'quit' otherwise.
```

自動で推定した型が問題なければ、Enterを押下してください。
そうすると、以下のような実効結果の表がずらーっと表示されます。

<style type="text/css">
#T_671a5_row5_col1, #T_671a5_row19_col1, #T_671a5_row44_col1 {
  background-color: lightgreen;
}
</style>
<table id="T_671a5">
  <thead>
    <tr>
      <th class="blank level0" >&nbsp;</th>
      <th id="T_671a5_level0_col0" class="col_heading level0 col0" >Description</th>
      <th id="T_671a5_level0_col1" class="col_heading level0 col1" >Value</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th id="T_671a5_level0_row0" class="row_heading level0 row0" >0</th>
      <td id="T_671a5_row0_col0" class="data row0 col0" >session_id</td>
      <td id="T_671a5_row0_col1" class="data row0 col1" >0</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row1" class="row_heading level0 row1" >1</th>
      <td id="T_671a5_row1_col0" class="data row1 col0" >Target</td>
      <td id="T_671a5_row1_col1" class="data row1 col1" >Survived</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row2" class="row_heading level0 row2" >2</th>
      <td id="T_671a5_row2_col0" class="data row2 col0" >Target Type</td>
      <td id="T_671a5_row2_col1" class="data row2 col1" >Binary</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row3" class="row_heading level0 row3" >3</th>
      <td id="T_671a5_row3_col0" class="data row3 col0" >Label Encoded</td>
      <td id="T_671a5_row3_col1" class="data row3 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row4" class="row_heading level0 row4" >4</th>
      <td id="T_671a5_row4_col0" class="data row4 col0" >Original Data</td>
      <td id="T_671a5_row4_col1" class="data row4 col1" >(891, 12)</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row5" class="row_heading level0 row5" >5</th>
      <td id="T_671a5_row5_col0" class="data row5 col0" >Missing Values</td>
      <td id="T_671a5_row5_col1" class="data row5 col1" >True</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row6" class="row_heading level0 row6" >6</th>
      <td id="T_671a5_row6_col0" class="data row6 col0" >Numeric Features</td>
      <td id="T_671a5_row6_col1" class="data row6 col1" >3</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row7" class="row_heading level0 row7" >7</th>
      <td id="T_671a5_row7_col0" class="data row7 col0" >Categorical Features</td>
      <td id="T_671a5_row7_col1" class="data row7 col1" >8</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row8" class="row_heading level0 row8" >8</th>
      <td id="T_671a5_row8_col0" class="data row8 col0" >Ordinal Features</td>
      <td id="T_671a5_row8_col1" class="data row8 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row9" class="row_heading level0 row9" >9</th>
      <td id="T_671a5_row9_col0" class="data row9 col0" >High Cardinality Features</td>
      <td id="T_671a5_row9_col1" class="data row9 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row10" class="row_heading level0 row10" >10</th>
      <td id="T_671a5_row10_col0" class="data row10 col0" >High Cardinality Method</td>
      <td id="T_671a5_row10_col1" class="data row10 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row11" class="row_heading level0 row11" >11</th>
      <td id="T_671a5_row11_col0" class="data row11 col0" >Transformed Train Set</td>
      <td id="T_671a5_row11_col1" class="data row11 col1" >(623, 717)</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row12" class="row_heading level0 row12" >12</th>
      <td id="T_671a5_row12_col0" class="data row12 col0" >Transformed Test Set</td>
      <td id="T_671a5_row12_col1" class="data row12 col1" >(268, 717)</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row13" class="row_heading level0 row13" >13</th>
      <td id="T_671a5_row13_col0" class="data row13 col0" >Shuffle Train-Test</td>
      <td id="T_671a5_row13_col1" class="data row13 col1" >True</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row14" class="row_heading level0 row14" >14</th>
      <td id="T_671a5_row14_col0" class="data row14 col0" >Stratify Train-Test</td>
      <td id="T_671a5_row14_col1" class="data row14 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row15" class="row_heading level0 row15" >15</th>
      <td id="T_671a5_row15_col0" class="data row15 col0" >Fold Generator</td>
      <td id="T_671a5_row15_col1" class="data row15 col1" >StratifiedKFold</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row16" class="row_heading level0 row16" >16</th>
      <td id="T_671a5_row16_col0" class="data row16 col0" >Fold Number</td>
      <td id="T_671a5_row16_col1" class="data row16 col1" >10</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row17" class="row_heading level0 row17" >17</th>
      <td id="T_671a5_row17_col0" class="data row17 col0" >CPU Jobs</td>
      <td id="T_671a5_row17_col1" class="data row17 col1" >-1</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row18" class="row_heading level0 row18" >18</th>
      <td id="T_671a5_row18_col0" class="data row18 col0" >Use GPU</td>
      <td id="T_671a5_row18_col1" class="data row18 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row19" class="row_heading level0 row19" >19</th>
      <td id="T_671a5_row19_col0" class="data row19 col0" >Log Experiment</td>
      <td id="T_671a5_row19_col1" class="data row19 col1" >True</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row20" class="row_heading level0 row20" >20</th>
      <td id="T_671a5_row20_col0" class="data row20 col0" >Experiment Name</td>
      <td id="T_671a5_row20_col1" class="data row20 col1" >Titanic</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row21" class="row_heading level0 row21" >21</th>
      <td id="T_671a5_row21_col0" class="data row21 col0" >USI</td>
      <td id="T_671a5_row21_col1" class="data row21 col1" >805e</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row22" class="row_heading level0 row22" >22</th>
      <td id="T_671a5_row22_col0" class="data row22 col0" >Imputation Type</td>
      <td id="T_671a5_row22_col1" class="data row22 col1" >simple</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row23" class="row_heading level0 row23" >23</th>
      <td id="T_671a5_row23_col0" class="data row23 col0" >Iterative Imputation Iteration</td>
      <td id="T_671a5_row23_col1" class="data row23 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row24" class="row_heading level0 row24" >24</th>
      <td id="T_671a5_row24_col0" class="data row24 col0" >Numeric Imputer</td>
      <td id="T_671a5_row24_col1" class="data row24 col1" >mean</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row25" class="row_heading level0 row25" >25</th>
      <td id="T_671a5_row25_col0" class="data row25 col0" >Iterative Imputation Numeric Model</td>
      <td id="T_671a5_row25_col1" class="data row25 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row26" class="row_heading level0 row26" >26</th>
      <td id="T_671a5_row26_col0" class="data row26 col0" >Categorical Imputer</td>
      <td id="T_671a5_row26_col1" class="data row26 col1" >constant</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row27" class="row_heading level0 row27" >27</th>
      <td id="T_671a5_row27_col0" class="data row27 col0" >Iterative Imputation Categorical Model</td>
      <td id="T_671a5_row27_col1" class="data row27 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row28" class="row_heading level0 row28" >28</th>
      <td id="T_671a5_row28_col0" class="data row28 col0" >Unknown Categoricals Handling</td>
      <td id="T_671a5_row28_col1" class="data row28 col1" >least_frequent</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row29" class="row_heading level0 row29" >29</th>
      <td id="T_671a5_row29_col0" class="data row29 col0" >Normalize</td>
      <td id="T_671a5_row29_col1" class="data row29 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row30" class="row_heading level0 row30" >30</th>
      <td id="T_671a5_row30_col0" class="data row30 col0" >Normalize Method</td>
      <td id="T_671a5_row30_col1" class="data row30 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row31" class="row_heading level0 row31" >31</th>
      <td id="T_671a5_row31_col0" class="data row31 col0" >Transformation</td>
      <td id="T_671a5_row31_col1" class="data row31 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row32" class="row_heading level0 row32" >32</th>
      <td id="T_671a5_row32_col0" class="data row32 col0" >Transformation Method</td>
      <td id="T_671a5_row32_col1" class="data row32 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row33" class="row_heading level0 row33" >33</th>
      <td id="T_671a5_row33_col0" class="data row33 col0" >PCA</td>
      <td id="T_671a5_row33_col1" class="data row33 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row34" class="row_heading level0 row34" >34</th>
      <td id="T_671a5_row34_col0" class="data row34 col0" >PCA Method</td>
      <td id="T_671a5_row34_col1" class="data row34 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row35" class="row_heading level0 row35" >35</th>
      <td id="T_671a5_row35_col0" class="data row35 col0" >PCA Components</td>
      <td id="T_671a5_row35_col1" class="data row35 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row36" class="row_heading level0 row36" >36</th>
      <td id="T_671a5_row36_col0" class="data row36 col0" >Ignore Low Variance</td>
      <td id="T_671a5_row36_col1" class="data row36 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row37" class="row_heading level0 row37" >37</th>
      <td id="T_671a5_row37_col0" class="data row37 col0" >Combine Rare Levels</td>
      <td id="T_671a5_row37_col1" class="data row37 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row38" class="row_heading level0 row38" >38</th>
      <td id="T_671a5_row38_col0" class="data row38 col0" >Rare Level Threshold</td>
      <td id="T_671a5_row38_col1" class="data row38 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row39" class="row_heading level0 row39" >39</th>
      <td id="T_671a5_row39_col0" class="data row39 col0" >Numeric Binning</td>
      <td id="T_671a5_row39_col1" class="data row39 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row40" class="row_heading level0 row40" >40</th>
      <td id="T_671a5_row40_col0" class="data row40 col0" >Remove Outliers</td>
      <td id="T_671a5_row40_col1" class="data row40 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row41" class="row_heading level0 row41" >41</th>
      <td id="T_671a5_row41_col0" class="data row41 col0" >Outliers Threshold</td>
      <td id="T_671a5_row41_col1" class="data row41 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row42" class="row_heading level0 row42" >42</th>
      <td id="T_671a5_row42_col0" class="data row42 col0" >Remove Multicollinearity</td>
      <td id="T_671a5_row42_col1" class="data row42 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row43" class="row_heading level0 row43" >43</th>
      <td id="T_671a5_row43_col0" class="data row43 col0" >Multicollinearity Threshold</td>
      <td id="T_671a5_row43_col1" class="data row43 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row44" class="row_heading level0 row44" >44</th>
      <td id="T_671a5_row44_col0" class="data row44 col0" >Remove Perfect Collinearity</td>
      <td id="T_671a5_row44_col1" class="data row44 col1" >True</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row45" class="row_heading level0 row45" >45</th>
      <td id="T_671a5_row45_col0" class="data row45 col0" >Clustering</td>
      <td id="T_671a5_row45_col1" class="data row45 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row46" class="row_heading level0 row46" >46</th>
      <td id="T_671a5_row46_col0" class="data row46 col0" >Clustering Iteration</td>
      <td id="T_671a5_row46_col1" class="data row46 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row47" class="row_heading level0 row47" >47</th>
      <td id="T_671a5_row47_col0" class="data row47 col0" >Polynomial Features</td>
      <td id="T_671a5_row47_col1" class="data row47 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row48" class="row_heading level0 row48" >48</th>
      <td id="T_671a5_row48_col0" class="data row48 col0" >Polynomial Degree</td>
      <td id="T_671a5_row48_col1" class="data row48 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row49" class="row_heading level0 row49" >49</th>
      <td id="T_671a5_row49_col0" class="data row49 col0" >Trignometry Features</td>
      <td id="T_671a5_row49_col1" class="data row49 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row50" class="row_heading level0 row50" >50</th>
      <td id="T_671a5_row50_col0" class="data row50 col0" >Polynomial Threshold</td>
      <td id="T_671a5_row50_col1" class="data row50 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row51" class="row_heading level0 row51" >51</th>
      <td id="T_671a5_row51_col0" class="data row51 col0" >Group Features</td>
      <td id="T_671a5_row51_col1" class="data row51 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row52" class="row_heading level0 row52" >52</th>
      <td id="T_671a5_row52_col0" class="data row52 col0" >Feature Selection</td>
      <td id="T_671a5_row52_col1" class="data row52 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row53" class="row_heading level0 row53" >53</th>
      <td id="T_671a5_row53_col0" class="data row53 col0" >Feature Selection Method</td>
      <td id="T_671a5_row53_col1" class="data row53 col1" >classic</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row54" class="row_heading level0 row54" >54</th>
      <td id="T_671a5_row54_col0" class="data row54 col0" >Features Selection Threshold</td>
      <td id="T_671a5_row54_col1" class="data row54 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row55" class="row_heading level0 row55" >55</th>
      <td id="T_671a5_row55_col0" class="data row55 col0" >Feature Interaction</td>
      <td id="T_671a5_row55_col1" class="data row55 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row56" class="row_heading level0 row56" >56</th>
      <td id="T_671a5_row56_col0" class="data row56 col0" >Feature Ratio</td>
      <td id="T_671a5_row56_col1" class="data row56 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row57" class="row_heading level0 row57" >57</th>
      <td id="T_671a5_row57_col0" class="data row57 col0" >Interaction Threshold</td>
      <td id="T_671a5_row57_col1" class="data row57 col1" >None</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row58" class="row_heading level0 row58" >58</th>
      <td id="T_671a5_row58_col0" class="data row58 col0" >Fix Imbalance</td>
      <td id="T_671a5_row58_col1" class="data row58 col1" >False</td>
    </tr>
    <tr>
      <th id="T_671a5_level0_row59" class="row_heading level0 row59" >59</th>
      <td id="T_671a5_row59_col0" class="data row59 col0" >Fix Imbalance Method</td>
      <td id="T_671a5_row59_col1" class="data row59 col1" >SMOTE</td>
    </tr>
  </tbody>
</table>



# 機械学習アルゴリズムの比較

PyCaretには複数の機械学習アルゴリズムを一度に比較することができます。  
アルゴリズムの比較には`compare_model`関数を使います。  
戻り値として、最も評価が高いモデルが返却されます。

```python
model = compare_models(
    sort='Accuracy', # ソートする評価指標を指定できます。
    fold=4, # cross validation の fold数
    exclude=[], # 比較から除外するアルゴリズム名を指定可能です。
)
```


<style type="text/css">
#T_f7188 th {
  text-align: left;
}
#T_f7188_row0_col0, #T_f7188_row0_col2, #T_f7188_row0_col3, #T_f7188_row0_col4, #T_f7188_row1_col0, #T_f7188_row1_col1, #T_f7188_row1_col2, #T_f7188_row1_col3, #T_f7188_row1_col5, #T_f7188_row1_col6, #T_f7188_row1_col7, #T_f7188_row2_col0, #T_f7188_row2_col1, #T_f7188_row2_col2, #T_f7188_row2_col3, #T_f7188_row2_col4, #T_f7188_row2_col5, #T_f7188_row2_col6, #T_f7188_row2_col7, #T_f7188_row3_col0, #T_f7188_row3_col1, #T_f7188_row3_col3, #T_f7188_row3_col4, #T_f7188_row3_col5, #T_f7188_row3_col6, #T_f7188_row3_col7, #T_f7188_row4_col0, #T_f7188_row4_col1, #T_f7188_row4_col2, #T_f7188_row4_col3, #T_f7188_row4_col4, #T_f7188_row4_col5, #T_f7188_row4_col6, #T_f7188_row4_col7, #T_f7188_row5_col0, #T_f7188_row5_col1, #T_f7188_row5_col2, #T_f7188_row5_col3, #T_f7188_row5_col4, #T_f7188_row5_col5, #T_f7188_row5_col6, #T_f7188_row5_col7, #T_f7188_row6_col0, #T_f7188_row6_col1, #T_f7188_row6_col2, #T_f7188_row6_col3, #T_f7188_row6_col4, #T_f7188_row6_col5, #T_f7188_row6_col6, #T_f7188_row6_col7, #T_f7188_row7_col0, #T_f7188_row7_col1, #T_f7188_row7_col2, #T_f7188_row7_col3, #T_f7188_row7_col4, #T_f7188_row7_col5, #T_f7188_row7_col6, #T_f7188_row7_col7, #T_f7188_row8_col0, #T_f7188_row8_col1, #T_f7188_row8_col2, #T_f7188_row8_col3, #T_f7188_row8_col4, #T_f7188_row8_col5, #T_f7188_row8_col6, #T_f7188_row8_col7, #T_f7188_row9_col0, #T_f7188_row9_col1, #T_f7188_row9_col2, #T_f7188_row9_col3, #T_f7188_row9_col4, #T_f7188_row9_col5, #T_f7188_row9_col6, #T_f7188_row9_col7, #T_f7188_row10_col0, #T_f7188_row10_col1, #T_f7188_row10_col2, #T_f7188_row10_col3, #T_f7188_row10_col4, #T_f7188_row10_col5, #T_f7188_row10_col6, #T_f7188_row10_col7, #T_f7188_row11_col0, #T_f7188_row11_col1, #T_f7188_row11_col2, #T_f7188_row11_col3, #T_f7188_row11_col4, #T_f7188_row11_col5, #T_f7188_row11_col6, #T_f7188_row11_col7, #T_f7188_row12_col0, #T_f7188_row12_col1, #T_f7188_row12_col2, #T_f7188_row12_col3, #T_f7188_row12_col4, #T_f7188_row12_col5, #T_f7188_row12_col6, #T_f7188_row12_col7, #T_f7188_row13_col0, #T_f7188_row13_col1, #T_f7188_row13_col2, #T_f7188_row13_col3, #T_f7188_row13_col4, #T_f7188_row13_col5, #T_f7188_row13_col6, #T_f7188_row13_col7, #T_f7188_row14_col0, #T_f7188_row14_col1, #T_f7188_row14_col2, #T_f7188_row14_col3, #T_f7188_row14_col4, #T_f7188_row14_col5, #T_f7188_row14_col6, #T_f7188_row14_col7, #T_f7188_row15_col0, #T_f7188_row15_col1, #T_f7188_row15_col2, #T_f7188_row15_col4, #T_f7188_row15_col5, #T_f7188_row15_col6, #T_f7188_row15_col7 {
  text-align: left;
}
#T_f7188_row0_col1, #T_f7188_row0_col5, #T_f7188_row0_col6, #T_f7188_row0_col7, #T_f7188_row1_col4, #T_f7188_row3_col2, #T_f7188_row15_col3 {
  text-align: left;
  background-color: yellow;
}
#T_f7188_row0_col8, #T_f7188_row1_col8, #T_f7188_row2_col8, #T_f7188_row3_col8, #T_f7188_row4_col8, #T_f7188_row5_col8, #T_f7188_row6_col8, #T_f7188_row7_col8, #T_f7188_row8_col8, #T_f7188_row9_col8, #T_f7188_row10_col8, #T_f7188_row11_col8, #T_f7188_row12_col8, #T_f7188_row14_col8, #T_f7188_row15_col8 {
  text-align: left;
  background-color: lightgrey;
}
#T_f7188_row13_col8 {
  text-align: left;
  background-color: yellow;
  background-color: lightgrey;
}
</style>
<table id="T_f7188">
  <thead>
    <tr>
      <th class="blank level0" >&nbsp;</th>
      <th id="T_f7188_level0_col0" class="col_heading level0 col0" >Model</th>
      <th id="T_f7188_level0_col1" class="col_heading level0 col1" >Accuracy</th>
      <th id="T_f7188_level0_col2" class="col_heading level0 col2" >AUC</th>
      <th id="T_f7188_level0_col3" class="col_heading level0 col3" >Recall</th>
      <th id="T_f7188_level0_col4" class="col_heading level0 col4" >Prec.</th>
      <th id="T_f7188_level0_col5" class="col_heading level0 col5" >F1</th>
      <th id="T_f7188_level0_col6" class="col_heading level0 col6" >Kappa</th>
      <th id="T_f7188_level0_col7" class="col_heading level0 col7" >MCC</th>
      <th id="T_f7188_level0_col8" class="col_heading level0 col8" >TT (Sec)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th id="T_f7188_level0_row0" class="row_heading level0 row0" >ridge</th>
      <td id="T_f7188_row0_col0" class="data row0 col0" >Ridge Classifier</td>
      <td id="T_f7188_row0_col1" class="data row0 col1" >0.8235</td>
      <td id="T_f7188_row0_col2" class="data row0 col2" >0.0000</td>
      <td id="T_f7188_row0_col3" class="data row0 col3" >0.7356</td>
      <td id="T_f7188_row0_col4" class="data row0 col4" >0.7954</td>
      <td id="T_f7188_row0_col5" class="data row0 col5" >0.7635</td>
      <td id="T_f7188_row0_col6" class="data row0 col6" >0.6231</td>
      <td id="T_f7188_row0_col7" class="data row0 col7" >0.6251</td>
      <td id="T_f7188_row0_col8" class="data row0 col8" >0.0400</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row1" class="row_heading level0 row1" >catboost</th>
      <td id="T_f7188_row1_col0" class="data row1 col0" >CatBoost Classifier</td>
      <td id="T_f7188_row1_col1" class="data row1 col1" >0.8138</td>
      <td id="T_f7188_row1_col2" class="data row1 col2" >0.8598</td>
      <td id="T_f7188_row1_col3" class="data row1 col3" >0.6525</td>
      <td id="T_f7188_row1_col4" class="data row1 col4" >0.8342</td>
      <td id="T_f7188_row1_col5" class="data row1 col5" >0.7305</td>
      <td id="T_f7188_row1_col6" class="data row1 col6" >0.5917</td>
      <td id="T_f7188_row1_col7" class="data row1 col7" >0.6034</td>
      <td id="T_f7188_row1_col8" class="data row1 col8" >1.3675</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row2" class="row_heading level0 row2" >lightgbm</th>
      <td id="T_f7188_row2_col0" class="data row2 col0" >Light Gradient Boosting Machine</td>
      <td id="T_f7188_row2_col1" class="data row2 col1" >0.8122</td>
      <td id="T_f7188_row2_col2" class="data row2 col2" >0.8545</td>
      <td id="T_f7188_row2_col3" class="data row2 col3" >0.6981</td>
      <td id="T_f7188_row2_col4" class="data row2 col4" >0.7934</td>
      <td id="T_f7188_row2_col5" class="data row2 col5" >0.7424</td>
      <td id="T_f7188_row2_col6" class="data row2 col6" >0.5956</td>
      <td id="T_f7188_row2_col7" class="data row2 col7" >0.5989</td>
      <td id="T_f7188_row2_col8" class="data row2 col8" >0.0650</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row3" class="row_heading level0 row3" >lr</th>
      <td id="T_f7188_row3_col0" class="data row3 col0" >Logistic Regression</td>
      <td id="T_f7188_row3_col1" class="data row3 col1" >0.8106</td>
      <td id="T_f7188_row3_col2" class="data row3 col2" >0.8614</td>
      <td id="T_f7188_row3_col3" class="data row3 col3" >0.7109</td>
      <td id="T_f7188_row3_col4" class="data row3 col4" >0.7830</td>
      <td id="T_f7188_row3_col5" class="data row3 col5" >0.7445</td>
      <td id="T_f7188_row3_col6" class="data row3 col6" >0.5947</td>
      <td id="T_f7188_row3_col7" class="data row3 col7" >0.5970</td>
      <td id="T_f7188_row3_col8" class="data row3 col8" >0.5450</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row4" class="row_heading level0 row4" >gbc</th>
      <td id="T_f7188_row4_col0" class="data row4 col0" >Gradient Boosting Classifier</td>
      <td id="T_f7188_row4_col1" class="data row4 col1" >0.8042</td>
      <td id="T_f7188_row4_col2" class="data row4 col2" >0.8522</td>
      <td id="T_f7188_row4_col3" class="data row4 col3" >0.6279</td>
      <td id="T_f7188_row4_col4" class="data row4 col4" >0.8302</td>
      <td id="T_f7188_row4_col5" class="data row4 col5" >0.7132</td>
      <td id="T_f7188_row4_col6" class="data row4 col6" >0.5689</td>
      <td id="T_f7188_row4_col7" class="data row4 col7" >0.5831</td>
      <td id="T_f7188_row4_col8" class="data row4 col8" >0.1550</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row5" class="row_heading level0 row5" >ada</th>
      <td id="T_f7188_row5_col0" class="data row5 col0" >Ada Boost Classifier</td>
      <td id="T_f7188_row5_col1" class="data row5 col1" >0.8010</td>
      <td id="T_f7188_row5_col2" class="data row5 col2" >0.8238</td>
      <td id="T_f7188_row5_col3" class="data row5 col3" >0.7069</td>
      <td id="T_f7188_row5_col4" class="data row5 col4" >0.7629</td>
      <td id="T_f7188_row5_col5" class="data row5 col5" >0.7336</td>
      <td id="T_f7188_row5_col6" class="data row5 col6" >0.5752</td>
      <td id="T_f7188_row5_col7" class="data row5 col7" >0.5765</td>
      <td id="T_f7188_row5_col8" class="data row5 col8" >0.1000</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row6" class="row_heading level0 row6" >rf</th>
      <td id="T_f7188_row6_col0" class="data row6 col0" >Random Forest Classifier</td>
      <td id="T_f7188_row6_col1" class="data row6 col1" >0.7993</td>
      <td id="T_f7188_row6_col2" class="data row6 col2" >0.8515</td>
      <td id="T_f7188_row6_col3" class="data row6 col3" >0.6691</td>
      <td id="T_f7188_row6_col4" class="data row6 col4" >0.7822</td>
      <td id="T_f7188_row6_col5" class="data row6 col5" >0.7202</td>
      <td id="T_f7188_row6_col6" class="data row6 col6" >0.5655</td>
      <td id="T_f7188_row6_col7" class="data row6 col7" >0.5704</td>
      <td id="T_f7188_row6_col8" class="data row6 col8" >0.1850</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row7" class="row_heading level0 row7" >dt</th>
      <td id="T_f7188_row7_col0" class="data row7 col0" >Decision Tree Classifier</td>
      <td id="T_f7188_row7_col1" class="data row7 col1" >0.7913</td>
      <td id="T_f7188_row7_col2" class="data row7 col2" >0.7691</td>
      <td id="T_f7188_row7_col3" class="data row7 col3" >0.6693</td>
      <td id="T_f7188_row7_col4" class="data row7 col4" >0.7642</td>
      <td id="T_f7188_row7_col5" class="data row7 col5" >0.7130</td>
      <td id="T_f7188_row7_col6" class="data row7 col6" >0.5503</td>
      <td id="T_f7188_row7_col7" class="data row7 col7" >0.5538</td>
      <td id="T_f7188_row7_col8" class="data row7 col8" >0.0400</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row8" class="row_heading level0 row8" >et</th>
      <td id="T_f7188_row8_col0" class="data row8 col0" >Extra Trees Classifier</td>
      <td id="T_f7188_row8_col1" class="data row8 col1" >0.7913</td>
      <td id="T_f7188_row8_col2" class="data row8 col2" >0.8421</td>
      <td id="T_f7188_row8_col3" class="data row8 col3" >0.6733</td>
      <td id="T_f7188_row8_col4" class="data row8 col4" >0.7624</td>
      <td id="T_f7188_row8_col5" class="data row8 col5" >0.7136</td>
      <td id="T_f7188_row8_col6" class="data row8 col6" >0.5507</td>
      <td id="T_f7188_row8_col7" class="data row8 col7" >0.5544</td>
      <td id="T_f7188_row8_col8" class="data row8 col8" >0.1675</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row9" class="row_heading level0 row9" >xgboost</th>
      <td id="T_f7188_row9_col0" class="data row9 col0" >Extreme Gradient Boosting</td>
      <td id="T_f7188_row9_col1" class="data row9 col1" >0.7882</td>
      <td id="T_f7188_row9_col2" class="data row9 col2" >0.8398</td>
      <td id="T_f7188_row9_col3" class="data row9 col3" >0.6942</td>
      <td id="T_f7188_row9_col4" class="data row9 col4" >0.7430</td>
      <td id="T_f7188_row9_col5" class="data row9 col5" >0.7177</td>
      <td id="T_f7188_row9_col6" class="data row9 col6" >0.5485</td>
      <td id="T_f7188_row9_col7" class="data row9 col7" >0.5494</td>
      <td id="T_f7188_row9_col8" class="data row9 col8" >0.7125</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row10" class="row_heading level0 row10" >knn</th>
      <td id="T_f7188_row10_col0" class="data row10 col0" >K Neighbors Classifier</td>
      <td id="T_f7188_row10_col1" class="data row10 col1" >0.6855</td>
      <td id="T_f7188_row10_col2" class="data row10 col2" >0.7068</td>
      <td id="T_f7188_row10_col3" class="data row10 col3" >0.5416</td>
      <td id="T_f7188_row10_col4" class="data row10 col4" >0.6059</td>
      <td id="T_f7188_row10_col5" class="data row10 col5" >0.5714</td>
      <td id="T_f7188_row10_col6" class="data row10 col6" >0.3244</td>
      <td id="T_f7188_row10_col7" class="data row10 col7" >0.3260</td>
      <td id="T_f7188_row10_col8" class="data row10 col8" >0.0700</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row11" class="row_heading level0 row11" >lda</th>
      <td id="T_f7188_row11_col0" class="data row11 col0" >Linear Discriminant Analysis</td>
      <td id="T_f7188_row11_col1" class="data row11 col1" >0.6487</td>
      <td id="T_f7188_row11_col2" class="data row11 col2" >0.6334</td>
      <td id="T_f7188_row11_col3" class="data row11 col3" >0.4919</td>
      <td id="T_f7188_row11_col4" class="data row11 col4" >0.5583</td>
      <td id="T_f7188_row11_col5" class="data row11 col5" >0.5214</td>
      <td id="T_f7188_row11_col6" class="data row11 col6" >0.2457</td>
      <td id="T_f7188_row11_col7" class="data row11 col7" >0.2480</td>
      <td id="T_f7188_row11_col8" class="data row11 col8" >0.0850</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row12" class="row_heading level0 row12" >svm</th>
      <td id="T_f7188_row12_col0" class="data row12 col0" >SVM - Linear Kernel</td>
      <td id="T_f7188_row12_col1" class="data row12 col1" >0.6165</td>
      <td id="T_f7188_row12_col2" class="data row12 col2" >0.0000</td>
      <td id="T_f7188_row12_col3" class="data row12 col3" >0.5908</td>
      <td id="T_f7188_row12_col4" class="data row12 col4" >0.5187</td>
      <td id="T_f7188_row12_col5" class="data row12 col5" >0.5042</td>
      <td id="T_f7188_row12_col6" class="data row12 col6" >0.2095</td>
      <td id="T_f7188_row12_col7" class="data row12 col7" >0.2460</td>
      <td id="T_f7188_row12_col8" class="data row12 col8" >0.0500</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row13" class="row_heading level0 row13" >dummy</th>
      <td id="T_f7188_row13_col0" class="data row13 col0" >Dummy Classifier</td>
      <td id="T_f7188_row13_col1" class="data row13 col1" >0.6116</td>
      <td id="T_f7188_row13_col2" class="data row13 col2" >0.5000</td>
      <td id="T_f7188_row13_col3" class="data row13 col3" >0.0000</td>
      <td id="T_f7188_row13_col4" class="data row13 col4" >0.0000</td>
      <td id="T_f7188_row13_col5" class="data row13 col5" >0.0000</td>
      <td id="T_f7188_row13_col6" class="data row13 col6" >0.0000</td>
      <td id="T_f7188_row13_col7" class="data row13 col7" >0.0000</td>
      <td id="T_f7188_row13_col8" class="data row13 col8" >0.0325</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row14" class="row_heading level0 row14" >qda</th>
      <td id="T_f7188_row14_col0" class="data row14 col0" >Quadratic Discriminant Analysis</td>
      <td id="T_f7188_row14_col1" class="data row14 col1" >0.5828</td>
      <td id="T_f7188_row14_col2" class="data row14 col2" >0.5474</td>
      <td id="T_f7188_row14_col3" class="data row14 col3" >0.3886</td>
      <td id="T_f7188_row14_col4" class="data row14 col4" >0.5156</td>
      <td id="T_f7188_row14_col5" class="data row14 col5" >0.4061</td>
      <td id="T_f7188_row14_col6" class="data row14 col6" >0.1050</td>
      <td id="T_f7188_row14_col7" class="data row14 col7" >0.1191</td>
      <td id="T_f7188_row14_col8" class="data row14 col8" >0.0650</td>
    </tr>
    <tr>
      <th id="T_f7188_level0_row15" class="row_heading level0 row15" >nb</th>
      <td id="T_f7188_row15_col0" class="data row15 col0" >Naive Bayes</td>
      <td id="T_f7188_row15_col1" class="data row15 col1" >0.4591</td>
      <td id="T_f7188_row15_col2" class="data row15 col2" >0.5404</td>
      <td id="T_f7188_row15_col3" class="data row15 col3" >0.9050</td>
      <td id="T_f7188_row15_col4" class="data row15 col4" >0.4109</td>
      <td id="T_f7188_row15_col5" class="data row15 col5" >0.5651</td>
      <td id="T_f7188_row15_col6" class="data row15 col6" >0.0663</td>
      <td id="T_f7188_row15_col7" class="data row15 col7" >0.1122</td>
      <td id="T_f7188_row15_col8" class="data row15 col8" >0.0375</td>
    </tr>
  </tbody>
</table>






    RidgeClassifier(alpha=1.0, class_weight=None, copy_X=True, fit_intercept=True,
                    max_iter=None, normalize=False, random_state=0, solver='auto',
                    tol=0.001)



今回の比較では、`Ridge Classifier`が最も`Accuracy`が高い結果となりました。  
`Ridge Classifier`を使用し、ハイパーパラメータ調整を行います。  


# モデルの作成

上記では複数のアルゴリズムを比較した結果でモデルを作成しましたが、  
対象のアルゴリズムが決まっている場合、`compare_models`を使わずに`create_model`関数を使って直接モデルを作成することができます。


```python
model = create_model(
    'ridge', # 対象アルゴリズムを指定
    cross_validation=True, # cross validationの実施有無
    fold=4, # cross validation の fold数
)
```


<style type="text/css">
#T_e0c51_row4_col0, #T_e0c51_row4_col1, #T_e0c51_row4_col2, #T_e0c51_row4_col3, #T_e0c51_row4_col4, #T_e0c51_row4_col5, #T_e0c51_row4_col6 {
  background: yellow;
}
</style>
<table id="T_e0c51">
  <thead>
    <tr>
      <th class="blank level0" >&nbsp;</th>
      <th id="T_e0c51_level0_col0" class="col_heading level0 col0" >Accuracy</th>
      <th id="T_e0c51_level0_col1" class="col_heading level0 col1" >AUC</th>
      <th id="T_e0c51_level0_col2" class="col_heading level0 col2" >Recall</th>
      <th id="T_e0c51_level0_col3" class="col_heading level0 col3" >Prec.</th>
      <th id="T_e0c51_level0_col4" class="col_heading level0 col4" >F1</th>
      <th id="T_e0c51_level0_col5" class="col_heading level0 col5" >Kappa</th>
      <th id="T_e0c51_level0_col6" class="col_heading level0 col6" >MCC</th>
    </tr>
    <tr>
      <th class="index_name level0" >Fold</th>
      <th class="blank col0" >&nbsp;</th>
      <th class="blank col1" >&nbsp;</th>
      <th class="blank col2" >&nbsp;</th>
      <th class="blank col3" >&nbsp;</th>
      <th class="blank col4" >&nbsp;</th>
      <th class="blank col5" >&nbsp;</th>
      <th class="blank col6" >&nbsp;</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th id="T_e0c51_level0_row0" class="row_heading level0 row0" >0</th>
      <td id="T_e0c51_row0_col0" class="data row0 col0" >0.8141</td>
      <td id="T_e0c51_row0_col1" class="data row0 col1" >0.0000</td>
      <td id="T_e0c51_row0_col2" class="data row0 col2" >0.7213</td>
      <td id="T_e0c51_row0_col3" class="data row0 col3" >0.7857</td>
      <td id="T_e0c51_row0_col4" class="data row0 col4" >0.7521</td>
      <td id="T_e0c51_row0_col5" class="data row0 col5" >0.6039</td>
      <td id="T_e0c51_row0_col6" class="data row0 col6" >0.6053</td>
    </tr>
    <tr>
      <th id="T_e0c51_level0_row1" class="row_heading level0 row1" >1</th>
      <td id="T_e0c51_row1_col0" class="data row1 col0" >0.8141</td>
      <td id="T_e0c51_row1_col1" class="data row1 col1" >0.0000</td>
      <td id="T_e0c51_row1_col2" class="data row1 col2" >0.7377</td>
      <td id="T_e0c51_row1_col3" class="data row1 col3" >0.7759</td>
      <td id="T_e0c51_row1_col4" class="data row1 col4" >0.7563</td>
      <td id="T_e0c51_row1_col5" class="data row1 col5" >0.6062</td>
      <td id="T_e0c51_row1_col6" class="data row1 col6" >0.6067</td>
    </tr>
    <tr>
      <th id="T_e0c51_level0_row2" class="row_heading level0 row2" >2</th>
      <td id="T_e0c51_row2_col0" class="data row2 col0" >0.8205</td>
      <td id="T_e0c51_row2_col1" class="data row2 col1" >0.0000</td>
      <td id="T_e0c51_row2_col2" class="data row2 col2" >0.6833</td>
      <td id="T_e0c51_row2_col3" class="data row2 col3" >0.8200</td>
      <td id="T_e0c51_row2_col4" class="data row2 col4" >0.7455</td>
      <td id="T_e0c51_row2_col5" class="data row2 col5" >0.6086</td>
      <td id="T_e0c51_row2_col6" class="data row2 col6" >0.6146</td>
    </tr>
    <tr>
      <th id="T_e0c51_level0_row3" class="row_heading level0 row3" >3</th>
      <td id="T_e0c51_row3_col0" class="data row3 col0" >0.8452</td>
      <td id="T_e0c51_row3_col1" class="data row3 col1" >0.0000</td>
      <td id="T_e0c51_row3_col2" class="data row3 col2" >0.8000</td>
      <td id="T_e0c51_row3_col3" class="data row3 col3" >0.8000</td>
      <td id="T_e0c51_row3_col4" class="data row3 col4" >0.8000</td>
      <td id="T_e0c51_row3_col5" class="data row3 col5" >0.6737</td>
      <td id="T_e0c51_row3_col6" class="data row3 col6" >0.6737</td>
    </tr>
    <tr>
      <th id="T_e0c51_level0_row4" class="row_heading level0 row4" >Mean</th>
      <td id="T_e0c51_row4_col0" class="data row4 col0" >0.8235</td>
      <td id="T_e0c51_row4_col1" class="data row4 col1" >0.0000</td>
      <td id="T_e0c51_row4_col2" class="data row4 col2" >0.7356</td>
      <td id="T_e0c51_row4_col3" class="data row4 col3" >0.7954</td>
      <td id="T_e0c51_row4_col4" class="data row4 col4" >0.7635</td>
      <td id="T_e0c51_row4_col5" class="data row4 col5" >0.6231</td>
      <td id="T_e0c51_row4_col6" class="data row4 col6" >0.6251</td>
    </tr>
    <tr>
      <th id="T_e0c51_level0_row5" class="row_heading level0 row5" >Std</th>
      <td id="T_e0c51_row5_col0" class="data row5 col0" >0.0128</td>
      <td id="T_e0c51_row5_col1" class="data row5 col1" >0.0000</td>
      <td id="T_e0c51_row5_col2" class="data row5 col2" >0.0421</td>
      <td id="T_e0c51_row5_col3" class="data row5 col3" >0.0166</td>
      <td id="T_e0c51_row5_col4" class="data row5 col4" >0.0214</td>
      <td id="T_e0c51_row5_col5" class="data row5 col5" >0.0293</td>
      <td id="T_e0c51_row5_col6" class="data row5 col6" >0.0283</td>
    </tr>
  </tbody>
</table>



# ハイパーパラメータのチューニング

作成したモデルに対してハイパーパラメータチューニングします。  
`tune_model`関数を使い、グリッドサーチ法を用いた調整を行うことができます。  



```python
tuned_model = tune_model(
    model, # 定義したモデル
    optimize="Accuracy", # 評価指標
    fold=4, # cross validation のfold数
    n_iter=30, # ランダムグリッド検索の試行回数。デフォルトは10
)
```

`n_iter`が未定義で紹介している記事をよく見かけますが、デフォルト回数だとチューニング前を超えない事が多々あります。  
ただ、その分処理時間は延びますのでご注意ください。


# モデルを確認
モデルに対し、`evaluate_model()`で`学習曲線`などの様々な指標を確認することができます。


```python
evaluate_model(tuned_model)
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/83511/7da79b8b-a997-8463-8733-124f18a65775.png)




# モデルの完成と予測
調整したモデルを`finalize_model()`を実行することで、全教師データを使用してモデルを完成させます。 
その後、完成させたモデルを使って`predict_model()`を実行しテストデータの予測を行います。  


```python
# モデルの決定
final_model = finalize_model(
    tuned_model, # ハイパーパラメータ調整後のモデル
)

# テストデータの予測
result = predict_model(
    final_model, # モデル
    data=test, # テストデータ
    raw_score=True, # ラベル予測時のscoreを表示
)
result
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>PassengerId</th>
      <th>Pclass</th>
      <th>Name</th>
      <th>Sex</th>
      <th>Age</th>
      <th>SibSp</th>
      <th>Parch</th>
      <th>Ticket</th>
      <th>Fare</th>
      <th>Cabin</th>
      <th>Embarked</th>
      <th>Label</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>892</td>
      <td>3</td>
      <td>Kelly, Mr. James</td>
      <td>male</td>
      <td>34.5</td>
      <td>0</td>
      <td>0</td>
      <td>330911</td>
      <td>7.8292</td>
      <td>NaN</td>
      <td>Q</td>
      <td>0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>893</td>
      <td>3</td>
      <td>Wilkes, Mrs. James (Ellen Needs)</td>
      <td>female</td>
      <td>47.0</td>
      <td>1</td>
      <td>0</td>
      <td>363272</td>
      <td>7.0000</td>
      <td>NaN</td>
      <td>S</td>
      <td>0</td>
    </tr>
    <tr>
      <th>2</th>
      <td>894</td>
      <td>2</td>
      <td>Myles, Mr. Thomas Francis</td>
      <td>male</td>
      <td>62.0</td>
      <td>0</td>
      <td>0</td>
      <td>240276</td>
      <td>9.6875</td>
      <td>NaN</td>
      <td>Q</td>
      <td>0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>895</td>
      <td>3</td>
      <td>Wirz, Mr. Albert</td>
      <td>male</td>
      <td>27.0</td>
      <td>0</td>
      <td>0</td>
      <td>315154</td>
      <td>8.6625</td>
      <td>NaN</td>
      <td>S</td>
      <td>0</td>
    </tr>
    <tr>
      <th>4</th>
      <td>896</td>
      <td>3</td>
      <td>Hirvonen, Mrs. Alexander (Helga E Lindqvist)</td>
      <td>female</td>
      <td>22.0</td>
      <td>1</td>
      <td>1</td>
      <td>3101298</td>
      <td>12.2875</td>
      <td>NaN</td>
      <td>S</td>
      <td>1</td>
    </tr>
    <tr>
      <th>...</th>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>413</th>
      <td>1305</td>
      <td>3</td>
      <td>Spector, Mr. Woolf</td>
      <td>male</td>
      <td>NaN</td>
      <td>0</td>
      <td>0</td>
      <td>A.5. 3236</td>
      <td>8.0500</td>
      <td>NaN</td>
      <td>S</td>
      <td>0</td>
    </tr>
    <tr>
      <th>414</th>
      <td>1306</td>
      <td>1</td>
      <td>Oliva y Ocana, Dona. Fermina</td>
      <td>female</td>
      <td>39.0</td>
      <td>0</td>
      <td>0</td>
      <td>PC 17758</td>
      <td>108.9000</td>
      <td>C105</td>
      <td>C</td>
      <td>1</td>
    </tr>
    <tr>
      <th>415</th>
      <td>1307</td>
      <td>3</td>
      <td>Saether, Mr. Simon Sivertsen</td>
      <td>male</td>
      <td>38.5</td>
      <td>0</td>
      <td>0</td>
      <td>SOTON/O.Q. 3101262</td>
      <td>7.2500</td>
      <td>NaN</td>
      <td>S</td>
      <td>0</td>
    </tr>
    <tr>
      <th>416</th>
      <td>1308</td>
      <td>3</td>
      <td>Ware, Mr. Frederick</td>
      <td>male</td>
      <td>NaN</td>
      <td>0</td>
      <td>0</td>
      <td>359309</td>
      <td>8.0500</td>
      <td>NaN</td>
      <td>S</td>
      <td>0</td>
    </tr>
    <tr>
      <th>417</th>
      <td>1309</td>
      <td>3</td>
      <td>Peter, Master. Michael J</td>
      <td>male</td>
      <td>NaN</td>
      <td>1</td>
      <td>1</td>
      <td>2668</td>
      <td>22.3583</td>
      <td>NaN</td>
      <td>C</td>
      <td>0</td>
    </tr>
  </tbody>
</table>
</div>



`predict_model()`の結果はDataFrameが返却されます。  
このDataFrameには対象のテストデータに予測結果である`Label`列が追加されています。    

# 提出ファイルの作成

予測結果から提出ファイルを作成します。  

ちなみに試しにKaggleに提出したところ、スコアは`0.78708`でした。  
高得点を目指すなら、`setup`の前段階で特徴量エンジニアリングをする必要がありそうです。  


```python
submit = result[['PassengerId', 'Label']]
submit = submit.rename(columns={'Label':'Survived'})
submit.to_csv("submission.csv", encoding='utf-8', index=False)
```

# MLflowの確認

MLflowはコマンドで起動します。  
以下のコマンドを実行し、表示されるURL(例えば http://127.0.0.1:5000 ) にアクセスします


```python
!mlflow ui
```

    [2022-10-23 11:16:04 +0900] [559] [INFO] Starting gunicorn 20.1.0
    [2022-10-23 11:16:04 +0900] [559] [INFO] Listening at: http://127.0.0.1:5000 (559)
    [2022-10-23 11:16:04 +0900] [559] [INFO] Using worker: sync
    [2022-10-23 11:16:04 +0900] [561] [INFO] Booting worker with pid: 561
    ^C
    [2022-10-23 11:16:11 +0900] [559] [INFO] Handling signal: int
    [2022-10-23 11:16:11 +0900] [561] [INFO] Worker exiting (pid: 561)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/83511/85bd718f-ec2d-1ab5-a96c-8aa37a1af346.png)


# おわりに

PyCaretを使用する事で諸々のコードを書く必要がなくなり、すぐにデータ分析やら特徴量の作成に勤しむことができるのが良きです。  
各種ツールも設定値が多々あり、使いこなせるよう色々調べていきたいです。