{"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":"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)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport plotly.express as px\nimport plotly.graph_objs as go\nimport plotly\n\n#Ignore warnings\nimport warnings\nwarnings.filterwarnings('ignore')\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-03-16T10:22:07.713958Z","iopub.execute_input":"2022-03-16T10:22:07.715149Z","iopub.status.idle":"2022-03-16T10:22:09.138637Z","shell.execute_reply.started":"2022-03-16T10:22:07.715050Z","shell.execute_reply":"2022-03-16T10:22:09.137769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/football-match-probability-prediction/train.csv\", index_col=0)\ntest = pd.read_csv(\"/kaggle/input/football-match-probability-prediction/test.csv\", index_col=0)\nsub = pd.read_csv(\"/kaggle/input/football-match-probability-prediction/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-16T10:22:12.945119Z","iopub.execute_input":"2022-03-16T10:22:12.945472Z","iopub.status.idle":"2022-03-16T10:22:24.116057Z","shell.execute_reply.started":"2022-03-16T10:22:12.945433Z","shell.execute_reply":"2022-03-16T10:22:24.114867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', None)\ntrain.sample(5) # everybody seems to take the head to examine the data, but that's not always representative, so sampling can sometimes identify issues that don't appear in the head","metadata":{"execution":{"iopub.status.busy":"2022-03-16T10:23:25.843149Z","iopub.execute_input":"2022-03-16T10:23:25.843479Z","iopub.status.idle":"2022-03-16T10:23:26.040317Z","shell.execute_reply.started":"2022-03-16T10:23:25.843446Z","shell.execute_reply":"2022-03-16T10:23:26.039205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install librosa --ignore-installed llvmlite","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-16T10:22:37.758341Z","iopub.execute_input":"2022-03-16T10:22:37.758677Z","iopub.status.idle":"2022-03-16T10:23:19.486582Z","shell.execute_reply.started":"2022-03-16T10:22:37.758640Z","shell.execute_reply":"2022-03-16T10:23:19.485420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install evalml","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-16T11:20:57.292212Z","iopub.execute_input":"2022-03-16T11:20:57.292645Z","iopub.status.idle":"2022-03-16T11:21:10.427272Z","shell.execute_reply.started":"2022-03-16T11:20:57.292604Z","shell.execute_reply":"2022-03-16T11:21:10.426477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# categorical features with missing values\ncategorical_nan = [feature for feature in train.columns if train[feature].isna().sum()>0 and train[feature].dtypes=='O']\nprint(categorical_nan)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-16T10:25:21.167452Z","iopub.execute_input":"2022-03-16T10:25:21.167860Z","iopub.status.idle":"2022-03-16T10:25:21.641704Z","shell.execute_reply.started":"2022-03-16T10:25:21.167817Z","shell.execute_reply":"2022-03-16T10:25:21.640474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# replacing missing values in categorical features\nfor feature in categorical_nan:\n    train[feature] = train[feature].fillna('None')","metadata":{"execution":{"iopub.status.busy":"2022-03-16T10:25:23.034747Z","iopub.execute_input":"2022-03-16T10:25:23.035061Z","iopub.status.idle":"2022-03-16T10:25:23.401304Z","shell.execute_reply.started":"2022-03-16T10:25:23.035028Z","shell.execute_reply":"2022-03-16T10:25:23.400279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[categorical_nan].isna().sum()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-16T10:25:25.797803Z","iopub.execute_input":"2022-03-16T10:25:25.798141Z","iopub.status.idle":"2022-03-16T10:25:26.146029Z","shell.execute_reply.started":"2022-03-16T10:25:25.798105Z","shell.execute_reply":"2022-03-16T10:25:26.145061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lets first handle numerical features with nan value\nnumerical_nan = [feature for feature in train.columns if train[feature].isna().sum()>1 and train[feature].dtypes!='O']\nnumerical_nan","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-16T10:25:32.616587Z","iopub.execute_input":"2022-03-16T10:25:32.617813Z","iopub.status.idle":"2022-03-16T10:25:33.038468Z","shell.execute_reply.started":"2022-03-16T10:25:32.617757Z","shell.execute_reply":"2022-03-16T10:25:33.037577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[numerical_nan].isna().sum()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-16T10:25:48.825567Z","iopub.execute_input":"2022-03-16T10:25:48.825933Z","iopub.status.idle":"2022-03-16T10:25:48.954002Z","shell.execute_reply.started":"2022-03-16T10:25:48.825894Z","shell.execute_reply":"2022-03-16T10:25:48.952811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Replacing the numerical Missing Values\n\nfor feature in numerical_nan:\n    ## We will replace by using median since there are outliers\n    median_value=train[feature].median()\n    \n    train[feature].fillna(median_value,inplace=True)\n    \ntrain[numerical_nan].isnull().sum()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-16T10:25:50.794834Z","iopub.execute_input":"2022-03-16T10:25:50.796136Z","iopub.status.idle":"2022-03-16T10:25:51.202466Z","shell.execute_reply.started":"2022-03-16T10:25:50.796079Z","shell.execute_reply":"2022-03-16T10:25:51.201382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Hamed Etezadi https://www.kaggle.com/hamedetezadi/heart-attack-risk-prediction-automl/notebook\n\n#Code by Peter Coles\n\nx = train.iloc[:,1:]\n#x= train.iloc[:,:-1]\nx","metadata":{"execution":{"iopub.status.busy":"2022-03-16T10:26:26.732417Z","iopub.execute_input":"2022-03-16T10:26:26.732786Z","iopub.status.idle":"2022-03-16T10:26:27.135161Z","shell.execute_reply.started":"2022-03-16T10:26:26.732751Z","shell.execute_reply":"2022-03-16T10:26:27.134014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nlbl= LabelEncoder()\n\n# y= train.iloc[:,-1:] (takes the last column which is just one of the features and not what we want to target)\ny = train[\"target\"]\ny = lbl.fit_transform(y)\ny","metadata":{"execution":{"iopub.status.busy":"2022-03-16T10:26:33.816140Z","iopub.execute_input":"2022-03-16T10:26:33.816487Z","iopub.status.idle":"2022-03-16T10:26:33.863768Z","shell.execute_reply.started":"2022-03-16T10:26:33.816454Z","shell.execute_reply":"2022-03-16T10:26:33.862786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This step is failing for me. Here's why ...\n#\n# evalml tries to import 'line_search_wolfe1' from 'sklearn.utils.fixes' and fails.\n# Tracing this back shows that sklearn tries to import and alias it from 'scipy.optimize._linesearch' (see below)\n#\n# try:\n#     from scipy.optimize._linesearch import line_search_wolfe2, line_search_wolfe1\n# except ImportError:  # SciPy < 1.8\n#     from scipy.optimize.linesearch import line_search_wolfe2, line_search_wolfe1  # type: ignore  # noqa\n#\n# and throws and exception if it can't, i.e. because SciPy is < 1.8\n#\n# but SciPy 1.8 requires Python >= 3.8\n#\n# and the Kaggle notebook launched for me was using Python 3.7.12, which is probably outside of our control.\n#\n# I've tried loading the pieces we want from evalml directly (rather than the whole package, e.g. the next cell),\n# but sadly the data checking and more crucially the pipeline searching stills fail. :(\n#\n# But puzzlingly, if I make a copy of it and run it, it works ... with the same version of python.\n\nimport evalml","metadata":{"execution":{"iopub.status.busy":"2022-03-16T11:21:12.346528Z","iopub.execute_input":"2022-03-16T11:21:12.347260Z","iopub.status.idle":"2022-03-16T11:21:12.418186Z","shell.execute_reply.started":"2022-03-16T11:21:12.347213Z","shell.execute_reply":"2022-03-16T11:21:12.416705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from evalml.preprocessing import split_data\nX_train, X_test, y_train, y_test = split_data(x, y, problem_type='regression')","metadata":{"execution":{"iopub.status.busy":"2022-03-16T10:38:07.044120Z","iopub.execute_input":"2022-03-16T10:38:07.044790Z","iopub.status.idle":"2022-03-16T10:38:22.762002Z","shell.execute_reply.started":"2022-03-16T10:38:07.044748Z","shell.execute_reply":"2022-03-16T10:38:22.760896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from evalml.problem_types import ProblemTypes\nProblemTypes.all_problem_types","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-16T10:38:50.236341Z","iopub.execute_input":"2022-03-16T10:38:50.236644Z","iopub.status.idle":"2022-03-16T10:38:50.243670Z","shell.execute_reply.started":"2022-03-16T10:38:50.236614Z","shell.execute_reply":"2022-03-16T10:38:50.242982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from evalml.data_checks import DefaultDataChecks\n\ndata_checks = DefaultDataChecks(\"regression\", \"log loss binary\")\ndata_checks.validate(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T10:48:14.858765Z","iopub.execute_input":"2022-03-16T10:48:14.860154Z","iopub.status.idle":"2022-03-16T10:48:14.927802Z","shell.execute_reply.started":"2022-03-16T10:48:14.860068Z","shell.execute_reply":"2022-03-16T10:48:14.926057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from evalml.automl import AutoMLSearch\nautoml = AutoMLSearch(X_train=X_train, y_train=y_train, problem_type='regression')\nautoml.search()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T10:39:56.635799Z","iopub.execute_input":"2022-03-16T10:39:56.636145Z","iopub.status.idle":"2022-03-16T10:39:56.685958Z","shell.execute_reply.started":"2022-03-16T10:39:56.636109Z","shell.execute_reply":"2022-03-16T10:39:56.683705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.utils.fixes import line_search_wolfe2\nfrom scipy.optimize.linesearch import line_search_wolfe1","metadata":{"execution":{"iopub.status.busy":"2022-03-16T10:48:02.098526Z","iopub.execute_input":"2022-03-16T10:48:02.099004Z","iopub.status.idle":"2022-03-16T10:48:02.106027Z","shell.execute_reply.started":"2022-03-16T10:48:02.098964Z","shell.execute_reply":"2022-03-16T10:48:02.104304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install scipy --upgrade","metadata":{"execution":{"iopub.status.busy":"2022-03-16T10:55:42.594028Z","iopub.execute_input":"2022-03-16T10:55:42.594379Z","iopub.status.idle":"2022-03-16T10:55:52.450138Z","shell.execute_reply.started":"2022-03-16T10:55:42.594341Z","shell.execute_reply":"2022-03-16T10:55:52.448878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import scipy\nprint(scipy.__version__)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T10:54:40.770963Z","iopub.execute_input":"2022-03-16T10:54:40.771749Z","iopub.status.idle":"2022-03-16T10:54:40.777045Z","shell.execute_reply.started":"2022-03-16T10:54:40.771695Z","shell.execute_reply":"2022-03-16T10:54:40.776309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"automl.rankings","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"automl.best_pipeline","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_pipeline=automl.best_pipeline","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"automl.describe_pipeline(automl.rankings.iloc[0][\"id\"])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_pipeline.score(X_test, y_test, objectives=[\"R2\",\"ExpVariance\",\"MSE\",\"MAE\"]) #original was auc\",\"f1\",\"Precision\",\"Recall","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"automl_logloss = AutoMLSearch(X_train=X_train, y_train=y_train,\n                          problem_type='regression',\n                          objective='R2', #there is no Log Loss\n                          additional_objectives=['MSE', 'MAE'],\n                          max_batches=1,\n                          optimize_thresholds=True)\n\nautoml_logloss.search()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"automl_logloss.rankings","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"automl_logloss.describe_pipeline(automl_logloss.rankings.iloc[0][\"id\"])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_pipeline_logloss = automl_logloss.best_pipeline","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get the score on holdout data\nbest_pipeline_logloss.score(X_test, y_test,  objectives=[\"R2\"])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Save the model","metadata":{}},{"cell_type":"code","source":"best_pipeline.save(\"model.pkl\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Loading the model","metadata":{}},{"cell_type":"code","source":"final_model=automl.load('model.pkl')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.predict(X_test)","metadata":{},"execution_count":null,"outputs":[]}]}