{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# FOOTBALL MATCH PREDICTION USING MACHINE LEARNING TOOL","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:11:55.563639Z","iopub.execute_input":"2022-04-08T21:11:55.563984Z","iopub.status.idle":"2022-04-08T21:11:55.573144Z","shell.execute_reply.started":"2022-04-08T21:11:55.563948Z","shell.execute_reply":"2022-04-08T21:11:55.572140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:11:55.574658Z","iopub.execute_input":"2022-04-08T21:11:55.574907Z","iopub.status.idle":"2022-04-08T21:11:55.587056Z","shell.execute_reply.started":"2022-04-08T21:11:55.574876Z","shell.execute_reply":"2022-04-08T21:11:55.586199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"../input/football-match-probability-prediction/test.csv\")","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:11:55.588427Z","iopub.execute_input":"2022-04-08T21:11:55.588877Z","iopub.status.idle":"2022-04-08T21:11:58.370275Z","shell.execute_reply.started":"2022-04-08T21:11:55.588812Z","shell.execute_reply":"2022-04-08T21:11:58.369342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testn = pd.read_csv(\"../input/football-match-probability-prediction/test.csv\")","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:11:58.371560Z","iopub.execute_input":"2022-04-08T21:11:58.372353Z","iopub.status.idle":"2022-04-08T21:12:01.175485Z","shell.execute_reply.started":"2022-04-08T21:11:58.372316Z","shell.execute_reply":"2022-04-08T21:12:01.174889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/football-match-probability-prediction/train.csv\")","metadata":{"_kg_hide-output":true,"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:01.176675Z","iopub.execute_input":"2022-04-08T21:12:01.177612Z","iopub.status.idle":"2022-04-08T21:12:05.843924Z","shell.execute_reply.started":"2022-04-08T21:12:01.177571Z","shell.execute_reply":"2022-04-08T21:12:05.843084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:05.845171Z","iopub.execute_input":"2022-04-08T21:12:05.845403Z","iopub.status.idle":"2022-04-08T21:12:05.878729Z","shell.execute_reply.started":"2022-04-08T21:12:05.845376Z","shell.execute_reply":"2022-04-08T21:12:05.877952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isna().sum()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:05.880186Z","iopub.execute_input":"2022-04-08T21:12:05.880445Z","iopub.status.idle":"2022-04-08T21:12:06.235499Z","shell.execute_reply.started":"2022-04-08T21:12:05.880415Z","shell.execute_reply":"2022-04-08T21:12:06.234843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{"editable":false}},{"cell_type":"code","source":"train.describe()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:06.236541Z","iopub.execute_input":"2022-04-08T21:12:06.237153Z","iopub.status.idle":"2022-04-08T21:12:07.355735Z","shell.execute_reply.started":"2022-04-08T21:12:06.237109Z","shell.execute_reply":"2022-04-08T21:12:07.355127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(train.iloc[:, [0,2,3,4,5,6]], axis=1, inplace=True)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:07.356706Z","iopub.execute_input":"2022-04-08T21:12:07.357308Z","iopub.status.idle":"2022-04-08T21:12:07.455292Z","shell.execute_reply.started":"2022-04-08T21:12:07.357264Z","shell.execute_reply":"2022-04-08T21:12:07.454381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:07.456419Z","iopub.execute_input":"2022-04-08T21:12:07.456742Z","iopub.status.idle":"2022-04-08T21:12:07.487977Z","shell.execute_reply.started":"2022-04-08T21:12:07.456708Z","shell.execute_reply":"2022-04-08T21:12:07.486896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop(test.iloc[:,0:5], axis=1, inplace=True)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:07.489139Z","iopub.execute_input":"2022-04-08T21:12:07.489373Z","iopub.status.idle":"2022-04-08T21:12:07.542803Z","shell.execute_reply.started":"2022-04-08T21:12:07.489344Z","shell.execute_reply":"2022-04-08T21:12:07.542157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d = train['is_cup']","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:12:07.543796Z","iopub.execute_input":"2022-04-08T21:12:07.544181Z","iopub.status.idle":"2022-04-08T21:12:07.548793Z","shell.execute_reply.started":"2022-04-08T21:12:07.544152Z","shell.execute_reply":"2022-04-08T21:12:07.547879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='is_cup', data=train)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:12:07.549741Z","iopub.execute_input":"2022-04-08T21:12:07.550415Z","iopub.status.idle":"2022-04-08T21:12:08.060728Z","shell.execute_reply.started":"2022-04-08T21:12:07.550375Z","shell.execute_reply":"2022-04-08T21:12:08.059894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='target', data=train)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:12:08.067226Z","iopub.execute_input":"2022-04-08T21:12:08.067938Z","iopub.status.idle":"2022-04-08T21:12:08.371886Z","shell.execute_reply.started":"2022-04-08T21:12:08.067897Z","shell.execute_reply":"2022-04-08T21:12:08.371192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop(test.iloc[:, [0,5,6,7,8]], axis=1, inplace=True)\ntrain.drop(train.iloc[:, [2,3,4,5,6,7,8,9]], axis=1, inplace=True)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:08.372788Z","iopub.execute_input":"2022-04-08T21:12:08.373231Z","iopub.status.idle":"2022-04-08T21:12:08.502721Z","shell.execute_reply.started":"2022-04-08T21:12:08.373194Z","shell.execute_reply":"2022-04-08T21:12:08.501873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop(test.iloc[:, [1,2,3,4,5,6,7,8]], axis=1, inplace=True)\ntrain.drop(train.iloc[:, [2,3,4,5]], axis=1, inplace=True)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:08.504004Z","iopub.execute_input":"2022-04-08T21:12:08.504391Z","iopub.status.idle":"2022-04-08T21:12:08.638076Z","shell.execute_reply.started":"2022-04-08T21:12:08.504349Z","shell.execute_reply":"2022-04-08T21:12:08.637036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:08.639505Z","iopub.execute_input":"2022-04-08T21:12:08.639863Z","iopub.status.idle":"2022-04-08T21:12:08.676691Z","shell.execute_reply.started":"2022-04-08T21:12:08.639793Z","shell.execute_reply":"2022-04-08T21:12:08.675582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:08.678205Z","iopub.execute_input":"2022-04-08T21:12:08.678486Z","iopub.status.idle":"2022-04-08T21:12:08.714946Z","shell.execute_reply.started":"2022-04-08T21:12:08.678446Z","shell.execute_reply":"2022-04-08T21:12:08.714156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(train.iloc[:, 72:92], axis=1, inplace=True)\ntest.drop(test.iloc[:, 71:91], axis=1, inplace=True)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:08.716779Z","iopub.execute_input":"2022-04-08T21:12:08.717143Z","iopub.status.idle":"2022-04-08T21:12:08.833664Z","shell.execute_reply.started":"2022-04-08T21:12:08.717099Z","shell.execute_reply":"2022-04-08T21:12:08.832703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(train.iloc[:, 142:190], axis=1, inplace=True)\ntest.drop(test.iloc[:, 141:190], axis=1, inplace=True)","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:08.835059Z","iopub.execute_input":"2022-04-08T21:12:08.835404Z","iopub.status.idle":"2022-04-08T21:12:08.928747Z","shell.execute_reply.started":"2022-04-08T21:12:08.835353Z","shell.execute_reply":"2022-04-08T21:12:08.927658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train['target']","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:08.930379Z","iopub.execute_input":"2022-04-08T21:12:08.931398Z","iopub.status.idle":"2022-04-08T21:12:08.946191Z","shell.execute_reply.started":"2022-04-08T21:12:08.931342Z","shell.execute_reply":"2022-04-08T21:12:08.945309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.unique()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:08.947596Z","iopub.execute_input":"2022-04-08T21:12:08.948645Z","iopub.status.idle":"2022-04-08T21:12:08.968679Z","shell.execute_reply.started":"2022-04-08T21:12:08.948591Z","shell.execute_reply":"2022-04-08T21:12:08.967889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ##LABEL ENCODING","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nlabelencoder = LabelEncoder()","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:08.970036Z","iopub.execute_input":"2022-04-08T21:12:08.970896Z","iopub.status.idle":"2022-04-08T21:12:08.975587Z","shell.execute_reply.started":"2022-04-08T21:12:08.970846Z","shell.execute_reply":"2022-04-08T21:12:08.974726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['is_cup'] = labelencoder.fit_transform(train['is_cup'])\ntest['is_cup'] = labelencoder.fit_transform(test['is_cup'])","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:08.976895Z","iopub.execute_input":"2022-04-08T21:12:08.977516Z","iopub.status.idle":"2022-04-08T21:12:09.026074Z","shell.execute_reply.started":"2022-04-08T21:12:08.977470Z","shell.execute_reply":"2022-04-08T21:12:09.025154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['target'] = labelencoder.fit_transform(train['target'])","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:09.027385Z","iopub.execute_input":"2022-04-08T21:12:09.027653Z","iopub.status.idle":"2022-04-08T21:12:09.070184Z","shell.execute_reply.started":"2022-04-08T21:12:09.027616Z","shell.execute_reply":"2022-04-08T21:12:09.069177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train.iloc[:, 1:]","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:09.071549Z","iopub.execute_input":"2022-04-08T21:12:09.071806Z","iopub.status.idle":"2022-04-08T21:12:09.122720Z","shell.execute_reply.started":"2022-04-08T21:12:09.071772Z","shell.execute_reply":"2022-04-08T21:12:09.121678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain = x","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:09.124417Z","iopub.execute_input":"2022-04-08T21:12:09.124927Z","iopub.status.idle":"2022-04-08T21:12:09.135490Z","shell.execute_reply.started":"2022-04-08T21:12:09.124878Z","shell.execute_reply":"2022-04-08T21:12:09.134709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Checking for missing values\nxtrain.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:12:09.136897Z","iopub.execute_input":"2022-04-08T21:12:09.137746Z","iopub.status.idle":"2022-04-08T21:12:09.183268Z","shell.execute_reply.started":"2022-04-08T21:12:09.137702Z","shell.execute_reply":"2022-04-08T21:12:09.182228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:12:09.184894Z","iopub.execute_input":"2022-04-08T21:12:09.185309Z","iopub.status.idle":"2022-04-08T21:12:09.215630Z","shell.execute_reply.started":"2022-04-08T21:12:09.185262Z","shell.execute_reply":"2022-04-08T21:12:09.214729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Replacing the missing values with mean of each columns\nxtrain.fillna(xtrain.mean(), inplace=True)\ntest.fillna(test.mean(), inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:12:09.216935Z","iopub.execute_input":"2022-04-08T21:12:09.217552Z","iopub.status.idle":"2022-04-08T21:12:09.412681Z","shell.execute_reply.started":"2022-04-08T21:12:09.217517Z","shell.execute_reply":"2022-04-08T21:12:09.411804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ytrain = train['target']","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:09.414000Z","iopub.execute_input":"2022-04-08T21:12:09.414249Z","iopub.status.idle":"2022-04-08T21:12:09.417665Z","shell.execute_reply.started":"2022-04-08T21:12:09.414218Z","shell.execute_reply":"2022-04-08T21:12:09.417063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtest = test","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:12:09.418790Z","iopub.execute_input":"2022-04-08T21:12:09.419159Z","iopub.status.idle":"2022-04-08T21:12:09.432757Z","shell.execute_reply.started":"2022-04-08T21:12:09.419130Z","shell.execute_reply":"2022-04-08T21:12:09.431546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.tree import DecisionTreeClassifier","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:09.434177Z","iopub.execute_input":"2022-04-08T21:12:09.434432Z","iopub.status.idle":"2022-04-08T21:12:09.446705Z","shell.execute_reply.started":"2022-04-08T21:12:09.434401Z","shell.execute_reply":"2022-04-08T21:12:09.445913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# # # USING LOGISTICREGRESSION FOR PREDICTION","metadata":{}},{"cell_type":"code","source":"#USING LOGISTICREGRESSION FOR PREDICTION\nlg = LogisticRegression(solver='lbfgs', class_weight='balanced', max_iter=10000)\nlg.fit(xtrain, ytrain)\ny_pred = lg.predict_proba(xtest)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:12:09.448277Z","iopub.execute_input":"2022-04-08T21:12:09.448767Z","iopub.status.idle":"2022-04-08T21:12:14.738621Z","shell.execute_reply.started":"2022-04-08T21:12:09.448731Z","shell.execute_reply":"2022-04-08T21:12:14.737701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.preprocessing import RobustScaler\n#fit scaler on training data\nnorm = MinMaxScaler().fit(xtrain)\n#transform training data\nxtrain_norm = norm.transform(xtrain)\n#transform testing data\nxtest_norm = norm.transform(xtest)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:12:14.740223Z","iopub.execute_input":"2022-04-08T21:12:14.740799Z","iopub.status.idle":"2022-04-08T21:12:15.133682Z","shell.execute_reply.started":"2022-04-08T21:12:14.740746Z","shell.execute_reply":"2022-04-08T21:12:15.133028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.calibration import CalibratedClassifierCV","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:15.134948Z","iopub.execute_input":"2022-04-08T21:12:15.135212Z","iopub.status.idle":"2022-04-08T21:12:15.139138Z","shell.execute_reply.started":"2022-04-08T21:12:15.135180Z","shell.execute_reply":"2022-04-08T21:12:15.138274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cc = CalibratedClassifierCV(lg, cv=3)\ncc.fit(xtrain_norm, ytrain)\nprediction_1 = cc.predict_proba(xtest_norm)\nprediction_1","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:12:15.140519Z","iopub.execute_input":"2022-04-08T21:12:15.140935Z","iopub.status.idle":"2022-04-08T21:13:03.349618Z","shell.execute_reply.started":"2022-04-08T21:12:15.140898Z","shell.execute_reply":"2022-04-08T21:13:03.348766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.DataFrame(testn.id), pd.DataFrame(prediction_1, columns=['home', 'draw', 'away'])[['home', 'draw', 'away']]], axis=1)\n                        ","metadata":{"editable":false,"execution":{"iopub.status.busy":"2022-04-08T21:13:03.350960Z","iopub.execute_input":"2022-04-08T21:13:03.351536Z","iopub.status.idle":"2022-04-08T21:13:03.364339Z","shell.execute_reply.started":"2022-04-08T21:13:03.351488Z","shell.execute_reply":"2022-04-08T21:13:03.363370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-08T21:13:03.365999Z","iopub.execute_input":"2022-04-08T21:13:03.366663Z","iopub.status.idle":"2022-04-08T21:13:03.946969Z","shell.execute_reply.started":"2022-04-08T21:13:03.366594Z","shell.execute_reply":"2022-04-08T21:13:03.946228Z"},"trusted":true},"execution_count":null,"outputs":[]}]}