{"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":"**Random Forest**","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","execution":{"iopub.status.busy":"2022-02-26T08:13:55.418639Z","iopub.execute_input":"2022-02-26T08:13:55.419293Z","iopub.status.idle":"2022-02-26T08:13:55.450304Z","shell.execute_reply.started":"2022-02-26T08:13:55.418997Z","shell.execute_reply":"2022-02-26T08:13:55.449017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_df = pd.read_csv('/kaggle/input/football-match-probability-prediction/train.csv')\nmain_df = main_df[main_df.target.isin(['away','home'])]","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:13:55.666429Z","iopub.execute_input":"2022-02-26T08:13:55.667692Z","iopub.status.idle":"2022-02-26T08:14:02.509162Z","shell.execute_reply.started":"2022-02-26T08:13:55.667628Z","shell.execute_reply":"2022-02-26T08:14:02.507018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# main_df","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:14:02.511766Z","iopub.execute_input":"2022-02-26T08:14:02.512685Z","iopub.status.idle":"2022-02-26T08:14:02.519282Z","shell.execute_reply.started":"2022-02-26T08:14:02.512624Z","shell.execute_reply":"2022-02-26T08:14:02.518172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"away_team = 1\nhome_team = 0\n\nrandom_state = 100","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:14:04.507269Z","iopub.execute_input":"2022-02-26T08:14:04.508193Z","iopub.status.idle":"2022-02-26T08:14:04.514177Z","shell.execute_reply.started":"2022-02-26T08:14:04.508141Z","shell.execute_reply":"2022-02-26T08:14:04.512852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def log(message,log=0):\n    if(log==1):\n        print(message)\n        \ndef drop_like(column):\n    return list(filter(lambda x : column in x,main_df.columns))","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:14:05.843544Z","iopub.execute_input":"2022-02-26T08:14:05.844928Z","iopub.status.idle":"2022-02-26T08:14:05.853568Z","shell.execute_reply.started":"2022-02-26T08:14:05.844840Z","shell.execute_reply":"2022-02-26T08:14:05.852291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = main_df\n\ndrop_columns_name = ['id','league_name']\ndrop_date_columns = drop_like('match_date')\ndrop_leage_id = drop_like('league_id')\ndrop_coach_id = drop_like('coach')\n\ndrop_columns = drop_columns_name + drop_date_columns + drop_leage_id + drop_coach_id\n\nintrested_column_like = \"rating\"\nintrested_columns = ['home_team_name','away_team_name']\ndf = df.drop(drop_columns,axis=1)\n\nfor column in df.columns:\n    log(f'{column} - {df[column].unique()}')\n    \n    if intrested_column_like in column:\n        intrested_columns.append(column)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:14:06.994928Z","iopub.execute_input":"2022-02-26T08:14:06.995394Z","iopub.status.idle":"2022-02-26T08:14:07.484028Z","shell.execute_reply.started":"2022-02-26T08:14:06.995350Z","shell.execute_reply":"2022-02-26T08:14:07.482532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df.away_team_name == \"Karitsa\"]","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:14:08.579487Z","iopub.execute_input":"2022-02-26T08:14:08.579856Z","iopub.status.idle":"2022-02-26T08:14:08.649456Z","shell.execute_reply.started":"2022-02-26T08:14:08.579820Z","shell.execute_reply":"2022-02-26T08:14:08.647775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = df[intrested_columns]\nt[t.away_team_name == \"Karitsa\"]","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:14:09.832698Z","iopub.execute_input":"2022-02-26T08:14:09.833106Z","iopub.status.idle":"2022-02-26T08:14:09.901046Z","shell.execute_reply.started":"2022-02-26T08:14:09.833069Z","shell.execute_reply":"2022-02-26T08:14:09.899494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder\nohe = OneHotEncoder(sparse = False)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:14:10.211362Z","iopub.execute_input":"2022-02-26T08:14:10.211735Z","iopub.status.idle":"2022-02-26T08:14:11.616578Z","shell.execute_reply.started":"2022-02-26T08:14:10.211696Z","shell.execute_reply":"2022-02-26T08:14:11.615387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_data = df.drop(['home_team_name','away_team_name'],axis=1)\nmodel_data = model_data[model_data.isna().sum(axis=1) / len(model_data.columns) == 0.0].copy()\nmodel_data['target'] = np.where(model_data.target == 'away',1,0)\nmodel_data['is_cup'] = np.where(model_data.is_cup == True,1,0)\nmodel_data.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:14:11.618574Z","iopub.execute_input":"2022-02-26T08:14:11.618857Z","iopub.status.idle":"2022-02-26T08:14:12.168692Z","shell.execute_reply.started":"2022-02-26T08:14:11.618826Z","shell.execute_reply":"2022-02-26T08:14:12.167253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nfrom sklearn.datasets import make_classification\nfrom sklearn.model_selection import train_test_split ","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:14:13.413231Z","iopub.execute_input":"2022-02-26T08:14:13.413646Z","iopub.status.idle":"2022-02-26T08:14:13.554265Z","shell.execute_reply.started":"2022-02-26T08:14:13.413603Z","shell.execute_reply":"2022-02-26T08:14:13.553096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = model_data.drop('target', axis =1)\ny = model_data['target']\nX_train, X_test, y_train, y_test = train_test_split( X, y, test_size = 0.2, random_state = 100) ","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:14:14.499714Z","iopub.execute_input":"2022-02-26T08:14:14.500053Z","iopub.status.idle":"2022-02-26T08:14:14.642332Z","shell.execute_reply.started":"2022-02-26T08:14:14.500020Z","shell.execute_reply":"2022-02-26T08:14:14.640644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1 = RandomForestClassifier(\n    n_estimators=100,\n    max_depth=20, random_state=100,n_jobs=-1\n)\nmodel_1.fit(X_train, y_train)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:31:40.362194Z","iopub.execute_input":"2022-02-26T08:31:40.362564Z","iopub.status.idle":"2022-02-26T08:31:55.344743Z","shell.execute_reply.started":"2022-02-26T08:31:40.362528Z","shell.execute_reply":"2022-02-26T08:31:55.343997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix,recall_score, accuracy_score , classification_report, balanced_accuracy_score,precision_score\ndef get_business_value(y,y_pred):\n    ravel = confusion_matrix(y, y_pred).ravel()\n    return (ravel[0]*100 + ravel[3] * 100) - (ravel[1]*75 + ravel[2] * 50)\n\ndef print_performance(text, y_pred):\n    \n    print(\"Accuracy Score for \", text ,\" is :\" , accuracy_score(y_test, y_pred))\n    print(\"Balance Accuracy Score for \", text ,\" is :\" , balanced_accuracy_score(y_test, y_pred))\n    print(\"Recall for \", text ,\" is :\", round(100*recall_score(y_test, y_pred),2) ,\"%\")\n    print(\"Confusion Matrix for \", text ,\" is :\")\n    ravel = confusion_matrix(y_test, y_pred).ravel()\n    print(  confusion_matrix(y_test, y_pred))\n    print(f\"Businees Value {(ravel[0]*100 + ravel[3] * 100) - (ravel[1]*75 + ravel[2] * 50)}\")\n    print(\"Classification Report for \", text ,\" is :\")\n    print(  classification_report(y_test, y_pred))\n    \n    return confusion_matrix(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:31:58.083199Z","iopub.execute_input":"2022-02-26T08:31:58.083678Z","iopub.status.idle":"2022-02-26T08:31:58.095085Z","shell.execute_reply.started":"2022-02-26T08:31:58.083624Z","shell.execute_reply":"2022-02-26T08:31:58.094224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_1.predict(X_test)\nprint_performance(\"Random Forest Tree\", y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:32:00.384797Z","iopub.execute_input":"2022-02-26T08:32:00.386215Z","iopub.status.idle":"2022-02-26T08:32:00.812255Z","shell.execute_reply.started":"2022-02-26T08:32:00.386160Z","shell.execute_reply":"2022-02-26T08:32:00.811343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# proba =  model_1.predict_proba(X_train)\n# model_1.predict_proba(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:34:23.335591Z","iopub.execute_input":"2022-02-26T08:34:23.336840Z","iopub.status.idle":"2022-02-26T08:34:23.339936Z","shell.execute_reply.started":"2022-02-26T08:34:23.336784Z","shell.execute_reply":"2022-02-26T08:34:23.339210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_predict_proba(model,data):\n    proba = model.predict_proba(data)\n    predict_proba = []\n    for prob in proba:\n        predict_proba.append(prob[1])\n\n    return pd.DataFrame(predict_proba,columns=['proba'])","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:32:07.401954Z","iopub.execute_input":"2022-02-26T08:32:07.402475Z","iopub.status.idle":"2022-02-26T08:32:07.410490Z","shell.execute_reply.started":"2022-02-26T08:32:07.402427Z","shell.execute_reply":"2022-02-26T08:32:07.409047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BV = []\npredict_proba = get_predict_proba(model_1,X_train)\n\nfor i in range(1,101):\n    thresold = i/100\n    y_pred_proba = (predict_proba > thresold).astype(int)\n    BV.append(get_business_value(y_train,y_pred_proba))\nthresold = np.argmax(BV) / 100\nprint(thresold)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:34:14.782764Z","iopub.execute_input":"2022-02-26T08:34:14.783262Z","iopub.status.idle":"2022-02-26T08:34:21.970325Z","shell.execute_reply.started":"2022-02-26T08:34:14.783226Z","shell.execute_reply":"2022-02-26T08:34:21.968266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predict = (get_predict_proba(model_1,X_test) > thresold).astype(int)\nget_business_value(y_test,test_predict.proba)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T08:34:29.244191Z","iopub.execute_input":"2022-02-26T08:34:29.245154Z","iopub.status.idle":"2022-02-26T08:34:29.605207Z","shell.execute_reply.started":"2022-02-26T08:34:29.245097Z","shell.execute_reply":"2022-02-26T08:34:29.604044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}