訓練並評估模型

使用 Keras 的進階深度學習

Zach Deane-Mayer

Data Scientist

籃球資料

目標:預測錦標賽結果

可用資料:主辦單位提供的球隊排名

import pandas as pd
games_tourney = pd.read_csv('datasets/games_tourney.csv')
games_tourney.head()

Out[1]:
   season  team_1  team_2  home  seed_diff  score_diff  score_1  score_2  won
0    1985     288      73     0         -3          -9       41       50    0
1    1985    5929      73     0          4           6       61       55    1
2    1985    9884      73     0          5          -4       59       63    0
3    1985      73     288     0          3           9       50       41    1
4    1985    3920     410     0          1          -9       54       63    0
使用 Keras 的進階深度學習

籃球資料

輸入:種子差

import pandas as pd
games_tourney = pd.read_csv('datasets/games_tourney.csv')
games_tourney.head()

使用 Keras 的進階深度學習

籃球資料

輸出:分數差

import pandas as pd
games_tourney = pd.read_csv('datasets/games_tourney.csv')
games_tourney.head()

使用 Keras 的進階深度學習

籃球資料

輸入:

  • 種子差:單一數值,-15 到 +15
  • 種子排名 1–16
  • 最大差:16-1 = +15
  • 最小差:1-16 = -15

輸出:

  • 分數差:單一數值,-50 到 +50
使用 Keras 的進階深度學習

籃球資料

  • 種子差:15
    • 隊伍 1:16
    • 隊伍 2:1
  • 種子差:-15
    • 隊伍 1:1
    • 隊伍 2:16

使用 Keras 的進階深度學習

籃球資料

  • 分數差:-9
    • 隊伍 1:41
    • 隊伍 2:50
  • 分數差:6
    • 隊伍 1:61
    • 隊伍 2:55

使用 Keras 的進階深度學習

籃球資料

import pandas as pd
games_tourney = pd.read_csv('datasets/games_tourney_samp.csv')
games_tourney.head()

Out[1]:
   season  team_1  team_2  home  seed_diff  score_diff  score_1  score_2  won
0    2017     320    6323     0         13          18      100       82    1
1    2017    6323     320     0        -13         -18       82      100    0
使用 Keras 的進階深度學習

建立模型

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense
input_tensor = Input(shape=(1,))
output_tensor = Dense(1)(input_tensor)
model = Model(input_tensor, output_tensor)
model.compile(optimizer='adam', loss='mae')

使用 Keras 的進階深度學習

訓練模型

from pandas import read_csv
games = read_csv('datasets/games_tourney.csv')
model.fit(games['seed_diff'],
          games['score_diff'],
          batch_size=64,
          validation_split=.20,
          verbose=True)

使用 Keras 的進階深度學習

評估模型

model.evaluate(games_test['seed_diff'],
               games_test['score_diff'])

1000/1000 [==============================] - 0s 26us/step
Out[1]: 9.145421981811523
使用 Keras 的進階深度學習

一起來練習吧!

使用 Keras 的進階深度學習

Preparing Video For Download...