<jupyter_start><jupyter_text>DNN Brasileirao Football Predict  Import the pertinent libraries<jupyter_code>import pandas as pd

import tensorflow as tf

from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report<jupyter_output><empty_output><jupyter_text>Data  http://www.football-data.co.uk/englandm.php Data Data Description Notes for Football DataAll data is in csv format, ready for use within standard spreadsheet applications. Please note that some abbreviations are no longer in use (in particular odds from specific bookmakers no longer used) and refer to data collected in earlier seasons. For a current list of what bookmakers are included in the dataset please visit http://www.football-data.co.uk/matches.php Key to results data:- Date = Match Date (dd/mm/yy)- Home = Home Team- Away = Away Team- HG = Full Time Home Team Goals- AG = Full Time Away Team Goals- Res = Full Time Result (H=Home Win, D=Draw, A=Away Win) Key to 1X2 (match) betting odds data:- PH = Pinnacle home win odds- PD = Pinnacle draw odds- PA = Pinnacle away win odds- MaxH = Oddsportal maximum home win odds- MaxD = Oddsportal maximum draw win odds- MaxA = Oddsportal maximum away win odds- AvgH = Oddsportal average home win odds- AvgD = Oddsportal average draw win odds- AvgA = Oddsportal average away win oddsCurrent results (full time, half time)Xcores - http://www.xcores.comBookmakers betting oddsBetbrain - http://www.betbrain.comOddsportal - http://www.oddsportal.comIndividual bookmakersBetting odds for weekend games are collected Friday afternoons, and on Tuesday afternoons for midweek games.<jupyter_code>glob.glob('../Data/Premier_League/data*.csv')
import glob
import pandas as pd

# glob.glob('data*.csv') - returns List[str]
# pd.read_csv(f) - returns pd.DataFrame()
# for f in glob.glob() - returns a List[DataFrames]
# pd.concat() - returns one pd.DataFrame()
df = pd.concat([pd.read_csv(f) for f in glob.glob('data*.csv')], ignore_index = True)
df = pd.read_csv('../Data/Brazil_Serie_A/BRA.csv')
df.tail()<jupyter_output><empty_output><jupyter_text>Separate the Features from the LabelsFeatures are the inputs for the neural net and labels are the ones to be predicted)<jupyter_code>df.drop(['Country', 'League', 'Date', 'Time', 'HG', 'AG'], axis = 1, inplace = True)
df.dropna(inplace=True)
df['Res'].unique()
df['Res'].replace(['H', 'D', 'A'],[1, 0, 2],inplace=True)
df['Res'].unique()
df['Season'] = df['Season'].apply(str)
df['Home'] = df['Home'].apply(str)
df['Away'] = df['Away'].apply(str)
df.dtypes
x_data = df.drop(['Res'], axis=1)
y_label = df['Res']<jupyter_output><empty_output><jupyter_text>Extract the test sample from the train data test_size is the percentage of the data to being used for the tests (normally is 30%)<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(x_data, y_label, test_size = 0.30)<jupyter_output><empty_output><jupyter_text>Create the feature columns Use "tf.feature_column.categorical_column_with_hash_bucket" para features em strings   hash_bucket_size é o número de parâmetros possíveis (se a feature em questão for A, B ou C; hash_bucket_size=3) Use "tf.feature_column.categorical_column_with_identity" para formatos em int   num_buckets é o número de parâmetros possíveis (se a feature for 1 até 7, num_buckets=7)<jupyter_code>df['Season'].nunique()
season = tf.feature_column.categorical_column_with_hash_bucket('Season', hash_bucket_size = df['Season'].nunique())

home = tf.feature_column.categorical_column_with_hash_bucket('Home',hash_bucket_size = df['Home'].nunique())
away = tf.feature_column.categorical_column_with_hash_bucket('Away',hash_bucket_size = df['Away'].nunique())
res = tf.feature_column.categorical_column_with_identity('Res', num_buckets = df['Res'].nunique())<jupyter_output><empty_output><jupyter_text>Passar as feature columns categoricas para embedded Usado para conseguir usar a Dense Neural Net<jupyter_code>df['Res'].nunique()
season = tf.feature_column.embedding_column(season, dimension = df['Season'].nunique())

home = tf.feature_column.embedding_column(home, dimension = df['Home'].nunique())
away = tf.feature_column.embedding_column(away, dimension = df['Away'].nunique())

res = tf.feature_column.embedding_column(res, dimension = df['Res'].nunique())<jupyter_output><empty_output><jupyter_text>Criar feature columns numéricas<jupyter_code>X_train.columns
#Odds
#hg = tf.feature_column.numeric_column('HG')
#ag = tf.feature_column.numeric_column('AG')

ph = tf.feature_column.numeric_column('PH')
pd = tf.feature_column.numeric_column('PD')
pa = tf.feature_column.numeric_column('PA')

max_h = tf.feature_column.numeric_column('MaxH')
max_d = tf.feature_column.numeric_column('MaxD')
max_a = tf.feature_column.numeric_column('MaxA')

avg_h = tf.feature_column.numeric_column('AvgH')
avg_d = tf.feature_column.numeric_column('AvgD')
avg_a = tf.feature_column.numeric_column('AvgA')<jupyter_output><empty_output><jupyter_text>Definir feature columns para input<jupyter_code>feat_cols = [season, home, away, ph, pd, pa, max_h, max_d, max_a, avg_h, avg_d, avg_a]<jupyter_output><empty_output><jupyter_text>Input para o model   batch_size é o número de dados que serão usados em cada passo, shuffle definirá se os dados serão aleatórios em cada passo<jupyter_code>len(df)
input_func = tf.estimator.inputs.pandas_input_fn(x = X_train, y = y_train, batch_size = 100, num_epochs = None, shuffle = True)<jupyter_output><empty_output><jupyter_text>Criação do modelo (O modelo salva automaticamente após ser treinado) CUIDADO COM O NUMERO DE NEURONIOS, TEM QUE SER IGUAL AO TREINAMENTO!!!!!   Se o número de neurônios for alterado, delete o modelo e começe o treinamento de novo   O número entre colchetes é o número de neurônios use uma camada apenas, com um número um pouco acima do número de inputs   model_dir determina onde o modelo será salvo, se alguma característica da base de dados ou da rede neural for modificada, delete o modelo e reinicie o TREINAMENTO    O modelo será salvo a partir da última rodada de treinamento<jupyter_code>model = tf.estimator.DNNClassifier([15, 12, 6, 3], feature_columns = feat_cols, n_classes = 3, model_dir ="./model")<jupyter_output><empty_output><jupyter_text>Treinar modelo  steps é o número de passos (cada passo utiliza um "batch" ou pacote de dados)<jupyter_code>model.train(input_fn = input_func, steps = 50000)<jupyter_output><empty_output><jupyter_text>Evaluation do teste<jupyter_code>eval_func = tf.estimator.inputs.pandas_input_fn(x = X_test, y = y_test, batch_size = len(X_test), num_epochs = 1, shuffle = False)<jupyter_output><empty_output><jupyter_text>Teste O teste vai determinar a precisão do seu modelo<jupyter_code>results = model.evaluate(eval_func)
results<jupyter_output><empty_output><jupyter_text>Input da previsão<jupyter_code>pred_func = tf.estimator.inputs.pandas_input_fn(x = X_test, batch_size = len(X_test), shuffle = False)<jupyter_output><empty_output><jupyter_text>PrevisãoA previsão vai determinar outras características do resultado final do seu modelo<jupyter_code>prediction = model.predict(pred_func)
prediction = list(prediction)

final_preds = [pred['class_ids'][0] for pred in prediction]
print(final_preds)
print(classification_report(y_test, final_preds))<jupyter_output><empty_output><jupyter_text>Precision: o algoritmo acertou X das previsões (X/X_true) Recall: de 'X_true' parametros, o algoritmo acertou X (X/X_true) batch 100[15, 12, 6, 3]steps 50000             precision    recall  f1-score   support          0       0.21      0.06      0.09       174          1       0.58      0.80      0.67       397          2       0.32      0.27      0.29       172avg / total       0.43      0.50      0.45       743+50k             precision    recall  f1-score   support          0       0.37      0.04      0.07       191          1       0.53      0.92      0.67       362          2       0.50      0.27      0.35       190avg / total       0.48      0.52      0.44       743+50k             precision    recall  f1-score   support          0       0.37      0.04      0.07       191          1       0.53      0.92      0.67       362          2       0.50      0.27      0.35       190avg / total       0.48      0.52      0.44       743+50k             precision    recall  f1-score   support          0       0.44      0.13      0.20       196          1       0.55      0.86      0.67       365          2       0.47      0.30      0.36       182avg / total       0.50      0.53      0.47       743<jupyter_code>import tensorflow as tf
from tensorflow import keras
dnn_keras_model = keras.Sequential()
dnn_keras_model.add(keras.layers.Dense(units=10, input_dim=10, activation='relu'))
dnn_keras_model.add(keras.layers.Dense(units=10, activation='relu'))
dnn_keras_model.add(keras.layers.Dense(units=10, activation='relu'))
dnn_keras_model.add(keras.layers.Dense(units=3, activation='softmax'))
from keras import losses,optimizers,metrics,activations
dnn_keras_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',
                        metrics=['accuracy'])

dnn_keras_model.compile(optimizer=tf.train.RMSPropOptimizer(0.01),
                        loss=keras.losses.categorical_crossentropy,
                        metrics=[keras.metrics.categorical_accuracy])
df_keras = pd.read_csv('../Data/Brazil_Serie_A/BRA.csv')
df_keras.tail()
df_keras.dropna(inplace=True)
df_keras.drop(['Country', 'League', 'Date', 'Time', 'Home', 'Away'], axis = 1, inplace = True)
df_keras['Res'].replace(['H', 'D', 'A'],[1, 0, 2], inplace=True)
x_data_keras = df_keras.drop(['HG', 'AG', 'Res'], axis=1)
y_label_keras = df_keras[['HG', 'AG', 'Res']]
X_train_keras, X_test_keras, y_train_keras, y_test_keras = train_test_split(x_data_keras, y_label_keras, test_size = 0.30)
dnn_keras_model.fit(X_train_keras, y_train_keras, epochs=1000)
predictions = dnn_keras_model.predict_classes(X_test_keras)
from sklearn.metrics import classification_report
y_test_keras.tail()
y_test_keras['HG'] = y_test_keras['HG'].apply(int)
y_test_keras['AG'] = y_test_keras['AG'].apply(int)
y_test_keras['Res'] = y_test_keras['Res'].apply(int)
print(classification_report(predictions, y_test_keras))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11029699101211411, 'fraction_numerical': 0.027842907385697536, 'mean_word_length': 4.471405665419562, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 5, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '112647', 'license': 'MIT', 'n_tokens_mistral': 3715, 'n_tokens_neox': 3520, 'n_words': 868, 'path': 'DNN_Model/.ipynb_checkpoints/DNN_Football_Train-checkpoint.ipynb', 'repo_name': 'chmbrs/Football_Predict', 'size': '10236'}