{"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-13T21:29:50.301265Z","iopub.execute_input":"2022-03-13T21:29:50.302102Z","iopub.status.idle":"2022-03-13T21:29:52.353426Z","shell.execute_reply.started":"2022-03-13T21:29:50.301983Z","shell.execute_reply":"2022-03-13T21:29:52.352492Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center style=\"font-family:verdana;\"><h1 style=\"font-size:200%; padding: 10px; background: #6495ED;\"><b style=\"color:#00008B;\">FLAML AutoML</b></h1></center>\n\n\"FLAML is a lightweight Python library that finds accurate machine learning models automatically, efficiently and economically. It frees users from selecting learners and hyperparameters for each learner.\"\n\n\"For common machine learning tasks like classification and regression, it quickly finds quality models for user-provided data with low computational resources. It supports both classifcal machine learning models and deep neural networks.\nIt is easy to customize or extend. Users can find their desired customizability from a smooth range: minimal customization (computational resource budget), medium customization (e.g., scikit-style learner, search space and metric), or full customization (arbitrary training and evaluation code).\"\n\n\"It supports fast automatic tuning, capable of handling complex constraints/guidance/early stopping. FLAML is powered by a new, cost-effective hyperparameter optimization and learner selection method invented by Microsoft Research.\nFLAML has a .NET implementation as well from ML.NET Model Builder in Visual Studio 2022. This ML.NET blog describes the improvement brought by FLAML.\"\n\nhttps://github.com/microsoft/FLAML","metadata":{}},{"cell_type":"markdown","source":"![](https://i0.wp.com/thecleverprogrammer.com/wp-content/uploads/2021/10/FLAML.png?fit=1920%2C1080&ssl=1)thecleverprogrammer","metadata":{}},{"cell_type":"markdown","source":"#It's not a TUTORIAL since it's my 1st time dealing with FLAML. I've just copied the script to check how it works.","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/football-match-probability-prediction/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/football-match-probability-prediction/test.csv\")\nsub = pd.read_csv(\"/kaggle/input/football-match-probability-prediction/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-13T21:31:36.503317Z","iopub.execute_input":"2022-03-13T21:31:36.503635Z","iopub.status.idle":"2022-03-13T21:31:45.275117Z","shell.execute_reply.started":"2022-03-13T21:31:36.503603Z","shell.execute_reply":"2022-03-13T21:31:45.274137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pd.set_option('display.max_columns', None)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T21:31:55.365735Z","iopub.execute_input":"2022-03-13T21:31:55.366004Z","iopub.status.idle":"2022-03-13T21:31:55.407021Z","shell.execute_reply.started":"2022-03-13T21:31:55.365976Z","shell.execute_reply":"2022-03-13T21:31:55.406024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install flaml\nfrom sklearn.metrics import mean_absolute_percentage_error","metadata":{"execution":{"iopub.status.busy":"2022-03-13T21:33:03.692125Z","iopub.execute_input":"2022-03-13T21:33:03.692480Z","iopub.status.idle":"2022-03-13T21:33:26.720188Z","shell.execute_reply.started":"2022-03-13T21:33:03.692447Z","shell.execute_reply":"2022-03-13T21:33:26.719250Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"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":{"execution":{"iopub.status.busy":"2022-03-13T21:33:53.381400Z","iopub.execute_input":"2022-03-13T21:33:53.381690Z","iopub.status.idle":"2022-03-13T21:33:53.630170Z","shell.execute_reply.started":"2022-03-13T21:33:53.381661Z","shell.execute_reply":"2022-03-13T21:33:53.629086Z"},"_kg_hide-output":true,"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-13T21:34:15.817556Z","iopub.execute_input":"2022-03-13T21:34:15.817843Z","iopub.status.idle":"2022-03-13T21:34:16.004388Z","shell.execute_reply.started":"2022-03-13T21:34:15.817814Z","shell.execute_reply":"2022-03-13T21:34:16.003478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[categorical_nan].isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T21:34:32.898053Z","iopub.execute_input":"2022-03-13T21:34:32.898755Z","iopub.status.idle":"2022-03-13T21:34:33.058478Z","shell.execute_reply.started":"2022-03-13T21:34:32.898706Z","shell.execute_reply":"2022-03-13T21:34:33.057783Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"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":{"execution":{"iopub.status.busy":"2022-03-13T21:35:05.738482Z","iopub.execute_input":"2022-03-13T21:35:05.739072Z","iopub.status.idle":"2022-03-13T21:35:05.951871Z","shell.execute_reply.started":"2022-03-13T21:35:05.739025Z","shell.execute_reply":"2022-03-13T21:35:05.950791Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[numerical_nan].isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T21:35:40.977824Z","iopub.execute_input":"2022-03-13T21:35:40.978099Z","iopub.status.idle":"2022-03-13T21:35:41.087880Z","shell.execute_reply.started":"2022-03-13T21:35:40.978070Z","shell.execute_reply":"2022-03-13T21:35:41.087032Z"},"_kg_hide-output":true,"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":{"execution":{"iopub.status.busy":"2022-03-13T21:36:07.294712Z","iopub.execute_input":"2022-03-13T21:36:07.295030Z","iopub.status.idle":"2022-03-13T21:36:07.681111Z","shell.execute_reply.started":"2022-03-13T21:36:07.295001Z","shell.execute_reply":"2022-03-13T21:36:07.680328Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Label Encoding","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nlbl= LabelEncoder()\n\ny= train.iloc[:,-1:]\ny= lbl.fit_transform(y)\ny","metadata":{"execution":{"iopub.status.busy":"2022-03-13T21:36:53.355718Z","iopub.execute_input":"2022-03-13T21:36:53.356003Z","iopub.status.idle":"2022-03-13T21:36:53.376104Z","shell.execute_reply.started":"2022-03-13T21:36:53.355970Z","shell.execute_reply":"2022-03-13T21:36:53.375521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<iframe width=\"942\" height=\"530\" src=\"https://www.youtube.com/embed/5uCPo6p97Pw\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>","metadata":{}},{"cell_type":"markdown","source":"#Que Bonito É (Na Cadencia do Samba)\n\nPelé´s goal against Argentina. 1970 preparation for World Cup. Football nostalgia. XX Century. ","metadata":{}},{"cell_type":"code","source":"#Code by Gaurav Dutta https://www.kaggle.com/gauravduttakiit/rice-msc-flaml-log-loss\n\nfrom sklearn.model_selection import train_test_split\ntrain, test = train_test_split( train, test_size=0.2, random_state=42,shuffle=True, stratify=None)#None is default\ntrain.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-13T21:41:39.672785Z","iopub.execute_input":"2022-03-13T21:41:39.673079Z","iopub.status.idle":"2022-03-13T21:41:39.990524Z","shell.execute_reply.started":"2022-03-13T21:41:39.673039Z","shell.execute_reply":"2022-03-13T21:41:39.989481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Gaurav Dutta https://www.kaggle.com/gauravduttakiit/rice-msc-flaml-log-loss\n\nfrom flaml import AutoML\nautoml = AutoML()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T21:42:47.311880Z","iopub.execute_input":"2022-03-13T21:42:47.312207Z","iopub.status.idle":"2022-03-13T21:42:48.919741Z","shell.execute_reply.started":"2022-03-13T21:42:47.312169Z","shell.execute_reply":"2022-03-13T21:42:48.918873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train.pop('target')\nX = train","metadata":{"execution":{"iopub.status.busy":"2022-03-13T21:45:30.684868Z","iopub.execute_input":"2022-03-13T21:45:30.685186Z","iopub.status.idle":"2022-03-13T21:45:30.691097Z","shell.execute_reply.started":"2022-03-13T21:45:30.685151Z","shell.execute_reply":"2022-03-13T21:45:30.690231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Gaurav Dutta https://www.kaggle.com/gauravduttakiit/rice-msc-flaml-log-loss\n\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42,shuffle=True, stratify=y)","metadata":{"execution":{"iopub.status.busy":"2022-03-13T21:46:38.375802Z","iopub.execute_input":"2022-03-13T21:46:38.376116Z","iopub.status.idle":"2022-03-13T21:46:38.925820Z","shell.execute_reply.started":"2022-03-13T21:46:38.376086Z","shell.execute_reply":"2022-03-13T21:46:38.924491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Gaurav Dutta https://www.kaggle.com/gauravduttakiit/rice-msc-flaml-log-loss\n\nautoml.fit(X_train, y_train, task=\"classification\",metric='log_loss',time_budget=900)","metadata":{"execution":{"iopub.status.busy":"2022-03-13T21:48:33.480703Z","iopub.execute_input":"2022-03-13T21:48:33.481025Z","iopub.status.idle":"2022-03-13T22:04:14.437030Z","shell.execute_reply.started":"2022-03-13T21:48:33.480988Z","shell.execute_reply":"2022-03-13T22:04:14.435974Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Gaurav Dutta https://www.kaggle.com/gauravduttakiit/rice-msc-flaml-log-loss\n\nprint('Best ML leaner:', automl.best_estimator)\nprint('Best hyperparmeter config:', automl.best_config)\nprint('Best log_loss on validation data: {0:.4g}'.format(automl.best_loss))\nprint('Training duration of best run: {0:.4g} s'.format(automl.best_config_train_time))","metadata":{"execution":{"iopub.status.busy":"2022-03-13T22:06:03.762695Z","iopub.execute_input":"2022-03-13T22:06:03.762994Z","iopub.status.idle":"2022-03-13T22:06:03.769693Z","shell.execute_reply.started":"2022-03-13T22:06:03.762960Z","shell.execute_reply":"2022-03-13T22:06:03.768687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Gaurav Dutta https://www.kaggle.com/gauravduttakiit/rice-msc-flaml-log-loss\n\nfrom sklearn.metrics import classification_report\nprint(classification_report(y_train, automl.predict(X_train)))","metadata":{"execution":{"iopub.status.busy":"2022-03-13T22:06:08.900317Z","iopub.execute_input":"2022-03-13T22:06:08.900999Z","iopub.status.idle":"2022-03-13T22:06:14.304862Z","shell.execute_reply.started":"2022-03-13T22:06:08.900959Z","shell.execute_reply":"2022-03-13T22:06:14.303866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Gaurav Dutta https://www.kaggle.com/gauravduttakiit/rice-msc-flaml-log-loss\n\nprint(classification_report(y_test, automl.predict(X_test)))","metadata":{"execution":{"iopub.status.busy":"2022-03-13T22:06:26.268709Z","iopub.execute_input":"2022-03-13T22:06:26.269145Z","iopub.status.idle":"2022-03-13T22:06:27.697835Z","shell.execute_reply.started":"2022-03-13T22:06:26.269110Z","shell.execute_reply":"2022-03-13T22:06:27.696910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Gaurav Dutta https://www.kaggle.com/gauravduttakiit/rice-msc-flaml-log-loss\n\ntest_=test.drop('target',axis=1)\ntest_.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T22:06:34.936167Z","iopub.execute_input":"2022-03-13T22:06:34.936931Z","iopub.status.idle":"2022-03-13T22:06:34.983581Z","shell.execute_reply.started":"2022-03-13T22:06:34.936887Z","shell.execute_reply":"2022-03-13T22:06:34.982665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Gaurav Dutta https://www.kaggle.com/gauravduttakiit/rice-msc-flaml-log-loss\n\ny_pred = automl.predict(test_)\ny_pred[:5]","metadata":{"execution":{"iopub.status.busy":"2022-03-13T22:06:41.564928Z","iopub.execute_input":"2022-03-13T22:06:41.565235Z","iopub.status.idle":"2022-03-13T22:06:42.694864Z","shell.execute_reply.started":"2022-03-13T22:06:41.565198Z","shell.execute_reply":"2022-03-13T22:06:42.694269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Gaurav Dutta https://www.kaggle.com/gauravduttakiit/rice-msc-flaml-log-loss\n\n#Why df?\n\ndf = pd.DataFrame(y_pred,columns=['target'])\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T22:06:50.123277Z","iopub.execute_input":"2022-03-13T22:06:50.124010Z","iopub.status.idle":"2022-03-13T22:06:50.133363Z","shell.execute_reply.started":"2022-03-13T22:06:50.123972Z","shell.execute_reply":"2022-03-13T22:06:50.132418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Df resulted in Home and Away!","metadata":{}},{"cell_type":"markdown","source":"![](https://pics.me.me/still-more-intersting-than-watching-home-away-anyone-watch-14233727.png)https://me.me/i/still-more-intersting-than-watching-home-away-anyone-watch-9693073","metadata":{}},{"cell_type":"code","source":"#Code by Gaurav Dutta https://www.kaggle.com/gauravduttakiit/rice-msc-flaml-log-loss\n\nprint(classification_report(test.target, df.target))","metadata":{"execution":{"iopub.status.busy":"2022-03-13T22:06:56.943620Z","iopub.execute_input":"2022-03-13T22:06:56.943897Z","iopub.status.idle":"2022-03-13T22:06:57.703442Z","shell.execute_reply.started":"2022-03-13T22:06:56.943859Z","shell.execute_reply":"2022-03-13T22:06:57.702812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Acknowledgement:\n\nGaurav Dutta https://www.kaggle.com/gauravduttakiit/rice-msc-flaml-log-loss","metadata":{}}]}