Data Every Day: Eコマースウィッシュでの夏服販売

# tldr

Kggleの[Sales of summer clothes in E-commerce Wish
](https://www.kaggle.com/jmmvutu/summer-products-and-sales-in-ecommerce-wish)を[Summer Clothing Sales Prediction - Data Every Day #026](https://www.youtube.com/watch?v=HSSWYT5ZFVY&list=PLs_CQl4mUvI7Z9HnwoeOS5EUu9gyvekjK&index=26)に沿ってやっていきます。

実行環境は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

import tensorflow as tf
```

# データのダウンロード

Google Driveをマウントします。


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

    Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).


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 = 'jmmvutu/summer-products-and-sales-in-ecommerce-wish'
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/summer-products-with-rating-and-performance_2020-08.csv'




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

    100%|██████████| 351k/351k [00:00<00:00, 39.1MB/s]

    Downloading summer-products-with-rating-and-performance_2020-08.csv.zip to /content
    


    





    True




```
import zipfile

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

# データの読み込み

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


```
data = pd.read_csv(file_path)
```


```
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>title</th>
      <th>title_orig</th>
      <th>price</th>
      <th>retail_price</th>
      <th>currency_buyer</th>
      <th>units_sold</th>
      <th>uses_ad_boosts</th>
      <th>rating</th>
      <th>rating_count</th>
      <th>rating_five_count</th>
      <th>rating_four_count</th>
      <th>rating_three_count</th>
      <th>rating_two_count</th>
      <th>rating_one_count</th>
      <th>badges_count</th>
      <th>badge_local_product</th>
      <th>badge_product_quality</th>
      <th>badge_fast_shipping</th>
      <th>tags</th>
      <th>product_color</th>
      <th>product_variation_size_id</th>
      <th>product_variation_inventory</th>
      <th>shipping_option_name</th>
      <th>shipping_option_price</th>
      <th>shipping_is_express</th>
      <th>countries_shipped_to</th>
      <th>inventory_total</th>
      <th>has_urgency_banner</th>
      <th>urgency_text</th>
      <th>origin_country</th>
      <th>merchant_title</th>
      <th>merchant_name</th>
      <th>merchant_info_subtitle</th>
      <th>merchant_rating_count</th>
      <th>merchant_rating</th>
      <th>merchant_id</th>
      <th>merchant_has_profile_picture</th>
      <th>merchant_profile_picture</th>
      <th>product_url</th>
      <th>product_picture</th>
      <th>product_id</th>
      <th>theme</th>
      <th>crawl_month</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>2020 Summer Vintage Flamingo Print  Pajamas Se...</td>
      <td>2020 Summer Vintage Flamingo Print  Pajamas Se...</td>
      <td>16.00</td>
      <td>14</td>
      <td>EUR</td>
      <td>100</td>
      <td>0</td>
      <td>3.76</td>
      <td>54</td>
      <td>26.0</td>
      <td>8.0</td>
      <td>10.0</td>
      <td>1.0</td>
      <td>9.0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>Summer,Fashion,womenunderwearsuit,printedpajam...</td>
      <td>white</td>
      <td>M</td>
      <td>50</td>
      <td>Livraison standard</td>
      <td>4</td>
      <td>0</td>
      <td>34</td>
      <td>50</td>
      <td>1.0</td>
      <td>Quantité limitée !</td>
      <td>CN</td>
      <td>zgrdejia</td>
      <td>zgrdejia</td>
      <td>(568 notes)</td>
      <td>568</td>
      <td>4.128521</td>
      <td>595097d6a26f6e070cb878d1</td>
      <td>0</td>
      <td>NaN</td>
      <td>https://www.wish.com/c/5e9ae51d43d6a96e303acdb0</td>
      <td>https://contestimg.wish.com/api/webimage/5e9ae...</td>
      <td>5e9ae51d43d6a96e303acdb0</td>
      <td>summer</td>
      <td>2020-08</td>
    </tr>
    <tr>
      <th>1</th>
      <td>SSHOUSE Summer Casual Sleeveless Soirée Party ...</td>
      <td>Women's Casual Summer Sleeveless Sexy Mini Dress</td>
      <td>8.00</td>
      <td>22</td>
      <td>EUR</td>
      <td>20000</td>
      <td>1</td>
      <td>3.45</td>
      <td>6135</td>
      <td>2269.0</td>
      <td>1027.0</td>
      <td>1118.0</td>
      <td>644.0</td>
      <td>1077.0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>Mini,womens dresses,Summer,Patchwork,fashion d...</td>
      <td>green</td>
      <td>XS</td>
      <td>50</td>
      <td>Livraison standard</td>
      <td>2</td>
      <td>0</td>
      <td>41</td>
      <td>50</td>
      <td>1.0</td>
      <td>Quantité limitée !</td>
      <td>CN</td>
      <td>SaraHouse</td>
      <td>sarahouse</td>
      <td>83 % avis positifs (17,752 notes)</td>
      <td>17752</td>
      <td>3.899673</td>
      <td>56458aa03a698c35c9050988</td>
      <td>0</td>
      <td>NaN</td>
      <td>https://www.wish.com/c/58940d436a0d3d5da4e95a38</td>
      <td>https://contestimg.wish.com/api/webimage/58940...</td>
      <td>58940d436a0d3d5da4e95a38</td>
      <td>summer</td>
      <td>2020-08</td>
    </tr>
    <tr>
      <th>2</th>
      <td>2020 Nouvelle Arrivée Femmes Printemps et Été ...</td>
      <td>2020 New Arrival Women Spring and Summer Beach...</td>
      <td>8.00</td>
      <td>43</td>
      <td>EUR</td>
      <td>100</td>
      <td>0</td>
      <td>3.57</td>
      <td>14</td>
      <td>5.0</td>
      <td>4.0</td>
      <td>2.0</td>
      <td>0.0</td>
      <td>3.0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>Summer,cardigan,women beachwear,chiffon,Sexy w...</td>
      <td>leopardprint</td>
      <td>XS</td>
      <td>1</td>
      <td>Livraison standard</td>
      <td>3</td>
      <td>0</td>
      <td>36</td>
      <td>50</td>
      <td>1.0</td>
      <td>Quantité limitée !</td>
      <td>CN</td>
      <td>hxt520</td>
      <td>hxt520</td>
      <td>86 % avis positifs (295 notes)</td>
      <td>295</td>
      <td>3.989831</td>
      <td>5d464a1ffdf7bc44ee933c65</td>
      <td>0</td>
      <td>NaN</td>
      <td>https://www.wish.com/c/5ea10e2c617580260d55310a</td>
      <td>https://contestimg.wish.com/api/webimage/5ea10...</td>
      <td>5ea10e2c617580260d55310a</td>
      <td>summer</td>
      <td>2020-08</td>
    </tr>
    <tr>
      <th>3</th>
      <td>Hot Summer Cool T-shirt pour les femmes Mode T...</td>
      <td>Hot Summer Cool T Shirt for Women Fashion Tops...</td>
      <td>8.00</td>
      <td>8</td>
      <td>EUR</td>
      <td>5000</td>
      <td>1</td>
      <td>4.03</td>
      <td>579</td>
      <td>295.0</td>
      <td>119.0</td>
      <td>87.0</td>
      <td>42.0</td>
      <td>36.0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>Summer,Shorts,Cotton,Cotton T Shirt,Sleeve,pri...</td>
      <td>black</td>
      <td>M</td>
      <td>50</td>
      <td>Livraison standard</td>
      <td>2</td>
      <td>0</td>
      <td>41</td>
      <td>50</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>CN</td>
      <td>allenfan</td>
      <td>allenfan</td>
      <td>(23,832 notes)</td>
      <td>23832</td>
      <td>4.020435</td>
      <td>58cfdefdacb37b556efdff7c</td>
      <td>0</td>
      <td>NaN</td>
      <td>https://www.wish.com/c/5cedf17ad1d44c52c59e4aca</td>
      <td>https://contestimg.wish.com/api/webimage/5cedf...</td>
      <td>5cedf17ad1d44c52c59e4aca</td>
      <td>summer</td>
      <td>2020-08</td>
    </tr>
    <tr>
      <th>4</th>
      <td>Femmes Shorts d'été à lacets taille élastique ...</td>
      <td>Women Summer Shorts Lace Up Elastic Waistband ...</td>
      <td>2.72</td>
      <td>3</td>
      <td>EUR</td>
      <td>100</td>
      <td>1</td>
      <td>3.10</td>
      <td>20</td>
      <td>6.0</td>
      <td>4.0</td>
      <td>2.0</td>
      <td>2.0</td>
      <td>6.0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>Summer,Plus Size,Lace,Casual pants,Bottom,pant...</td>
      <td>yellow</td>
      <td>S</td>
      <td>1</td>
      <td>Livraison standard</td>
      <td>1</td>
      <td>0</td>
      <td>35</td>
      <td>50</td>
      <td>1.0</td>
      <td>Quantité limitée !</td>
      <td>CN</td>
      <td>youngpeopleshop</td>
      <td>happyhorses</td>
      <td>85 % avis positifs (14,482 notes)</td>
      <td>14482</td>
      <td>4.001588</td>
      <td>5ab3b592c3911a095ad5dadb</td>
      <td>0</td>
      <td>NaN</td>
      <td>https://www.wish.com/c/5ebf5819ebac372b070b0e70</td>
      <td>https://contestimg.wish.com/api/webimage/5ebf5...</td>
      <td>5ebf5819ebac372b070b0e70</td>
      <td>summer</td>
      <td>2020-08</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>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>1568</th>
      <td>Nouvelle Mode Femmes Bohême Pissenlit Imprimer...</td>
      <td>New Fashion Women Bohemia Dandelion Print Tee ...</td>
      <td>6.00</td>
      <td>9</td>
      <td>EUR</td>
      <td>10000</td>
      <td>1</td>
      <td>4.08</td>
      <td>1367</td>
      <td>722.0</td>
      <td>293.0</td>
      <td>185.0</td>
      <td>77.0</td>
      <td>90.0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>bohemia,Plus Size,dandelionfloralprinted,short...</td>
      <td>navyblue</td>
      <td>S</td>
      <td>50</td>
      <td>Livraison standard</td>
      <td>2</td>
      <td>0</td>
      <td>41</td>
      <td>50</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>CN</td>
      <td>cxuelin99126</td>
      <td>cxuelin99126</td>
      <td>90 % avis positifs (5,316 notes)</td>
      <td>5316</td>
      <td>4.224605</td>
      <td>5b507899ab577736508a0782</td>
      <td>0</td>
      <td>NaN</td>
      <td>https://www.wish.com/c/5d5fadc99febd9356cbc52ee</td>
      <td>https://contestimg.wish.com/api/webimage/5d5fa...</td>
      <td>5d5fadc99febd9356cbc52ee</td>
      <td>summer</td>
      <td>2020-08</td>
    </tr>
    <tr>
      <th>1569</th>
      <td>10 couleurs femmes shorts d'été lacent ceintur...</td>
      <td>10 Color Women Summer Shorts Lace Up Elastic W...</td>
      <td>2.00</td>
      <td>56</td>
      <td>EUR</td>
      <td>100</td>
      <td>1</td>
      <td>3.07</td>
      <td>28</td>
      <td>11.0</td>
      <td>3.0</td>
      <td>1.0</td>
      <td>3.0</td>
      <td>10.0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>Summer,Panties,Elastic,Lace,Casual pants,casua...</td>
      <td>lightblue</td>
      <td>S</td>
      <td>2</td>
      <td>Livraison standard</td>
      <td>1</td>
      <td>0</td>
      <td>26</td>
      <td>50</td>
      <td>1.0</td>
      <td>Quantité limitée !</td>
      <td>CN</td>
      <td>sell best quality goods</td>
      <td>sellbestqualitygoods</td>
      <td>(4,435 notes)</td>
      <td>4435</td>
      <td>3.696054</td>
      <td>54d83b6b6b8a771e478558de</td>
      <td>0</td>
      <td>NaN</td>
      <td>https://www.wish.com/c/5eccd22b4497b86fd48f16b4</td>
      <td>https://contestimg.wish.com/api/webimage/5eccd...</td>
      <td>5eccd22b4497b86fd48f16b4</td>
      <td>summer</td>
      <td>2020-08</td>
    </tr>
    <tr>
      <th>1570</th>
      <td>Nouveautés Hommes Siwmwear Beach-Shorts Hommes...</td>
      <td>New Men Siwmwear Beach-Shorts Men Summer Quick...</td>
      <td>5.00</td>
      <td>19</td>
      <td>EUR</td>
      <td>100</td>
      <td>0</td>
      <td>3.71</td>
      <td>59</td>
      <td>24.0</td>
      <td>15.0</td>
      <td>8.0</td>
      <td>3.0</td>
      <td>9.0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>runningshort,Beach Shorts,beachpant,menbeachsh...</td>
      <td>white</td>
      <td>SIZE S</td>
      <td>15</td>
      <td>Livraison standard</td>
      <td>2</td>
      <td>0</td>
      <td>11</td>
      <td>50</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>CN</td>
      <td>shixueying</td>
      <td>shixueying</td>
      <td>86 % avis positifs (210 notes)</td>
      <td>210</td>
      <td>3.961905</td>
      <td>5b42da1bf64320209fc8da69</td>
      <td>0</td>
      <td>NaN</td>
      <td>https://www.wish.com/c/5e74be96034d613d42b52dfe</td>
      <td>https://contestimg.wish.com/api/webimage/5e74b...</td>
      <td>5e74be96034d613d42b52dfe</td>
      <td>summer</td>
      <td>2020-08</td>
    </tr>
    <tr>
      <th>1571</th>
      <td>Mode femmes d'été sans manches robes col en V ...</td>
      <td>Fashion Women Summer Sleeveless Dresses V Neck...</td>
      <td>13.00</td>
      <td>11</td>
      <td>EUR</td>
      <td>100</td>
      <td>0</td>
      <td>2.50</td>
      <td>2</td>
      <td>0.0</td>
      <td>1.0</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>1.0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>Summer,fashion women,Fashion,Lace,Dresses,Dres...</td>
      <td>white</td>
      <td>Size S.</td>
      <td>36</td>
      <td>Livraison standard</td>
      <td>3</td>
      <td>0</td>
      <td>29</td>
      <td>50</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>CN</td>
      <td>modai</td>
      <td>modai</td>
      <td>77 % avis positifs (31 notes)</td>
      <td>31</td>
      <td>3.774194</td>
      <td>5d56b32c40defd78043d5af9</td>
      <td>0</td>
      <td>NaN</td>
      <td>https://www.wish.com/c/5eda07ab0e295c2097c36590</td>
      <td>https://contestimg.wish.com/api/webimage/5eda0...</td>
      <td>5eda07ab0e295c2097c36590</td>
      <td>summer</td>
      <td>2020-08</td>
    </tr>
    <tr>
      <th>1572</th>
      <td>Pantalon de yoga pour femmes à la mode Slim Fi...</td>
      <td>Fashion Women Yoga Pants Slim Fit Fitness Runn...</td>
      <td>7.00</td>
      <td>6</td>
      <td>EUR</td>
      <td>100</td>
      <td>1</td>
      <td>4.07</td>
      <td>14</td>
      <td>8.0</td>
      <td>3.0</td>
      <td>1.0</td>
      <td>0.0</td>
      <td>2.0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>Summer,Leggings,slim,Yoga,pants,Slim Fit,Women...</td>
      <td>red</td>
      <td>S</td>
      <td>50</td>
      <td>Livraison standard</td>
      <td>2</td>
      <td>0</td>
      <td>41</td>
      <td>50</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>CN</td>
      <td>AISHOPPINGMALL</td>
      <td>aishoppingmall</td>
      <td>90 % avis positifs (7,023 notes)</td>
      <td>7023</td>
      <td>4.235939</td>
      <td>5a409cf87b584e7951b2e25f</td>
      <td>0</td>
      <td>NaN</td>
      <td>https://www.wish.com/c/5e857321f53c3d2d8f25e7ed</td>
      <td>https://contestimg.wish.com/api/webimage/5e857...</td>
      <td>5e857321f53c3d2d8f25e7ed</td>
      <td>summer</td>
      <td>2020-08</td>
    </tr>
  </tbody>
</table>
<p>1573 rows × 43 columns</p>
</div>



# 下準備

# 不要な列の削除


```
columns_to_drop = [
    'title',
    'title_orig',
    'currency_buyer',
    'shipping_option_name',
    'urgency_text',
    'merchant_title',
    'merchant_name',
    'merchant_info_subtitle',
    'merchant_id',
    'merchant_profile_picture',
    'product_url',
    'product_picture',
    'product_id',
    'tags',
    'has_urgency_banner',
    'theme',
    'crawl_month',
    'origin_country',
 ]
```


```
data = data.drop(columns_to_drop, axis=1)
```

## エンコード

## Ordinal Features


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




    price                            0
    retail_price                     0
    units_sold                       0
    uses_ad_boosts                   0
    rating                           0
    rating_count                     0
    rating_five_count               45
    rating_four_count               45
    rating_three_count              45
    rating_two_count                45
    rating_one_count                45
    badges_count                     0
    badge_local_product              0
    badge_product_quality            0
    badge_fast_shipping              0
    product_color                   41
    product_variation_size_id       14
    product_variation_inventory      0
    shipping_option_price            0
    shipping_is_express              0
    countries_shipped_to             0
    inventory_total                  0
    merchant_rating_count            0
    merchant_rating                  0
    merchant_has_profile_picture     0
    dtype: int64




```
size_ordering = ['XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL']
```


```
def ordinal_encode(data, column, ordering):
    return data[column].apply(lambda x: ordering.index(x) if x in ordering else None)
```


```
data['product_variation_size_id'] = ordinal_encode(data, 'product_variation_size_id', size_ordering)
```

# Onehot Features


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


```
data = onehot_encode(data, 'product_color')
```


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




    0



# 欠損値の処理


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




    price               0
    retail_price        0
    units_sold          0
    uses_ad_boosts      0
    rating              0
                       ..
    wine                0
    wine red            0
    winered             0
    winered & yellow    0
    yellow              0
    Length: 125, dtype: int64




```
null_columns = ['rating_five_count', 'rating_four_count', 'rating_three_count', 'rating_two_count', 'rating_one_count', 'product_variation_size_id']
```


```
for column in null_columns:
    data[column] = data[column].fillna(data[column].mean())
```


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




    0



# スケーリング


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


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


```
y.unique()
```




    array([   100,  20000,   5000,     10,  50000,   1000,  10000, 100000,
               50,      1,      7,      2,      3,      8,      6])



# yのエンコード


```
encoder = sp.LabelEncoder()
y = encoder.fit_transform(y)
y_mappings = {index: label for index, label in enumerate(encoder.classes_)}
y_mappings
```




    {0: 1,
     1: 2,
     2: 3,
     3: 6,
     4: 7,
     5: 8,
     6: 10,
     7: 50,
     8: 100,
     9: 1000,
     10: 5000,
     11: 10000,
     12: 20000,
     13: 50000,
     14: 100000}



# トレーニング


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


```
model = tf.keras.Sequential([
    tf.keras.layers.Dense(16, activation='relu', input_shape=(124,)),
    tf.keras.layers.Dense(16, activation='relu'),
    tf.keras.layers.Dense(15, activation='softmax'),
])

model.summary()
```

    Model: "sequential_2"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    dense_6 (Dense)              (None, 16)                2000      
    _________________________________________________________________
    dense_7 (Dense)              (None, 16)                272       
    _________________________________________________________________
    dense_8 (Dense)              (None, 15)                255       
    =================================================================
    Total params: 2,527
    Trainable params: 2,527
    Non-trainable params: 0
    _________________________________________________________________



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


```
batch_size = 32
epochs = 300

history = model.fit(
    X_train, 
    y_train,
    validation_split=0.2,
    batch_size=batch_size,
    epochs=epochs,
    verbose=0,
)
```


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

epochs_range = range(1, epochs + 1)
train_loss = history.history['loss']
val_loss = history.history['val_loss']

plt.plot(epochs_range, train_loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')

plt.xlabel('Epoch')
plt.xlabel('Loss')

plt.show()
```


    
![png](Sales_of_summer_clothes_in_E_commerce_Wish_files/Sales_of_summer_clothes_in_E_commerce_Wish_46_0.png)
    


Epochがある時点を超えるとValidation Lossが上昇し、過学習が起こっていることが確認できます。
過学習が起きる直前のValidation Lossが最小値のEpochを求めます。


```
np.argmin(val_loss)
```




    94