Date Every Day: マイニングプロセスにおける品質予測

# tldr

Kggleの[Quality Prediction in a Mining Process](https://www.kaggle.com/edumagalhaes/quality-prediction-in-a-mining-process)を[Mining Quality Prediction - Data Every Day #039](https://www.youtube.com/watch?v=8TgEAhWZB1k&list=PLs_CQl4mUvI7Z9HnwoeOS5EUu9gyvekjK&index=39)に沿ってやっていきます。

実行環境はGoogle Colaboratorです。

# インポート


```python
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
import sklearn.linear_model as slm

import tensorflow as tf
```

# データのダウンロード

Google Driveをマウントします。


```python
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`として置いてあります。


```python
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を使ってデータをダウンロードします。


```python
dataset_id = 'edumagalhaes/quality-prediction-in-a-mining-process'
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
```




    '/content/MiningProcess_Flotation_Plant_Database.csv'




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

     10%|▉         | 5.00M/50.9M [00:00<00:00, 50.0MB/s]

    Downloading MiningProcess_Flotation_Plant_Database.csv.zip to /content


    100%|██████████| 50.9M/50.9M [00:00<00:00, 87.3MB/s]

    


    





    True



# データの読み込み

Pedumagalhaes/quality-prediction-in-a-mining-processadasを使ってダウンロードしてきたCSVファイルを読み込みます。


```python
data = pd.read_csv(file_path+'.zip')
```


```python
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>date</th>
      <th>% Iron Feed</th>
      <th>% Silica Feed</th>
      <th>Starch Flow</th>
      <th>Amina Flow</th>
      <th>Ore Pulp Flow</th>
      <th>Ore Pulp pH</th>
      <th>Ore Pulp Density</th>
      <th>Flotation Column 01 Air Flow</th>
      <th>Flotation Column 02 Air Flow</th>
      <th>Flotation Column 03 Air Flow</th>
      <th>Flotation Column 04 Air Flow</th>
      <th>Flotation Column 05 Air Flow</th>
      <th>Flotation Column 06 Air Flow</th>
      <th>Flotation Column 07 Air Flow</th>
      <th>Flotation Column 01 Level</th>
      <th>Flotation Column 02 Level</th>
      <th>Flotation Column 03 Level</th>
      <th>Flotation Column 04 Level</th>
      <th>Flotation Column 05 Level</th>
      <th>Flotation Column 06 Level</th>
      <th>Flotation Column 07 Level</th>
      <th>% Iron Concentrate</th>
      <th>% Silica Concentrate</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>2017-03-10 01:00:00</td>
      <td>55,2</td>
      <td>16,98</td>
      <td>3019,53</td>
      <td>557,434</td>
      <td>395,713</td>
      <td>10,0664</td>
      <td>1,74</td>
      <td>249,214</td>
      <td>253,235</td>
      <td>250,576</td>
      <td>295,096</td>
      <td>306,4</td>
      <td>250,225</td>
      <td>250,884</td>
      <td>457,396</td>
      <td>432,962</td>
      <td>424,954</td>
      <td>443,558</td>
      <td>502,255</td>
      <td>446,37</td>
      <td>523,344</td>
      <td>66,91</td>
      <td>1,31</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2017-03-10 01:00:00</td>
      <td>55,2</td>
      <td>16,98</td>
      <td>3024,41</td>
      <td>563,965</td>
      <td>397,383</td>
      <td>10,0672</td>
      <td>1,74</td>
      <td>249,719</td>
      <td>250,532</td>
      <td>250,862</td>
      <td>295,096</td>
      <td>306,4</td>
      <td>250,137</td>
      <td>248,994</td>
      <td>451,891</td>
      <td>429,56</td>
      <td>432,939</td>
      <td>448,086</td>
      <td>496,363</td>
      <td>445,922</td>
      <td>498,075</td>
      <td>66,91</td>
      <td>1,31</td>
    </tr>
    <tr>
      <th>2</th>
      <td>2017-03-10 01:00:00</td>
      <td>55,2</td>
      <td>16,98</td>
      <td>3043,46</td>
      <td>568,054</td>
      <td>399,668</td>
      <td>10,068</td>
      <td>1,74</td>
      <td>249,741</td>
      <td>247,874</td>
      <td>250,313</td>
      <td>295,096</td>
      <td>306,4</td>
      <td>251,345</td>
      <td>248,071</td>
      <td>451,24</td>
      <td>468,927</td>
      <td>434,61</td>
      <td>449,688</td>
      <td>484,411</td>
      <td>447,826</td>
      <td>458,567</td>
      <td>66,91</td>
      <td>1,31</td>
    </tr>
    <tr>
      <th>3</th>
      <td>2017-03-10 01:00:00</td>
      <td>55,2</td>
      <td>16,98</td>
      <td>3047,36</td>
      <td>568,665</td>
      <td>397,939</td>
      <td>10,0689</td>
      <td>1,74</td>
      <td>249,917</td>
      <td>254,487</td>
      <td>250,049</td>
      <td>295,096</td>
      <td>306,4</td>
      <td>250,422</td>
      <td>251,147</td>
      <td>452,441</td>
      <td>458,165</td>
      <td>442,865</td>
      <td>446,21</td>
      <td>471,411</td>
      <td>437,69</td>
      <td>427,669</td>
      <td>66,91</td>
      <td>1,31</td>
    </tr>
    <tr>
      <th>4</th>
      <td>2017-03-10 01:00:00</td>
      <td>55,2</td>
      <td>16,98</td>
      <td>3033,69</td>
      <td>558,167</td>
      <td>400,254</td>
      <td>10,0697</td>
      <td>1,74</td>
      <td>250,203</td>
      <td>252,136</td>
      <td>249,895</td>
      <td>295,096</td>
      <td>306,4</td>
      <td>249,983</td>
      <td>248,928</td>
      <td>452,441</td>
      <td>452,9</td>
      <td>450,523</td>
      <td>453,67</td>
      <td>462,598</td>
      <td>443,682</td>
      <td>425,679</td>
      <td>66,91</td>
      <td>1,31</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>
    </tr>
    <tr>
      <th>737448</th>
      <td>2017-09-09 23:00:00</td>
      <td>49,75</td>
      <td>23,2</td>
      <td>2710,94</td>
      <td>441,052</td>
      <td>386,57</td>
      <td>9,62129</td>
      <td>1,65365</td>
      <td>302,344</td>
      <td>298,786</td>
      <td>299,163</td>
      <td>299,92</td>
      <td>299,623</td>
      <td>346,794</td>
      <td>313,695</td>
      <td>392,16</td>
      <td>430,702</td>
      <td>872,008</td>
      <td>418,725</td>
      <td>497,548</td>
      <td>446,357</td>
      <td>416,892</td>
      <td>64,27</td>
      <td>1,71</td>
    </tr>
    <tr>
      <th>737449</th>
      <td>2017-09-09 23:00:00</td>
      <td>49,75</td>
      <td>23,2</td>
      <td>2692,01</td>
      <td>473,436</td>
      <td>384,939</td>
      <td>9,62063</td>
      <td>1,65352</td>
      <td>303,013</td>
      <td>301,879</td>
      <td>299,487</td>
      <td>299,71</td>
      <td>300,465</td>
      <td>330,023</td>
      <td>236,7</td>
      <td>401,505</td>
      <td>404,616</td>
      <td>864,409</td>
      <td>418,377</td>
      <td>506,398</td>
      <td>372,995</td>
      <td>426,337</td>
      <td>64,27</td>
      <td>1,71</td>
    </tr>
    <tr>
      <th>737450</th>
      <td>2017-09-09 23:00:00</td>
      <td>49,75</td>
      <td>23,2</td>
      <td>2692,2</td>
      <td>500,488</td>
      <td>383,496</td>
      <td>9,61874</td>
      <td>1,65338</td>
      <td>303,662</td>
      <td>307,397</td>
      <td>299,487</td>
      <td>299,927</td>
      <td>299,707</td>
      <td>329,59</td>
      <td>225,879</td>
      <td>408,899</td>
      <td>399,316</td>
      <td>867,598</td>
      <td>419,531</td>
      <td>503,414</td>
      <td>336,035</td>
      <td>433,13</td>
      <td>64,27</td>
      <td>1,71</td>
    </tr>
    <tr>
      <th>737451</th>
      <td>2017-09-09 23:00:00</td>
      <td>49,75</td>
      <td>23,2</td>
      <td>1164,12</td>
      <td>491,548</td>
      <td>384,976</td>
      <td>9,61686</td>
      <td>1,65324</td>
      <td>302,55</td>
      <td>301,959</td>
      <td>298,045</td>
      <td>299,372</td>
      <td>298,819</td>
      <td>351,453</td>
      <td>308,115</td>
      <td>405,107</td>
      <td>466,832</td>
      <td>876,591</td>
      <td>407,299</td>
      <td>502,301</td>
      <td>340,844</td>
      <td>433,966</td>
      <td>64,27</td>
      <td>1,71</td>
    </tr>
    <tr>
      <th>737452</th>
      <td>2017-09-09 23:00:00</td>
      <td>49,75</td>
      <td>23,2</td>
      <td>1164,12</td>
      <td>468,019</td>
      <td>384,801</td>
      <td>9,61497</td>
      <td>1,6531</td>
      <td>300,355</td>
      <td>292,865</td>
      <td>298,625</td>
      <td>298,717</td>
      <td>297,395</td>
      <td>362,464</td>
      <td>308,115</td>
      <td>413,754</td>
      <td>514,143</td>
      <td>881,323</td>
      <td>378,969</td>
      <td>500,1</td>
      <td>374,354</td>
      <td>441,182</td>
      <td>64,27</td>
      <td>1,71</td>
    </tr>
  </tbody>
</table>
<p>737453 rows × 24 columns</p>
</div>



# 下準備


```python
for column in data.columns:
    data[column] = data[column].apply(lambda x: x.replace(',', '.'))
```


```python
import re
data['date'] = data['date'].apply(lambda x: re.search('[0-9]*-[0-9]*', x).group(0))
data['Year'] = data['date'].apply(lambda x: re.search('^[^-]*', x).group(0))
data['Month'] = data['date'].apply(lambda x: re.search('[^-]*$', x).group(0))

data = data.drop('date', axis=1)
```


```python
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>% Iron Feed</th>
      <th>% Silica Feed</th>
      <th>Starch Flow</th>
      <th>Amina Flow</th>
      <th>Ore Pulp Flow</th>
      <th>Ore Pulp pH</th>
      <th>Ore Pulp Density</th>
      <th>Flotation Column 01 Air Flow</th>
      <th>Flotation Column 02 Air Flow</th>
      <th>Flotation Column 03 Air Flow</th>
      <th>Flotation Column 04 Air Flow</th>
      <th>Flotation Column 05 Air Flow</th>
      <th>Flotation Column 06 Air Flow</th>
      <th>Flotation Column 07 Air Flow</th>
      <th>Flotation Column 01 Level</th>
      <th>Flotation Column 02 Level</th>
      <th>Flotation Column 03 Level</th>
      <th>Flotation Column 04 Level</th>
      <th>Flotation Column 05 Level</th>
      <th>Flotation Column 06 Level</th>
      <th>Flotation Column 07 Level</th>
      <th>% Iron Concentrate</th>
      <th>% Silica Concentrate</th>
      <th>Year</th>
      <th>Month</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>55.2</td>
      <td>16.98</td>
      <td>3019.53</td>
      <td>557.434</td>
      <td>395.713</td>
      <td>10.0664</td>
      <td>1.74</td>
      <td>249.214</td>
      <td>253.235</td>
      <td>250.576</td>
      <td>295.096</td>
      <td>306.4</td>
      <td>250.225</td>
      <td>250.884</td>
      <td>457.396</td>
      <td>432.962</td>
      <td>424.954</td>
      <td>443.558</td>
      <td>502.255</td>
      <td>446.37</td>
      <td>523.344</td>
      <td>66.91</td>
      <td>1.31</td>
      <td>2017</td>
      <td>03</td>
    </tr>
    <tr>
      <th>1</th>
      <td>55.2</td>
      <td>16.98</td>
      <td>3024.41</td>
      <td>563.965</td>
      <td>397.383</td>
      <td>10.0672</td>
      <td>1.74</td>
      <td>249.719</td>
      <td>250.532</td>
      <td>250.862</td>
      <td>295.096</td>
      <td>306.4</td>
      <td>250.137</td>
      <td>248.994</td>
      <td>451.891</td>
      <td>429.56</td>
      <td>432.939</td>
      <td>448.086</td>
      <td>496.363</td>
      <td>445.922</td>
      <td>498.075</td>
      <td>66.91</td>
      <td>1.31</td>
      <td>2017</td>
      <td>03</td>
    </tr>
    <tr>
      <th>2</th>
      <td>55.2</td>
      <td>16.98</td>
      <td>3043.46</td>
      <td>568.054</td>
      <td>399.668</td>
      <td>10.068</td>
      <td>1.74</td>
      <td>249.741</td>
      <td>247.874</td>
      <td>250.313</td>
      <td>295.096</td>
      <td>306.4</td>
      <td>251.345</td>
      <td>248.071</td>
      <td>451.24</td>
      <td>468.927</td>
      <td>434.61</td>
      <td>449.688</td>
      <td>484.411</td>
      <td>447.826</td>
      <td>458.567</td>
      <td>66.91</td>
      <td>1.31</td>
      <td>2017</td>
      <td>03</td>
    </tr>
    <tr>
      <th>3</th>
      <td>55.2</td>
      <td>16.98</td>
      <td>3047.36</td>
      <td>568.665</td>
      <td>397.939</td>
      <td>10.0689</td>
      <td>1.74</td>
      <td>249.917</td>
      <td>254.487</td>
      <td>250.049</td>
      <td>295.096</td>
      <td>306.4</td>
      <td>250.422</td>
      <td>251.147</td>
      <td>452.441</td>
      <td>458.165</td>
      <td>442.865</td>
      <td>446.21</td>
      <td>471.411</td>
      <td>437.69</td>
      <td>427.669</td>
      <td>66.91</td>
      <td>1.31</td>
      <td>2017</td>
      <td>03</td>
    </tr>
    <tr>
      <th>4</th>
      <td>55.2</td>
      <td>16.98</td>
      <td>3033.69</td>
      <td>558.167</td>
      <td>400.254</td>
      <td>10.0697</td>
      <td>1.74</td>
      <td>250.203</td>
      <td>252.136</td>
      <td>249.895</td>
      <td>295.096</td>
      <td>306.4</td>
      <td>249.983</td>
      <td>248.928</td>
      <td>452.441</td>
      <td>452.9</td>
      <td>450.523</td>
      <td>453.67</td>
      <td>462.598</td>
      <td>443.682</td>
      <td>425.679</td>
      <td>66.91</td>
      <td>1.31</td>
      <td>2017</td>
      <td>03</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>
    </tr>
    <tr>
      <th>737448</th>
      <td>49.75</td>
      <td>23.2</td>
      <td>2710.94</td>
      <td>441.052</td>
      <td>386.57</td>
      <td>9.62129</td>
      <td>1.65365</td>
      <td>302.344</td>
      <td>298.786</td>
      <td>299.163</td>
      <td>299.92</td>
      <td>299.623</td>
      <td>346.794</td>
      <td>313.695</td>
      <td>392.16</td>
      <td>430.702</td>
      <td>872.008</td>
      <td>418.725</td>
      <td>497.548</td>
      <td>446.357</td>
      <td>416.892</td>
      <td>64.27</td>
      <td>1.71</td>
      <td>2017</td>
      <td>09</td>
    </tr>
    <tr>
      <th>737449</th>
      <td>49.75</td>
      <td>23.2</td>
      <td>2692.01</td>
      <td>473.436</td>
      <td>384.939</td>
      <td>9.62063</td>
      <td>1.65352</td>
      <td>303.013</td>
      <td>301.879</td>
      <td>299.487</td>
      <td>299.71</td>
      <td>300.465</td>
      <td>330.023</td>
      <td>236.7</td>
      <td>401.505</td>
      <td>404.616</td>
      <td>864.409</td>
      <td>418.377</td>
      <td>506.398</td>
      <td>372.995</td>
      <td>426.337</td>
      <td>64.27</td>
      <td>1.71</td>
      <td>2017</td>
      <td>09</td>
    </tr>
    <tr>
      <th>737450</th>
      <td>49.75</td>
      <td>23.2</td>
      <td>2692.2</td>
      <td>500.488</td>
      <td>383.496</td>
      <td>9.61874</td>
      <td>1.65338</td>
      <td>303.662</td>
      <td>307.397</td>
      <td>299.487</td>
      <td>299.927</td>
      <td>299.707</td>
      <td>329.59</td>
      <td>225.879</td>
      <td>408.899</td>
      <td>399.316</td>
      <td>867.598</td>
      <td>419.531</td>
      <td>503.414</td>
      <td>336.035</td>
      <td>433.13</td>
      <td>64.27</td>
      <td>1.71</td>
      <td>2017</td>
      <td>09</td>
    </tr>
    <tr>
      <th>737451</th>
      <td>49.75</td>
      <td>23.2</td>
      <td>1164.12</td>
      <td>491.548</td>
      <td>384.976</td>
      <td>9.61686</td>
      <td>1.65324</td>
      <td>302.55</td>
      <td>301.959</td>
      <td>298.045</td>
      <td>299.372</td>
      <td>298.819</td>
      <td>351.453</td>
      <td>308.115</td>
      <td>405.107</td>
      <td>466.832</td>
      <td>876.591</td>
      <td>407.299</td>
      <td>502.301</td>
      <td>340.844</td>
      <td>433.966</td>
      <td>64.27</td>
      <td>1.71</td>
      <td>2017</td>
      <td>09</td>
    </tr>
    <tr>
      <th>737452</th>
      <td>49.75</td>
      <td>23.2</td>
      <td>1164.12</td>
      <td>468.019</td>
      <td>384.801</td>
      <td>9.61497</td>
      <td>1.6531</td>
      <td>300.355</td>
      <td>292.865</td>
      <td>298.625</td>
      <td>298.717</td>
      <td>297.395</td>
      <td>362.464</td>
      <td>308.115</td>
      <td>413.754</td>
      <td>514.143</td>
      <td>881.323</td>
      <td>378.969</td>
      <td>500.1</td>
      <td>374.354</td>
      <td>441.182</td>
      <td>64.27</td>
      <td>1.71</td>
      <td>2017</td>
      <td>09</td>
    </tr>
  </tbody>
</table>
<p>737453 rows × 25 columns</p>
</div>




```python
data['Year'].unique()
```




    array(['2017'], dtype=object)




```python
data = data.drop('Year', axis=1)
```

# データの分割


```python
target = '% Silica Concentrate'

y = data[target]
X_n = data.drop([target, '% Iron Concentrate'], axis=1)
X_i = data.drop([target], axis=1)
```

# スケーリング


```python
scaler = sp.StandardScaler()
X_n = scaler.fit_transform(X_n)
X_i = scaler.fit_transform(X_i)
```


```python
X_n_train, X_n_test, y_n_train, y_n_test = train_test_split(X_n, y, train_size=0.7)
X_i_train, X_i_test, y_i_train, y_i_test = train_test_split(X_i, y, train_size=0.7)
```

# トレーニング


```python
model_n = slm.LinearRegression()
model_i = slm.LinearRegression()
```


```python
model_n.fit(X_n_train, y_n_train)
print('Model without iron R^2 Score:', model_n.score(X_n_test, y_n_test))
```

    Model without iron R^2 Score: 0.15409635166200997



```python
model_i.fit(X_i_train, y_i_train)
print('Model with iron R^2 Score:', model_i.score(X_i_test, y_i_test))
```

    Model with iron R^2 Score: 0.6875874592764709