{"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":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport gc\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-05T18:28:51.979788Z","iopub.execute_input":"2022-10-05T18:28:51.980461Z","iopub.status.idle":"2022-10-05T18:28:53.294250Z","shell.execute_reply.started":"2022-10-05T18:28:51.980328Z","shell.execute_reply":"2022-10-05T18:28:53.292666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Training DataSet","metadata":{}},{"cell_type":"code","source":"train_dataset_ = pd.read_feather('../input/amexfeather/train_data.ftr')\n# Keep the latest statement features for each customer\ntrain_dataset = train_dataset_.groupby('customer_ID').tail(1).set_index('customer_ID', drop=True).sort_index()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:29:01.137743Z","iopub.execute_input":"2022-10-05T18:29:01.138194Z","iopub.status.idle":"2022-10-05T18:29:25.439943Z","shell.execute_reply.started":"2022-10-05T18:29:01.138161Z","shell.execute_reply":"2022-10-05T18:29:25.432414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The dataset of this competition has a huge size. If you're reading raw CSV files, It will create a out of memory error. That's why we read the data from AMEX-Feather-Dataset.","metadata":{}},{"cell_type":"code","source":"del train_dataset_\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:51:06.436529Z","iopub.execute_input":"2022-10-05T18:51:06.437769Z","iopub.status.idle":"2022-10-05T18:51:06.798641Z","shell.execute_reply.started":"2022-10-05T18:51:06.437707Z","shell.execute_reply":"2022-10-05T18:51:06.797168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:51:13.492865Z","iopub.execute_input":"2022-10-05T18:51:13.493324Z","iopub.status.idle":"2022-10-05T18:51:13.532712Z","shell.execute_reply.started":"2022-10-05T18:51:13.493287Z","shell.execute_reply":"2022-10-05T18:51:13.531183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.info(max_cols=191,show_counts=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:51:16.309015Z","iopub.execute_input":"2022-10-05T18:51:16.309511Z","iopub.status.idle":"2022-10-05T18:51:16.889968Z","shell.execute_reply.started":"2022-10-05T18:51:16.309471Z","shell.execute_reply":"2022-10-05T18:51:16.886995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.describe()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:51:23.529249Z","iopub.execute_input":"2022-10-05T18:51:23.529904Z","iopub.status.idle":"2022-10-05T18:51:38.893212Z","shell.execute_reply.started":"2022-10-05T18:51:23.529820Z","shell.execute_reply":"2022-10-05T18:51:38.891905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Explore a Pattern","metadata":{}},{"cell_type":"markdown","source":"The dataset contains aggregated profile features for each customer at each statement date. Features are anonymized and normalized, and fall into the following general categories:\n\n* D_* = Delinquency variables (bad or criminal behaviour, especially among young people)\n* S_* = Spend variables\n* P_* = Payment variables\n* B_* = Balance variables\n* R_* = Risk variables\n\nwith the following features being categorical: ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']","metadata":{}},{"cell_type":"code","source":"categorical_cols = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']\n\nnum_cols = [col for col in train_dataset.columns if col not in categorical_cols + [\"target\"]]\n\nprint(f'Total number of features: {1}')\nprint(f'Total number of categorical features: {len(categorical_cols)}')\nprint(f'Total number of continuos features: {len(num_cols)}')","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:51:57.641270Z","iopub.execute_input":"2022-10-05T18:51:57.641730Z","iopub.status.idle":"2022-10-05T18:51:57.649928Z","shell.execute_reply.started":"2022-10-05T18:51:57.641698Z","shell.execute_reply":"2022-10-05T18:51:57.648445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing Target","metadata":{}},{"cell_type":"code","source":"sns.countplot(x = 'target', data = train_dataset)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:52:01.860914Z","iopub.execute_input":"2022-10-05T18:52:01.861381Z","iopub.status.idle":"2022-10-05T18:52:02.315736Z","shell.execute_reply.started":"2022-10-05T18:52:01.861348Z","shell.execute_reply":"2022-10-05T18:52:02.314144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing categorial features","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20, 30))\nfor i, k in enumerate(categorical_cols):\n    plt.subplot(6, 2, i+1)\n    temp_val = pd.DataFrame(train_dataset[k].value_counts(dropna=False, normalize=True).sort_index().rename('count'))\n    temp_val.index.name = 'value'\n    temp_val.reset_index(inplace=True)\n    plt.bar(temp_val.index, temp_val['count'], alpha=0.5)\n    plt.xlabel(k)\n    plt.ylabel('frequency')\n    plt.xticks(temp_val.index, temp_val.value)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:52:37.072983Z","iopub.execute_input":"2022-10-05T18:52:37.073474Z","iopub.status.idle":"2022-10-05T18:52:38.611685Z","shell.execute_reply.started":"2022-10-05T18:52:37.073438Z","shell.execute_reply":"2022-10-05T18:52:38.610290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing categorial features based on the target","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20, 30))\nfor i, f in enumerate(categorical_cols):\n    plt.subplot(6, 2, i+1)\n    temp = pd.DataFrame(train_dataset[f][train_dataset.target == 0].value_counts(dropna=False, normalize=True).sort_index().rename('count'))\n    temp.index.name = 'value'\n    temp.reset_index(inplace=True)\n    plt.bar(temp.index, temp['count'], alpha=0.5, label='target=0')\n    temp = pd.DataFrame(train_dataset[f][train_dataset.target == 1].value_counts(dropna=False, normalize=True).sort_index().rename('count'))\n    temp.index.name = 'value'\n    temp.reset_index(inplace=True)\n    plt.bar(temp.index, temp['count'], alpha=0.5, label='target=1')\n    plt.xlabel(f)\n    plt.ylabel('frequency')\n    plt.legend()\n    plt.xticks(temp.index, temp.value)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:55:18.596894Z","iopub.execute_input":"2022-10-05T18:55:18.597917Z","iopub.status.idle":"2022-10-05T18:55:20.979435Z","shell.execute_reply.started":"2022-10-05T18:55:18.597867Z","shell.execute_reply":"2022-10-05T18:55:20.978172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Aggregated profile features","metadata":{}},{"cell_type":"code","source":"Delinquency = [d for d in train_dataset.columns if d.startswith('D_')]\nSpend = [s for s in train_dataset.columns if s.startswith('S_')]\nPayment = [p for p in train_dataset.columns if p.startswith('P_')]\nBalance = [b for b in train_dataset.columns if b.startswith('B_')]\nRisk = [r for r in train_dataset.columns if r.startswith('R_')]\nDict = {'Delinquency': len(Delinquency), 'Spend': len(Spend), 'Payment': len(Payment), 'Balance': len(Balance), 'Risk': len(Risk),}\n\nplt.figure(figsize=(10,5))\nsns.barplot(x=list(Dict.keys()), y=list(Dict.values()));","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:57:07.846296Z","iopub.execute_input":"2022-10-05T18:57:07.846861Z","iopub.status.idle":"2022-10-05T18:57:08.055063Z","shell.execute_reply.started":"2022-10-05T18:57:07.846787Z","shell.execute_reply":"2022-10-05T18:57:08.053699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check null values","metadata":{}},{"cell_type":"code","source":"NaN_Val = np.array(train_dataset.isnull().sum())\nNaN_prec = np.array((train_dataset.isnull().sum() * 100 / len(train_dataset)).round(2))\nNaN_Col = pd.DataFrame([np.array(list(train_dataset.columns)).T,NaN_Val.T,NaN_prec.T,np.array(list(train_dataset.dtypes)).T], index=['Features','Num of Missing values','Percentage','DataType']\n).transpose()\npd.set_option('display.max_rows', None)\nNaN_Col","metadata":{"execution":{"iopub.status.busy":"2022-10-05T18:58:43.450935Z","iopub.execute_input":"2022-10-05T18:58:43.451471Z","iopub.status.idle":"2022-10-05T18:58:44.421899Z","shell.execute_reply.started":"2022-10-05T18:58:43.451433Z","shell.execute_reply":"2022-10-05T18:58:44.421045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are many missing values in the dataset","metadata":{}},{"cell_type":"markdown","source":"# Drop unuseful columns","metadata":{}},{"cell_type":"markdown","source":"Remove columns if there are > 80% of missing values","metadata":{}},{"cell_type":"code","source":"train_dataset = train_dataset.drop(['S_2','D_66','D_42','D_49','D_73','D_76','R_9','B_29','D_87','D_88','D_106','R_26','D_108','D_110','D_111','B_39','B_42','D_132','D_134','D_135','D_136','D_137','D_138','D_142'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:02:55.169712Z","iopub.execute_input":"2022-10-05T19:02:55.170248Z","iopub.status.idle":"2022-10-05T19:02:55.203468Z","shell.execute_reply.started":"2022-10-05T19:02:55.170209Z","shell.execute_reply":"2022-10-05T19:02:55.201564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fill null values","metadata":{}},{"cell_type":"code","source":"selected_col = np.array(['P_2','S_3','B_2','D_41','D_43','B_3','D_44','D_45','D_46','D_48','D_50','D_53','S_7','D_56','S_9','B_6','B_8','D_52','P_3','D_54','D_55','B_13','D_59','D_61','B_15','D_62','B_16','B_17','D_77','B_19','B_20','D_69','B_22','D_70','D_72','D_74','R_7','B_25','B_26','D_78','D_79','D_80','B_27','D_81','R_12','D_82','D_105','S_27','D_83','R_14','D_84','D_86','R_20','B_33','D_89','D_91','S_22','S_23','S_24','S_25','S_26','D_102','D_103','D_104','D_107','B_37','R_27','D_109','D_112','B_40','D_113','D_115','D_118','D_119','D_121','D_122','D_123','D_124','D_125','D_128','D_129','B_41','D_130','D_131','D_133','D_139','D_140','D_141','D_143','D_144','D_145'])\n\nfor col in selected_col:\n    train_dataset[col] = train_dataset[col].fillna(train_dataset[col].median())","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:17:10.842275Z","iopub.execute_input":"2022-10-05T19:17:10.842870Z","iopub.status.idle":"2022-10-05T19:17:12.543436Z","shell.execute_reply.started":"2022-10-05T19:17:10.842806Z","shell.execute_reply":"2022-10-05T19:17:12.542153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In describe session you saw, lot of cloumns means are NaN. So, that's why i have used median to fill NaN values. ","metadata":{}},{"cell_type":"code","source":"selcted_col2 = np.array(['D_68','B_30','B_38','D_64','D_114','D_116','D_117','D_120','D_126'])\n\nfor col2 in selcted_col2:\n    train_dataset[col2] =  train_dataset[col2].fillna(train_dataset[col2].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:17:16.795992Z","iopub.execute_input":"2022-10-05T19:17:16.797174Z","iopub.status.idle":"2022-10-05T19:17:16.855237Z","shell.execute_reply.started":"2022-10-05T19:17:16.797114Z","shell.execute_reply":"2022-10-05T19:17:16.853895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check again null values","metadata":{}},{"cell_type":"code","source":"print(train_dataset.isnull().sum().to_string())","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:17:20.781187Z","iopub.execute_input":"2022-10-05T19:17:20.781629Z","iopub.status.idle":"2022-10-05T19:17:21.167897Z","shell.execute_reply.started":"2022-10-05T19:17:20.781596Z","shell.execute_reply":"2022-10-05T19:17:21.166883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are no more missing values","metadata":{}},{"cell_type":"code","source":"train_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:18:31.149960Z","iopub.execute_input":"2022-10-05T19:18:31.150398Z","iopub.status.idle":"2022-10-05T19:18:31.157759Z","shell.execute_reply.started":"2022-10-05T19:18:31.150364Z","shell.execute_reply":"2022-10-05T19:18:31.156681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:18:42.675878Z","iopub.execute_input":"2022-10-05T19:18:42.676353Z","iopub.status.idle":"2022-10-05T19:18:42.710589Z","shell.execute_reply.started":"2022-10-05T19:18:42.676315Z","shell.execute_reply":"2022-10-05T19:18:42.708895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Testing DataSet","metadata":{}},{"cell_type":"code","source":"test_dataset_ = pd.read_feather('../input/amexfeather/test_data.ftr')\n# Keep the latest statement features for each customer\ntest_dataset = test_dataset_.groupby('customer_ID').tail(1).set_index('customer_ID', drop=True).sort_index()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:18:51.341934Z","iopub.execute_input":"2022-10-05T19:18:51.342380Z","iopub.status.idle":"2022-10-05T19:19:09.765238Z","shell.execute_reply.started":"2022-10-05T19:18:51.342346Z","shell.execute_reply":"2022-10-05T19:19:09.762857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test_dataset_\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:19:13.475354Z","iopub.execute_input":"2022-10-05T19:19:13.475845Z","iopub.status.idle":"2022-10-05T19:19:13.694673Z","shell.execute_reply.started":"2022-10-05T19:19:13.475784Z","shell.execute_reply":"2022-10-05T19:19:13.693299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:19:18.815197Z","iopub.execute_input":"2022-10-05T19:19:18.815683Z","iopub.status.idle":"2022-10-05T19:19:18.850168Z","shell.execute_reply.started":"2022-10-05T19:19:18.815645Z","shell.execute_reply":"2022-10-05T19:19:18.849039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:19:24.635266Z","iopub.execute_input":"2022-10-05T19:19:24.635709Z","iopub.status.idle":"2022-10-05T19:19:24.643441Z","shell.execute_reply.started":"2022-10-05T19:19:24.635674Z","shell.execute_reply":"2022-10-05T19:19:24.642141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check null values","metadata":{}},{"cell_type":"code","source":"NaN_Val2 = np.array(test_dataset.isnull().sum())\nNaN_prec2 = np.array((test_dataset.isnull().sum() * 100 / len(test_dataset)).round(2))\nNaN_Col2 = pd.DataFrame([np.array(list(test_dataset.columns)).T,NaN_Val2.T,NaN_prec2.T,np.array(list(test_dataset.dtypes)).T], index=['Features','Num of Missing values','Percentage','DataType']\n).transpose()\npd.set_option('display.max_rows', None)\n\nNaN_Col2","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:19:28.660497Z","iopub.execute_input":"2022-10-05T19:19:28.660979Z","iopub.status.idle":"2022-10-05T19:19:30.528944Z","shell.execute_reply.started":"2022-10-05T19:19:28.660941Z","shell.execute_reply":"2022-10-05T19:19:30.527701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Drop unuseful columns","metadata":{}},{"cell_type":"code","source":"test_dataset = test_dataset.drop(['S_2','D_42','D_49','D_66','D_73','D_76','R_9','B_29','D_87','D_88','D_106','R_26','D_108','D_110','D_111','B_39','B_42','D_132','D_134','D_135','D_136','D_137','D_138','D_142'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-16T19:56:32.411051Z","iopub.execute_input":"2022-08-16T19:56:32.411452Z","iopub.status.idle":"2022-08-16T19:56:32.981672Z","shell.execute_reply.started":"2022-08-16T19:56:32.411418Z","shell.execute_reply":"2022-08-16T19:56:32.980209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fill null values","metadata":{}},{"cell_type":"code","source":"selected_column = np.array(['P_2','S_3','B_2','D_41','D_43','B_3','D_44','D_45','D_46','D_48','D_50','D_53','S_7','D_56','S_9','S_12','S_17','B_6','B_8','D_52','P_3','D_54','D_55','B_13','D_59','D_61','B_15','D_62','B_16','B_17','D_77','B_19','B_20','D_69','B_22','D_70','D_72','D_74','R_7','B_25','B_26','D_78','D_79','D_80','B_27','D_81','R_12','D_82','D_105','S_27','D_83','R_14','D_84','D_86','R_20','B_33','D_89','D_91','S_22','S_23','S_24','S_25','S_26','D_102','D_103','D_104','D_107','B_37','R_27','D_109','D_112','B_40','D_113','D_115','D_118','D_119','D_121','D_122','D_123','D_124','D_125','D_128','D_129','B_41','D_130','D_131','D_133','D_139','D_140','D_141','D_143','D_144','D_145'])\n\nfor column in selected_column:\n    test_dataset[column] = test_dataset[column].fillna(test_dataset[column].median())","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:19:51.399017Z","iopub.execute_input":"2022-10-05T19:19:51.399492Z","iopub.status.idle":"2022-10-05T19:19:55.225675Z","shell.execute_reply.started":"2022-10-05T19:19:51.399456Z","shell.execute_reply":"2022-10-05T19:19:55.224408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selected_column2 = np.array(['D_68','B_30','B_38','D_114','D_116','D_117','D_120','D_126'])\n\nfor column2 in selected_column2:\n    test_dataset[column2] =  test_dataset[column2].fillna(test_dataset[column2].mode()[0])","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:25:59.027065Z","iopub.execute_input":"2022-10-05T19:25:59.028659Z","iopub.status.idle":"2022-10-05T19:25:59.124155Z","shell.execute_reply.started":"2022-10-05T19:25:59.028588Z","shell.execute_reply":"2022-10-05T19:25:59.122803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check again null values","metadata":{}},{"cell_type":"code","source":"print(test_dataset.isnull().sum().to_string())","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:26:01.246497Z","iopub.execute_input":"2022-10-05T19:26:01.247353Z","iopub.status.idle":"2022-10-05T19:26:02.098435Z","shell.execute_reply.started":"2022-10-05T19:26:01.247311Z","shell.execute_reply":"2022-10-05T19:26:02.097051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:26:13.055847Z","iopub.execute_input":"2022-10-05T19:26:13.056356Z","iopub.status.idle":"2022-10-05T19:26:13.064905Z","shell.execute_reply.started":"2022-10-05T19:26:13.056317Z","shell.execute_reply":"2022-10-05T19:26:13.063683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:26:14.993538Z","iopub.execute_input":"2022-10-05T19:26:14.994584Z","iopub.status.idle":"2022-10-05T19:26:15.027621Z","shell.execute_reply.started":"2022-10-05T19:26:14.994534Z","shell.execute_reply":"2022-10-05T19:26:15.026394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convert categorical variable to numbers","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import OrdinalEncoder\n\nenc = OrdinalEncoder()\ncategorical_cols.remove('D_66')\n\ntrain_dataset[categorical_cols] = enc.fit_transform(train_dataset[categorical_cols])\ntest_dataset[categorical_cols] = enc.transform(test_dataset[categorical_cols])","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:26:17.575395Z","iopub.execute_input":"2022-10-05T19:26:17.576303Z","iopub.status.idle":"2022-10-05T19:26:19.749121Z","shell.execute_reply.started":"2022-10-05T19:26:17.576254Z","shell.execute_reply":"2022-10-05T19:26:19.747861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Remove highly correlated features","metadata":{}},{"cell_type":"markdown","source":"Remove columns if there are > 90% of correlations","metadata":{}},{"cell_type":"code","source":"train_dataset_without_target = train_dataset.drop([\"target\"],axis=1)\n\ncor_matrix = train_dataset_without_target.corr()\ncol_core = set()\n\nfor i in range(len(cor_matrix.columns)):\n    for j in range(i):\n        if(cor_matrix.iloc[i, j] > 0.9):\n            col_name = cor_matrix.columns[i]\n            col_core.add(col_name)\ncol_core","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:26:22.943567Z","iopub.execute_input":"2022-10-05T19:26:22.944058Z","iopub.status.idle":"2022-10-05T19:26:58.409180Z","shell.execute_reply.started":"2022-10-05T19:26:22.944018Z","shell.execute_reply":"2022-10-05T19:26:58.407922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = train_dataset.drop(col_core, axis=1)\ntest_dataset = test_dataset.drop(col_core, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:27:08.273013Z","iopub.execute_input":"2022-10-05T19:27:08.273491Z","iopub.status.idle":"2022-10-05T19:27:09.531210Z","shell.execute_reply.started":"2022-10-05T19:27:08.273447Z","shell.execute_reply":"2022-10-05T19:27:09.529367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:27:11.864421Z","iopub.execute_input":"2022-10-05T19:27:11.865247Z","iopub.status.idle":"2022-10-05T19:27:11.873569Z","shell.execute_reply.started":"2022-10-05T19:27:11.865192Z","shell.execute_reply":"2022-10-05T19:27:11.872643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Model","metadata":{}},{"cell_type":"code","source":"num_columns = [col for col in train_dataset.columns if col not in [\"target\"]]\n\nX = train_dataset[num_columns]\ny = train_dataset['target']\n\nprint(f\"X shape is = {X.shape}\" )\nprint(f\"Y shape is = {y.shape}\" )","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:27:14.611663Z","iopub.execute_input":"2022-10-05T19:27:14.612732Z","iopub.status.idle":"2022-10-05T19:27:15.015065Z","shell.execute_reply.started":"2022-10-05T19:27:14.612683Z","shell.execute_reply":"2022-10-05T19:27:15.013637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\n\nx_train,x_test,y_train,y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)\n\nprint(f\"X_train shape is = {x_train.shape}\" )\nprint(f\"Y_train shape is = {y_train.shape}\" )\nprint(f\"X_test shape is = {x_test.shape}\" )\nprint(f\"Y_test shape is = {y_test.shape}\" )","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:27:17.947015Z","iopub.execute_input":"2022-10-05T19:27:17.947474Z","iopub.status.idle":"2022-10-05T19:27:19.347596Z","shell.execute_reply.started":"2022-10-05T19:27:17.947440Z","shell.execute_reply":"2022-10-05T19:27:19.346167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import lightgbm as lgb\n\nd_train = lgb.Dataset(x_train, label=y_train, categorical_feature = categorical_cols)\n\nparams = {'objective': 'binary','n_estimators': 1200,'metric': 'binary_logloss','boosting': 'gbdt','num_leaves': 90,'reg_lambda' : 50,'colsample_bytree': 0.19,'learning_rate': 0.03,'min_child_samples': 2400,'max_bins': 511,'seed': 42,'verbose': -1}\n\n# trained model with 100 iterations\nmodel = lgb.train(params, d_train, 100)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:27:21.524279Z","iopub.execute_input":"2022-10-05T19:27:21.524742Z","iopub.status.idle":"2022-10-05T19:29:06.045948Z","shell.execute_reply.started":"2022-10-05T19:27:21.524703Z","shell.execute_reply":"2022-10-05T19:29:06.044177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make Prediction","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test_dataset[num_columns])\npredictions","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:29:35.875852Z","iopub.execute_input":"2022-10-05T19:29:35.877072Z","iopub.status.idle":"2022-10-05T19:30:52.523304Z","shell.execute_reply.started":"2022-10-05T19:29:35.877024Z","shell.execute_reply":"2022-10-05T19:30:52.521917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Output","metadata":{}},{"cell_type":"code","source":"sample_dataset = pd.read_csv('/kaggle/input/amex-default-prediction/sample_submission.csv')\noutput = pd.DataFrame({'customer_ID': sample_dataset.customer_ID, 'prediction': predictions})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T19:30:52.525486Z","iopub.execute_input":"2022-10-05T19:30:52.526275Z","iopub.status.idle":"2022-10-05T19:30:58.415067Z","shell.execute_reply.started":"2022-10-05T19:30:52.526216Z","shell.execute_reply":"2022-10-05T19:30:58.413763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}