Data Every Day: AV : Healthcare Analytics II

# tldr

Kggleの[AV : Healthcare Analytics II
](https://www.kaggle.com/nehaprabhavalkar/av-healthcare-analytics-ii)を[Predicting Hospital Stays - Data Every Day #024](https://www.youtube.com/watch?v=rcKeo-Jh3XM)に沿ってやっていきます。

実行環境はGoogle Colaboratorです。

# インポート


```
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

import sklearn.preprocessing as sp
from sklearn.model_selection import train_test_split

from sklearn.linear_model import LogisticRegression

import tensorflow as tf
```

# データのダウンロード

Google Driveをマウントします。




```
from google.colab import drive
drive.mount('/content/drive')
```

    Mounted at /content/drive


KaggleのAPIクライアントを初期化し、認証します。
認証情報はGoogle Drive内（`/content/drive/My Drive/Colab Notebooks/Kaggle`）に`kaggle.json`として置いてあります。


```
import os
kaggle_path = "/content/drive/My Drive/Colab Notebooks/Kaggle"
os.environ['KAGGLE_CONFIG_DIR'] = kaggle_path

from kaggle.api.kaggle_api_extended import KaggleApi
api = KaggleApi()
api.authenticate() 
```

Kaggle APIを使ってデータをダウンロードします。


```
dataset_id = 'nehaprabhavalkar/av-healthcare-analytics-ii'
dataset = api.dataset_list_files(dataset_id)
file_name = dataset.files[0].name
file_path = os.path.join(api.get_default_download_dir(), file_name)
file_path
```

    Warning: Looks like you're using an outdated API Version, please consider updating (server 1.5.10 / client 1.5.9)





    '/content/healthcare/train_data.csv'




```
api.dataset_download_file(dataset_id, file_name, force=True, quiet=False)
```

    100%|██████████| 4.45M/4.45M [00:00<00:00, 213MB/s]

    Downloading train_data.csv.zip to /content
    


    





    True




```
os.listdir('/content')
```




    ['.config', 'train_data.csv.zip', 'drive', 'sample_data']




```
import zipfile

zip_path = '/content/train_data.csv.zip'
with zipfile.ZipFile(zip_path) as existing_zip:
    existing_zip.extractall('/content')
```


```
os.listdir('/content')
```




    ['.config', 'train_data.csv.zip', 'drive', 'train_data.csv', 'sample_data']



# データの読み込み

Padasを使ってダウンロードしてきたCSVファイルを読み込みます。


```
data = pd.read_csv('/content/train_data.csv')
```


```
data
```




<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>case_id</th>
      <th>Hospital_code</th>
      <th>Hospital_type_code</th>
      <th>City_Code_Hospital</th>
      <th>Hospital_region_code</th>
      <th>Available Extra Rooms in Hospital</th>
      <th>Department</th>
      <th>Ward_Type</th>
      <th>Ward_Facility_Code</th>
      <th>Bed Grade</th>
      <th>patientid</th>
      <th>City_Code_Patient</th>
      <th>Type of Admission</th>
      <th>Severity of Illness</th>
      <th>Visitors with Patient</th>
      <th>Age</th>
      <th>Admission_Deposit</th>
      <th>Stay</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>8</td>
      <td>c</td>
      <td>3</td>
      <td>Z</td>
      <td>3</td>
      <td>radiotherapy</td>
      <td>R</td>
      <td>F</td>
      <td>2.0</td>
      <td>31397</td>
      <td>7.0</td>
      <td>Emergency</td>
      <td>Extreme</td>
      <td>2</td>
      <td>51-60</td>
      <td>4911.0</td>
      <td>0-10</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2</td>
      <td>2</td>
      <td>c</td>
      <td>5</td>
      <td>Z</td>
      <td>2</td>
      <td>radiotherapy</td>
      <td>S</td>
      <td>F</td>
      <td>2.0</td>
      <td>31397</td>
      <td>7.0</td>
      <td>Trauma</td>
      <td>Extreme</td>
      <td>2</td>
      <td>51-60</td>
      <td>5954.0</td>
      <td>41-50</td>
    </tr>
    <tr>
      <th>2</th>
      <td>3</td>
      <td>10</td>
      <td>e</td>
      <td>1</td>
      <td>X</td>
      <td>2</td>
      <td>anesthesia</td>
      <td>S</td>
      <td>E</td>
      <td>2.0</td>
      <td>31397</td>
      <td>7.0</td>
      <td>Trauma</td>
      <td>Extreme</td>
      <td>2</td>
      <td>51-60</td>
      <td>4745.0</td>
      <td>31-40</td>
    </tr>
    <tr>
      <th>3</th>
      <td>4</td>
      <td>26</td>
      <td>b</td>
      <td>2</td>
      <td>Y</td>
      <td>2</td>
      <td>radiotherapy</td>
      <td>R</td>
      <td>D</td>
      <td>2.0</td>
      <td>31397</td>
      <td>7.0</td>
      <td>Trauma</td>
      <td>Extreme</td>
      <td>2</td>
      <td>51-60</td>
      <td>7272.0</td>
      <td>41-50</td>
    </tr>
    <tr>
      <th>4</th>
      <td>5</td>
      <td>26</td>
      <td>b</td>
      <td>2</td>
      <td>Y</td>
      <td>2</td>
      <td>radiotherapy</td>
      <td>S</td>
      <td>D</td>
      <td>2.0</td>
      <td>31397</td>
      <td>7.0</td>
      <td>Trauma</td>
      <td>Extreme</td>
      <td>2</td>
      <td>51-60</td>
      <td>5558.0</td>
      <td>41-50</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>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>318433</th>
      <td>318434</td>
      <td>6</td>
      <td>a</td>
      <td>6</td>
      <td>X</td>
      <td>3</td>
      <td>radiotherapy</td>
      <td>Q</td>
      <td>F</td>
      <td>4.0</td>
      <td>86499</td>
      <td>23.0</td>
      <td>Emergency</td>
      <td>Moderate</td>
      <td>3</td>
      <td>41-50</td>
      <td>4144.0</td>
      <td>11-20</td>
    </tr>
    <tr>
      <th>318434</th>
      <td>318435</td>
      <td>24</td>
      <td>a</td>
      <td>1</td>
      <td>X</td>
      <td>2</td>
      <td>anesthesia</td>
      <td>Q</td>
      <td>E</td>
      <td>4.0</td>
      <td>325</td>
      <td>8.0</td>
      <td>Urgent</td>
      <td>Moderate</td>
      <td>4</td>
      <td>81-90</td>
      <td>6699.0</td>
      <td>31-40</td>
    </tr>
    <tr>
      <th>318435</th>
      <td>318436</td>
      <td>7</td>
      <td>a</td>
      <td>4</td>
      <td>X</td>
      <td>3</td>
      <td>gynecology</td>
      <td>R</td>
      <td>F</td>
      <td>4.0</td>
      <td>125235</td>
      <td>10.0</td>
      <td>Emergency</td>
      <td>Minor</td>
      <td>3</td>
      <td>71-80</td>
      <td>4235.0</td>
      <td>11-20</td>
    </tr>
    <tr>
      <th>318436</th>
      <td>318437</td>
      <td>11</td>
      <td>b</td>
      <td>2</td>
      <td>Y</td>
      <td>3</td>
      <td>anesthesia</td>
      <td>Q</td>
      <td>D</td>
      <td>3.0</td>
      <td>91081</td>
      <td>8.0</td>
      <td>Trauma</td>
      <td>Minor</td>
      <td>5</td>
      <td>11-20</td>
      <td>3761.0</td>
      <td>11-20</td>
    </tr>
    <tr>
      <th>318437</th>
      <td>318438</td>
      <td>19</td>
      <td>a</td>
      <td>7</td>
      <td>Y</td>
      <td>5</td>
      <td>gynecology</td>
      <td>Q</td>
      <td>C</td>
      <td>2.0</td>
      <td>21641</td>
      <td>8.0</td>
      <td>Emergency</td>
      <td>Minor</td>
      <td>2</td>
      <td>11-20</td>
      <td>4752.0</td>
      <td>0-10</td>
    </tr>
  </tbody>
</table>
<p>318438 rows × 18 columns</p>
</div>



# 下準備

## 欠損値の処理


```
data.isnull().sum()
```




    case_id                                 0
    Hospital_code                           0
    Hospital_type_code                      0
    City_Code_Hospital                      0
    Hospital_region_code                    0
    Available Extra Rooms in Hospital       0
    Department                              0
    Ward_Type                               0
    Ward_Facility_Code                      0
    Bed Grade                             113
    patientid                               0
    City_Code_Patient                    4532
    Type of Admission                       0
    Severity of Illness                     0
    Visitors with Patient                   0
    Age                                     0
    Admission_Deposit                       0
    Stay                                    0
    dtype: int64



欠損値に列全体の平均を挿入します。


```
def impute_missing_values(data, columns):
    for column in columns:
        data[column] = data[column].fillna(data[column].mean())
```


```
impute_columns = ['Bed Grade', 'City_Code_Patient']
impute_missing_values(data, impute_columns)
```


```
data.isnull().sum()
```




    case_id                              0
    Hospital_code                        0
    Hospital_type_code                   0
    City_Code_Hospital                   0
    Hospital_region_code                 0
    Available Extra Rooms in Hospital    0
    Department                           0
    Ward_Type                            0
    Ward_Facility_Code                   0
    Bed Grade                            0
    patientid                            0
    City_Code_Patient                    0
    Type of Admission                    0
    Severity of Illness                  0
    Visitors with Patient                0
    Age                                  0
    Admission_Deposit                    0
    Stay                                 0
    dtype: int64



## オブジェクト型の処理


```
data.dtypes
```




    case_id                                int64
    Hospital_code                          int64
    Hospital_type_code                    object
    City_Code_Hospital                     int64
    Hospital_region_code                  object
    Available Extra Rooms in Hospital      int64
    Department                            object
    Ward_Type                             object
    Ward_Facility_Code                    object
    Bed Grade                            float64
    patientid                              int64
    City_Code_Patient                    float64
    Type of Admission                     object
    Severity of Illness                   object
    Visitors with Patient                  int64
    Age                                   object
    Admission_Deposit                    float64
    Stay                                  object
    dtype: object




```
def get_categorical_unique(data):
    # object型の列のみをリストにする
    categorical_columns = [column for column in data.dtypes.index if data.dtypes[column] == 'object']
    # 各列の値を取り出しマップを作る
    categorical_uniques = {column: data[column].unique() for column in categorical_columns}
    return categorical_uniques
```


```
get_categorical_unique(data)
```




    {'Age': array(['51-60', '71-80', '31-40', '41-50', '81-90', '61-70', '21-30',
            '11-20', '0-10', '91-100'], dtype=object),
     'Department': array(['radiotherapy', 'anesthesia', 'gynecology', 'TB & Chest disease',
            'surgery'], dtype=object),
     'Hospital_region_code': array(['Z', 'X', 'Y'], dtype=object),
     'Hospital_type_code': array(['c', 'e', 'b', 'a', 'f', 'd', 'g'], dtype=object),
     'Severity of Illness': array(['Extreme', 'Moderate', 'Minor'], dtype=object),
     'Stay': array(['0-10', '41-50', '31-40', '11-20', '51-60', '21-30', '71-80',
            'More than 100 Days', '81-90', '61-70', '91-100'], dtype=object),
     'Type of Admission': array(['Emergency', 'Trauma', 'Urgent'], dtype=object),
     'Ward_Facility_Code': array(['F', 'E', 'D', 'B', 'A', 'C'], dtype=object),
     'Ward_Type': array(['R', 'S', 'Q', 'P', 'T', 'U'], dtype=object)}



各データの特性を見て

- Labelエンコード
- Onehotエンコード
- Ordinalエンコード

のどれでエンコードするか判断する

### Onehotエンコード


```
def onehot_encode(data, columns):
    for column in columns:
        dummies = pd.get_dummies(data[column])
        data = pd.concat([data, dummies], axis=1)
        data = data.drop(column, axis=1)
    return data
```


```
onehot_columns =  ['Department', 'Hospital_region_code', 'Hospital_type_code', 'Ward_Facility_Code', 'Ward_Type']
```


```
data = onehot_encode(data, onehot_columns)
```

### Ordinalエンコード


```
categorical_uniques = get_categorical_unique(data)
categorical_uniques
```




    {'Age': array(['51-60', '71-80', '31-40', '41-50', '81-90', '61-70', '21-30',
            '11-20', '0-10', '91-100'], dtype=object),
     'Severity of Illness': array(['Extreme', 'Moderate', 'Minor'], dtype=object),
     'Stay': array(['0-10', '41-50', '31-40', '11-20', '51-60', '21-30', '71-80',
            'More than 100 Days', '81-90', '61-70', '91-100'], dtype=object),
     'Type of Admission': array(['Emergency', 'Trauma', 'Urgent'], dtype=object)}




```
for column in categorical_uniques:
    categorical_uniques[column] = sorted(categorical_uniques[column])
```


```
categorical_uniques
```




    {'Age': ['0-10',
      '11-20',
      '21-30',
      '31-40',
      '41-50',
      '51-60',
      '61-70',
      '71-80',
      '81-90',
      '91-100'],
     'Severity of Illness': ['Extreme', 'Minor', 'Moderate'],
     'Stay': ['0-10',
      '11-20',
      '21-30',
      '31-40',
      '41-50',
      '51-60',
      '61-70',
      '71-80',
      '81-90',
      '91-100',
      'More than 100 Days'],
     'Type of Admission': ['Emergency', 'Trauma', 'Urgent']}




```
unique_list = categorical_uniques['Type of Admission']
unique_list.insert(0, unique_list.pop(unique_list.index('Urgent')))
unique_list.insert(0, unique_list.pop(unique_list.index('Trauma')))

unique_list = categorical_uniques['Severity of Illness']
unique_list.insert(0, unique_list.pop(unique_list.index('Moderate')))
unique_list.insert(0, unique_list.pop(unique_list.index('Minor')))
```


```
categorical_uniques
```




    {'Age': ['0-10',
      '11-20',
      '21-30',
      '31-40',
      '41-50',
      '51-60',
      '61-70',
      '71-80',
      '81-90',
      '91-100'],
     'Severity of Illness': ['Minor', 'Moderate', 'Extreme'],
     'Stay': ['0-10',
      '11-20',
      '21-30',
      '31-40',
      '41-50',
      '51-60',
      '61-70',
      '71-80',
      '81-90',
      '91-100',
      'More than 100 Days'],
     'Type of Admission': ['Trauma', 'Urgent', 'Emergency']}




```
stay_mappings = {value: index for index, value in enumerate(categorical_uniques['Stay'])}
```


```
def ordinal_encode(data, uniques):
    for column in uniques:
        data[column] = data[column].apply(lambda x: uniques[column].index(x))
```


```
ordinal_encode(data, categorical_uniques)
```


```
data['Stay']
```




    0         0
    1         4
    2         3
    3         4
    4         4
             ..
    318433    1
    318434    3
    318435    1
    318436    1
    318437    0
    Name: Stay, Length: 318438, dtype: int64



すべてが数値になっていることを確認


```
(data.dtypes == 'object').sum()
```




    0



# スケーリング


```
data = data.set_index('case_id')
```


```
y = data['Stay']
X = data.drop(['Stay'], axis=1)
```


```
y
```




    case_id
    1         0
    2         4
    3         3
    4         4
    5         4
             ..
    318434    1
    318435    3
    318436    1
    318437    1
    318438    0
    Name: Stay, Length: 318438, dtype: int64




```
scaler = sp.StandardScaler()
X = pd.DataFrame(scaler.fit_transform(X), index=X.index, columns=X.columns)
```


```
X
```




<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>Hospital_code</th>
      <th>City_Code_Hospital</th>
      <th>Available Extra Rooms in Hospital</th>
      <th>Bed Grade</th>
      <th>patientid</th>
      <th>City_Code_Patient</th>
      <th>Type of Admission</th>
      <th>Severity of Illness</th>
      <th>Visitors with Patient</th>
      <th>Age</th>
      <th>Admission_Deposit</th>
      <th>TB &amp; Chest disease</th>
      <th>anesthesia</th>
      <th>gynecology</th>
      <th>radiotherapy</th>
      <th>surgery</th>
      <th>X</th>
      <th>Y</th>
      <th>Z</th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
      <th>d</th>
      <th>e</th>
      <th>f</th>
      <th>g</th>
      <th>A</th>
      <th>B</th>
      <th>C</th>
      <th>D</th>
      <th>E</th>
      <th>F</th>
      <th>P</th>
      <th>Q</th>
      <th>R</th>
      <th>S</th>
      <th>T</th>
      <th>U</th>
    </tr>
    <tr>
      <th>case_id</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1</th>
      <td>-1.195176</td>
      <td>-0.571055</td>
      <td>-0.169177</td>
      <td>-0.716855</td>
      <td>-0.904442</td>
      <td>-0.053458</td>
      <td>1.212557</td>
      <td>1.646648</td>
      <td>-0.727923</td>
      <td>0.461600</td>
      <td>0.027835</td>
      <td>-0.176175</td>
      <td>-0.320416</td>
      <td>-1.902171</td>
      <td>3.188572</td>
      <td>-0.061529</td>
      <td>-0.848727</td>
      <td>-0.790317</td>
      <td>2.020115</td>
      <td>-0.905268</td>
      <td>-0.525686</td>
      <td>2.435861</td>
      <td>-0.26155</td>
      <td>-0.290425</td>
      <td>-0.186494</td>
      <td>-0.116679</td>
      <td>-0.309922</td>
      <td>-0.352282</td>
      <td>-0.354009</td>
      <td>-0.440807</td>
      <td>-0.458683</td>
      <td>1.350633</td>
      <td>-0.126891</td>
      <td>-0.707202</td>
      <td>1.220175</td>
      <td>-0.568572</td>
      <td>-0.068263</td>
      <td>-0.005316</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-1.890124</td>
      <td>0.073580</td>
      <td>-1.025217</td>
      <td>-0.716855</td>
      <td>-0.904442</td>
      <td>-0.053458</td>
      <td>-0.974973</td>
      <td>1.646648</td>
      <td>-0.727923</td>
      <td>0.461600</td>
      <td>0.987556</td>
      <td>-0.176175</td>
      <td>-0.320416</td>
      <td>-1.902171</td>
      <td>3.188572</td>
      <td>-0.061529</td>
      <td>-0.848727</td>
      <td>-0.790317</td>
      <td>2.020115</td>
      <td>-0.905268</td>
      <td>-0.525686</td>
      <td>2.435861</td>
      <td>-0.26155</td>
      <td>-0.290425</td>
      <td>-0.186494</td>
      <td>-0.116679</td>
      <td>-0.309922</td>
      <td>-0.352282</td>
      <td>-0.354009</td>
      <td>-0.440807</td>
      <td>-0.458683</td>
      <td>1.350633</td>
      <td>-0.126891</td>
      <td>-0.707202</td>
      <td>-0.819554</td>
      <td>1.758792</td>
      <td>-0.068263</td>
      <td>-0.005316</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.963527</td>
      <td>-1.215691</td>
      <td>-1.025217</td>
      <td>-0.716855</td>
      <td>-0.904442</td>
      <td>-0.053458</td>
      <td>-0.974973</td>
      <td>1.646648</td>
      <td>-0.727923</td>
      <td>0.461600</td>
      <td>-0.124910</td>
      <td>-0.176175</td>
      <td>3.120939</td>
      <td>-1.902171</td>
      <td>-0.313620</td>
      <td>-0.061529</td>
      <td>1.178235</td>
      <td>-0.790317</td>
      <td>-0.495021</td>
      <td>-0.905268</td>
      <td>-0.525686</td>
      <td>-0.410533</td>
      <td>-0.26155</td>
      <td>3.443224</td>
      <td>-0.186494</td>
      <td>-0.116679</td>
      <td>-0.309922</td>
      <td>-0.352282</td>
      <td>-0.354009</td>
      <td>-0.440807</td>
      <td>2.180153</td>
      <td>-0.740394</td>
      <td>-0.126891</td>
      <td>-0.707202</td>
      <td>-0.819554</td>
      <td>1.758792</td>
      <td>-0.068263</td>
      <td>-0.005316</td>
    </tr>
    <tr>
      <th>4</th>
      <td>0.889668</td>
      <td>-0.893373</td>
      <td>-1.025217</td>
      <td>-0.716855</td>
      <td>-0.904442</td>
      <td>-0.053458</td>
      <td>-0.974973</td>
      <td>1.646648</td>
      <td>-0.727923</td>
      <td>0.461600</td>
      <td>2.200319</td>
      <td>-0.176175</td>
      <td>-0.320416</td>
      <td>-1.902171</td>
      <td>3.188572</td>
      <td>-0.061529</td>
      <td>-0.848727</td>
      <td>1.265315</td>
      <td>-0.495021</td>
      <td>-0.905268</td>
      <td>1.902277</td>
      <td>-0.410533</td>
      <td>-0.26155</td>
      <td>-0.290425</td>
      <td>-0.186494</td>
      <td>-0.116679</td>
      <td>-0.309922</td>
      <td>-0.352282</td>
      <td>-0.354009</td>
      <td>2.268564</td>
      <td>-0.458683</td>
      <td>-0.740394</td>
      <td>-0.126891</td>
      <td>-0.707202</td>
      <td>1.220175</td>
      <td>-0.568572</td>
      <td>-0.068263</td>
      <td>-0.005316</td>
    </tr>
    <tr>
      <th>5</th>
      <td>0.889668</td>
      <td>-0.893373</td>
      <td>-1.025217</td>
      <td>-0.716855</td>
      <td>-0.904442</td>
      <td>-0.053458</td>
      <td>-0.974973</td>
      <td>1.646648</td>
      <td>-0.727923</td>
      <td>0.461600</td>
      <td>0.623175</td>
      <td>-0.176175</td>
      <td>-0.320416</td>
      <td>-1.902171</td>
      <td>3.188572</td>
      <td>-0.061529</td>
      <td>-0.848727</td>
      <td>1.265315</td>
      <td>-0.495021</td>
      <td>-0.905268</td>
      <td>1.902277</td>
      <td>-0.410533</td>
      <td>-0.26155</td>
      <td>-0.290425</td>
      <td>-0.186494</td>
      <td>-0.116679</td>
      <td>-0.309922</td>
      <td>-0.352282</td>
      <td>-0.354009</td>
      <td>2.268564</td>
      <td>-0.458683</td>
      <td>-0.740394</td>
      <td>-0.126891</td>
      <td>-0.707202</td>
      <td>-0.819554</td>
      <td>1.758792</td>
      <td>-0.068263</td>
      <td>-0.005316</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>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <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>318434</th>
      <td>-1.426825</td>
      <td>0.395897</td>
      <td>-0.169177</td>
      <td>1.574123</td>
      <td>0.546379</td>
      <td>3.342582</td>
      <td>1.212557</td>
      <td>0.138090</td>
      <td>-0.161049</td>
      <td>-0.067622</td>
      <td>-0.677923</td>
      <td>-0.176175</td>
      <td>-0.320416</td>
      <td>-1.902171</td>
      <td>3.188572</td>
      <td>-0.061529</td>
      <td>1.178235</td>
      <td>-0.790317</td>
      <td>-0.495021</td>
      <td>1.104645</td>
      <td>-0.525686</td>
      <td>-0.410533</td>
      <td>-0.26155</td>
      <td>-0.290425</td>
      <td>-0.186494</td>
      <td>-0.116679</td>
      <td>-0.309922</td>
      <td>-0.352282</td>
      <td>-0.354009</td>
      <td>-0.440807</td>
      <td>-0.458683</td>
      <td>1.350633</td>
      <td>-0.126891</td>
      <td>1.414024</td>
      <td>-0.819554</td>
      <td>-0.568572</td>
      <td>-0.068263</td>
      <td>-0.005316</td>
    </tr>
    <tr>
      <th>318435</th>
      <td>0.658018</td>
      <td>-1.215691</td>
      <td>-1.025217</td>
      <td>1.574123</td>
      <td>-1.722559</td>
      <td>0.158795</td>
      <td>0.118792</td>
      <td>0.138090</td>
      <td>0.405826</td>
      <td>2.049268</td>
      <td>1.673071</td>
      <td>-0.176175</td>
      <td>3.120939</td>
      <td>-1.902171</td>
      <td>-0.313620</td>
      <td>-0.061529</td>
      <td>1.178235</td>
      <td>-0.790317</td>
      <td>-0.495021</td>
      <td>1.104645</td>
      <td>-0.525686</td>
      <td>-0.410533</td>
      <td>-0.26155</td>
      <td>-0.290425</td>
      <td>-0.186494</td>
      <td>-0.116679</td>
      <td>-0.309922</td>
      <td>-0.352282</td>
      <td>-0.354009</td>
      <td>-0.440807</td>
      <td>2.180153</td>
      <td>-0.740394</td>
      <td>-0.126891</td>
      <td>1.414024</td>
      <td>-0.819554</td>
      <td>-0.568572</td>
      <td>-0.068263</td>
      <td>-0.005316</td>
    </tr>
    <tr>
      <th>318436</th>
      <td>-1.311001</td>
      <td>-0.248738</td>
      <td>-0.169177</td>
      <td>1.574123</td>
      <td>1.566288</td>
      <td>0.583300</td>
      <td>1.212557</td>
      <td>-1.370469</td>
      <td>-0.161049</td>
      <td>1.520045</td>
      <td>-0.594189</td>
      <td>-0.176175</td>
      <td>-0.320416</td>
      <td>0.525715</td>
      <td>-0.313620</td>
      <td>-0.061529</td>
      <td>1.178235</td>
      <td>-0.790317</td>
      <td>-0.495021</td>
      <td>1.104645</td>
      <td>-0.525686</td>
      <td>-0.410533</td>
      <td>-0.26155</td>
      <td>-0.290425</td>
      <td>-0.186494</td>
      <td>-0.116679</td>
      <td>-0.309922</td>
      <td>-0.352282</td>
      <td>-0.354009</td>
      <td>-0.440807</td>
      <td>-0.458683</td>
      <td>1.350633</td>
      <td>-0.126891</td>
      <td>-0.707202</td>
      <td>1.220175</td>
      <td>-0.568572</td>
      <td>-0.068263</td>
      <td>-0.005316</td>
    </tr>
    <tr>
      <th>318437</th>
      <td>-0.847702</td>
      <td>-0.893373</td>
      <td>-0.169177</td>
      <td>0.428634</td>
      <td>0.667022</td>
      <td>0.158795</td>
      <td>-0.974973</td>
      <td>-1.370469</td>
      <td>0.972701</td>
      <td>-1.655290</td>
      <td>-1.030342</td>
      <td>-0.176175</td>
      <td>3.120939</td>
      <td>-1.902171</td>
      <td>-0.313620</td>
      <td>-0.061529</td>
      <td>-0.848727</td>
      <td>1.265315</td>
      <td>-0.495021</td>
      <td>-0.905268</td>
      <td>1.902277</td>
      <td>-0.410533</td>
      <td>-0.26155</td>
      <td>-0.290425</td>
      <td>-0.186494</td>
      <td>-0.116679</td>
      <td>-0.309922</td>
      <td>-0.352282</td>
      <td>-0.354009</td>
      <td>2.268564</td>
      <td>-0.458683</td>
      <td>-0.740394</td>
      <td>-0.126891</td>
      <td>1.414024</td>
      <td>-0.819554</td>
      <td>-0.568572</td>
      <td>-0.068263</td>
      <td>-0.005316</td>
    </tr>
    <tr>
      <th>318438</th>
      <td>0.078895</td>
      <td>0.718215</td>
      <td>1.542903</td>
      <td>-0.716855</td>
      <td>-1.161314</td>
      <td>0.158795</td>
      <td>1.212557</td>
      <td>-1.370469</td>
      <td>-0.727923</td>
      <td>-1.655290</td>
      <td>-0.118469</td>
      <td>-0.176175</td>
      <td>-0.320416</td>
      <td>0.525715</td>
      <td>-0.313620</td>
      <td>-0.061529</td>
      <td>-0.848727</td>
      <td>1.265315</td>
      <td>-0.495021</td>
      <td>1.104645</td>
      <td>-0.525686</td>
      <td>-0.410533</td>
      <td>-0.26155</td>
      <td>-0.290425</td>
      <td>-0.186494</td>
      <td>-0.116679</td>
      <td>-0.309922</td>
      <td>-0.352282</td>
      <td>2.824791</td>
      <td>-0.440807</td>
      <td>-0.458683</td>
      <td>-0.740394</td>
      <td>-0.126891</td>
      <td>1.414024</td>
      <td>-0.819554</td>
      <td>-0.568572</td>
      <td>-0.068263</td>
      <td>-0.005316</td>
    </tr>
  </tbody>
</table>
<p>318438 rows × 38 columns</p>
</div>



# トレーニング


```
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8)
```


```
log_model = LogisticRegression()
log_model.fit(X_train, y_train)
```

    /usr/local/lib/python3.6/dist-packages/sklearn/linear_model/_logistic.py:940: ConvergenceWarning: lbfgs failed to converge (status=1):
    STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
    
    Increase the number of iterations (max_iter) or scale the data as shown in:
        https://scikit-learn.org/stable/modules/preprocessing.html
    Please also refer to the documentation for alternative solver options:
        https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
      extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)





    LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
                       intercept_scaling=1, l1_ratio=None, max_iter=100,
                       multi_class='auto', n_jobs=None, penalty='l2',
                       random_state=None, solver='lbfgs', tol=0.0001, verbose=0,
                       warm_start=False)




```
nn_model = tf.keras.Sequential([
    tf.keras.layers.Dense(16, activation='relu', input_shape=(38, )),
    tf.keras.layers.Dense(16, activation='relu'),
    tf.keras.layers.Dense(11, activation='softmax'),
])
```


```
nn_model.summary()
```

    Model: "sequential_1"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    dense_3 (Dense)              (None, 16)                624       
    _________________________________________________________________
    dense_4 (Dense)              (None, 16)                272       
    _________________________________________________________________
    dense_5 (Dense)              (None, 11)                187       
    =================================================================
    Total params: 1,083
    Trainable params: 1,083
    Non-trainable params: 0
    _________________________________________________________________



```
nn_model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'],
)
```


```
batch_size = 32
epochs = 10

history = nn_model.fit(
    X_train,
    y_train,
    validation_split=0.2,
    batch_size=batch_size,
    epochs=epochs,
)
```

    Epoch 1/10
    6369/6369 [==============================] - 8s 1ms/step - loss: 1.6387 - accuracy: 0.3739 - val_loss: 1.5888 - val_accuracy: 0.3951
    Epoch 2/10
    6369/6369 [==============================] - 8s 1ms/step - loss: 1.5738 - accuracy: 0.3990 - val_loss: 1.5667 - val_accuracy: 0.4029
    Epoch 3/10
    6369/6369 [==============================] - 8s 1ms/step - loss: 1.5593 - accuracy: 0.4034 - val_loss: 1.5563 - val_accuracy: 0.4081
    Epoch 4/10
    6369/6369 [==============================] - 8s 1ms/step - loss: 1.5528 - accuracy: 0.4057 - val_loss: 1.5530 - val_accuracy: 0.4080
    Epoch 5/10
    6369/6369 [==============================] - 8s 1ms/step - loss: 1.5488 - accuracy: 0.4075 - val_loss: 1.5494 - val_accuracy: 0.4100
    Epoch 6/10
    6369/6369 [==============================] - 8s 1ms/step - loss: 1.5462 - accuracy: 0.4083 - val_loss: 1.5463 - val_accuracy: 0.4104
    Epoch 7/10
    6369/6369 [==============================] - 8s 1ms/step - loss: 1.5445 - accuracy: 0.4084 - val_loss: 1.5462 - val_accuracy: 0.4093
    Epoch 8/10
    6369/6369 [==============================] - 8s 1ms/step - loss: 1.5427 - accuracy: 0.4096 - val_loss: 1.5454 - val_accuracy: 0.4086
    Epoch 9/10
    6369/6369 [==============================] - 8s 1ms/step - loss: 1.5416 - accuracy: 0.4102 - val_loss: 1.5452 - val_accuracy: 0.4117
    Epoch 10/10
    6369/6369 [==============================] - 8s 1ms/step - loss: 1.5404 - accuracy: 0.4109 - val_loss: 1.5431 - val_accuracy: 0.4120



```
print(f'Logistic Regression Acc: {log_model.score(X_test, y_test)}')
print(f'     Neural Network Acc: {nn_model.evaluate(X_test, y_test, verbose=0)[1]}')
```

    Logistic Regression Acc: 0.38908428589373195
         Neural Network Acc: 0.41164740920066833



```
plt.figure(figsize=(14, 10))

plt.plot(range(epochs), history.history['loss'], label='Training Loss')
plt.plot(range(epochs), history.history['val_loss'], label='Validation Loss')

plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend(loc='upper right')

plt.show()
```


    
![AV _ Healthcare Analytics II_60_0.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/141539/7ad6eabd-0992-9929-5145-5999c42a6328.png)
    



```
np.argmin(history.history['val_loss']) + 1
```




    10



今回はsklearnのロジスティック回帰とtensorflowの両方でモデルを構築しました。
比較するとtensorflowのほうが若干良い精度がでていました。
NNいいですね。