{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n\nimport os\n## for dirname, _, filenames in os.walk('/kaggle/input'):\n##    for filename in filenames:\n##        print(os.path.join(dirname, filename))\n\nfrom matplotlib import pyplot as plt\nfrom sklearn.ensemble import GradientBoostingClassifier\nfrom sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.metrics import roc_auc_score, classification_report, average_precision_score\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.preprocessing import OneHotEncoder\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-15T03:55:55.177499Z","iopub.execute_input":"2022-03-15T03:55:55.177837Z","iopub.status.idle":"2022-03-15T03:55:55.185411Z","shell.execute_reply.started":"2022-03-15T03:55:55.177804Z","shell.execute_reply":"2022-03-15T03:55:55.184349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfRaw = pd.read_csv('../input/hnm-data-process/merged_reduced_dataset.csv', sep='\\,')\ndfRaw.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:21:45.730802Z","iopub.execute_input":"2022-03-15T03:21:45.731151Z","iopub.status.idle":"2022-03-15T03:21:52.545204Z","shell.execute_reply.started":"2022-03-15T03:21:45.731087Z","shell.execute_reply":"2022-03-15T03:21:52.544564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = dfRaw.drop(['FN', 'fashion_news_frequency'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:21:55.377005Z","iopub.execute_input":"2022-03-15T03:21:55.377498Z","iopub.status.idle":"2022-03-15T03:21:55.402175Z","shell.execute_reply.started":"2022-03-15T03:21:55.377432Z","shell.execute_reply":"2022-03-15T03:21:55.401375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:21:57.567726Z","iopub.execute_input":"2022-03-15T03:21:57.568307Z","iopub.status.idle":"2022-03-15T03:21:57.574304Z","shell.execute_reply.started":"2022-03-15T03:21:57.568267Z","shell.execute_reply":"2022-03-15T03:21:57.573364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:22:00.007153Z","iopub.execute_input":"2022-03-15T03:22:00.007625Z","iopub.status.idle":"2022-03-15T03:22:00.015569Z","shell.execute_reply.started":"2022-03-15T03:22:00.007573Z","shell.execute_reply":"2022-03-15T03:22:00.014701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['purchase'] = df['purchase'] * 1\n## 1: purchased; 0 not purchased\ndf['purchase'].value_counts().plot(kind='bar')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:22:02.477245Z","iopub.execute_input":"2022-03-15T03:22:02.477555Z","iopub.status.idle":"2022-03-15T03:22:02.664067Z","shell.execute_reply.started":"2022-03-15T03:22:02.477517Z","shell.execute_reply":"2022-03-15T03:22:02.663066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.groupby('article2idx').purchase.mean().reset_index().head(10)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:22:05.145715Z","iopub.execute_input":"2022-03-15T03:22:05.146611Z","iopub.status.idle":"2022-03-15T03:22:05.173407Z","shell.execute_reply.started":"2022-03-15T03:22:05.146568Z","shell.execute_reply":"2022-03-15T03:22:05.17242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.groupby('cust2idx').purchase.mean().reset_index().head()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:22:07.460733Z","iopub.execute_input":"2022-03-15T03:22:07.46101Z","iopub.status.idle":"2022-03-15T03:22:07.481291Z","shell.execute_reply.started":"2022-03-15T03:22:07.460981Z","shell.execute_reply":"2022-03-15T03:22:07.48065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df[:160000]\ndf_test = df[160001:]\nprint(df_train.shape, df_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:22:09.806437Z","iopub.execute_input":"2022-03-15T03:22:09.806773Z","iopub.status.idle":"2022-03-15T03:22:09.812482Z","shell.execute_reply.started":"2022-03-15T03:22:09.806742Z","shell.execute_reply":"2022-03-15T03:22:09.811677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:01:51.406059Z","iopub.execute_input":"2022-03-15T03:01:51.406774Z","iopub.status.idle":"2022-03-15T03:01:51.415387Z","shell.execute_reply.started":"2022-03-15T03:01:51.406701Z","shell.execute_reply":"2022-03-15T03:01:51.414556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# discrete features\nCATEGORICAL_FEATURES = ['perceived_colour_value_id', 'Active', 'club_member_status']\n# continuous features\nCONTINUOUS_FEATURES = ['price', 'age']\n# vector_features: non in this dataset ","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:22:38.350103Z","iopub.execute_input":"2022-03-15T03:22:38.350612Z","iopub.status.idle":"2022-03-15T03:22:38.354709Z","shell.execute_reply.started":"2022-03-15T03:22:38.350552Z","shell.execute_reply":"2022-03-15T03:22:38.354072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_label(df_):\n    return df_['purchase'].values","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:22:41.389834Z","iopub.execute_input":"2022-03-15T03:22:41.3908Z","iopub.status.idle":"2022-03-15T03:22:41.395446Z","shell.execute_reply.started":"2022-03-15T03:22:41.390737Z","shell.execute_reply":"2022-03-15T03:22:41.394309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_continuous_features(df_):\n    return df_[CONTINUOUS_FEATURES].values","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:22:43.285886Z","iopub.execute_input":"2022-03-15T03:22:43.286737Z","iopub.status.idle":"2022-03-15T03:22:43.290962Z","shell.execute_reply.started":"2022-03-15T03:22:43.286692Z","shell.execute_reply":"2022-03-15T03:22:43.290019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_categorical_features(df_train, df_test):\n    feats_all = pd.concat([df_train[CATEGORICAL_FEATURES], df_test[CATEGORICAL_FEATURES]], axis=0).values\n    enc = OneHotEncoder()\n    enc.fit(feats_all)\n    return enc.transform(df_train[CATEGORICAL_FEATURES].values).toarray(), enc.transform(df_test[CATEGORICAL_FEATURES].values).toarray()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:22:45.684738Z","iopub.execute_input":"2022-03-15T03:22:45.68506Z","iopub.status.idle":"2022-03-15T03:22:45.69084Z","shell.execute_reply.started":"2022-03-15T03:22:45.685025Z","shell.execute_reply":"2022-03-15T03:22:45.690136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_vector_features(df_train, df_test):\n    feats_train, feats_test = None, None\n    for f_key in VECTOR_FEATURES:\n        cv = CountVectorizer()\n        cv.fit(pd.concat([df_train[f_key], df_test[f_key]], axis=0).values)\n        if not feats_train:\n            feats_train = cv.transform(df_train[f_key].values).toarray()\n            feats_test = cv.transform(df_test[f_key].values).toarray()\n        else:\n            feats_train = np.hstack([feats_train, cv.transform(df_train[f_key].values).toarray()])\n            feats_test = np.hstack([feats_test, cv.transform(df_test[f_key].values).toarray()])\n    return feats_train, feats_test","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:22:47.830497Z","iopub.execute_input":"2022-03-15T03:22:47.830942Z","iopub.status.idle":"2022-03-15T03:22:47.839027Z","shell.execute_reply.started":"2022-03-15T03:22:47.830911Z","shell.execute_reply":"2022-03-15T03:22:47.838185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['price'] = df['price'].fillna(df.groupby('article2idx')['price'].transform('mean'))","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:22:50.474815Z","iopub.execute_input":"2022-03-15T03:22:50.475094Z","iopub.status.idle":"2022-03-15T03:22:50.501799Z","shell.execute_reply.started":"2022-03-15T03:22:50.475065Z","shell.execute_reply":"2022-03-15T03:22:50.500851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['price'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:22:52.43454Z","iopub.execute_input":"2022-03-15T03:22:52.435245Z","iopub.status.idle":"2022-03-15T03:22:52.443056Z","shell.execute_reply.started":"2022-03-15T03:22:52.434945Z","shell.execute_reply":"2022-03-15T03:22:52.442056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['age'].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:22:54.416119Z","iopub.execute_input":"2022-03-15T03:22:54.416601Z","iopub.status.idle":"2022-03-15T03:22:54.423962Z","shell.execute_reply.started":"2022-03-15T03:22:54.416558Z","shell.execute_reply":"2022-03-15T03:22:54.4233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_gbm = get_continuous_features(df_train)\ny_train = get_label(df_train)\nX_test_gbm = get_continuous_features(df_test)\ny_test = get_label(df_test)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:22:56.917138Z","iopub.execute_input":"2022-03-15T03:22:56.917594Z","iopub.status.idle":"2022-03-15T03:22:56.94602Z","shell.execute_reply.started":"2022-03-15T03:22:56.917552Z","shell.execute_reply":"2022-03-15T03:22:56.945228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gbm = GradientBoostingClassifier(n_estimators=20, max_depth=2, random_state=0, verbose=1)\ngbm.fit(X_train_gbm, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:46:02.30732Z","iopub.execute_input":"2022-03-15T03:46:02.3077Z","iopub.status.idle":"2022-03-15T03:46:03.665459Z","shell.execute_reply.started":"2022-03-15T03:46:02.307657Z","shell.execute_reply":"2022-03-15T03:46:03.66443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_lr_gbm = np.squeeze(gbm.apply(X_train_gbm))\nX_test_lr_gbm = np.squeeze(gbm.apply(X_test_gbm))\nprint(X_train_lr_gbm.shape, X_test_lr_gbm.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:46:07.66686Z","iopub.execute_input":"2022-03-15T03:46:07.667352Z","iopub.status.idle":"2022-03-15T03:46:07.810516Z","shell.execute_reply.started":"2022-03-15T03:46:07.667306Z","shell.execute_reply":"2022-03-15T03:46:07.809552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# onehot\nenc = OneHotEncoder()\nenc.fit(X_train_lr_gbm)\nX_train_lr_gbm_oh = enc.transform(X_train_lr_gbm).toarray()\nX_test_lr_gbm_oh = enc.transform(X_test_lr_gbm).toarray()\nprint(X_train_lr_gbm_oh.shape, X_test_lr_gbm_oh.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:46:10.131766Z","iopub.execute_input":"2022-03-15T03:46:10.132097Z","iopub.status.idle":"2022-03-15T03:46:10.99284Z","shell.execute_reply.started":"2022-03-15T03:46:10.13206Z","shell.execute_reply":"2022-03-15T03:46:10.991898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_lr_category, X_test_lr_category = get_categorical_features(df_train, df_test)\nprint(X_train_lr_category.shape, X_test_lr_category.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:46:15.594988Z","iopub.execute_input":"2022-03-15T03:46:15.595325Z","iopub.status.idle":"2022-03-15T03:46:15.963057Z","shell.execute_reply.started":"2022-03-15T03:46:15.595287Z","shell.execute_reply":"2022-03-15T03:46:15.962043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_lr = np.hstack([X_train_lr_gbm_oh, X_train_lr_category])\nX_test_lr = np.hstack([X_test_lr_gbm_oh, X_test_lr_category])\nprint(X_train_lr.shape, X_test_lr.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:46:21.081779Z","iopub.execute_input":"2022-03-15T03:46:21.082058Z","iopub.status.idle":"2022-03-15T03:46:21.177229Z","shell.execute_reply.started":"2022-03-15T03:46:21.082028Z","shell.execute_reply":"2022-03-15T03:46:21.176168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lrm = LogisticRegression(C=100, max_iter=500, n_jobs=8)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:46:24.036497Z","iopub.execute_input":"2022-03-15T03:46:24.036969Z","iopub.status.idle":"2022-03-15T03:46:24.040578Z","shell.execute_reply.started":"2022-03-15T03:46:24.036925Z","shell.execute_reply":"2022-03-15T03:46:24.039915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lrm.fit(X_train_lr, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:46:26.424964Z","iopub.execute_input":"2022-03-15T03:46:26.425346Z","iopub.status.idle":"2022-03-15T03:48:31.571235Z","shell.execute_reply.started":"2022-03-15T03:46:26.425311Z","shell.execute_reply":"2022-03-15T03:48:31.569961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prob_test = lrm.predict_proba(X_test_lr)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:49:39.786873Z","iopub.execute_input":"2022-03-15T03:49:39.7873Z","iopub.status.idle":"2022-03-15T03:49:39.809549Z","shell.execute_reply.started":"2022-03-15T03:49:39.787256Z","shell.execute_reply":"2022-03-15T03:49:39.808518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roc_auc_score(y_test, prob_test[:, 1])","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:49:53.076538Z","iopub.execute_input":"2022-03-15T03:49:53.077152Z","iopub.status.idle":"2022-03-15T03:49:53.115898Z","shell.execute_reply.started":"2022-03-15T03:49:53.077102Z","shell.execute_reply":"2022-03-15T03:49:53.115037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_values = df_test['purchase'].values","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:57:51.311334Z","iopub.execute_input":"2022-03-15T03:57:51.312346Z","iopub.status.idle":"2022-03-15T03:57:51.317565Z","shell.execute_reply.started":"2022-03-15T03:57:51.312286Z","shell.execute_reply":"2022-03-15T03:57:51.316881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"average_precision_score(true_values, prob_test[:, 1])","metadata":{"execution":{"iopub.status.busy":"2022-03-15T03:59:11.966046Z","iopub.execute_input":"2022-03-15T03:59:11.966386Z","iopub.status.idle":"2022-03-15T03:59:11.994432Z","shell.execute_reply.started":"2022-03-15T03:59:11.966342Z","shell.execute_reply":"2022-03-15T03:59:11.993512Z"},"trusted":true},"execution_count":null,"outputs":[]}]}