{"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-12-12T16:21:16.232615Z","iopub.execute_input":"2022-12-12T16:21:16.233231Z","iopub.status.idle":"2022-12-12T16:21:16.242615Z","shell.execute_reply.started":"2022-12-12T16:21:16.233191Z","shell.execute_reply":"2022-12-12T16:21:16.241925Z"},"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-12-12T16:21:16.243868Z","iopub.execute_input":"2022-12-12T16:21:16.244124Z","iopub.status.idle":"2022-12-12T16:21:33.933827Z","shell.execute_reply.started":"2022-12-12T16:21:16.244095Z","shell.execute_reply":"2022-12-12T16:21:33.932990Z"},"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-12-12T16:21:33.934839Z","iopub.execute_input":"2022-12-12T16:21:33.935058Z","iopub.status.idle":"2022-12-12T16:21:34.054222Z","shell.execute_reply.started":"2022-12-12T16:21:33.935032Z","shell.execute_reply":"2022-12-12T16:21:34.053654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T16:21:34.055026Z","iopub.execute_input":"2022-12-12T16:21:34.055201Z","iopub.status.idle":"2022-12-12T16:21:34.092244Z","shell.execute_reply.started":"2022-12-12T16:21:34.055178Z","shell.execute_reply":"2022-12-12T16:21:34.091587Z"},"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-12-12T16:21:34.094027Z","iopub.execute_input":"2022-12-12T16:21:34.094211Z","iopub.status.idle":"2022-12-12T16:21:34.494124Z","shell.execute_reply.started":"2022-12-12T16:21:34.094189Z","shell.execute_reply":"2022-12-12T16:21:34.493237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.describe()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T16:21:34.495091Z","iopub.execute_input":"2022-12-12T16:21:34.495296Z","iopub.status.idle":"2022-12-12T16:21:46.144608Z","shell.execute_reply.started":"2022-12-12T16:21:34.495271Z","shell.execute_reply":"2022-12-12T16:21:46.143941Z"},"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-12-12T16:21:46.145619Z","iopub.execute_input":"2022-12-12T16:21:46.145826Z","iopub.status.idle":"2022-12-12T16:21:46.151664Z","shell.execute_reply.started":"2022-12-12T16:21:46.145800Z","shell.execute_reply":"2022-12-12T16:21:46.151096Z"},"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-12-12T16:21:46.152454Z","iopub.execute_input":"2022-12-12T16:21:46.152664Z","iopub.status.idle":"2022-12-12T16:21:46.456849Z","shell.execute_reply.started":"2022-12-12T16:21:46.152637Z","shell.execute_reply":"2022-12-12T16:21:46.456243Z"},"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-12-12T16:21:46.457863Z","iopub.execute_input":"2022-12-12T16:21:46.458096Z","iopub.status.idle":"2022-12-12T16:21:47.820339Z","shell.execute_reply.started":"2022-12-12T16:21:46.458068Z","shell.execute_reply":"2022-12-12T16:21:47.819720Z"},"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-12-12T16:21:47.821239Z","iopub.execute_input":"2022-12-12T16:21:47.821435Z","iopub.status.idle":"2022-12-12T16:21:50.112913Z","shell.execute_reply.started":"2022-12-12T16:21:47.821411Z","shell.execute_reply":"2022-12-12T16:21:50.112300Z"},"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-12-12T16:21:50.113945Z","iopub.execute_input":"2022-12-12T16:21:50.114161Z","iopub.status.idle":"2022-12-12T16:21:50.264817Z","shell.execute_reply.started":"2022-12-12T16:21:50.114133Z","shell.execute_reply":"2022-12-12T16:21:50.264227Z"},"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-12-12T16:21:50.265773Z","iopub.execute_input":"2022-12-12T16:21:50.265985Z","iopub.status.idle":"2022-12-12T16:21:50.862004Z","shell.execute_reply.started":"2022-12-12T16:21:50.265959Z","shell.execute_reply":"2022-12-12T16:21:50.861365Z"},"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-12-12T16:21:50.863038Z","iopub.execute_input":"2022-12-12T16:21:50.863267Z","iopub.status.idle":"2022-12-12T16:21:51.002756Z","shell.execute_reply.started":"2022-12-12T16:21:50.863239Z","shell.execute_reply":"2022-12-12T16:21:51.002087Z"},"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-12-12T16:21:51.005932Z","iopub.execute_input":"2022-12-12T16:21:51.006154Z","iopub.status.idle":"2022-12-12T16:21:52.240443Z","shell.execute_reply.started":"2022-12-12T16:21:51.006105Z","shell.execute_reply":"2022-12-12T16:21:52.239794Z"},"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-12-12T16:21:52.241352Z","iopub.execute_input":"2022-12-12T16:21:52.241546Z","iopub.status.idle":"2022-12-12T16:21:52.284917Z","shell.execute_reply.started":"2022-12-12T16:21:52.241522Z","shell.execute_reply":"2022-12-12T16:21:52.284258Z"},"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-12-12T16:21:52.285835Z","iopub.execute_input":"2022-12-12T16:21:52.286063Z","iopub.status.idle":"2022-12-12T16:21:52.504849Z","shell.execute_reply.started":"2022-12-12T16:21:52.286026Z","shell.execute_reply":"2022-12-12T16:21:52.504013Z"},"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-12-12T16:21:52.505926Z","iopub.execute_input":"2022-12-12T16:21:52.506371Z","iopub.status.idle":"2022-12-12T16:21:52.510867Z","shell.execute_reply.started":"2022-12-12T16:21:52.506342Z","shell.execute_reply":"2022-12-12T16:21:52.510267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T16:21:52.511788Z","iopub.execute_input":"2022-12-12T16:21:52.512010Z","iopub.status.idle":"2022-12-12T16:21:52.542962Z","shell.execute_reply.started":"2022-12-12T16:21:52.511984Z","shell.execute_reply":"2022-12-12T16:21:52.542424Z"},"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-12-12T16:21:52.543856Z","iopub.execute_input":"2022-12-12T16:21:52.544087Z","iopub.status.idle":"2022-12-12T16:22:28.224346Z","shell.execute_reply.started":"2022-12-12T16:21:52.544060Z","shell.execute_reply":"2022-12-12T16:22:28.223664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test_dataset_\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T16:22:28.225344Z","iopub.execute_input":"2022-12-12T16:22:28.225598Z","iopub.status.idle":"2022-12-12T16:22:28.399712Z","shell.execute_reply.started":"2022-12-12T16:22:28.225570Z","shell.execute_reply":"2022-12-12T16:22:28.399130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T16:22:28.400603Z","iopub.execute_input":"2022-12-12T16:22:28.400804Z","iopub.status.idle":"2022-12-12T16:22:28.433953Z","shell.execute_reply.started":"2022-12-12T16:22:28.400779Z","shell.execute_reply":"2022-12-12T16:22:28.433376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-12T16:22:28.434779Z","iopub.execute_input":"2022-12-12T16:22:28.435136Z","iopub.status.idle":"2022-12-12T16:22:28.439256Z","shell.execute_reply.started":"2022-12-12T16:22:28.435109Z","shell.execute_reply":"2022-12-12T16:22:28.438743Z"},"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-12-12T16:22:28.440064Z","iopub.execute_input":"2022-12-12T16:22:28.440252Z","iopub.status.idle":"2022-12-12T16:22:29.802608Z","shell.execute_reply.started":"2022-12-12T16:22:28.440229Z","shell.execute_reply":"2022-12-12T16:22:29.802005Z"},"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-12-12T16:22:29.803519Z","iopub.execute_input":"2022-12-12T16:22:29.803731Z","iopub.status.idle":"2022-12-12T16:22:30.073012Z","shell.execute_reply.started":"2022-12-12T16:22:29.803706Z","shell.execute_reply":"2022-12-12T16:22:30.072324Z"},"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-12-12T16:22:30.074121Z","iopub.execute_input":"2022-12-12T16:22:30.074335Z","iopub.status.idle":"2022-12-12T16:22:32.562631Z","shell.execute_reply.started":"2022-12-12T16:22:30.074308Z","shell.execute_reply":"2022-12-12T16:22:32.561941Z"},"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-12-12T16:22:32.563602Z","iopub.execute_input":"2022-12-12T16:22:32.563798Z","iopub.status.idle":"2022-12-12T16:22:32.631458Z","shell.execute_reply.started":"2022-12-12T16:22:32.563773Z","shell.execute_reply":"2022-12-12T16:22:32.630835Z"},"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-12-12T16:22:32.632355Z","iopub.execute_input":"2022-12-12T16:22:32.632549Z","iopub.status.idle":"2022-12-12T16:22:33.060697Z","shell.execute_reply.started":"2022-12-12T16:22:32.632525Z","shell.execute_reply":"2022-12-12T16:22:33.060068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-12T16:22:33.061597Z","iopub.execute_input":"2022-12-12T16:22:33.061788Z","iopub.status.idle":"2022-12-12T16:22:33.066342Z","shell.execute_reply.started":"2022-12-12T16:22:33.061764Z","shell.execute_reply":"2022-12-12T16:22:33.065703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T16:22:33.067242Z","iopub.execute_input":"2022-12-12T16:22:33.067455Z","iopub.status.idle":"2022-12-12T16:22:33.096654Z","shell.execute_reply.started":"2022-12-12T16:22:33.067429Z","shell.execute_reply":"2022-12-12T16:22:33.096070Z"},"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-12-12T16:22:33.097517Z","iopub.execute_input":"2022-12-12T16:22:33.097716Z","iopub.status.idle":"2022-12-12T16:22:35.208032Z","shell.execute_reply.started":"2022-12-12T16:22:33.097690Z","shell.execute_reply":"2022-12-12T16:22:35.207253Z"},"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-12-12T16:22:35.209089Z","iopub.execute_input":"2022-12-12T16:22:35.209298Z","iopub.status.idle":"2022-12-12T16:23:05.868201Z","shell.execute_reply.started":"2022-12-12T16:22:35.209273Z","shell.execute_reply":"2022-12-12T16:23:05.867519Z"},"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-12-12T16:23:05.869234Z","iopub.execute_input":"2022-12-12T16:23:05.869459Z","iopub.status.idle":"2022-12-12T16:23:06.319385Z","shell.execute_reply.started":"2022-12-12T16:23:05.869431Z","shell.execute_reply":"2022-12-12T16:23:06.318613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-12T16:23:06.320475Z","iopub.execute_input":"2022-12-12T16:23:06.320681Z","iopub.status.idle":"2022-12-12T16:23:06.325303Z","shell.execute_reply.started":"2022-12-12T16:23:06.320656Z","shell.execute_reply":"2022-12-12T16:23:06.324698Z"},"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-12-12T16:23:06.326167Z","iopub.execute_input":"2022-12-12T16:23:06.326364Z","iopub.status.idle":"2022-12-12T16:23:06.473415Z","shell.execute_reply.started":"2022-12-12T16:23:06.326340Z","shell.execute_reply":"2022-12-12T16:23:06.472696Z"},"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-12-12T16:23:06.474493Z","iopub.execute_input":"2022-12-12T16:23:06.474690Z","iopub.status.idle":"2022-12-12T16:23:07.253221Z","shell.execute_reply.started":"2022-12-12T16:23:06.474666Z","shell.execute_reply":"2022-12-12T16:23:07.252587Z"},"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-12-12T16:23:07.254121Z","iopub.execute_input":"2022-12-12T16:23:07.254321Z","iopub.status.idle":"2022-12-12T16:24:31.964825Z","shell.execute_reply.started":"2022-12-12T16:23:07.254296Z","shell.execute_reply":"2022-12-12T16:24:31.964039Z"},"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-12-12T16:24:31.965992Z","iopub.execute_input":"2022-12-12T16:24:31.966238Z","iopub.status.idle":"2022-12-12T16:24:37.424754Z","shell.execute_reply.started":"2022-12-12T16:24:31.966209Z","shell.execute_reply":"2022-12-12T16:24:37.424002Z"},"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-12-12T16:24:37.426186Z","iopub.execute_input":"2022-12-12T16:24:37.426675Z","iopub.status.idle":"2022-12-12T16:24:44.485497Z","shell.execute_reply.started":"2022-12-12T16:24:37.426637Z","shell.execute_reply":"2022-12-12T16:24:44.484738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}