Data Every Day: ビデオゲームセールス

# tldr

Kggleの[Video Game Sales](https://www.kaggle.com/gregorut/videogamesales)を[Video Game Sales Prediction - Data Every Day #028](https://www.youtube.com/watch?v=HMSwljTtAlc&list=PLs_CQl4mUvI7Z9HnwoeOS5EUu9gyvekjK&index=28)に沿ってやっていきます。

実行環境は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
from tensorflow_addons.metrics import RSquare
```

# データのダウンロード

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 = 'gregorut/videogamesales'
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/vgsales.csv'




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

    100%|██████████| 381k/381k [00:00<00:00, 65.9MB/s]

    Downloading vgsales.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, index_col='Rank')
```


```
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>Name</th>
      <th>Platform</th>
      <th>Year</th>
      <th>Genre</th>
      <th>Publisher</th>
      <th>NA_Sales</th>
      <th>EU_Sales</th>
      <th>JP_Sales</th>
      <th>Other_Sales</th>
      <th>Global_Sales</th>
    </tr>
    <tr>
      <th>Rank</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>Wii Sports</td>
      <td>Wii</td>
      <td>2006.0</td>
      <td>Sports</td>
      <td>Nintendo</td>
      <td>41.49</td>
      <td>29.02</td>
      <td>3.77</td>
      <td>8.46</td>
      <td>82.74</td>
    </tr>
    <tr>
      <th>2</th>
      <td>Super Mario Bros.</td>
      <td>NES</td>
      <td>1985.0</td>
      <td>Platform</td>
      <td>Nintendo</td>
      <td>29.08</td>
      <td>3.58</td>
      <td>6.81</td>
      <td>0.77</td>
      <td>40.24</td>
    </tr>
    <tr>
      <th>3</th>
      <td>Mario Kart Wii</td>
      <td>Wii</td>
      <td>2008.0</td>
      <td>Racing</td>
      <td>Nintendo</td>
      <td>15.85</td>
      <td>12.88</td>
      <td>3.79</td>
      <td>3.31</td>
      <td>35.82</td>
    </tr>
    <tr>
      <th>4</th>
      <td>Wii Sports Resort</td>
      <td>Wii</td>
      <td>2009.0</td>
      <td>Sports</td>
      <td>Nintendo</td>
      <td>15.75</td>
      <td>11.01</td>
      <td>3.28</td>
      <td>2.96</td>
      <td>33.00</td>
    </tr>
    <tr>
      <th>5</th>
      <td>Pokemon Red/Pokemon Blue</td>
      <td>GB</td>
      <td>1996.0</td>
      <td>Role-Playing</td>
      <td>Nintendo</td>
      <td>11.27</td>
      <td>8.89</td>
      <td>10.22</td>
      <td>1.00</td>
      <td>31.37</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>
    </tr>
    <tr>
      <th>16596</th>
      <td>Woody Woodpecker in Crazy Castle 5</td>
      <td>GBA</td>
      <td>2002.0</td>
      <td>Platform</td>
      <td>Kemco</td>
      <td>0.01</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.01</td>
    </tr>
    <tr>
      <th>16597</th>
      <td>Men in Black II: Alien Escape</td>
      <td>GC</td>
      <td>2003.0</td>
      <td>Shooter</td>
      <td>Infogrames</td>
      <td>0.01</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.01</td>
    </tr>
    <tr>
      <th>16598</th>
      <td>SCORE International Baja 1000: The Official Game</td>
      <td>PS2</td>
      <td>2008.0</td>
      <td>Racing</td>
      <td>Activision</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.01</td>
    </tr>
    <tr>
      <th>16599</th>
      <td>Know How 2</td>
      <td>DS</td>
      <td>2010.0</td>
      <td>Puzzle</td>
      <td>7G//AMES</td>
      <td>0.00</td>
      <td>0.01</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.01</td>
    </tr>
    <tr>
      <th>16600</th>
      <td>Spirits &amp; Spells</td>
      <td>GBA</td>
      <td>2003.0</td>
      <td>Platform</td>
      <td>Wanadoo</td>
      <td>0.01</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.01</td>
    </tr>
  </tbody>
</table>
<p>16598 rows × 10 columns</p>
</div>



# 不要な列の削除


```
columns_to_drop = [
    'Name',
    'NA_Sales',
    'EU_Sales',
    'JP_Sales',
    'Other_Sales'
 ]
```


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


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




    Platform          0
    Year            271
    Genre             0
    Publisher        58
    Global_Sales      0
    dtype: int64



# 欠損値の処理


```
data['Year'] =data['Year'].fillna(data['Year'].mean())
```


```
data = data.dropna(axis=0)
```


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




    Platform        0
    Year            0
    Genre           0
    Publisher       0
    Global_Sales    0
    dtype: int64



# エンコード

## Onehot Features


```
counts = data['Publisher'].value_counts()

data['Publisher'] = data['Publisher'].apply(lambda x: 'Small Publisher' if counts[x] < 50 else x)
```


```
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 = ['Platform', 'Genre', 'Publisher']
```


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


```
data.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>Year</th>
      <th>Global_Sales</th>
      <th>2600</th>
      <th>3DO</th>
      <th>3DS</th>
      <th>DC</th>
      <th>DS</th>
      <th>GB</th>
      <th>GBA</th>
      <th>GC</th>
      <th>GEN</th>
      <th>GG</th>
      <th>N64</th>
      <th>NES</th>
      <th>NG</th>
      <th>PC</th>
      <th>PCFX</th>
      <th>PS</th>
      <th>PS2</th>
      <th>PS3</th>
      <th>PS4</th>
      <th>PSP</th>
      <th>PSV</th>
      <th>SAT</th>
      <th>SCD</th>
      <th>SNES</th>
      <th>TG16</th>
      <th>WS</th>
      <th>Wii</th>
      <th>WiiU</th>
      <th>X360</th>
      <th>XB</th>
      <th>XOne</th>
      <th>Action</th>
      <th>Adventure</th>
      <th>Fighting</th>
      <th>Misc</th>
      <th>Platform</th>
      <th>Puzzle</th>
      <th>Racing</th>
      <th>...</th>
      <th>Bethesda Softworks</th>
      <th>Capcom</th>
      <th>Codemasters</th>
      <th>Crave Entertainment</th>
      <th>D3Publisher</th>
      <th>Deep Silver</th>
      <th>Disney Interactive Studios</th>
      <th>Eidos Interactive</th>
      <th>Electronic Arts</th>
      <th>Empire Interactive</th>
      <th>Focus Home Interactive</th>
      <th>Hudson Soft</th>
      <th>Idea Factory</th>
      <th>Ignition Entertainment</th>
      <th>Infogrames</th>
      <th>Kadokawa Shoten</th>
      <th>Konami Digital Entertainment</th>
      <th>LucasArts</th>
      <th>Majesco Entertainment</th>
      <th>Marvelous Interactive</th>
      <th>Microsoft Game Studios</th>
      <th>Midway Games</th>
      <th>Namco Bandai Games</th>
      <th>Nintendo</th>
      <th>Nippon Ichi Software</th>
      <th>Rising Star Games</th>
      <th>Sega</th>
      <th>Small Publisher</th>
      <th>Sony Computer Entertainment</th>
      <th>Square Enix</th>
      <th>SquareSoft</th>
      <th>THQ</th>
      <th>Take-Two Interactive</th>
      <th>Tecmo Koei</th>
      <th>Ubisoft</th>
      <th>Unknown</th>
      <th>Virgin Interactive</th>
      <th>Vivendi Games</th>
      <th>Warner Bros. Interactive Entertainment</th>
      <th>Zoo Digital Publishing</th>
    </tr>
    <tr>
      <th>Rank</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>
      <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></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1</th>
      <td>2006.0</td>
      <td>82.74</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>...</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>2</th>
      <td>1985.0</td>
      <td>40.24</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>...</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>2008.0</td>
      <td>35.82</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>...</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>4</th>
      <td>2009.0</td>
      <td>33.00</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>...</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>5</th>
      <td>1996.0</td>
      <td>31.37</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>...</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>1</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
  </tbody>
</table>
<p>5 rows × 92 columns</p>
</div>



# スケーリング


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


```
scaler = sp.StandardScaler()
X = scaler.fit_transform(X)
```


```
pd.DataFrame(X).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>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      <th>4</th>
      <th>5</th>
      <th>6</th>
      <th>7</th>
      <th>8</th>
      <th>9</th>
      <th>10</th>
      <th>11</th>
      <th>12</th>
      <th>13</th>
      <th>14</th>
      <th>15</th>
      <th>16</th>
      <th>17</th>
      <th>18</th>
      <th>19</th>
      <th>20</th>
      <th>21</th>
      <th>22</th>
      <th>23</th>
      <th>24</th>
      <th>25</th>
      <th>26</th>
      <th>27</th>
      <th>28</th>
      <th>29</th>
      <th>30</th>
      <th>31</th>
      <th>32</th>
      <th>33</th>
      <th>34</th>
      <th>35</th>
      <th>36</th>
      <th>37</th>
      <th>38</th>
      <th>39</th>
      <th>...</th>
      <th>51</th>
      <th>52</th>
      <th>53</th>
      <th>54</th>
      <th>55</th>
      <th>56</th>
      <th>57</th>
      <th>58</th>
      <th>59</th>
      <th>60</th>
      <th>61</th>
      <th>62</th>
      <th>63</th>
      <th>64</th>
      <th>65</th>
      <th>66</th>
      <th>67</th>
      <th>68</th>
      <th>69</th>
      <th>70</th>
      <th>71</th>
      <th>72</th>
      <th>73</th>
      <th>74</th>
      <th>75</th>
      <th>76</th>
      <th>77</th>
      <th>78</th>
      <th>79</th>
      <th>80</th>
      <th>81</th>
      <th>82</th>
      <th>83</th>
      <th>84</th>
      <th>85</th>
      <th>86</th>
      <th>87</th>
      <th>88</th>
      <th>89</th>
      <th>90</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>-0.070070</td>
      <td>-0.090035</td>
      <td>-0.013469</td>
      <td>-0.177827</td>
      <td>-0.056159</td>
      <td>-0.387155</td>
      <td>-0.077203</td>
      <td>-0.224853</td>
      <td>-0.186507</td>
      <td>-0.040436</td>
      <td>-0.007776</td>
      <td>-0.140235</td>
      <td>-0.077203</td>
      <td>-0.026945</td>
      <td>-0.247404</td>
      <td>-0.007776</td>
      <td>-0.27881</td>
      <td>-0.387464</td>
      <td>-0.295223</td>
      <td>-0.143999</td>
      <td>-0.28107</td>
      <td>-0.159631</td>
      <td>-0.102811</td>
      <td>-0.01905</td>
      <td>-0.121085</td>
      <td>-0.010997</td>
      <td>-0.01905</td>
      <td>3.390051</td>
      <td>-0.093387</td>
      <td>-0.287283</td>
      <td>-0.228977</td>
      <td>-0.114219</td>
      <td>-0.500094</td>
      <td>-0.289865</td>
      <td>-0.232177</td>
      <td>-0.33979</td>
      <td>-0.237621</td>
      <td>-0.190803</td>
      <td>-0.285677</td>
      <td>-0.314184</td>
      <td>...</td>
      <td>-0.065659</td>
      <td>-0.153552</td>
      <td>-0.096307</td>
      <td>-0.065659</td>
      <td>-0.106065</td>
      <td>-0.086202</td>
      <td>-0.115569</td>
      <td>-0.110073</td>
      <td>-0.298238</td>
      <td>-0.056159</td>
      <td>-0.059321</td>
      <td>-0.070152</td>
      <td>-0.08866</td>
      <td>-0.060841</td>
      <td>-0.06134</td>
      <td>-0.055065</td>
      <td>-0.230145</td>
      <td>-0.073967</td>
      <td>-0.074789</td>
      <td>-0.058286</td>
      <td>-0.107512</td>
      <td>-0.110073</td>
      <td>-0.244362</td>
      <td>4.746339</td>
      <td>-0.07993</td>
      <td>-0.072296</td>
      <td>-0.200465</td>
      <td>-0.50321</td>
      <td>-0.207539</td>
      <td>-0.119534</td>
      <td>-0.056159</td>
      <td>-0.21256</td>
      <td>-0.160029</td>
      <td>-0.144436</td>
      <td>-0.24283</td>
      <td>-0.111471</td>
      <td>-0.06134</td>
      <td>-0.100073</td>
      <td>-0.119273</td>
      <td>-0.079546</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-3.698162</td>
      <td>-0.090035</td>
      <td>-0.013469</td>
      <td>-0.177827</td>
      <td>-0.056159</td>
      <td>-0.387155</td>
      <td>-0.077203</td>
      <td>-0.224853</td>
      <td>-0.186507</td>
      <td>-0.040436</td>
      <td>-0.007776</td>
      <td>-0.140235</td>
      <td>12.952819</td>
      <td>-0.026945</td>
      <td>-0.247404</td>
      <td>-0.007776</td>
      <td>-0.27881</td>
      <td>-0.387464</td>
      <td>-0.295223</td>
      <td>-0.143999</td>
      <td>-0.28107</td>
      <td>-0.159631</td>
      <td>-0.102811</td>
      <td>-0.01905</td>
      <td>-0.121085</td>
      <td>-0.010997</td>
      <td>-0.01905</td>
      <td>-0.294981</td>
      <td>-0.093387</td>
      <td>-0.287283</td>
      <td>-0.228977</td>
      <td>-0.114219</td>
      <td>-0.500094</td>
      <td>-0.289865</td>
      <td>-0.232177</td>
      <td>-0.33979</td>
      <td>4.208373</td>
      <td>-0.190803</td>
      <td>-0.285677</td>
      <td>-0.314184</td>
      <td>...</td>
      <td>-0.065659</td>
      <td>-0.153552</td>
      <td>-0.096307</td>
      <td>-0.065659</td>
      <td>-0.106065</td>
      <td>-0.086202</td>
      <td>-0.115569</td>
      <td>-0.110073</td>
      <td>-0.298238</td>
      <td>-0.056159</td>
      <td>-0.059321</td>
      <td>-0.070152</td>
      <td>-0.08866</td>
      <td>-0.060841</td>
      <td>-0.06134</td>
      <td>-0.055065</td>
      <td>-0.230145</td>
      <td>-0.073967</td>
      <td>-0.074789</td>
      <td>-0.058286</td>
      <td>-0.107512</td>
      <td>-0.110073</td>
      <td>-0.244362</td>
      <td>4.746339</td>
      <td>-0.07993</td>
      <td>-0.072296</td>
      <td>-0.200465</td>
      <td>-0.50321</td>
      <td>-0.207539</td>
      <td>-0.119534</td>
      <td>-0.056159</td>
      <td>-0.21256</td>
      <td>-0.160029</td>
      <td>-0.144436</td>
      <td>-0.24283</td>
      <td>-0.111471</td>
      <td>-0.06134</td>
      <td>-0.100073</td>
      <td>-0.119273</td>
      <td>-0.079546</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0.275463</td>
      <td>-0.090035</td>
      <td>-0.013469</td>
      <td>-0.177827</td>
      <td>-0.056159</td>
      <td>-0.387155</td>
      <td>-0.077203</td>
      <td>-0.224853</td>
      <td>-0.186507</td>
      <td>-0.040436</td>
      <td>-0.007776</td>
      <td>-0.140235</td>
      <td>-0.077203</td>
      <td>-0.026945</td>
      <td>-0.247404</td>
      <td>-0.007776</td>
      <td>-0.27881</td>
      <td>-0.387464</td>
      <td>-0.295223</td>
      <td>-0.143999</td>
      <td>-0.28107</td>
      <td>-0.159631</td>
      <td>-0.102811</td>
      <td>-0.01905</td>
      <td>-0.121085</td>
      <td>-0.010997</td>
      <td>-0.01905</td>
      <td>3.390051</td>
      <td>-0.093387</td>
      <td>-0.287283</td>
      <td>-0.228977</td>
      <td>-0.114219</td>
      <td>-0.500094</td>
      <td>-0.289865</td>
      <td>-0.232177</td>
      <td>-0.33979</td>
      <td>-0.237621</td>
      <td>-0.190803</td>
      <td>3.500458</td>
      <td>-0.314184</td>
      <td>...</td>
      <td>-0.065659</td>
      <td>-0.153552</td>
      <td>-0.096307</td>
      <td>-0.065659</td>
      <td>-0.106065</td>
      <td>-0.086202</td>
      <td>-0.115569</td>
      <td>-0.110073</td>
      <td>-0.298238</td>
      <td>-0.056159</td>
      <td>-0.059321</td>
      <td>-0.070152</td>
      <td>-0.08866</td>
      <td>-0.060841</td>
      <td>-0.06134</td>
      <td>-0.055065</td>
      <td>-0.230145</td>
      <td>-0.073967</td>
      <td>-0.074789</td>
      <td>-0.058286</td>
      <td>-0.107512</td>
      <td>-0.110073</td>
      <td>-0.244362</td>
      <td>4.746339</td>
      <td>-0.07993</td>
      <td>-0.072296</td>
      <td>-0.200465</td>
      <td>-0.50321</td>
      <td>-0.207539</td>
      <td>-0.119534</td>
      <td>-0.056159</td>
      <td>-0.21256</td>
      <td>-0.160029</td>
      <td>-0.144436</td>
      <td>-0.24283</td>
      <td>-0.111471</td>
      <td>-0.06134</td>
      <td>-0.100073</td>
      <td>-0.119273</td>
      <td>-0.079546</td>
    </tr>
    <tr>
      <th>3</th>
      <td>0.448229</td>
      <td>-0.090035</td>
      <td>-0.013469</td>
      <td>-0.177827</td>
      <td>-0.056159</td>
      <td>-0.387155</td>
      <td>-0.077203</td>
      <td>-0.224853</td>
      <td>-0.186507</td>
      <td>-0.040436</td>
      <td>-0.007776</td>
      <td>-0.140235</td>
      <td>-0.077203</td>
      <td>-0.026945</td>
      <td>-0.247404</td>
      <td>-0.007776</td>
      <td>-0.27881</td>
      <td>-0.387464</td>
      <td>-0.295223</td>
      <td>-0.143999</td>
      <td>-0.28107</td>
      <td>-0.159631</td>
      <td>-0.102811</td>
      <td>-0.01905</td>
      <td>-0.121085</td>
      <td>-0.010997</td>
      <td>-0.01905</td>
      <td>3.390051</td>
      <td>-0.093387</td>
      <td>-0.287283</td>
      <td>-0.228977</td>
      <td>-0.114219</td>
      <td>-0.500094</td>
      <td>-0.289865</td>
      <td>-0.232177</td>
      <td>-0.33979</td>
      <td>-0.237621</td>
      <td>-0.190803</td>
      <td>-0.285677</td>
      <td>-0.314184</td>
      <td>...</td>
      <td>-0.065659</td>
      <td>-0.153552</td>
      <td>-0.096307</td>
      <td>-0.065659</td>
      <td>-0.106065</td>
      <td>-0.086202</td>
      <td>-0.115569</td>
      <td>-0.110073</td>
      <td>-0.298238</td>
      <td>-0.056159</td>
      <td>-0.059321</td>
      <td>-0.070152</td>
      <td>-0.08866</td>
      <td>-0.060841</td>
      <td>-0.06134</td>
      <td>-0.055065</td>
      <td>-0.230145</td>
      <td>-0.073967</td>
      <td>-0.074789</td>
      <td>-0.058286</td>
      <td>-0.107512</td>
      <td>-0.110073</td>
      <td>-0.244362</td>
      <td>4.746339</td>
      <td>-0.07993</td>
      <td>-0.072296</td>
      <td>-0.200465</td>
      <td>-0.50321</td>
      <td>-0.207539</td>
      <td>-0.119534</td>
      <td>-0.056159</td>
      <td>-0.21256</td>
      <td>-0.160029</td>
      <td>-0.144436</td>
      <td>-0.24283</td>
      <td>-0.111471</td>
      <td>-0.06134</td>
      <td>-0.100073</td>
      <td>-0.119273</td>
      <td>-0.079546</td>
    </tr>
    <tr>
      <th>4</th>
      <td>-1.797732</td>
      <td>-0.090035</td>
      <td>-0.013469</td>
      <td>-0.177827</td>
      <td>-0.056159</td>
      <td>-0.387155</td>
      <td>12.952819</td>
      <td>-0.224853</td>
      <td>-0.186507</td>
      <td>-0.040436</td>
      <td>-0.007776</td>
      <td>-0.140235</td>
      <td>-0.077203</td>
      <td>-0.026945</td>
      <td>-0.247404</td>
      <td>-0.007776</td>
      <td>-0.27881</td>
      <td>-0.387464</td>
      <td>-0.295223</td>
      <td>-0.143999</td>
      <td>-0.28107</td>
      <td>-0.159631</td>
      <td>-0.102811</td>
      <td>-0.01905</td>
      <td>-0.121085</td>
      <td>-0.010997</td>
      <td>-0.01905</td>
      <td>-0.294981</td>
      <td>-0.093387</td>
      <td>-0.287283</td>
      <td>-0.228977</td>
      <td>-0.114219</td>
      <td>-0.500094</td>
      <td>-0.289865</td>
      <td>-0.232177</td>
      <td>-0.33979</td>
      <td>-0.237621</td>
      <td>-0.190803</td>
      <td>-0.285677</td>
      <td>3.182853</td>
      <td>...</td>
      <td>-0.065659</td>
      <td>-0.153552</td>
      <td>-0.096307</td>
      <td>-0.065659</td>
      <td>-0.106065</td>
      <td>-0.086202</td>
      <td>-0.115569</td>
      <td>-0.110073</td>
      <td>-0.298238</td>
      <td>-0.056159</td>
      <td>-0.059321</td>
      <td>-0.070152</td>
      <td>-0.08866</td>
      <td>-0.060841</td>
      <td>-0.06134</td>
      <td>-0.055065</td>
      <td>-0.230145</td>
      <td>-0.073967</td>
      <td>-0.074789</td>
      <td>-0.058286</td>
      <td>-0.107512</td>
      <td>-0.110073</td>
      <td>-0.244362</td>
      <td>4.746339</td>
      <td>-0.07993</td>
      <td>-0.072296</td>
      <td>-0.200465</td>
      <td>-0.50321</td>
      <td>-0.207539</td>
      <td>-0.119534</td>
      <td>-0.056159</td>
      <td>-0.21256</td>
      <td>-0.160029</td>
      <td>-0.144436</td>
      <td>-0.24283</td>
      <td>-0.111471</td>
      <td>-0.06134</td>
      <td>-0.100073</td>
      <td>-0.119273</td>
      <td>-0.079546</td>
    </tr>
  </tbody>
</table>
<p>5 rows × 91 columns</p>
</div>



# トレーニング


```
X.shape
```




    (16540, 91)




```
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(128, activation='relu', input_shape=(91,)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(1),
])

model.summary()
```

    Model: "sequential_3"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    dense_9 (Dense)              (None, 128)               11776     
    _________________________________________________________________
    dense_10 (Dense)             (None, 128)               16512     
    _________________________________________________________________
    dense_11 (Dense)             (None, 1)                 129       
    =================================================================
    Total params: 28,417
    Trainable params: 28,417
    Non-trainable params: 0
    _________________________________________________________________



```
model.compile(
    optimizer=tf.keras.optimizers.RMSprop(0.001),
    loss='mse',
)
```


```
batch_size = 64
epochs = 100

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](Video_Game_Sales_files/Video_Game_Sales_42_0.png)
    



```
y_pred = np.squeeze(model.predict(X_test))

result = RSquare()
result.update_state(y_test, y_pred)
print(f'R^2 Score:', result.result())
```

    R^2 Score: tf.Tensor(0.0019521117, shape=(), dtype=float32)