{"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)\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":"2023-01-08T21:28:50.244310Z","iopub.execute_input":"2023-01-08T21:28:50.244810Z","iopub.status.idle":"2023-01-08T21:28:50.256403Z","shell.execute_reply.started":"2023-01-08T21:28:50.244773Z","shell.execute_reply":"2023-01-08T21:28:50.255033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:05:44.824513Z","iopub.execute_input":"2023-01-08T21:05:44.824933Z","iopub.status.idle":"2023-01-08T21:05:44.830903Z","shell.execute_reply.started":"2023-01-08T21:05:44.824900Z","shell.execute_reply":"2023-01-08T21:05:44.829347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train= pd.read_csv(\"/kaggle/input/emsi-5iir-ds/train.csv\")\ndf_test= pd.read_csv(\"/kaggle/input/emsi-5iir-ds/test.csv\")\ndf_subm= pd.read_csv(\"/kaggle/input/emsi-5iir-ds/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:05:56.486469Z","iopub.execute_input":"2023-01-08T21:05:56.486920Z","iopub.status.idle":"2023-01-08T21:06:04.626699Z","shell.execute_reply.started":"2023-01-08T21:05:56.486883Z","shell.execute_reply":"2023-01-08T21:06:04.625545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:06:17.017740Z","iopub.execute_input":"2023-01-08T21:06:17.018213Z","iopub.status.idle":"2023-01-08T21:06:17.045695Z","shell.execute_reply.started":"2023-01-08T21:06:17.018151Z","shell.execute_reply":"2023-01-08T21:06:17.044505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:06:28.628892Z","iopub.execute_input":"2023-01-08T21:06:28.629713Z","iopub.status.idle":"2023-01-08T21:06:29.760395Z","shell.execute_reply.started":"2023-01-08T21:06:28.629670Z","shell.execute_reply":"2023-01-08T21:06:29.759103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe(include=['object'])","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:06:36.701135Z","iopub.execute_input":"2023-01-08T21:06:36.701592Z","iopub.status.idle":"2023-01-08T21:06:37.555768Z","shell.execute_reply.started":"2023-01-08T21:06:36.701556Z","shell.execute_reply":"2023-01-08T21:06:37.554646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:06:45.931243Z","iopub.execute_input":"2023-01-08T21:06:45.931625Z","iopub.status.idle":"2023-01-08T21:06:46.021580Z","shell.execute_reply.started":"2023-01-08T21:06:45.931595Z","shell.execute_reply":"2023-01-08T21:06:46.020374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.isna().sum().any()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:06:50.902476Z","iopub.execute_input":"2023-01-08T21:06:50.902864Z","iopub.status.idle":"2023-01-08T21:06:50.977784Z","shell.execute_reply.started":"2023-01-08T21:06:50.902833Z","shell.execute_reply":"2023-01-08T21:06:50.976651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_class = pd.DataFrame({'count': df_train.target.value_counts(),\n                             'percentage': df_train['target'].value_counts() / df_train.shape[0] * 100\n})","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:07:07.969922Z","iopub.execute_input":"2023-01-08T21:07:07.970366Z","iopub.status.idle":"2023-01-08T21:07:07.993837Z","shell.execute_reply.started":"2023-01-08T21:07:07.970330Z","shell.execute_reply":"2023-01-08T21:07:07.992692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_class","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:07:11.426480Z","iopub.execute_input":"2023-01-08T21:07:11.426897Z","iopub.status.idle":"2023-01-08T21:07:11.437228Z","shell.execute_reply.started":"2023-01-08T21:07:11.426862Z","shell.execute_reply":"2023-01-08T21:07:11.435990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef feature_27(data):\n    # This function was adapted from the following notebooks:\n    # https://www.kaggle.com/code/paultimothymooney/getting-started-with-tensorflow-decision-forests\n    for df in [data]:\n        # Extract the 10 letters of f_27 into individual features\n        for i in range(10):   # defines a number from 0 to 25 for each letter, for example if ch1 is A then get 0 and so on\n            df[f'ch{i}'] = df.f_27.str.get(i).apply(ord) - ord('A')\n            \n        \n  \n    return data","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:07:40.922196Z","iopub.execute_input":"2023-01-08T21:07:40.922671Z","iopub.status.idle":"2023-01-08T21:07:40.929319Z","shell.execute_reply.started":"2023-01-08T21:07:40.922634Z","shell.execute_reply":"2023-01-08T21:07:40.928207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = feature_27(df_train)\ntest_df = feature_27(df_test)\ntrain_df.columns","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:07:47.324750Z","iopub.execute_input":"2023-01-08T21:07:47.325200Z","iopub.status.idle":"2023-01-08T21:07:59.367449Z","shell.execute_reply.started":"2023-01-08T21:07:47.325147Z","shell.execute_reply":"2023-01-08T21:07:59.366332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[['f_27', 'ch0', 'ch1', 'ch2', 'ch3', 'ch4', 'ch5', 'ch6', 'ch7', 'ch8', 'ch9',]]","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:08:06.249294Z","iopub.execute_input":"2023-01-08T21:08:06.249732Z","iopub.status.idle":"2023-01-08T21:08:06.388506Z","shell.execute_reply.started":"2023-01-08T21:08:06.249698Z","shell.execute_reply":"2023-01-08T21:08:06.387237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.drop(columns=['id', 'f_27'])\ntest_df = test_df.drop(columns=['id', 'f_27'])","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:08:22.221663Z","iopub.execute_input":"2023-01-08T21:08:22.222107Z","iopub.status.idle":"2023-01-08T21:08:22.252788Z","shell.execute_reply.started":"2023-01-08T21:08:22.222058Z","shell.execute_reply":"2023-01-08T21:08:22.251451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:08:29.172921Z","iopub.execute_input":"2023-01-08T21:08:29.173343Z","iopub.status.idle":"2023-01-08T21:08:29.181524Z","shell.execute_reply.started":"2023-01-08T21:08:29.173306Z","shell.execute_reply":"2023-01-08T21:08:29.180058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.tabular.core import cont_cat_split\ncont_col, cat_col = cont_cat_split(train_df)\nprint(f'Continuous Columns: {cont_col}\\n')\nprint(f'Categorical Columns: {cat_col}')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:08:33.399404Z","iopub.execute_input":"2023-01-08T21:08:33.399847Z","iopub.status.idle":"2023-01-08T21:08:33.539110Z","shell.execute_reply.started":"2023-01-08T21:08:33.399810Z","shell.execute_reply":"2023-01-08T21:08:33.537972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:08:39.559010Z","iopub.execute_input":"2023-01-08T21:08:39.559444Z","iopub.status.idle":"2023-01-08T21:08:39.564158Z","shell.execute_reply.started":"2023-01-08T21:08:39.559408Z","shell.execute_reply":"2023-01-08T21:08:39.563270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(abs(train_df[cont_col].corr()))","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:08:44.748177Z","iopub.execute_input":"2023-01-08T21:08:44.748619Z","iopub.status.idle":"2023-01-08T21:08:45.942396Z","shell.execute_reply.started":"2023-01-08T21:08:44.748581Z","shell.execute_reply":"2023-01-08T21:08:45.941199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(abs(train_df[cat_col].corr()))","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:08:51.413281Z","iopub.execute_input":"2023-01-08T21:08:51.413707Z","iopub.status.idle":"2023-01-08T21:08:53.899733Z","shell.execute_reply.started":"2023-01-08T21:08:51.413672Z","shell.execute_reply":"2023-01-08T21:08:53.898637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f_cat_col = ['f_07', 'f_08', 'f_09', 'f_10', 'f_11', 'f_12', 'f_13', 'f_14', 'f_15', 'f_16', 'f_17', 'f_18', ]\nfor column in f_cat_col:    # para cada variavel na lista\n    percent = train_df[column].value_counts(normalize=True) * 100    # calculates the relative frequency of unique values in percentage\n    display(percent)\n    print('\\n')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:09:01.881807Z","iopub.execute_input":"2023-01-08T21:09:01.882250Z","iopub.status.idle":"2023-01-08T21:09:02.060772Z","shell.execute_reply.started":"2023-01-08T21:09:01.882218Z","shell.execute_reply":"2023-01-08T21:09:02.059602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['f_07'] = train_df['f_07'].replace([5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 5)\ntrain_df['f_08'] = train_df['f_08'].replace([5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16], 5)\ntrain_df['f_09'] = train_df['f_09'].replace([5, 6, 7, 8, 9, 10, 11, 12, 13, 14], 5)\ntrain_df['f_10'] = train_df['f_10'].replace([5, 6, 7, 8, 9, 10, 11, 12, 13, 14], 5)\ntrain_df['f_11'] = train_df['f_11'].replace([4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 4)\ntrain_df['f_12'] = train_df['f_12'].replace([6, 7, 8, 9, 10, 11, 12, 13, 14, 16], 6)\ntrain_df['f_13'] = train_df['f_13'].replace([5, 6, 7, 8, 9, 10, 11, 12], 5)\ntrain_df['f_14'] = train_df['f_14'].replace([4, 5, 6, 7, 8, 9, 10, 11, 12, 14], 4)\ntrain_df['f_15'] = train_df['f_15'].replace([5, 6, 7, 8, 9, 10, 11, 12, 13, 14], 5)\ntrain_df['f_16'] = train_df['f_16'].replace([5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 5)\ntrain_df['f_17'] = train_df['f_17'].replace([4, 5, 6, 7, 8, 9, 10, 11, 12, 14], 4)\ntrain_df['f_18'] = train_df['f_15'].replace([5, 6, 7, 8, 9, 10, 11, 12, 13], 5)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:09:09.526856Z","iopub.execute_input":"2023-01-08T21:09:09.527304Z","iopub.status.idle":"2023-01-08T21:09:09.762557Z","shell.execute_reply.started":"2023-01-08T21:09:09.527268Z","shell.execute_reply":"2023-01-08T21:09:09.761407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for column in f_cat_col:\n    percent = train_df[column].value_counts(normalize=True) * 100\n    display(percent)\n    print('\\n')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:09:17.337324Z","iopub.execute_input":"2023-01-08T21:09:17.337725Z","iopub.status.idle":"2023-01-08T21:09:17.499098Z","shell.execute_reply.started":"2023-01-08T21:09:17.337694Z","shell.execute_reply":"2023-01-08T21:09:17.498008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:09:24.271676Z","iopub.execute_input":"2023-01-08T21:09:24.272131Z","iopub.status.idle":"2023-01-08T21:09:24.277862Z","shell.execute_reply.started":"2023-01-08T21:09:24.272093Z","shell.execute_reply":"2023-01-08T21:09:24.276586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_df.drop(columns=['target'])    # input\ny = train_df.target    # output\n\n# split the data into training and testing\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)\n\nprint(f'X_train shape: {X_train.shape}')\nprint(f'y_train shape: {y_train.shape}')\nprint(f'-' * 27)\nprint(f'X_test shape: {X_test.shape}')\nprint(f'y_test shape: {y_test.shape}')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:09:30.371553Z","iopub.execute_input":"2023-01-08T21:09:30.371967Z","iopub.status.idle":"2023-01-08T21:09:31.181030Z","shell.execute_reply.started":"2023-01-08T21:09:30.371935Z","shell.execute_reply":"2023-01-08T21:09:31.179759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tps_model = RandomForestClassifier(random_state=0)\ntps_model.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:09:39.241253Z","iopub.execute_input":"2023-01-08T21:09:39.241716Z","iopub.status.idle":"2023-01-08T21:18:03.379448Z","shell.execute_reply.started":"2023-01-08T21:09:39.241680Z","shell.execute_reply":"2023-01-08T21:18:03.378218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from yellowbrick.classifier import confusion_matrix\nfrom yellowbrick.classifier.rocauc import roc_auc","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:18:15.583504Z","iopub.execute_input":"2023-01-08T21:18:15.584806Z","iopub.status.idle":"2023-01-08T21:18:15.590133Z","shell.execute_reply.started":"2023-01-08T21:18:15.584753Z","shell.execute_reply":"2023-01-08T21:18:15.588884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6, 6))\nconfusion_matrix(tps_model, X_train, y_train, X_test, y_test, percent=True)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:18:20.061589Z","iopub.execute_input":"2023-01-08T21:18:20.061991Z","iopub.status.idle":"2023-01-08T21:18:47.000023Z","shell.execute_reply.started":"2023-01-08T21:18:20.061959Z","shell.execute_reply":"2023-01-08T21:18:46.998484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6, 6))\nroc_auc(tps_model, X_train, y_train, X_test=X_test, y_test=y_test)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:19:30.734277Z","iopub.execute_input":"2023-01-08T21:19:30.735388Z","iopub.status.idle":"2023-01-08T21:19:57.246152Z","shell.execute_reply.started":"2023-01-08T21:19:30.735346Z","shell.execute_reply":"2023-01-08T21:19:57.244985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_df = pd.read_csv('/kaggle/input/emsi-5iir-ds/sample_submission.csv')\nsample_submission_df['target'] = tps_model.predict(test_df)\nsample_submission_df.to_csv('/kaggle/working/submission.csv', index=False)\n\n\nsample_submission_df","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:20:06.804020Z","iopub.execute_input":"2023-01-08T21:20:06.804548Z","iopub.status.idle":"2023-01-08T21:20:41.721865Z","shell.execute_reply.started":"2023-01-08T21:20:06.804510Z","shell.execute_reply":"2023-01-08T21:20:41.720633Z"},"trusted":true},"execution_count":null,"outputs":[]}]}