# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# [View in Colaboratory](https://colab.research.google.com/github/XinyueZ/tf/blob/master/ipynb/Bundesliga_Results_estimator.ipynb)

# + [markdown] id="rg2f5tmt2rcR" colab_type="text"
# # Train model to evaluate football result. 

# + id="usywsvFLF2bo" colab_type="code" colab={}
import tensorflow as tf
from tensorflow.python.data import Dataset
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder

# + id="M1XjwLH4P00k" colab_type="code" colab={}
tf.logging.set_verbosity(tf.logging.INFO)

# + [markdown] id="dy53WMcs27Ez" colab_type="text"
# Data-source from https://www.kaggle.com/thefc17/bundesliga-results-19932018

# + [markdown] id="TcCdsIeI225b" colab_type="text"
# This dataset contains results from every Bundesliga match from 1993-1994 to 2017-2018. It also includes half time results, but only from 1995-96 to 2017-18. Columns include Division (denoted as D1), HomeTeam, AwayTeam, FTHG (final time home goals), FTAG (final time away goals), FTR (full time result), HTHG (half time home goals), HTAG (half time away goals), HTR (half time result), and season.
#
# Data compiled into one file from this site: http://www.football-data.co.uk/germanym.php

# + id="6gRBSBTfiO1x" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="e606c1e9-283a-4afc-f13c-bf2cf5c5e80a"
df = pd.read_csv("https://dl.dropbox.com/s/3jzvvjl2iqnlqzz/Bundesliga_Results.csv", sep=",")
df = df[pd.notnull(df["FTHG"])]
df = df[pd.notnull(df["FTAG"])]
df = df[pd.notnull(df["FTR"])]
df.head()


# + id="PWk86xsBDkch" colab_type="code" colab={}
def make_dataset_and_labels_and_class_num(df, label_name):
  """This method will prepare dataset, labels for train, evaluation, test and classes.
     Args:
      df: DataFrame format of datasource.
      label_name: The name of column in datasource which will be as target for train.
     Return: 
       Tuple of (ds_train, ds_eval, ds_test, label_train, label_eval, label_test, classes)
      
  """
  target_label_col = "label" #New column name in original table.
  
  encoder = LabelEncoder()
  label = encoder.fit_transform(df[label_name])
  df.insert(8, target_label_col, label) 
  result_fit = encoder.fit(df[label_name])
  
  random_seed = None
  np.random.seed(random_seed)
  
  ds_train = df.sample(frac=0.9, random_state=random_seed)
  label_train = ds_train[target_label_col]
  
  ds_rest = df.drop(ds_train.index)
  
  ds_eval = ds_rest.sample(frac=0.8, random_state=random_seed)
  label_eval = ds_eval[target_label_col]
  
  ds_test = ds_rest.drop(ds_eval.index)
  label_test = ds_test[target_label_col]
  
  return ds_train[["FTHG", "FTAG"]], ds_eval[["FTHG", "FTAG"]], ds_test[["FTHG", "FTAG"]], label_train, label_eval, label_test, result_fit.classes_


# + id="NgkHsXdun_dO" colab_type="code" colab={}
x_train, x_eval, x_test, y_train, y_eval, y_test, result_classes = make_dataset_and_labels_and_class_num(df, "FTR")

# + id="akLoHfAXAqOi" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="70f8a611-2e22-4f30-9656-3b04c9e3a667"
result_classes

# + id="mgVYA89XoHIV" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 297} outputId="f27d75c6-f6ff-4095-c469-490e4dd0b11b"
x_train.describe()

# + id="oMlOOyPQoI1E" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 297} outputId="6ed9f08b-c96d-4b33-bf98-75d8624269dc"
x_eval.describe()

# + id="xciCTqS2ageV" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 297} outputId="61683902-32b5-49bb-e9e4-13525a814ec5"
x_test.describe()

# + id="Yi1g3htco3st" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="c3bcc5e2-700d-4b5c-a7b8-a379f78d367c"
x_train.head()

# + id="6VRcJQxqpInl" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="bf9a3c20-afe1-4c29-de53-452fccdf82d1"
x_eval.head()

# + id="g5fkf_HZaj2u" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="26f4b36b-f104-4e11-d776-116320d0a5f0"
x_test.head()


# + id="udjYU2TaCClX" colab_type="code" colab={}
def input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):
    """Trains a linear regression model of one feature.
  
    Args:
      features: pandas DataFrame of features
      targets: pandas DataFrame of targets
      batch_size: Size of batches to be passed to the model
      shuffle: True or False. Whether to shuffle the data.
      num_epochs: Number of epochs for which data should be repeated. None = repeat indefinitely
    Returns:
      Tuple of (features, labels) for next data batch
    """
 
    # Construct a dataset, and configure batching/repeating
    features = {key:np.array(value) for key,value in dict(features).items()} 
  
    ds = Dataset.from_tensor_slices((features, targets))
    ds = ds.batch(batch_size).repeat(num_epochs)
    
    # Shuffle the data, if specified
    if shuffle:
      ds = ds.shuffle(buffer_size=10000)
    
    # Return the next batch of data
    features, labels = ds.make_one_shot_iterator().get_next()
    return features, labels


# + id="XauzWrcNCh0J" colab_type="code" colab={}
train_input_fn = lambda: input_fn(x_train, y_train)

# + id="MUKqyBqocZJ_" colab_type="code" colab={}
train_predict_input_fn = lambda: input_fn(x_eval, y_eval, num_epochs=1, shuffle=False)

# + id="_QCb9zcOCf3A" colab_type="code" colab={}
eval_predict_input_fn = lambda: input_fn(x_eval, y_eval, num_epochs=1, shuffle=False)

# + id="YXjdJK8zbfW5" colab_type="code" colab={}
test_predict_input_fun = lambda: input_fn(x_test, y_test, num_epochs=1, shuffle=False)

# + id="_rvAbHuFjCND" colab_type="code" colab={}
STEPS = 5000  # Steps of train loop.
HIDDEN = [1000, 1000, 1000, 1000]
PERIODS = 10
STEPS_PER_PERIOD = STEPS / PERIODS

# + id="3AkQHk0ObqPJ" colab_type="code" colab={}


# + id="O4Fyeywlz4dm" colab_type="code" colab={}
feature_cols = [
  tf.feature_column.numeric_column("FTHG"),
  tf.feature_column.numeric_column("FTAG")
]

# + id="wR1wAHC0b8R5" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 88} outputId="0ca1c4bf-e4c8-435b-e797-30a0e840a3d3"
model = tf.estimator.DNNClassifier(
    feature_columns = feature_cols,
    hidden_units = HIDDEN,
    n_classes = len(result_classes)
)

# + id="xibCYiz3by1B" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 18530} outputId="6f67dae8-dcbd-4fc4-be1a-b7cac7291f5b"
for period in range(0, PERIODS):
  model.train(input_fn=train_input_fn, steps=STEPS)
  train_predict = model.predict(input_fn=train_predict_input_fn)
  eval_predict = model.predict(input_fn=eval_predict_input_fn)

# + id="-2swRvPBdXvZ" colab_type="code" colab={}
test_predict = model.predict(input_fn=test_predict_input_fun)

# + id="GWsXI3wpeins" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 119} outputId="deae8783-fa4b-4f02-e11a-34b15c1b99d7"
test_predict = np.array([item['classes'][0] for item in test_predict])

# + id="89JkWcZBfzd6" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 2618} outputId="0cfe5e53-08a7-4b83-d74b-285e6f8a6f3f"
for clazz in test_predict:
  result = result_classes[int(clazz)]
  print(result)

StarcoderdataJupyterScriptsDedupFiltered