# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:data science]
#     language: python
#     name: conda-env-data_science-py
# ---

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import seaborn as sns 
import matplotlib.pyplot as plt
from sklearn.preprocessing import OrdinalEncoder
country = pd.read_csv("../dataset/country.csv")
league = pd.read_csv("../dataset/league.csv")
match = pd.read_csv("../dataset/match.csv")
player = pd.read_csv("../dataset/player.csv")
player_attributes = pd.read_csv("../dataset/player_attributes.csv")
team = pd.read_csv("../dataset/team.csv", encoding = "ISO-8859-1")
team_attributes = pd.read_csv("team_attributes_GrpByNewTEAMID.csv")

dogdata = pd.read_csv("../groupscores/dogdata_new.csv")

dogs = dogdata["dog"].to_list()

dogdata["dogscore"]= (dogdata["homerevenue"] + dogdata["awayrevenue"])/(dogdata["homegames"] + dogdata["awaygames"])-1
dogdata = dogdata.sort_values(by = "dogscore", ascending = False)
good_dogs = dogdata[:200]["dog"].to_list()
good_dogs

dogdata

dogdata[dogdata["dogscore"]>0]

team_attributes.columns

simp_team_attributes = pd.DataFrame()
simp_team_attributes["team_id"] = team_attributes['new_id']
simp_team_attributes["buildUpScore"] =team_attributes[["buildUpPlaySpeed", "buildUpPlayPassing"]].mean(axis = 1)
simp_team_attributes["chanceCreationScore"] =team_attributes[["chanceCreationPassing", "chanceCreationCrossing","chanceCreationShooting"]].mean(axis = 1)
simp_team_attributes["defenceScore"] = team_attributes[["defencePressure", "defenceAggression","defenceTeamWidth"]].mean(axis = 1)

simp_team_attributes["is_dog"]= simp_team_attributes["team_id"].isin(dogs)
simp_team_attributes["is_gooddog"] = simp_team_attributes["team_id"].isin(good_dogs)
simp_dog_attributes = simp_team_attributes[simp_team_attributes.is_dog]
simp_good_dog_attributes = simp_dog_attributes[simp_dog_attributes.is_gooddog]
simp_bad_dog_attributes = simp_dog_attributes[~simp_dog_attributes.is_gooddog]

team_attributes["is_dog"]= team_attributes["new_id"].isin(dogs)
team_attributes["is_gooddog"] = team_attributes["new_id"].isin(good_dogs)
dog_attributes = team_attributes[team_attributes.is_dog]
good_dog_attributes = dog_attributes[dog_attributes.is_gooddog]
bad_dog_attributes = dog_attributes[~dog_attributes.is_gooddog]

# # 3D scatter plot

# +
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
font = {'family' : 'normal',
        'size'   : 12}


plt.rc('font', **font)

fig = plt.figure(figsize = (10, 7))
ax = plt.axes(projection ="3d")

x = simp_bad_dog_attributes.buildUpScore
y = simp_bad_dog_attributes.chanceCreationScore
z = simp_bad_dog_attributes.defenceScore
 
ax.scatter(x, y, z)
plt.title("Profitable underdogs' profile")

x = simp_good_dog_attributes.buildUpScore
y = simp_good_dog_attributes.chanceCreationScore
z = simp_good_dog_attributes.defenceScore
 
ax.scatter(x, y, z,color ='red')

ax.set_xlabel('buildUpScore')
ax.set_ylabel('chanceCreationScore')
ax.set_zlabel('defenceScore')

ax.legend(['other underdogs', 'profitable underdogs'])
# show plot
plt.show()

fig.savefig('underdogs.pdf', bbox_inches='tight')
# -

num_col = ['buildUpPlaySpeed', 
       'buildUpPlayPassing', 'chanceCreationPassing', 'chanceCreationCrossing',
       'chanceCreationShooting', 'defencePressure', 'defenceAggression',
       'defenceTeamWidth']
cat_col = ['buildUpPlayPositioningClass','chanceCreationPositioningClass', 'defenceDefenderLineClass']

good_dog_attributes

dogscore = dogdata[["dog","dogscore"]]
dogscore = dogscore.set_index(["dog"])
dog_attributes = dog_attributes.set_index("new_id")

dog_num = dog_attributes[num_col+cat_col].join(dogscore)
dog_num

# +
num_features_all = pd.melt(dog_attributes[num_col])
num_features_highdogs = pd.melt(good_dog_attributes[num_col])
font = {'family' : 'normal',
        'weight' : 'normal',
        'size'   : 20}
plt.rc('font', **font)

df = pd.concat([num_features_all, num_features_highdogs],keys = ["all dogs","good dogs"])
df = df.reset_index()
df = df.drop(["level_1"], axis =1)
df = df.rename({"level_0": "category"}, axis =1)

# Initialize the figure with a logarithmic x axis
f, ax = plt.subplots(figsize=(20, 15))

# Plot the orbital period with horizontal boxes
sns.boxplot(y="variable", x="value", hue = "category", data = df,
            whis=[0, 100], width=.6, palette="vlag")


# Tweak the visual presentation
ax.xaxis.grid(True)
ax.set(ylabel="")
plt.legend(loc='upper right')
sns.despine(trim=True, left=True)

plt.savefig('underdogs2.pdf', bbox_inches='tight')
# -



cat_col = ['buildUpPlayPositioningClass','chanceCreationPositioningClass', 'defenceDefenderLineClass']

cat_features_all = dog_attributes[cat_col]
cat_features_highdogs = good_dog_attributes[cat_col]
df = pd.concat([pd.melt(cat_features_all),pd.melt(cat_features_highdogs)],keys = ["all","high-dogs"])
df = df.reset_index()
df = df.drop(["level_1"], axis =1)
df = df.rename({"level_0": "category"}, axis =1)

# +
#sns.set_theme(style="ticks")

# Initialize the figure with a logarithmic x axis
f, ax = plt.subplots(figsize=(30, 20))
#ax.set_xscale("log")

# Plot the orbital period with horizontal boxes
sns.barplot(y="variable", x="value", hue = "category", data = df)

# Tweak the visual presentation
ax.xaxis.grid(True)
ax.set(ylabel="")
sns.despine(trim=True, left=True)
# -

# # Linear regression

dog_lr = dog_num

for i in num_col:
    X = dog_lr[i]
    X = (X - X.min())/(X.max()- X.min())
    dog_lr[i] = X



# +
from sklearn.linear_model import LinearRegression

y = dog_lr.dogscore
X = dog_lr.drop(["dogscore"], axis = 1)
reg = LinearRegression().fit(X, y)
reg.coef_

# -

# # Correlations

for i in num_col + cat_col:
    print(dog_num["dogscore"].corr(dog_num[i]))

num_col + cat_col

















StarcoderdataJupyterScriptsDedupFiltered