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

# + [markdown] id="BxC7W6HplhEW"
# # League of Legends Diamond Ranked Games (10 min)

# + [markdown] id="OKooCxuIlkyQ"
# ## (1) introduction
# "League of Legends" is a team-based strategy game. Two teams with five powerful summoners will face each other in a canyon. The goal of the team is to tear down each other's base.
#
# A typical League of Legends game usually lasts 30 to 45 minutes, and each game can be divided into three stages: the laning stage, the mid-term and late-stage. Players usually spend the first 10 to 15 minutes in their own branch (up, middle, down, JG) to develop in order to gain the advantage of equipment and level as early as possible.
#
# The combination of heroes of each team will have a crucial impact on the outcome of the game, because some heroes are very strong in the early game, while other heroes will grow substantially in the middle and late game.
# In the high score game of LOL (diamond to master segment), the final direction of the game is analyzed based on the data of the first 10 minutes. 
# -

# ### dataset 
#
# https://www.kaggle.com/bobbyscience/league-of-legends-diamond-ranked-games-10-min

# + [markdown] id="i0cKFYOCmOda"
# ## (2) method
#
# Based on neural network, we use the model to train the dataset and predict the result, win or lose.
#
# The neural network structure is that:
#
# input size = 9
#
# 16 units dense layer
#
# 32 units full connect layer
#
# 16 units dense layer
#
# 8 units dense layer
#
# the binary output layer
#
#

# + id="n4yDdq5xWDEU"
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

# + id="YT89HBsDWGIf"
df = pd.read_csv('./data/lol/high_diamond_ranked_10min.csv')

cols = ['gameId', 'blueWardsPlaced', 'blueWardsDestroyed',
        'blueDeaths', 
       'blueEliteMonsters', 'blueHeralds',
       'blueTowersDestroyed', 'blueTotalGold', 'blueAvgLevel',
       'blueTotalExperience', 'blueTotalMinionsKilled',
       'blueTotalJungleMinionsKilled', 'blueGoldDiff', 'blueExperienceDiff',
        'blueGoldPerMin', 'redWardsPlaced', 'redWardsDestroyed',
       'redFirstBlood', 'redDeaths',
       'redEliteMonsters', 'redHeralds', 'redTowersDestroyed',
       'redTotalGold', 'redAvgLevel', 'redTotalExperience',
       'redTotalMinionsKilled', 'redTotalJungleMinionsKilled', 'redGoldDiff',
       'redExperienceDiff', 'redGoldPerMin']
df = df.drop(cols, axis=1)
y = df['blueWins']

# + colab={"base_uri": "https://localhost:8080/"} id="T0VjI836Xvjn" outputId="a6ee141a-ecfb-4a3e-9747-7df17310ad28"
clist = df[df.columns[1:]].apply(lambda x: x.corr(df['blueWins']))
cols=[]
for col in clist.index:
    if (clist[col]>0.2 or clist[col]<-0.2):
        cols.append(col)
df = df[cols]
print(df)

# + id="IesTWlTYXwrQ"
scaler = MinMaxScaler()
scaler.fit(df)
X = scaler.transform(df)
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)

# + colab={"base_uri": "https://localhost:8080/"} id="obPJbc-SX24e" outputId="eaa1265c-be8e-4274-ab99-ef7cb3690af6"
model = Sequential()
model.add(Dense(16, input_shape=(9,)))
model.add(Dense(32))
model.add(Dense(16))
model.add(Dense(8))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='SGD',metrics=['accuracy'])
history = model.fit(X_train,y_train,validation_split=0.3, epochs=10)

# + [markdown] id="c4k0A4Q4nz7e"
# ## (3) results
#
# accuracy

# + colab={"base_uri": "https://localhost:8080/", "height": 285} id="Q6wzyYY5cdn9" outputId="1fa0c0f1-7c3b-4c8d-94dd-a4fb2db0aff1"
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])

# + [markdown] id="wEb_AM3vn7lE"
# loss

# + colab={"base_uri": "https://localhost:8080/", "height": 282} id="hgzfXRC7cmhg" outputId="e492a809-07e9-4818-ce5b-6df37d863a7b"
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])

# + [markdown] id="tAruggwYn_X5"
# ## (4) discussion
#
# The results are good at about 70% accuracy, but not good enough for the prediction. There are only 9 elements for the input, which is a very low-dimensional data set, because "League of Legends" may capture hundreds of variables from each game. For further experiments, functions such as hero combination, time range, and champion proficiency of a specific player can be included in the analysis. 

StarcoderdataJupyterScriptsDedupFiltered