# ---
# 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
# ---

# ### Importing libraries and reading the dataset

# +
import pandas as pd
import numpy as np
from sklearn import preprocessing
from IPython.display import display

pd.options.display.max_columns = None
# -

df = pd.read_csv('dataset/Features Dataset.csv')
df.head()

# +
df.index = list(df.ID)

drop_list = ['Match', 'ID', 'Result']
df.drop(drop_list, axis=1, inplace=True)
# -

df.head()

# ### Transforming and removing attributes

df["Date"] = pd.to_datetime(df["Date"] )
df.head()

# +
column_date = df['Date']

df_date = pd.DataFrame({"year": column_date.dt.year,
                        "month": column_date.dt.month,
                        "day": column_date.dt.day,
                        "hour": column_date.dt.hour,
                        "dayofyear": column_date.dt.dayofyear,
                        "week": column_date.dt.week,
                        "weekofyear": column_date.dt.weekofyear,
                        "dayofweek": column_date.dt.dayofweek,
                        "weekday": column_date.dt.weekday,
                        "quarter": column_date.dt.quarter,
                       })
# -

df_date.head()


# ### Filling NaN values

def health_transform(v):
    if v == 'H':
        return 4
    if v == 'S':
        return 3
    if v == 'A':
        return 2
    if v == 'I':
        return 1
    return 4


def prev_match_transform(v):
    if v == 'W':
        return 2
    if v == 'L':
        return 1
    return 0


def rank_transform(v):
    v = v.replace('-', ' ')
    split = v.split(' ')
    wins = float(split[0])
    loses = float(split[1])
    loses = loses if loses > 0 else 0.1
    rank = wins / loses
    return rank


df['Sex'] = df['Sex'].map(lambda s: 1 if s == 'Male' else 0)
df['Health'] = df['Health'].apply(health_transform)
df['Prev. Match'] = df['Prev. Match'].apply(prev_match_transform)

df['Rank_T'] = 0

for index, row in df.iterrows():
    if row['Sport'] != 'Tennis':
        df.loc[(df.index==index), 'Rank_T'] = rank_transform(row['Rank'])
    else:
        df.loc[(df.index==index), 'Rank_T'] = float(row['Rank'])

df['Rank'] = df['Rank_T']

df['Rank_T'] = 0

for index, row in df.iterrows():
    if row['Sport'] != 'Tennis':
        df.loc[(df.index==index), 'Rank_T'] = rank_transform(row['Rank Opp.'])
    else:
        df.loc[(df.index==index), 'Rank_T'] = float(row['Rank Opp.'])

df['Rank Opp.'] = df['Rank_T']


def sport_transform(v):
    if v == 'Box':
        return 1
    if v == 'MMA':
        return 2
    if v == 'Tennis':
        return 3
    return None


df['Sport'] = df['Sport'].apply(sport_transform)

df.drop('Rank_T', axis=1, inplace=True)


def label_transform(v):
    if v == 'YES':
        return 1
    if v == 'NO':
        return 0
    return None


df['Final Result'] = df['Final Result'].apply(label_transform)

interviewee = set(df['Interviewee'].unique())
opponent = set(df['Opponent'].unique())

players = interviewee.union(opponent)
players

d = dict.fromkeys(players, 0)

counter = 0
for k, v in d.items():
    d[k] = counter
    counter += 1


def set_id(v):
    return d[v]


df['Interviewee'] = df['Interviewee'].apply(set_id)
df['Opponent'] = df['Opponent'].apply(set_id)

df.drop('Date', axis=1, inplace=True)
df = df.join(df_date)

df['label'] = 0
df['label'] = df['Final Result']
df.drop('Final Result', axis=1, inplace=True)

display(df)

# ### Save Dataframe as csv file

# +
# file_name = 'Dataset/pool_matches_numerical_classified.csv'
# df.to_csv(file_name, sep=',', encoding='utf-8', header=True, index=True)
# -

# ### Normalization

x = df.values
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
df_norm = pd.DataFrame(x_scaled, index=df.index, columns=df.columns)

df_norm.head()

# ### Save normalized Dataframe as csv file

file_name = 'dataset/features_norm.csv'
df_norm.to_csv(file_name, sep=',', encoding='utf-8', header=True, index=True)

StarcoderdataJupyterScriptsDedupFiltered