<jupyter_start><jupyter_text>Defining the question You have been recruited as a football analyst in a company - Mchezopesa Ltd and tasked to accomplish the task below.A prediction result of a game between team 1 and team 2, based on who's home and who's away, and on whether or not the game is friendly (include rank in your training). **Approach 1: Polynomial approach**What to train given:*Rank of home team**Rank of away team**Tournament type* **Model 1**: Predict how many goals the home team scores.**Model 2**: Predict how many goals the away team scores.**Approach 2: Logistic approach**Feature Engineering: Figure out from the home team’s perspective if the game is a Win, Lose or Draw (W, L, D) Importing our python libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# now, let us load our datasets onto our notebook
#
fifa_rank = pd.read_csv('/content/fifa_ranking.csv')
results = pd.read_csv('/content/results.csv')
#let us preview our datasets
fifa_rank.head()
#let us preview our datasets
results.head()
#checking how many records we have
results.shape<jupyter_output><empty_output><jupyter_text>Cleaning the datasets Adding 'Status' column We will add a new column to our dataset, 'Status'. This will show whether the teams had a win(W), loss(L) or a draw(D)<jupyter_code>#by getting the difference between the home scores and the away scores, we can tell whether the match was a win, a loss or a draw
results['Status'] = results['home_score'] - results['away_score'] 
results['Status']
#using a function to represent the above thesis
# if the difference is >= 1, the home team scored more than the away team, hence warranting a win
# if the difference is 0, the home team scored the same as the away team, hence warranting a draw
# if the difference is <= -1, the home team scored less than the away team, hence warranting a loss

def stats(fifa):
    column1 = []
    for i in fifa['Status']:
        if i == 0:
            column1.append('D')
        elif i >= 1:
            column1.append('W')
        elif i <= -1:
            column1.append('L')
    return column1 
        
#introducing the new column 'Status' by calling the function above
X = stats(results)
Status = pd.DataFrame(X)
#viewing the new column
Status
# now, we merge the dataset 'results' with the new column 'Status'
fifa1 = pd.merge(results, Status, left_index= True, right_index= True )
fifa1<jupyter_output><empty_output><jupyter_text>The final dataset.<jupyter_code># now, we merge the dataset above with the 'fifa_rank' dataset to obtain our final dataset
# we will use the common columns 'country_full' and 'home_team' for merging
fifa= pd.merge(fifa_rank, fifa1, left_on = 'country_full', right_on= 'home_team')
fifa
#let us check how many records we have 
fifa.shape<jupyter_output><empty_output><jupyter_text>Now we will drop the records that date before the year 1993 and later than 2018, for easier analysis of the ranking of the teams.<jupyter_code>#using the loc() function
fifa= fifa.loc[(fifa['date']>='1993-08-07')&(fifa['date']<'2018-06-08')]

#now, let us check how many records we have left
fifa.shape<jupyter_output><empty_output><jupyter_text>Sampling<jupyter_code># our dataset is quite large so for easier analysis, we will use 10% of the dataset
#Stratified sample using the strata 'rank'
fifa_sample = fifa.groupby('rank', group_keys=False).apply(lambda grouped_subset : grouped_subset.sample(frac=0.1))

# proportion of the non_stratified sample
print(fifa_sample['rank'].value_counts())

# Check the stratified output
fifa_sample.shape<jupyter_output>1      5248
2      5079
5      4826
4      4771
3      4755
       ... 
205     428
206     320
207     255
208      50
209      17
Name: rank, Length: 209, dtype: int64<jupyter_text>Exploratory Data Analysis Univariate analysis<jupyter_code># we want to analyse the distribution of points across our dataset
#using the groupby() function, we will group our points according to country
points = fifa.groupby(['country_full'])
points
# sort the values in descending order and preview them
series = points['total_points'].mean().sort_values( ascending = False).head(20)
#plot a histogram to visualize the point distribution
series1 = points['total_points'].mean().sort_values(ascending = False)
series1.plot(kind = 'hist')
plt.title('The frequency of points distribution across the Board')
plt.grid()
plt.show()<jupyter_output><empty_output><jupyter_text>Bivariate analysis<jupyter_code># we want to analyze the relationship between home scores and countries/home teams
#using the groupby() function, we will group the home scores acccording to home teams
country = fifa.groupby(['home_team'])
#sort the values in descending order
sort = country['home_score'].sum().sort_values( ascending = False).head(20)
sort
#plot a bar chart to visualize the relationship
sort.plot(kind = 'bar')
plt.title('The Number of home scores per country')
plt.grid()
plt.show()

# we can see that Mexico has the highest recorded scores while Tunisia has the lowest recorded scores.<jupyter_output><empty_output><jupyter_text>Multivariate analysis<jupyter_code>#now, let us check out the relationship between home scores and away scores, with regards to neutrality of the matches
#use a scatterplot to visualize this relationship
ax = sns.scatterplot(data = fifa_sample,x = 'home_score', y ='away_score', hue = 'neutral' )
ax.set(ylim = (0,30))
ax.grid()<jupyter_output><empty_output><jupyter_text>Logistic regression Encoding<jupyter_code>!pip install category_encoders
import category_encoders as ce
encoder = ce.OneHotEncoder(cols = 0, use_cat_names = True)<jupyter_output><empty_output><jupyter_text>Splitting our values into train and test models<jupyter_code>#import the train test split libraries
from sklearn.model_selection import train_test_split
train, test = train_test_split(fifa_sample, random_state = 0)
fifa_encoded = encoder.fit_transform(train)
fifa_encoded.head(5)
#use a heatmap to check multicollinearity of fields 
sns.heatmap(fifa_encoded.corr())
# Assign the variables for logistic regression
X = fifa_encoded[['home_score', 'away_score', 'rank','total_points']]
y = fifa_encoded[['0_D']]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .3, random_state=1000)
from sklearn.linear_model import LogisticRegression

LogReg = LogisticRegression()
LogReg.fit(X_train, y_train)
y_pred = LogReg.predict(X_test)
#using a confusion matrix, we will build our model
#import the confusion matrix libraries
from sklearn.metrics import confusion_matrix
confusion_matrix = confusion_matrix(y_test, y_pred)
confusion_matrix
#to finally test our model, let us check the metrics
#import the metrics libraries
from sklearn import metrics

# MAE - Mean absolute error
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))

# MSE - Mean squared error
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))  

# RMSE - Root mean squared error
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))

#the RMSE is 0.39; our model is quite decent.<jupyter_output>Mean Absolute Error: 0.15560913829217204
Mean Squared Error: 0.15560913829217204
Root Mean Squared Error: 0.3944732415413903<jupyter_text>Polynomial regression<jupyter_code>#import libraries for polynomial regression
#
from sklearn.preprocessing import PolynomialFeatures<jupyter_output><empty_output><jupyter_text>Encoding<jupyter_code>X = fifa_encoded[['home_score', 'away_score', 'rank','total_points']]
y = fifa_encoded[['0_D']]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .3, random_state=1000)<jupyter_output><empty_output><jupyter_text>Assigning variables for polynomial regression and degrees for linear regression<jupyter_code>polyreg = PolynomialFeatures(degree = 2) 
X2 = polyreg.fit_transform(X)

# importing libraries for linear regression
from sklearn.linear_model import LinearRegression
polreg = LinearRegression()
polreg.fit(X2, y)
# we will move on to calculate the metrics
# import all necessary libraries
from sklearn import metrics
import operator

import numpy as np
import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.preprocessing import PolynomialFeatures 

# RMSE - Root mean squared error
rmse = np.sqrt(mean_squared_error(y,polreg.predict(X2)))
rmse<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0877853612614733, 'fraction_numerical': 0.022475876676865146, 'mean_word_length': 5.6554424432263115, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '592553', 'license': 'MIT', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2570, 'n_words': 932, 'path': '(Final)_Core_II_Wk_1.ipynb', 'repo_name': 'CindyMG/Core-II-Wk-1', 'size': '8498'}