{"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":"2022-07-16T19:41:33.733805Z","iopub.execute_input":"2022-07-16T19:41:33.734650Z","iopub.status.idle":"2022-07-16T19:41:33.763554Z","shell.execute_reply.started":"2022-07-16T19:41:33.734534Z","shell.execute_reply":"2022-07-16T19:41:33.762560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import dask.dataframe as dd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2022-07-16T19:42:43.429451Z","iopub.execute_input":"2022-07-16T19:42:43.429861Z","iopub.status.idle":"2022-07-16T19:42:44.671545Z","shell.execute_reply.started":"2022-07-16T19:42:43.429827Z","shell.execute_reply":"2022-07-16T19:42:44.670463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = dd.read_csv(\"/kaggle/input/amex-default-prediction/sample_submission.csv\").compute()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:05:31.423702Z","iopub.execute_input":"2022-07-16T20:05:31.424132Z","iopub.status.idle":"2022-07-16T20:05:34.108469Z","shell.execute_reply.started":"2022-07-16T20:05:31.424100Z","shell.execute_reply":"2022-07-16T20:05:34.107339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = ['customer_ID','B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_68']","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:05:36.278764Z","iopub.execute_input":"2022-07-16T20:05:36.279135Z","iopub.status.idle":"2022-07-16T20:05:36.284477Z","shell.execute_reply.started":"2022-07-16T20:05:36.279105Z","shell.execute_reply":"2022-07-16T20:05:36.283305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"treino = dd.read_csv('/kaggle/input/amex-default-prediction/train_data.csv', usecols=cols)\ntreino_df = treino.compute()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:05:39.264615Z","iopub.execute_input":"2022-07-16T20:05:39.264976Z","iopub.status.idle":"2022-07-16T20:07:30.007199Z","shell.execute_reply.started":"2022-07-16T20:05:39.264946Z","shell.execute_reply":"2022-07-16T20:07:30.005899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lab = pd.read_csv('/kaggle/input/amex-default-prediction/train_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:07:39.512290Z","iopub.execute_input":"2022-07-16T20:07:39.514405Z","iopub.status.idle":"2022-07-16T20:07:40.517835Z","shell.execute_reply.started":"2022-07-16T20:07:39.514347Z","shell.execute_reply":"2022-07-16T20:07:40.516651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"treino_df2 = treino_df.merge(lab, left_on='customer_ID', right_on='customer_ID')\ntreino_df2.drop_duplicates(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:07:43.899682Z","iopub.execute_input":"2022-07-16T20:07:43.900083Z","iopub.status.idle":"2022-07-16T20:07:49.686800Z","shell.execute_reply.started":"2022-07-16T20:07:43.900052Z","shell.execute_reply":"2022-07-16T20:07:49.685197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = treino_df2[['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_68']].values\ny = treino_df2[['target']].values\nX_train, X_test, y_train, y_test = train_test_split(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:07:55.389498Z","iopub.execute_input":"2022-07-16T20:07:55.390419Z","iopub.status.idle":"2022-07-16T20:07:55.874289Z","shell.execute_reply.started":"2022-07-16T20:07:55.390378Z","shell.execute_reply":"2022-07-16T20:07:55.873093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imp_most = SimpleImputer(missing_values=np.nan,strategy='most_frequent')\nimp_most.fit(X_train)\nX_train = imp_most.transform(X_train)\nX_test = imp_most.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:08:04.930614Z","iopub.execute_input":"2022-07-16T20:08:04.930992Z","iopub.status.idle":"2022-07-16T20:08:05.656174Z","shell.execute_reply.started":"2022-07-16T20:08:04.930960Z","shell.execute_reply":"2022-07-16T20:08:05.655083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelo = DecisionTreeClassifier()\nmodelo.fit(X_train,y_train)\nprevisao = modelo.predict(X_test)\nprint(classification_report(y_test,previsao))","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:08:07.870445Z","iopub.execute_input":"2022-07-16T20:08:07.871436Z","iopub.status.idle":"2022-07-16T20:08:12.324901Z","shell.execute_reply.started":"2022-07-16T20:08:07.871383Z","shell.execute_reply":"2022-07-16T20:08:12.323422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importancia = modelo.feature_importances_","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:08:19.431710Z","iopub.execute_input":"2022-07-16T20:08:19.432112Z","iopub.status.idle":"2022-07-16T20:08:19.437896Z","shell.execute_reply.started":"2022-07-16T20:08:19.432078Z","shell.execute_reply":"2022-07-16T20:08:19.436891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance_df = pd.DataFrame({\"Feature\":['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_68'],\"Importance\":importancia})","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:08:23.852884Z","iopub.execute_input":"2022-07-16T20:08:23.853293Z","iopub.status.idle":"2022-07-16T20:08:23.860321Z","shell.execute_reply.started":"2022-07-16T20:08:23.853257Z","shell.execute_reply":"2022-07-16T20:08:23.859410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance_df.plot.barh(x='Feature',y='Importance')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:08:26.569235Z","iopub.execute_input":"2022-07-16T20:08:26.570242Z","iopub.status.idle":"2022-07-16T20:08:26.807267Z","shell.execute_reply.started":"2022-07-16T20:08:26.570174Z","shell.execute_reply":"2022-07-16T20:08:26.806127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols_select = ['customer_ID','B_30', 'B_38', 'D_114', 'D_117', 'D_120', 'D_68']","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:08:59.791478Z","iopub.execute_input":"2022-07-16T20:08:59.791900Z","iopub.status.idle":"2022-07-16T20:08:59.797493Z","shell.execute_reply.started":"2022-07-16T20:08:59.791867Z","shell.execute_reply":"2022-07-16T20:08:59.796645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = treino_df2[['B_30', 'B_38', 'D_114', 'D_117', 'D_120', 'D_68']].values\ny = treino_df2[['target']].values\nX_train, X_test, y_train, y_test = train_test_split(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:09:45.940504Z","iopub.execute_input":"2022-07-16T20:09:45.941053Z","iopub.status.idle":"2022-07-16T20:09:46.414417Z","shell.execute_reply.started":"2022-07-16T20:09:45.941007Z","shell.execute_reply":"2022-07-16T20:09:46.412575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imp_most = SimpleImputer(missing_values=np.nan,strategy='most_frequent')\nimp_most.fit(X_train)\nX_train = imp_most.transform(X_train)\nX_test = imp_most.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:09:57.473217Z","iopub.execute_input":"2022-07-16T20:09:57.473640Z","iopub.status.idle":"2022-07-16T20:09:58.024716Z","shell.execute_reply.started":"2022-07-16T20:09:57.473606Z","shell.execute_reply":"2022-07-16T20:09:58.023581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelo = DecisionTreeClassifier()\nmodelo.fit(X_train,y_train)\nprevisao = modelo.predict(X_test)\nprint(classification_report(y_test,previsao))","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:10:06.398388Z","iopub.execute_input":"2022-07-16T20:10:06.398835Z","iopub.status.idle":"2022-07-16T20:10:10.461465Z","shell.execute_reply.started":"2022-07-16T20:10:06.398796Z","shell.execute_reply":"2022-07-16T20:10:10.460306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del treino_df\ndel treino_df2","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:11:03.600419Z","iopub.execute_input":"2022-07-16T20:11:03.601460Z","iopub.status.idle":"2022-07-16T20:11:03.637173Z","shell.execute_reply.started":"2022-07-16T20:11:03.601418Z","shell.execute_reply":"2022-07-16T20:11:03.635909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"teste = dd.read_csv('/kaggle/input/amex-default-prediction/test_data.csv', usecols=cols_select)\nteste.drop_duplicates(inplace=True)\nteste_df = teste.compute()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:11:06.582361Z","iopub.execute_input":"2022-07-16T20:11:06.582778Z","iopub.status.idle":"2022-07-16T20:14:26.230973Z","shell.execute_reply.started":"2022-07-16T20:11:06.582741Z","shell.execute_reply":"2022-07-16T20:14:26.229797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"teste","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:20:11.661751Z","iopub.execute_input":"2022-07-16T20:20:11.662128Z","iopub.status.idle":"2022-07-16T20:20:11.686697Z","shell.execute_reply.started":"2022-07-16T20:20:11.662097Z","shell.execute_reply":"2022-07-16T20:20:11.685657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"teste_id = teste[['customer_ID']].compute()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:22:08.510277Z","iopub.execute_input":"2022-07-16T20:22:08.510714Z","iopub.status.idle":"2022-07-16T20:25:25.258259Z","shell.execute_reply.started":"2022-07-16T20:22:08.510677Z","shell.execute_reply":"2022-07-16T20:25:25.257041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"teste_df = imp_most.fit_transform(teste_df)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:14:34.946716Z","iopub.execute_input":"2022-07-16T20:14:34.947360Z","iopub.status.idle":"2022-07-16T20:15:21.213304Z","shell.execute_reply.started":"2022-07-16T20:14:34.947321Z","shell.execute_reply":"2022-07-16T20:15:21.212309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_teste = teste_df[:,1:]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:15:27.799764Z","iopub.execute_input":"2022-07-16T20:15:27.800145Z","iopub.status.idle":"2022-07-16T20:15:27.805643Z","shell.execute_reply.started":"2022-07-16T20:15:27.800114Z","shell.execute_reply":"2022-07-16T20:15:27.804693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_teste","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:15:29.140977Z","iopub.execute_input":"2022-07-16T20:15:29.141920Z","iopub.status.idle":"2022-07-16T20:15:29.149191Z","shell.execute_reply.started":"2022-07-16T20:15:29.141876Z","shell.execute_reply":"2022-07-16T20:15:29.147760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"previsao_final = modelo.predict_proba(X_teste)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:15:32.272229Z","iopub.execute_input":"2022-07-16T20:15:32.272656Z","iopub.status.idle":"2022-07-16T20:15:38.215982Z","shell.execute_reply.started":"2022-07-16T20:15:32.272584Z","shell.execute_reply":"2022-07-16T20:15:38.214755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"previsao_final[:,0]","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:16:51.726694Z","iopub.execute_input":"2022-07-16T20:16:51.727090Z","iopub.status.idle":"2022-07-16T20:16:51.734925Z","shell.execute_reply.started":"2022-07-16T20:16:51.727056Z","shell.execute_reply":"2022-07-16T20:16:51.733776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"teste_id['prediction'] = previsao_final[:,0]\nteste_id","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:26:15.052908Z","iopub.execute_input":"2022-07-16T20:26:15.053312Z","iopub.status.idle":"2022-07-16T20:26:15.171167Z","shell.execute_reply.started":"2022-07-16T20:26:15.053279Z","shell.execute_reply":"2022-07-16T20:26:15.170042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame()\nsub['customer_ID']= sample_sub['customer_ID']","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:16:05.676446Z","iopub.execute_input":"2022-07-16T20:16:05.676934Z","iopub.status.idle":"2022-07-16T20:16:06.006682Z","shell.execute_reply.started":"2022-07-16T20:16:05.676881Z","shell.execute_reply":"2022-07-16T20:16:06.005496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = dd.merge(teste_id, sub, on='customer_ID', how='right')","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:35:42.124705Z","iopub.execute_input":"2022-07-16T20:35:42.125084Z","iopub.status.idle":"2022-07-16T20:35:45.273753Z","shell.execute_reply.started":"2022-07-16T20:35:42.125056Z","shell.execute_reply":"2022-07-16T20:35:45.272643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission2 = submission.groupby(['customer_ID']).mean().reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:36:59.187122Z","iopub.execute_input":"2022-07-16T20:36:59.187506Z","iopub.status.idle":"2022-07-16T20:37:02.465738Z","shell.execute_reply.started":"2022-07-16T20:36:59.187476Z","shell.execute_reply":"2022-07-16T20:37:02.464454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission2","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:37:22.687933Z","iopub.execute_input":"2022-07-16T20:37:22.688873Z","iopub.status.idle":"2022-07-16T20:37:22.706239Z","shell.execute_reply.started":"2022-07-16T20:37:22.688803Z","shell.execute_reply":"2022-07-16T20:37:22.704633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission2.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T20:37:33.810791Z","iopub.execute_input":"2022-07-16T20:37:33.811245Z","iopub.status.idle":"2022-07-16T20:37:38.403626Z","shell.execute_reply.started":"2022-07-16T20:37:33.811209Z","shell.execute_reply":"2022-07-16T20:37:38.402642Z"},"trusted":true},"execution_count":null,"outputs":[]}]}