{"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":"markdown","source":"# Train a model with balance columns","metadata":{}},{"cell_type":"markdown","source":"### The balance columns were generated from [this notebook](https://www.kaggle.com/code/amineteffal/read-all-rows-of-balance-###columns)","metadata":{}},{"cell_type":"code","source":"# import pachages\nimport os\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport gc","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-06T14:27:36.099077Z","iopub.execute_input":"2022-07-06T14:27:36.099551Z","iopub.status.idle":"2022-07-06T14:27:36.126440Z","shell.execute_reply.started":"2022-07-06T14:27:36.099451Z","shell.execute_reply":"2022-07-06T14:27:36.125537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# paths of data\ncompetition_data_path = \"/kaggle/input/amex-default-prediction/\"\nbalance_data_path = \"/kaggle/input/read-all-rows-of-balance-columns/\"","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:27:36.374546Z","iopub.execute_input":"2022-07-06T14:27:36.375030Z","iopub.status.idle":"2022-07-06T14:27:36.386914Z","shell.execute_reply.started":"2022-07-06T14:27:36.374982Z","shell.execute_reply":"2022-07-06T14:27:36.384407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read balance train data\nbalance_train = pd.read_csv(balance_data_path + \"train_balance_cols.csv\")\nbalance_train.head(5)\nprint('shape of balance data : ', balance_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:27:36.611570Z","iopub.execute_input":"2022-07-06T14:27:36.612040Z","iopub.status.idle":"2022-07-06T14:28:54.881240Z","shell.execute_reply.started":"2022-07-06T14:27:36.611997Z","shell.execute_reply":"2022-07-06T14:28:54.880167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Change S_2 column to date\nbalance_train['S_2'] = pd.to_datetime(balance_train.S_2)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:28:54.883315Z","iopub.execute_input":"2022-07-06T14:28:54.884368Z","iopub.status.idle":"2022-07-06T14:28:55.856908Z","shell.execute_reply.started":"2022-07-06T14:28:54.884326Z","shell.execute_reply":"2022-07-06T14:28:55.855805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Check for nas","metadata":{}},{"cell_type":"code","source":"nas = pd.DataFrame(balance_train.isna().sum()).reset_index()\nnas.columns = ['variable', 'number_of_nas']\nnas['ratio_of_nas'] = nas['number_of_nas']/balance_train.shape[0]\nnas = nas.sort_values(by='ratio_of_nas', ascending = False)\nnas.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:28:55.858226Z","iopub.execute_input":"2022-07-06T14:28:55.858587Z","iopub.status.idle":"2022-07-06T14:28:56.777328Z","shell.execute_reply.started":"2022-07-06T14:28:55.858551Z","shell.execute_reply":"2022-07-06T14:28:56.776134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### We will remove the 4 first variables B_39, B_42, B_29 and B_17\n### and drop only the rows with nas for the remaining columns","metadata":{}},{"cell_type":"code","source":"balance_train = balance_train.drop(['B_39', 'B_42', 'B_29' , 'B_17'], axis = 1)\nbalance_train = balance_train.dropna()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:28:56.780102Z","iopub.execute_input":"2022-07-06T14:28:56.780590Z","iopub.status.idle":"2022-07-06T14:28:59.228362Z","shell.execute_reply.started":"2022-07-06T14:28:56.780550Z","shell.execute_reply":"2022-07-06T14:28:59.227277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Fisrt of all let's count the number of customers in train and some stats for target","metadata":{}},{"cell_type":"code","source":"customers = balance_train[['customer_ID', 'target']].groupby(['customer_ID']).agg(['count', 'min', 'sum', 'max']).reset_index()\ncustomers.columns = ['customer_ID', 'number_of_rows', 'min_of_target', 'sum_of_target', 'max_of_target' ]\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:28:59.229810Z","iopub.execute_input":"2022-07-06T14:28:59.230203Z","iopub.status.idle":"2022-07-06T14:29:00.683966Z","shell.execute_reply.started":"2022-07-06T14:28:59.230160Z","shell.execute_reply":"2022-07-06T14:29:00.682814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:00.685990Z","iopub.execute_input":"2022-07-06T14:29:00.686737Z","iopub.status.idle":"2022-07-06T14:29:00.702131Z","shell.execute_reply.started":"2022-07-06T14:29:00.686696Z","shell.execute_reply":"2022-07-06T14:29:00.701156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number of different customers in train : ', len(customers))\nprint('Number of customer who default at least one time : ', len(customers.loc[customers['max_of_target'] == 1, :]))\nprint('Number of customer who never default  : ', len(customers.loc[customers['max_of_target'] == 0, :]))\nprint('Number of default in train : ', balance_train['target'].sum())","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:00.703699Z","iopub.execute_input":"2022-07-06T14:29:00.704052Z","iopub.status.idle":"2022-07-06T14:29:00.750266Z","shell.execute_reply.started":"2022-07-06T14:29:00.704015Z","shell.execute_reply":"2022-07-06T14:29:00.749148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check the number of default in train","metadata":{}},{"cell_type":"code","source":"(customers.max_of_target * customers.number_of_rows).sum() == balance_train['target'].sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:00.751669Z","iopub.execute_input":"2022-07-06T14:29:00.752200Z","iopub.status.idle":"2022-07-06T14:29:00.768104Z","shell.execute_reply.started":"2022-07-06T14:29:00.752160Z","shell.execute_reply":"2022-07-06T14:29:00.767146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### It seems that there are some customers whose target is always at 1 ! : sum_of_target = number_of_raws\n### Let's check them","metadata":{}},{"cell_type":"code","source":"customers.loc[customers['min_of_target'] == 1, : ]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:00.769900Z","iopub.execute_input":"2022-07-06T14:29:00.770287Z","iopub.status.idle":"2022-07-06T14:29:00.796077Z","shell.execute_reply.started":"2022-07-06T14:29:00.770251Z","shell.execute_reply":"2022-07-06T14:29:00.795165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### These are exactly the customers who default.\n### So, there is no customer which have a mix of target values : It's either all 0 or all 1 :\n","metadata":{}},{"cell_type":"code","source":"customers.loc[(customers['min_of_target'] == 0) & (customers['max_of_target'] == 1), : ]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:00.800448Z","iopub.execute_input":"2022-07-06T14:29:00.800724Z","iopub.status.idle":"2022-07-06T14:29:00.814978Z","shell.execute_reply.started":"2022-07-06T14:29:00.800699Z","shell.execute_reply":"2022-07-06T14:29:00.814133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Let's compare the balance of the customers who default at least once and those who never default","metadata":{"execution":{"iopub.status.busy":"2022-07-04T10:07:09.954102Z","iopub.execute_input":"2022-07-04T10:07:09.954907Z","iopub.status.idle":"2022-07-04T10:07:09.964826Z","shell.execute_reply.started":"2022-07-04T10:07:09.954855Z","shell.execute_reply":"2022-07-04T10:07:09.963414Z"}}},{"cell_type":"code","source":"customer_who_defaults = list(customers.loc[customers['max_of_target'] == 1, :]['customer_ID'].unique())\ncustomer_who_defaults_df = balance_train.set_index('customer_ID').loc[customer_who_defaults]\ncustomer_who_defaults_df = customer_who_defaults_df.drop('S_2', axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:00.816709Z","iopub.execute_input":"2022-07-06T14:29:00.817076Z","iopub.status.idle":"2022-07-06T14:29:04.208138Z","shell.execute_reply.started":"2022-07-06T14:29:00.817040Z","shell.execute_reply":"2022-07-06T14:29:04.207125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer_never_defaults = list(customers.loc[customers['max_of_target'] == 0, :]['customer_ID'].unique())\ncustomer_never_defaults_df = balance_train.set_index('customer_ID','S_2').loc[customer_never_defaults]\ncustomer_never_defaults_df = customer_never_defaults_df.drop('S_2', axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:04.209580Z","iopub.execute_input":"2022-07-06T14:29:04.210003Z","iopub.status.idle":"2022-07-06T14:29:11.830491Z","shell.execute_reply.started":"2022-07-06T14:29:04.209963Z","shell.execute_reply":"2022-07-06T14:29:11.829405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Calculate the mins, means and maxs of their balance variables","metadata":{}},{"cell_type":"code","source":"stats_customer_who_defaults = {'min' : customer_who_defaults_df.min(), \\\n                               'mean' : customer_who_defaults_df.mean(),\\\n                               'max' : customer_who_defaults_df.max()}\n\nstats_customer_who_never_defaults = {'min' : customer_never_defaults_df.min(), \\\n                                     'mean' : customer_never_defaults_df.mean(),\\\n                                     'max' : customer_never_defaults_df.max()}\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:11.831788Z","iopub.execute_input":"2022-07-06T14:29:11.832177Z","iopub.status.idle":"2022-07-06T14:29:13.269907Z","shell.execute_reply.started":"2022-07-06T14:29:11.832140Z","shell.execute_reply":"2022-07-06T14:29:13.268834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Let's create a function that plot a given stat (min, mean or max) for the customers who defaults and the others","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ndef plot_stats(stats_default, stats_did_not_default, stat = 'min'):\n    plt.figure(figsize=(25, 10))\n    x = range(0, len(stats_default[stat]))\n    y_defaults = list(stats_default[stat])\n    y_never_defaults = list(stats_did_not_default[stat])\n    xticks = list(stats_default[stat].index)\n    i_start = 0   #0\n    i_end =  len(x)    #len(x)\n    w = 0.4\n    plt.bar(x[i_start:i_end], y_defaults[i_start:i_end], width=w, color = 'r')\n    plt.bar([(x[i] + w) for i in range(i_start, i_end)] , y_never_defaults[i_start:i_end], width=w, color = 'g')\n    plt.xticks([(x[i] + w/2) for i in range(i_start, i_end)], xticks[i_start:i_end])\n    plt.xlabel('balance variable')\n    plt.ylabel(stat + ' of balance variable')\n    plt.title(stat + ' of balance variables by type of customer (defaults once/never defaults)')\n    plt.legend([\"defaults once\", \"never defaults\"], loc =\"upper right\")\n    plt.show()\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:13.271370Z","iopub.execute_input":"2022-07-06T14:29:13.271797Z","iopub.status.idle":"2022-07-06T14:29:13.283254Z","shell.execute_reply.started":"2022-07-06T14:29:13.271757Z","shell.execute_reply":"2022-07-06T14:29:13.282133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plot the max","metadata":{}},{"cell_type":"code","source":"plot_stats(stats_customer_who_defaults,stats_customer_who_never_defaults, 'max')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:13.284753Z","iopub.execute_input":"2022-07-06T14:29:13.285234Z","iopub.status.idle":"2022-07-06T14:29:13.740140Z","shell.execute_reply.started":"2022-07-06T14:29:13.285183Z","shell.execute_reply":"2022-07-06T14:29:13.739171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plot the mean","metadata":{}},{"cell_type":"code","source":"plot_stats(stats_customer_who_defaults,stats_customer_who_never_defaults, 'mean')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:13.741707Z","iopub.execute_input":"2022-07-06T14:29:13.742328Z","iopub.status.idle":"2022-07-06T14:29:14.216316Z","shell.execute_reply.started":"2022-07-06T14:29:13.742289Z","shell.execute_reply":"2022-07-06T14:29:14.215188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plot the min","metadata":{}},{"cell_type":"code","source":"plot_stats(stats_customer_who_defaults,stats_customer_who_never_defaults, 'min')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:14.217907Z","iopub.execute_input":"2022-07-06T14:29:14.218220Z","iopub.status.idle":"2022-07-06T14:29:14.638871Z","shell.execute_reply.started":"2022-07-06T14:29:14.218194Z","shell.execute_reply":"2022-07-06T14:29:14.637961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### As you can see there are some variables whose stats( min, max or mean) are very different for customers who default and the others.\n### Let's calculate some ratios to get it more clearly","metadata":{}},{"cell_type":"code","source":"def compare_stats(stats_default, stats_did_not_default, stat = 'min'):\n    compare_stats_df = pd.DataFrame({'variable' : list(stats_default[stat].index),  \\\n                                     'defaults_once' : list(stats_default[stat]), \\\n                                     'never_defaults': list(stats_did_not_default[stat])},  \\\n                                      columns = ['variable', 'defaults_once', 'never_defaults'])\n    \n    # ratio of max of the stat on min of the stat\n    compare_stats_df['max'] = compare_stats_df[['defaults_once', 'never_defaults']].apply(np.max, axis = 1)\n    compare_stats_df['min'] = compare_stats_df[['defaults_once', 'never_defaults']].apply(np.min, axis = 1)\n    compare_stats_df['max_on_min'] = compare_stats_df['max']/compare_stats_df['min']\n    compare_stats_df = compare_stats_df.drop(['min', 'max'], axis = 1)\n\n#     print(compare_stats_df.sort_values(by='max_on_min', ascending = False))\n    \n    return compare_stats_df.sort_values(by='max_on_min', ascending = False)\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:14.641830Z","iopub.execute_input":"2022-07-06T14:29:14.642596Z","iopub.status.idle":"2022-07-06T14:29:14.651122Z","shell.execute_reply.started":"2022-07-06T14:29:14.642554Z","shell.execute_reply":"2022-07-06T14:29:14.649992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get the top 3 except target\ncompare_stats(stats_customer_who_defaults,stats_customer_who_never_defaults, 'max').head(4)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:14.654155Z","iopub.execute_input":"2022-07-06T14:29:14.654431Z","iopub.status.idle":"2022-07-06T14:29:14.683104Z","shell.execute_reply.started":"2022-07-06T14:29:14.654406Z","shell.execute_reply":"2022-07-06T14:29:14.682140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get the top 3 except target\ncompare_stats(stats_customer_who_defaults,stats_customer_who_never_defaults, 'min').head(4)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:14.684514Z","iopub.execute_input":"2022-07-06T14:29:14.685362Z","iopub.status.idle":"2022-07-06T14:29:14.711701Z","shell.execute_reply.started":"2022-07-06T14:29:14.685320Z","shell.execute_reply":"2022-07-06T14:29:14.710770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get the top 3 except target\ncompare_stats(stats_customer_who_defaults,stats_customer_who_never_defaults, 'mean').head(4)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:14.713211Z","iopub.execute_input":"2022-07-06T14:29:14.713582Z","iopub.status.idle":"2022-07-06T14:29:14.738115Z","shell.execute_reply.started":"2022-07-06T14:29:14.713545Z","shell.execute_reply":"2022-07-06T14:29:14.737131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### the top 3 variables that shows big ratio are :\n* ### max_B_27, max_B_12 and max_B_13 \n* ### mean_B_21, mean_B_24 and mean_B_30 \n* ### min_B_5, min_B_4 and min_B_16 ","metadata":{}},{"cell_type":"markdown","source":"### Let's construct this dataframe from the balance_train","metadata":{}},{"cell_type":"code","source":"customers_1 = balance_train[['customer_ID', 'B_27', 'B_12', 'B_13', 'target']].groupby(['customer_ID']).agg(['max']).reset_index()\ncustomers_1.columns = ['customer_ID', 'max_of_B_27', 'max_of_B_12', 'max_of_B_13', 'target']\ncustomers_1 = customers_1.set_index('customer_ID')\n\ncustomers_2 = balance_train[['customer_ID', 'B_21', 'B_24', 'B_30']].groupby(['customer_ID']).agg(['mean']).reset_index()\ncustomers_2.columns = ['customer_ID', 'mean_of_B_21', 'mean_of_B_24', 'mean_of_B_30']\ncustomers_2 = customers_2.set_index('customer_ID')\n\ncustomers_3 = balance_train[['customer_ID', 'B_5', 'B_4', 'B_16']].groupby(['customer_ID']).agg(['min']).reset_index()\ncustomers_3.columns = ['customer_ID', 'min_of_B_5', 'min_of_B_4', 'min_of_B_16']\ncustomers_3 = customers_3.set_index('customer_ID')\n\ncustomers_4 = pd.concat([customers_1, customers_2, customers_3], axis=1).reset_index()\n\n# The previous aggregation transforms target to float, so let's transform it back to int\ncustomers_4['target'] = customers_4['target'].apply(int)\n\n# rearange columns\ncustomers_4 = customers_4[['customer_ID', 'min_of_B_5', 'min_of_B_4', 'min_of_B_16', \\\n                           'mean_of_B_21', 'mean_of_B_24', 'mean_of_B_30', 'max_of_B_27', \\\n                           'max_of_B_12', 'max_of_B_13', 'target' ]]\n\ndel customers_1\ndel customers_2\ndel customers_3\ngc.collect()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:14.739463Z","iopub.execute_input":"2022-07-06T14:29:14.739781Z","iopub.status.idle":"2022-07-06T14:29:20.107069Z","shell.execute_reply.started":"2022-07-06T14:29:14.739755Z","shell.execute_reply":"2022-07-06T14:29:20.106039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_4","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:20.108312Z","iopub.execute_input":"2022-07-06T14:29:20.108689Z","iopub.status.idle":"2022-07-06T14:29:20.132968Z","shell.execute_reply.started":"2022-07-06T14:29:20.108650Z","shell.execute_reply":"2022-07-06T14:29:20.132143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_4.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:20.134438Z","iopub.execute_input":"2022-07-06T14:29:20.134791Z","iopub.status.idle":"2022-07-06T14:29:20.331286Z","shell.execute_reply.started":"2022-07-06T14:29:20.134754Z","shell.execute_reply":"2022-07-06T14:29:20.330282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### it seems that we need some scaling, let's do it :","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\nscaler = MinMaxScaler()\ncols = ['min_of_B_5', 'min_of_B_4', 'min_of_B_16', \\\n        'mean_of_B_21', 'mean_of_B_24', 'mean_of_B_30', \\\n        'max_of_B_27',  'max_of_B_12', 'max_of_B_13']\n\nscaler.fit(customers_4[cols])                \ncustomers_4_scaled = pd.DataFrame(scaler.fit_transform(customers_4[cols]))\ncustomers_4_scaled.columns = cols\ncustomers_4_scaled.insert(0, 'customer_ID', customers_4.customer_ID)\ncustomers_4_scaled['target'] = customers_4.target\n\ncustomers_4_scaled","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:20.332850Z","iopub.execute_input":"2022-07-06T14:29:20.333469Z","iopub.status.idle":"2022-07-06T14:29:21.141276Z","shell.execute_reply.started":"2022-07-06T14:29:20.333427Z","shell.execute_reply":"2022-07-06T14:29:21.140322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Let's now plot some scatterplots :","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:21.142769Z","iopub.execute_input":"2022-07-06T14:29:21.143137Z","iopub.status.idle":"2022-07-06T14:29:21.249120Z","shell.execute_reply.started":"2022-07-06T14:29:21.143083Z","shell.execute_reply":"2022-07-06T14:29:21.248230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# code adapted from https://seaborn.pydata.org/tutorial/axis_grids.html\nplt.figure(figsize=(20,20))\ng = sns.PairGrid(customers_4_scaled ,vars=[\"min_of_B_5\", \"min_of_B_4\", \"min_of_B_16\"], hue=\"target\")\ng.map(sns.scatterplot)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:29:21.251043Z","iopub.execute_input":"2022-07-06T14:29:21.251755Z","iopub.status.idle":"2022-07-06T14:30:33.055919Z","shell.execute_reply.started":"2022-07-06T14:29:21.251714Z","shell.execute_reply":"2022-07-06T14:30:33.054890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# code adapted from https://seaborn.pydata.org/tutorial/axis_grids.html\nplt.figure(figsize=(20,20))\ng = sns.PairGrid(customers_4_scaled ,vars=[\"max_of_B_27\", \"max_of_B_12\", \"max_of_B_13\"], hue=\"target\")\ng.map(sns.scatterplot)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:33:02.117216Z","iopub.execute_input":"2022-07-06T14:33:02.118147Z","iopub.status.idle":"2022-07-06T14:34:10.954020Z","shell.execute_reply.started":"2022-07-06T14:33:02.118083Z","shell.execute_reply":"2022-07-06T14:34:10.953019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# code adapted from https://seaborn.pydata.org/tutorial/axis_grids.html\nplt.figure(figsize=(20,20))\ng = sns.PairGrid(customers_4_scaled ,vars=[\"mean_of_B_24\", \"mean_of_B_30\", \"mean_of_B_21\"], hue=\"target\")\ng.map(sns.scatterplot)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:47:22.252721Z","iopub.execute_input":"2022-07-06T14:47:22.253136Z","iopub.status.idle":"2022-07-06T14:48:29.981058Z","shell.execute_reply.started":"2022-07-06T14:47:22.253081Z","shell.execute_reply":"2022-07-06T14:48:29.980065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### From the graphs we see that we have some correlations. Let's check for them","metadata":{}},{"cell_type":"code","source":"corr = customers_4_scaled[cols + ['target']].corr()\ncorr.style.background_gradient(cmap='coolwarm')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T14:55:32.960844Z","iopub.execute_input":"2022-07-06T14:55:32.961297Z","iopub.status.idle":"2022-07-06T14:55:33.134510Z","shell.execute_reply.started":"2022-07-06T14:55:32.961260Z","shell.execute_reply":"2022-07-06T14:55:33.133391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### max_of_B_12 and max_of_B_13 are very correlated\n### Let's do a PCA to get uncorrealted conponents","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\nfrom sklearn.preprocessing import StandardScaler\n\ndef fit_PCA(train_X, train_y, n_comp):\n    # scale\n    train_X = StandardScaler().fit_transform(train_X)\n    \n    pca = PCA(n_components=n_comp)\n    pca.fit(train_X)\n    train_X_pca = pca.transform(train_X)\n    \n    df_train_X_pca = pd.DataFrame(data = train_X_pca\n             , columns = ['princ_comp_' + str(i+1) for i in range(n_comp)])\n    \n    # add target column back\n    df_train_X_pca['target'] = list(train_y)\n    \n    return df_train_X_pca, pca\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:18:07.225188Z","iopub.execute_input":"2022-07-06T15:18:07.226243Z","iopub.status.idle":"2022-07-06T15:18:07.236218Z","shell.execute_reply.started":"2022-07-06T15:18:07.226200Z","shell.execute_reply":"2022-07-06T15:18:07.234935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_X_pca, pca = fit_PCA(customers_4_scaled[cols], customers_4_scaled.target, 4)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:18:14.636986Z","iopub.execute_input":"2022-07-06T15:18:14.639440Z","iopub.status.idle":"2022-07-06T15:18:16.460354Z","shell.execute_reply.started":"2022-07-06T15:18:14.639384Z","shell.execute_reply":"2022-07-06T15:18:16.459281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot princ_comp_1 against princ_comp_2 for train data\nplt.figure(figsize=(15,15))\ng = sns.PairGrid(df_train_X_pca , hue=\"target\")\ng.map(sns.scatterplot)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T15:10:34.891145Z","iopub.execute_input":"2022-07-06T15:10:34.891942Z","iopub.status.idle":"2022-07-06T15:12:41.488113Z","shell.execute_reply.started":"2022-07-06T15:10:34.891899Z","shell.execute_reply":"2022-07-06T15:12:41.486975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### We can see that a value of 0 of princ_comp_3 can be used to separate the two types of customers, but it must be investigated in depth...","metadata":{}}]}