{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":35332,"databundleVersionId":3723648,"sourceType":"competition"},{"sourceId":3703374,"sourceType":"datasetVersion","datasetId":2215340}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-30T09:45:13.151799Z","iopub.execute_input":"2023-11-30T09:45:13.152170Z","iopub.status.idle":"2023-11-30T09:45:13.161465Z","shell.execute_reply.started":"2023-11-30T09:45:13.152142Z","shell.execute_reply":"2023-11-30T09:45:13.160265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test = pd.read_feather(\"/kaggle/input/amex-default-prediction-feather/test.feather\")\n# test = pd.read_csv(\"/kaggle/input/amex-default-prediction/test_data.csv\")\nsample_size = 10000\ntrain_x = pd.read_feather(\"//kaggle/input/amex-default-prediction-feather/train.feather\").head(sample_size)\ntrain_y = pd.read_csv(\"/kaggle/input/amex-default-prediction/train_labels.csv\")\ntrain_y = train_y[train_y.customer_ID.isin(train_x.customer_ID.unique())]\n","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-11-30T09:45:13.991276Z","iopub.execute_input":"2023-11-30T09:45:13.991736Z","iopub.status.idle":"2023-11-30T09:45:20.642328Z","shell.execute_reply.started":"2023-11-30T09:45:13.991702Z","shell.execute_reply":"2023-11-30T09:45:20.640990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Reading Test Data","metadata":{}},{"cell_type":"code","source":"pd.set_option('display.max_rows', None)    # Show all rows\npd.set_option('display.max_columns', None) # Show all columns\npd.set_option('display.width', None)\n\ncategorical = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']\nnumerical = [col for col in train_x.columns if col not in categorical]\nnumerical.remove('customer_ID')\nnumerical.remove('S_2')","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:45:20.644552Z","iopub.execute_input":"2023-11-30T09:45:20.644988Z","iopub.status.idle":"2023-11-30T09:45:20.653049Z","shell.execute_reply.started":"2023-11-30T09:45:20.644946Z","shell.execute_reply":"2023-11-30T09:45:20.651772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Count of target variable\nfig, ax = plt.subplots(figsize=(10,5))\nsns.countplot(x=train_y.target)\nplt.show()","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-11-30T09:45:20.654471Z","iopub.execute_input":"2023-11-30T09:45:20.654819Z","iopub.status.idle":"2023-11-30T09:45:20.900127Z","shell.execute_reply.started":"2023-11-30T09:45:20.654770Z","shell.execute_reply":"2023-11-30T09:45:20.898907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Preprocessing","metadata":{}},{"cell_type":"code","source":"train_x['S_2'] = pd.to_datetime(train_x['S_2'], format='%Y-%m-%d')","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-11-30T09:45:20.903469Z","iopub.execute_input":"2023-11-30T09:45:20.903858Z","iopub.status.idle":"2023-11-30T09:45:20.916821Z","shell.execute_reply.started":"2023-11-30T09:45:20.903821Z","shell.execute_reply":"2023-11-30T09:45:20.915835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  EDA","metadata":{}},{"cell_type":"code","source":"print(f'There are records from {train_x[\"S_2\"].min()} to {train_x[\"S_2\"].max()}')\n","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-11-30T09:45:22.088147Z","iopub.execute_input":"2023-11-30T09:45:22.088521Z","iopub.status.idle":"2023-11-30T09:45:22.094216Z","shell.execute_reply.started":"2023-11-30T09:45:22.088494Z","shell.execute_reply":"2023-11-30T09:45:22.093454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Overall the number of statements taken is uniform throughout the relevent  time frame","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\n\ndaily_counts = train_x.groupby(pd.to_datetime(train_x[\"S_2\"]).dt.date).count()[\"customer_ID\"]\n\nsns.lineplot(x=daily_counts.index, y=daily_counts.values)\n\nplt.title('Number of Statements Taken per Day')\nplt.xlabel('Date')\nplt.ylabel('Count')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:45:23.842965Z","iopub.execute_input":"2023-11-30T09:45:23.843720Z","iopub.status.idle":"2023-11-30T09:45:24.325986Z","shell.execute_reply.started":"2023-11-30T09:45:23.843683Z","shell.execute_reply":"2023-11-30T09:45:24.325125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train data count rows =\",train_x.shape[0])\n# print(\"Test data count rows =\",test.shape[0])\nprint(\"Number of Columns =\", train_x.shape[1])","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-11-30T09:45:26.068859Z","iopub.execute_input":"2023-11-30T09:45:26.069576Z","iopub.status.idle":"2023-11-30T09:45:26.074555Z","shell.execute_reply.started":"2023-11-30T09:45:26.069534Z","shell.execute_reply":"2023-11-30T09:45:26.073784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Perecetage if nan values in each col sorted in descending order\ntrain_x.isnull().sum().sort_values(ascending = False).head(40)/sample_size*100","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:45:28.185237Z","iopub.execute_input":"2023-11-30T09:45:28.185607Z","iopub.status.idle":"2023-11-30T09:45:28.204089Z","shell.execute_reply.started":"2023-11-30T09:45:28.185578Z","shell.execute_reply":"2023-11-30T09:45:28.203034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(20,5))\nsns.countplot(x=train_x.groupby(\"customer_ID\")['customer_ID'].count().values)\nplt.title(\"customer statement count\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:45:45.515528Z","iopub.execute_input":"2023-11-30T09:45:45.516199Z","iopub.status.idle":"2023-11-30T09:45:45.863355Z","shell.execute_reply.started":"2023-11-30T09:45:45.516154Z","shell.execute_reply":"2023-11-30T09:45:45.862192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"statements_per_customer  = train_x.customer_ID.value_counts()\nstatements_per_customer_w_labels =  pd.merge(train_y, statements_per_customer, on=\"customer_ID\")\n# statements_per_customer_w_labels = statements_per_customer_w_labels.drop(columns='customer_ID')\n\ntot_occ = statements_per_customer_w_labels.groupby(by=[\"target\", \"count\"]).size().reset_index(name='occurrences').sort_values(by='count')\ntot_occ.reset_index(drop=True, inplace=True)\n\n\n# Calculate the total occurrences for each count\ntot_occ['total_occurrences'] = tot_occ.groupby('count')['occurrences'].transform('sum')\ntot_occ['ratio'] = tot_occ['occurrences'] / tot_occ['total_occurrences']\ntot_occ.drop(columns='total_occurrences', inplace=True)\ntot_occ = tot_occ[tot_occ['target'] == 0]\n\nplt.figure(figsize=(10, 6))\nplt.bar(tot_occ['count'], tot_occ['ratio']*100, color='skyblue')\nplt.xlabel('Count of statements')\nplt.ylabel('Perecentage of bad customers')\nplt.title('Statement Count')\nplt.xticks(rotation=45, ha='right')  # Rotate x-axis labels for better readability\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:45:51.643933Z","iopub.execute_input":"2023-11-30T09:45:51.644322Z","iopub.status.idle":"2023-11-30T09:45:52.020248Z","shell.execute_reply.started":"2023-11-30T09:45:51.644290Z","shell.execute_reply":"2023-11-30T09:45:52.019403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.stats import pointbiserialr\n\ncorrelation_result = pointbiserialr(statements_per_customer_w_labels['count'], statements_per_customer_w_labels['target'])\n\ncorrelation_coefficient = correlation_result.correlation\np_value = correlation_result.pvalue\n\nprint(f\"Point-Biserial Correlation Coefficient: {correlation_coefficient}\")\nprint(f\"P-value: {p_value}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:45:56.527671Z","iopub.execute_input":"2023-11-30T09:45:56.528040Z","iopub.status.idle":"2023-11-30T09:45:56.536310Z","shell.execute_reply.started":"2023-11-30T09:45:56.528011Z","shell.execute_reply":"2023-11-30T09:45:56.535386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:49:33.330989Z","iopub.execute_input":"2023-11-30T09:49:33.331399Z","iopub.status.idle":"2023-11-30T09:49:33.474349Z","shell.execute_reply.started":"2023-11-30T09:49:33.331367Z","shell.execute_reply":"2023-11-30T09:49:33.473442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 8))\nplt.plot(train_x.S_2.unique(), (train_x.drop(\"customer_ID\", axis = 1).groupby(\"S_2\").mean())['D_39'], label='Time Series Data', color='blue')\n\nplt.title('Time Series Data')\nplt.xlabel('Date')\nplt.ylabel('Value')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:49:11.209333Z","iopub.execute_input":"2023-11-30T09:49:11.209700Z","iopub.status.idle":"2023-11-30T09:49:11.661031Z","shell.execute_reply.started":"2023-11-30T09:49:11.209671Z","shell.execute_reply":"2023-11-30T09:49:11.660240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Numerical Variables","metadata":{}},{"cell_type":"code","source":"train_x[numerical] = train_x[numerical].astype('float32')","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:44:57.891552Z","iopub.status.idle":"2023-11-30T09:44:57.891888Z","shell.execute_reply.started":"2023-11-30T09:44:57.891723Z","shell.execute_reply":"2023-11-30T09:44:57.891740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x.sort_values(by='S_2').head(5)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:50:11.841692Z","iopub.execute_input":"2023-11-30T09:50:11.842098Z","iopub.status.idle":"2023-11-30T09:50:11.996420Z","shell.execute_reply.started":"2023-11-30T09:50:11.842045Z","shell.execute_reply":"2023-11-30T09:50:11.995405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\n\n# Assuming 'train_x' is your DataFrame and 'time_series_data' is your time series data\ngrouped_data = train_x.drop(\"customer_ID\", axis = 1).groupby('S_2').mean().reset_index()\n\n# Assuming 'time_series_data' has a datetime index and a column 'value'\nsns.lineplot(x=grouped_data['S_2'], y=grouped_data['D_88'], label='Time Series Data')\n\nplt.title('Time Series Data')\nplt.xlabel('Date')\nplt.ylabel('Value')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:50:13.166267Z","iopub.execute_input":"2023-11-30T09:50:13.166644Z","iopub.status.idle":"2023-11-30T09:50:13.579637Z","shell.execute_reply.started":"2023-11-30T09:50:13.166614Z","shell.execute_reply":"2023-11-30T09:50:13.578769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Categorical Variables","metadata":{}},{"cell_type":"markdown","source":"There are no nan values in any categorical variables. Following shows the number of good and bad customers for each category ","metadata":{}},{"cell_type":"code","source":"train = pd.merge(train_x, train_y, on =\"customer_ID\")\nfor col in categorical:\n    plt.figure(figsize=(10, 6))\n    \n    # Group by target and col, count occurrences, and reset index\n    grouped_data = train.groupby(['target', col]).size().unstack(fill_value=0).reset_index()\n    \n    # Melt the DataFrame to have 'target', col', and 'count' as columns\n    grouped_data_melted = pd.melt(grouped_data, id_vars=['target'], var_name=col, value_name='count')\n    \n    # Create count plot\n    sns.barplot(x=col, y='count', hue='target', data=grouped_data_melted, order=train[col].value_counts(dropna=False).index)\n    \n    plt.title(f'Distribution of {col} by Target')\n    plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:50:24.819186Z","iopub.execute_input":"2023-11-30T09:50:24.819581Z","iopub.status.idle":"2023-11-30T09:50:28.026100Z","shell.execute_reply.started":"2023-11-30T09:50:24.819552Z","shell.execute_reply":"2023-11-30T09:50:28.025059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x[numerical].describe()","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:51:02.798087Z","iopub.execute_input":"2023-11-30T09:51:02.798889Z","iopub.status.idle":"2023-11-30T09:51:03.541511Z","shell.execute_reply.started":"2023-11-30T09:51:02.798848Z","shell.execute_reply":"2023-11-30T09:51:03.540762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ydata_profiling import ProfileReport\nprofile = ProfileReport(train_x, title = \"Profile Report\", minimal=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:44:57.901177Z","iopub.status.idle":"2023-11-30T09:44:57.901849Z","shell.execute_reply.started":"2023-11-30T09:44:57.901662Z","shell.execute_reply":"2023-11-30T09:44:57.901682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# profile.to_widgets()\n\nprofile.to_notebook_iframe()\n","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-11-30T09:44:57.902946Z","iopub.status.idle":"2023-11-30T09:44:57.903624Z","shell.execute_reply.started":"2023-11-30T09:44:57.903425Z","shell.execute_reply":"2023-11-30T09:44:57.903445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Models\n","metadata":{}},{"cell_type":"code","source":"# Sort the DataFrame by 'customer_ID' and 'S_2'\ntrain_x = train_x.sort_values(['customer_ID', 'S_2'])\ntrain_y = train_y.sort_values('customer_ID')\n\n# Calculate the time difference between consecutive statement dates for each customer\ntrain_x['time_to_previous_statement'] = train_x.groupby('customer_ID')['S_2'].diff()\n\n# Display the DataFrame with the new column\ntrain_x[['customer_ID', 'S_2', 'time_to_previous_statement']].head(5)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:51:06.821029Z","iopub.execute_input":"2023-11-30T09:51:06.821442Z","iopub.status.idle":"2023-11-30T09:51:06.847757Z","shell.execute_reply.started":"2023-11-30T09:51:06.821409Z","shell.execute_reply":"2023-11-30T09:51:06.846722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"first_date_per_customer = train_x.groupby('customer_ID')['S_2'].first().dt.month\nmean_time_difference_per_customer = train_x.groupby('customer_ID')['time_to_previous_statement'].mean().dt.days\nhighest_time_difference_per_customer = train_x.groupby('customer_ID')['time_to_previous_statement'].max().dt.days\nleast_time_difference_per_customer = train_x.groupby('customer_ID')['time_to_previous_statement'].min().dt.days\ncount = train_x.groupby('customer_ID')['time_to_previous_statement'].size()\nstd = train_x.groupby('customer_ID')['time_to_previous_statement'].std().dt.days\n\n\n# Display the results\nresult_df = pd.DataFrame({\n    'customer_ID': first_date_per_customer.index,\n    'first_date': first_date_per_customer.values,\n    'mean_time_difference': mean_time_difference_per_customer.values,\n    'max_time' : highest_time_difference_per_customer.values,\n    'min_time' : least_time_difference_per_customer.values,\n    'count' : count.values,\n    'std' : std.values\n})\nresult_df = pd.merge(result_df, train_y, on=\"customer_ID\")\nresult_df = pd.merge(result_df, train_x.drop(['S_2'], axis = 1).groupby(\"customer_ID\").mean(), on=\"customer_ID\")\nresult_df.sort_values('mean_time_difference').head(10)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:51:08.198194Z","iopub.execute_input":"2023-11-30T09:51:08.198591Z","iopub.status.idle":"2023-11-30T09:51:08.435481Z","shell.execute_reply.started":"2023-11-30T09:51:08.198557Z","shell.execute_reply":"2023-11-30T09:51:08.434228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:51:09.539078Z","iopub.execute_input":"2023-11-30T09:51:09.539446Z","iopub.status.idle":"2023-11-30T09:51:10.056190Z","shell.execute_reply.started":"2023-11-30T09:51:09.539418Z","shell.execute_reply":"2023-11-30T09:51:10.055102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_df.drop(['customer_ID'], axis = 1).groupby(\"target\").mean()","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:51:10.345726Z","iopub.execute_input":"2023-11-30T09:51:10.346131Z","iopub.status.idle":"2023-11-30T09:51:10.480461Z","shell.execute_reply.started":"2023-11-30T09:51:10.346096Z","shell.execute_reply":"2023-11-30T09:51:10.479415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:51:11.124921Z","iopub.execute_input":"2023-11-30T09:51:11.125980Z","iopub.status.idle":"2023-11-30T09:51:11.289482Z","shell.execute_reply.started":"2023-11-30T09:51:11.125940Z","shell.execute_reply":"2023-11-30T09:51:11.288372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Decision Tree Implementation","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split, StratifiedKFold\nfrom sklearn.ensemble import HistGradientBoostingClassifier \nfrom sklearn.metrics import accuracy_score, classification_report\n\nresult_df.drop([\"customer_ID\",\"time_to_previous_statement\"], axis = 1, inplace = True)\n\nskf = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)\nlst_accu_stratified = []\nclf = HistGradientBoostingClassifier(random_state=42,max_depth = 50)\n\nx = result_df.drop(\"target\", axis = 1)\ny =  result_df['target']\n\nfor train_index, test_index in skf.split(x,y):\n    x_train_fold, x_test_fold = x.iloc[train_index], x.iloc[test_index]\n    y_train_fold, y_test_fold = y.iloc[train_index], y.iloc[test_index]\n    clf.fit(x_train_fold, y_train_fold)\n    y_pred = clf.predict(x_test_fold)\n\n    # Evaluate the model\n    accuracy = accuracy_score(y_test_fold, y_pred)\n    classification_rep = classification_report(y_test_fold, y_pred)\n\n    print(f\"Accuracy: {accuracy}\")\n    print(\"Classification Report:\")\n    print(classification_rep)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T09:51:28.635125Z","iopub.execute_input":"2023-11-30T09:51:28.635538Z","iopub.status.idle":"2023-11-30T09:51:36.082005Z","shell.execute_reply.started":"2023-11-30T09:51:28.635506Z","shell.execute_reply":"2023-11-30T09:51:36.080952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"test_x = pd.read_feather(\"/kaggle/input/amex-default-prediction-feather/test.feather\")","metadata":{"execution":{"iopub.status.busy":"2023-11-30T10:09:56.792795Z","iopub.execute_input":"2023-11-30T10:09:56.793241Z","iopub.status.idle":"2023-11-30T10:10:19.406483Z","shell.execute_reply.started":"2023-11-30T10:09:56.793204Z","shell.execute_reply":"2023-11-30T10:10:19.402314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.pipeline import Pipeline\n\ntest_x = test_x.sort_values(['customer_ID', 'S_2'])\ntest_x[\"S_2\"] = pd.to_datetime(test_x[\"S_2\"])\n\n# Calculate the time difference between consecutive statement dates for each customer\ntest_x['time_to_previous_statement'] = test_x.groupby('customer_ID')['S_2'].diff()\nfirst_date_per_customer = test_x.groupby('customer_ID')['S_2'].first().dt.month\nmean_time_difference_per_customer = test_x.groupby('customer_ID')['time_to_previous_statement'].mean().dt.days\nhighest_time_difference_per_customer = test_x.groupby('customer_ID')['time_to_previous_statement'].max().dt.days\nleast_time_difference_per_customer = test_x.groupby('customer_ID')['time_to_previous_statement'].min().dt.days\ncount = test_x.groupby('customer_ID')['time_to_previous_statement'].size()\nstd = test_x.groupby('customer_ID')['time_to_previous_statement'].std().dt.days\n\n\ntest_x.drop(['S_2'], axis = 1, inplace = True)\n# Display the results\ntest = pd.DataFrame({\n    'customer_ID': first_date_per_customer.index,\n    'first_date': first_date_per_customer.values,\n    'mean_time_difference': mean_time_difference_per_customer.values,\n    'max_time' : highest_time_difference_per_customer.values,\n    'min_time' : least_time_difference_per_customer.values,\n    'count' : count.values,\n    'std' : std.values\n})\n\ntest = pd.merge(test, test_x.groupby(\"customer_ID\").mean(), on=\"customer_ID\")\n","metadata":{"execution":{"iopub.status.busy":"2023-11-30T10:10:31.214268Z","iopub.execute_input":"2023-11-30T10:10:31.214797Z","iopub.status.idle":"2023-11-30T10:11:56.086556Z","shell.execute_reply.started":"2023-11-30T10:10:31.214752Z","shell.execute_reply":"2023-11-30T10:11:56.085382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"test.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T10:20:48.450099Z","iopub.execute_input":"2023-11-30T10:20:48.450559Z","iopub.status.idle":"2023-11-30T10:20:48.622988Z","shell.execute_reply.started":"2023-11-30T10:20:48.450523Z","shell.execute_reply":"2023-11-30T10:20:48.621837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = clf.predict(test.drop([\"customer_ID\", \"time_to_previous_statement\"], axis = 1))","metadata":{"execution":{"iopub.status.busy":"2023-11-30T10:21:21.065667Z","iopub.execute_input":"2023-11-30T10:21:21.066139Z","iopub.status.idle":"2023-11-30T10:21:30.558585Z","shell.execute_reply.started":"2023-11-30T10:21:21.066102Z","shell.execute_reply":"2023-11-30T10:21:30.557501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"prediction\"] = pred","metadata":{"execution":{"iopub.status.busy":"2023-11-30T10:23:26.326123Z","iopub.execute_input":"2023-11-30T10:23:26.326550Z","iopub.status.idle":"2023-11-30T10:23:26.333342Z","shell.execute_reply.started":"2023-11-30T10:23:26.326516Z","shell.execute_reply":"2023-11-30T10:23:26.332106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = test[['customer_ID', 'prediction']].copy()\nsub_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-30T10:23:39.185103Z","iopub.execute_input":"2023-11-30T10:23:39.185526Z","iopub.status.idle":"2023-11-30T10:23:41.836351Z","shell.execute_reply.started":"2023-11-30T10:23:39.185490Z","shell.execute_reply":"2023-11-30T10:23:41.835142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}