Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Modeling the game
%spark.pyspark
import csv
from io import StringIO
import json
import sklearn
import random
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn.naive_bayes import GaussianNB
size = 80000
def learn():
in_file = "kq-out-2.csv"
df = pd.read_csv(in_file, header=None)
# print(df)
X = df.iloc[:, 0:16]
y_team_won = df.iloc[:, 16:17]
y_win_type = df.iloc[:, 17:18]
# print(X.head(), y_team_won.head(), y_win_type.head())
# print(len(X[:6000]), len(X[6000:]))
X_train, y_tw_train, y_wt_train, X_test, y_tw_test, y_wt_test = X[:size], y_team_won[:size], y_win_type[:size], \
X[size:], y_team_won[size:], y_win_type[size:]
X_train = (X_train - X_train.mean(axis=0)) / X_train.std(axis=0)
X_train = X_train.fillna(0)
X_test = (X_test - X_test.mean(axis=0)) / X_test.std(axis=0)
X_test = X_test.fillna(0)
logreg_tw = LogisticRegression()
# logreg_tw = GaussianNB()
logreg_tw.fit(X_train, y_tw_train)
# y_tw_pred = logreg_tw.predict(X_test)
# print(metrics.mean_squared_error(y_tw_test, y_tw_pred))
logreg_wt = LogisticRegression()
# logreg_wt = GaussianNB()
logreg_wt.fit(X_train, y_wt_train)
# y_wt_pred = logreg_wt.predict(X_test)
# print(metrics.mean_squared_error(y_wt_test, y_wt_pred))
return logreg_tw, logreg_wt
def predict(logreg_tw, logreg_wt):
index_map = {
0: 'blue_minion_bot',1: 'blue_queen_bot', 2: 'blue_berries_dep', 3: 'blue_kills', 4: 'blue_speed_players', 5: 'blue_warriors', 6: 'blue_gates', 7: 'blue_queens_lives',
8: 'gold_minion_bot',9: 'gold_queen_bot', 10: 'gold_berries_depo', 11: 'gold_kills', 12: 'gold_speed_players', 13: 'gold_warriors', 14: 'gold_gates', 15: 'gold_queens_lives',
}
files_to_predict = "kq-logs-3-test.csv"
f = open(files_to_predict, 'r')
reader = csv.reader(f.readlines())
count = 0
unique_game_ids = []
game_logs_per_id = {}
for row in reader:
if count == 0:
count += 1
continue
# print(row[2])
_raw_log = str(row[2])
# print(_raw_log)
json_str = _raw_log[_raw_log.index(" - {") + 3:]
json_row = json.loads(json_str, encoding='utf-8')
# print(json_row)
if (json_row['gameId'] not in unique_game_ids):
unique_game_ids.append(json_row['gameId'])
id = str(json_row['gameId'])
# print(id)
if id not in game_logs_per_id:
game_logs_per_id[id] = []
logs = game_logs_per_id[id]
logs.append(json_row)
game_logs_per_id[id] = logs
count += 1
# print(len(unique_game_ids))
cnt = 0
for id in unique_game_ids:
game_num = random.randint(0,len(unique_game_ids)-1)
_id = unique_game_ids[game_num]
# print(_id)
game_logs = game_logs_per_id[str(_id)]
game_logs_strings = parse_game_logs(game_logs)
test_df = pd.read_csv(StringIO('\n'.join(game_logs_strings)), header=None)
X = test_df.iloc[:, 0:16]
y_team_won = test_df.iloc[:, 16:17]
if y_team_won.iloc[0,0] == 0:
continue
cnt += 1
y_win_type = test_df.iloc[:, 17:18]
y_wt_pred = logreg_wt.predict_proba(X)
y_tw_pred = logreg_tw.predict_proba(X)
prev = 0
prev_frame = None
for i in range(len(y_tw_pred)):
y_tw = y_tw_pred[i, 1]
if prev == 0:
prev = y_tw
prev_frame = X.iloc[i,:]
prev_other_frame = X.iloc[i, :]
elif y_tw > prev:
diff =X.iloc[i,:] - prev_frame
indx_gt = diff[diff>0].index
indx_lt = diff[diff<0].index
elif y_tw < prev:
diff =X.iloc[i,:] - prev_other_frame
indx_gt = diff[diff>0].index
indx_lt = diff[diff<0].index
prev = y_tw
prev_frame = X.iloc[i, :]
prev_other_frame = X.iloc[i, :]
sns.set()
ax = sns.lineplot(data = y_tw_pred[:,1])
ax.set(xlabel='Game Progress', ylabel='Probability of winning')
plt.show(ax)
if cnt > 1:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.