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

# ## Predicting players rating
# In this project you are going to predict the overall rating of soccer player based on their attributes
# such as 'crossing', 'finishing etc.
# The dataset you are going to use is from European Soccer Database
# (https://www.kaggle.com/hugomathien/soccer) has more than 25,000 matches and more than
# 10,000 players for European professional soccer seasons from 2008 to 2016.
# Download the data in the same folder and run the following commmand to get it in the environment

# ### Import required libraries

import sqlite3
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split,GridSearchCV
from sklearn.metrics import mean_squared_error, r2_score,make_scorer
from math import sqrt
from matplotlib import pyplot as plt
import xgboost as xgb
# %matplotlib inline

# ## Step:1 Download Data from source

# Create your connection.
cnx = sqlite3.connect('database.sqlite')
df = pd.read_sql_query("SELECT * FROM Player_Attributes", cnx)

# ## Step:2 Exploring Data

df.head(10).transpose()

# ### Data columns

df.columns

# ### Data description

df.describe().transpose()

df.hist(figsize=(25,25))

# ### Count of null values in each column

df.isnull().sum(axis=0)

# ### Unique values in Categorical Variable

df.preferred_foot.unique()

df.attacking_work_rate.unique()

df.defensive_work_rate.unique()

# ## Step:3 Data Cleaning

# ### Drop null values

total_rows_with_null_values = df.shape[0]
df=df.dropna()
total_rows_removed = total_rows_with_null_values - df.shape[0]
print("Total Rows Removed {}".format(total_rows_removed))

# ### Convert Categorical values into numerical values

preferred_foot_num = pd.factorize(df.preferred_foot)
attacking_work_rate_num = pd.factorize(df.attacking_work_rate)
defensive_work_rate = pd.factorize(df.defensive_work_rate)

df.preferred_foot=preferred_foot_num[0]
df.attacking_work_rate = attacking_work_rate_num[0]
df.defensive_work_rate = defensive_work_rate[0]


# ## Step: 4 Feature Correlation Analysis

df.corr().sort_values(['overall_rating'])['overall_rating']

# #### Note :  As per above analysis, features which are correlated morethan .3 with overal rating has been considered for traning model
# Features List :
# heading_accuracy       (0.313324)
# strength               (0.315684)
# aggression             (0.322782)
# stamina                (0.325606)
# finishing              (0.330079)
# free_kick_accuracy     (0.349800)
# dribbling              (0.354191)
# crossing               (0.357320)
# curve                  (0.357566)
# volleys                (0.361739)
# positioning            (0.368978)
# long_shots             (0.392668)
# penalties              (0.392715)
# shot_power             (0.428053)
# vision                 (0.431493)
# long_passing           (0.434525)
# ball_control           (0.443991)
# short_passing          (0.458243)
# potential              (0.765435)
# reactions              (0.771856)

data = df[['heading_accuracy','strength','aggression','stamina','finishing','free_kick_accuracy','dribbling','crossing','curve','volleys','positioning','long_shots','penalties','shot_power','vision','long_passing','ball_control','short_passing','potential','reactions','overall_rating']].copy()

data.head()

# ## Step: 5 Split Data for Training & Testing

X = np.asarray(data[['heading_accuracy','strength','aggression','stamina','finishing','free_kick_accuracy','dribbling','crossing','curve','volleys','positioning','long_shots','penalties','shot_power','vision','long_passing','ball_control','short_passing','potential','reactions']])
#X = np.asarray(data[['volleys','positioning','long_shots','penalties','shot_power','vision','long_passing','ball_control','short_passing','potential','reactions']])
Y = np.asarray(data.overall_rating)

X_train, X_test, Y_train, Y_test = train_test_split(X,Y,test_size=0.33)

# ### 1.  LinearRegression

lregr = LinearRegression()
# Fit data
lregr.fit(X_train, Y_train)
# Predict Y values
Y_pred = lregr.predict(X_test)
print("Mean squared error: %.2f"
      % mean_squared_error(Y_test, Y_pred))
#  variance score: 1 is perfect prediction
print('Variance score: %.2f' % r2_score(Y_test, Y_pred))

# ### 2. DecisionTreeRegressor

# #### Hyperparameter tuning

# +
# scoring method
scoring = make_scorer(r2_score)

# GridSearchCV
g_cv = GridSearchCV(DecisionTreeRegressor(random_state=0),
              param_grid={'min_samples_split': range(2, 10)},
              scoring=scoring, cv=5, refit=True)
g_cv.fit(X_train, Y_train)

#best hyper parameters
g_cv.best_params_
# -

dregr = DecisionTreeRegressor(random_state=0,min_samples_split= 4)
# Fit Data
dregr.fit(X_train, Y_train)
# Predict Y Value
Y_pred = dregr.predict(X_test)
print("Mean squared error: %.2f"
      % mean_squared_error(Y_test, Y_pred))
#  variance score: 1 is perfect prediction
print('Variance score: %.2f' % r2_score(Y_test, Y_pred))

# ### 3. RandomForestRegressor

# #### Hyper Parameter Tuning

# +
# scoring method
scoring = make_scorer(r2_score)

# GridSearchCV
g_cv = GridSearchCV(RandomForestRegressor(),
              param_grid={'max_features':range(2,10), 'min_samples_split':range(2,10), 'min_samples_leaf':range(2,10)},
              scoring=scoring, cv=5, refit=True)
g_cv.fit(X_train, Y_train)
#best hyper parameters
g_cv.best_params_
# -

ranregr = RandomForestRegressor(max_features=2, min_samples_split=4, n_estimators=50, min_samples_leaf=2)
# Fit Data
ranregr.fit(X_train, Y_train)
# Predict Y Value
Y_pred = ranregr.predict(X_test)
print("Mean squared error: %.2f"
      % mean_squared_error(Y_test, Y_pred))
# variance score: 1 is perfect prediction
print('Variance score: %.2f' % r2_score(Y_test, Y_pred))

# ### 4. XGBoost Regressor

# +
# scoring method
scoring = make_scorer(r2_score)

# GridSearchCV
g_cv = GridSearchCV(xgb.XGBRegressor(),
              param_grid={'min_child_weight':[4,5], 'gamma':[i/10.0 for i in range(3,6)],  'subsample':[i/10.0 for i in range(6,11)],
'colsample_bytree':[i/10.0 for i in range(6,11)], 'max_depth': [20,25,30,35,40,45,50]},
              scoring=scoring, cv=5, refit=True)
g_cv.fit(X_train, Y_train)
#best hyper parameters
g_cv.best_params_
# -

xgregr=xgb.XGBRegressor(min_child_weight=11,gamma=0.5,subsample=0.8,colsample_bytree=0.6,max_depth=65)
# Fit Data
xgregr.fit(X_train, Y_train)
# Predict Y Value
Y_pred = xgregr.predict(X_test)
print("Mean squared error: %.2f"
      % mean_squared_error(Y_test, Y_pred))
#  variance score: 1 is perfect prediction
print('Variance score: %.2f' % r2_score(Y_test, Y_pred))

# ## Note : From the analysis above we can see that XGBost is giveing less Mean Squared Error & highest  Variance Score



StarcoderdataJupyterScriptsDedupFiltered