<reponame>dstushar7/Soccer-ML-Prediction
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from datetime import datetime as dt
import itertools
from pandas.plotting import scatter_matrix
from sklearn.preprocessing import scale

dataset = pd.read_csv('dataset.csv')


#correlation matrix for dataset
# plt.figure(figsize=(20,10)) 
# sns.heatmap(dataset.corr(), annot=True)
# plt.show(block=True)


# Remove few column
dataset2 = dataset.copy().drop(columns =['Date', 'HomeTeam', 'AwayTeam', 'FTHG', 'FTAG',
       'HTGS', 'ATGS', 'HTGC', 'ATGC',
       'HM4', 'HM5','AM4', 'AM5', 'MW', 'HTFormPtsStr',
       'ATFormPtsStr', 'HTFormPts', 'ATFormPts', 'HTWinStreak3',
       'HTWinStreak5', 'HTLossStreak3', 'HTLossStreak5', 'ATWinStreak3',
       'ATWinStreak5', 'ATLossStreak3', 'ATLossStreak5',
       'DiffPts'] )


# Total number of matches.
n_matches = dataset2.shape[0]

# Calculate number of features. -1 because we are saving one as the target variable (win/lose/draw)
n_features = dataset2.shape[1] - 1

# Calculate matches won by home team.
n_homewins = len(dataset2[dataset2.FTR == 'H'])

# Calculate win rate for home team.
win_rate = (float(n_homewins) / (n_matches)) * 100

# Print the results
# print("Total number of matches: {}".format(n_matches))
# print ("Number of features: {}".format(n_features))
# print( "Number of matches won by home team: {}".format(n_homewins))
# print ("Win rate of home team: {:.2f}%".format(win_rate))



# Plotting the scatter matrix
# scatter_matrix(dataset2[['HTGD','ATGD','HTP','ATP','DiffFormPts']], figsize=(15,15))
# plt.show(block=True)

# Separate into feature set and target variable
#FTR = Full Time Result (H=Home Win, D=Draw, A=Away Win)
X_all = dataset2.drop(['FTR'],1)
y_all = dataset2['FTR']




#Center to the mean and component wise scale to unit variance.
cols = [['HTGD','ATGD','HTP','ATP']]
for col in cols:
    X_all[col] = scale(X_all[col])



#last 3 wins for both sides
X_all.HM1 = X_all.HM1.astype('str')
X_all.HM2 = X_all.HM2.astype('str')
X_all.HM3 = X_all.HM3.astype('str')
X_all.AM1 = X_all.AM1.astype('str')
X_all.AM2 = X_all.AM2.astype('str')
X_all.AM3 = X_all.AM3.astype('str')

#we want continous vars that are integers for our input data, so lets remove any categorical vars
def preprocess_features(X):
    ''' Preprocesses the football data and converts catagorical variables into dummy variables. '''
    
    # Initialize new output DataFrame
    output = pd.DataFrame(index = X.index)

    # Investigate each feature column for the data
    for col, col_data in X.iteritems():

        # If data type is categorical, convert to dummy variables
        if col_data.dtype == object:
            col_data = pd.get_dummies(col_data, prefix = col)
                    
        # Collect the revised columns
        output = output.join(col_data)
    
    return output

X_all = preprocess_features(X_all)
# print ("Processed feature columns ({} total features):\n{}".format(len(X_all.columns), list(X_all.columns)))
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from datetime import datetime as dt
import itertools
from pandas.plotting import scatter_matrix
from sklearn.preprocessing import scale

dataset = pd.read_csv('dataset.csv')


#correlation matrix for dataset
# plt.figure(figsize=(20,10)) 
# sns.heatmap(dataset.corr(), annot=True)
# plt.show(block=True)


# Remove few column
dataset2 = dataset.copy().drop(columns =['Date', 'HomeTeam', 'AwayTeam', 'FTHG', 'FTAG',
       'HTGS', 'ATGS', 'HTGC', 'ATGC',
       'HM4', 'HM5','AM4', 'AM5', 'MW', 'HTFormPtsStr',
       'ATFormPtsStr', 'HTFormPts', 'ATFormPts', 'HTWinStreak3',
       'HTWinStreak5', 'HTLossStreak3', 'HTLossStreak5', 'ATWinStreak3',
       'ATWinStreak5', 'ATLossStreak3', 'ATLossStreak5',
       'DiffPts'] )


# Total number of matches.
n_matches = dataset2.shape[0]

# Calculate number of features. -1 because we are saving one as the target variable (win/lose/draw)
n_features = dataset2.shape[1] - 1

# Calculate matches won by home team.
n_homewins = len(dataset2[dataset2.FTR == 'H'])

# Calculate win rate for home team.
win_rate = (float(n_homewins) / (n_matches)) * 100

# Print the results
# print("Total number of matches: {}".format(n_matches))
# print ("Number of features: {}".format(n_features))
# print( "Number of matches won by home team: {}".format(n_homewins))
# print ("Win rate of home team: {:.2f}%".format(win_rate))



# Plotting the scatter matrix
# scatter_matrix(dataset2[['HTGD','ATGD','HTP','ATP','DiffFormPts']], figsize=(15,15))
# plt.show(block=True)

# Separate into feature set and target variable
#FTR = Full Time Result (H=Home Win, D=Draw, A=Away Win)
X_all = dataset2.drop(['FTR'],1)
y_all = dataset2['FTR']




#Center to the mean and component wise scale to unit variance.
cols = [['HTGD','ATGD','HTP','ATP']]
for col in cols:
    X_all[col] = scale(X_all[col])



#last 3 wins for both sides
X_all.HM1 = X_all.HM1.astype('str')
X_all.HM2 = X_all.HM2.astype('str')
X_all.HM3 = X_all.HM3.astype('str')
X_all.AM1 = X_all.AM1.astype('str')
X_all.AM2 = X_all.AM2.astype('str')
X_all.AM3 = X_all.AM3.astype('str')

#we want continous vars that are integers for our input data, so lets remove any categorical vars
def preprocess_features(X):
    ''' Preprocesses the football data and converts catagorical variables into dummy variables. '''
    
    # Initialize new output DataFrame
    output = pd.DataFrame(index = X.index)

    # Investigate each feature column for the data
    for col, col_data in X.iteritems():

        # If data type is categorical, convert to dummy variables
        if col_data.dtype == object:
            col_data = pd.get_dummies(col_data, prefix = col)
                    
        # Collect the revised columns
        output = output.join(col_data)
    
    return output

X_all = preprocess_features(X_all)
# print ("Processed feature columns ({} total features):\n{}".format(len(X_all.columns), list(X_all.columns)))
#correlation matrix for dataset # plt.figure(figsize=(20,10)) # sns.heatmap(dataset.corr(), annot=True) # plt.show(block=True) # Remove few column # Total number of matches. # Calculate number of features. -1 because we are saving one as the target variable (win/lose/draw) # Calculate matches won by home team. # Calculate win rate for home team. # Print the results # print("Total number of matches: {}".format(n_matches)) # print ("Number of features: {}".format(n_features)) # print( "Number of matches won by home team: {}".format(n_homewins)) # print ("Win rate of home team: {:.2f}%".format(win_rate)) # Plotting the scatter matrix # scatter_matrix(dataset2[['HTGD','ATGD','HTP','ATP','DiffFormPts']], figsize=(15,15)) # plt.show(block=True) # Separate into feature set and target variable #FTR = Full Time Result (H=Home Win, D=Draw, A=Away Win) #Center to the mean and component wise scale to unit variance. #last 3 wins for both sides #we want continous vars that are integers for our input data, so lets remove any categorical vars Preprocesses the football data and converts catagorical variables into dummy variables. # Initialize new output DataFrame # Investigate each feature column for the data # If data type is categorical, convert to dummy variables # Collect the revised columns # print ("Processed feature columns ({} total features):\n{}".format(len(X_all.columns), list(X_all.columns)))