{"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":"# 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)\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        \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-06-14T14:51:49.009600Z","iopub.execute_input":"2023-06-14T14:51:49.010466Z","iopub.status.idle":"2023-06-14T14:51:49.047427Z","shell.execute_reply.started":"2023-06-14T14:51:49.010434Z","shell.execute_reply":"2023-06-14T14:51:49.046364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport random\nimport gc","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:51:51.601882Z","iopub.execute_input":"2023-06-14T14:51:51.602290Z","iopub.status.idle":"2023-06-14T14:51:52.988191Z","shell.execute_reply.started":"2023-06-14T14:51:51.602255Z","shell.execute_reply":"2023-06-14T14:51:52.987224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtype={'id': np.dtype(int),\n    'click': np.dtype(int),\n    'hour': np.str,\n    'C1': np.dtype(str),\n    'banner_pos': np.dtype(str),\n    'site_id': np.dtype(str),\n    'site_domain': np.dtype(str), \n    'site_category': np.dtype(str),\n    'app_id': np.dtype(str),\n    'app_domain': np.dtype(str),\n    'app_category': np.dtype(str),\n    'device_id': np.dtype(str),\n    'device_ip': np.dtype(str),\n    'device_model': np.dtype(str),\n    'device_type': np.dtype(str),\n    'device_conn_type': np.dtype(str),\n    'C14': np.dtype(str),\n    'C15': np.dtype(str),\n    'C16': np.dtype(str),\n    'C17': np.dtype(str),\n    'C18': np.dtype(str),\n    'C19': np.dtype(str),\n    'C20': np.dtype(str),\n    'C21':np.dtype(str)\n      }","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:51:54.450472Z","iopub.execute_input":"2023-06-14T14:51:54.450826Z","iopub.status.idle":"2023-06-14T14:51:54.459021Z","shell.execute_reply.started":"2023-06-14T14:51:54.450774Z","shell.execute_reply":"2023-06-14T14:51:54.457432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 0 导入数据","metadata":{}},{"cell_type":"code","source":"num_records = 40428967\nsample_size = 8000000\nskip_values = sorted(random.sample(range(1,num_records), num_records - sample_size))\nparse_date = lambda val : pd.datetime.strptime(val, '%y%m%d%H')\n\ndata = pd.read_csv(\"../input/avazu-ctr-prediction/train.gz\", parse_dates=['hour'], date_parser=parse_date, dtype=dtype, skiprows=skip_values)\nprint('dataset:',data.shape)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:51:58.652005Z","iopub.execute_input":"2023-06-14T14:51:58.652614Z","iopub.status.idle":"2023-06-14T14:56:11.047215Z","shell.execute_reply.started":"2023-06-14T14:51:58.652583Z","shell.execute_reply":"2023-06-14T14:56:11.046294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"认识数据，浏览数据特征","metadata":{}},{"cell_type":"code","source":"# 查看是否有缺失值\ndata.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-06-14T01:27:33.268555Z","iopub.execute_input":"2023-06-14T01:27:33.268916Z","iopub.status.idle":"2023-06-14T01:28:17.576199Z","shell.execute_reply.started":"2023-06-14T01:27:33.268887Z","shell.execute_reply":"2023-06-14T01:28:17.575357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# click\ndata.click.value_counts().plot(kind='bar')\nprint(data.click.value_counts())","metadata":{"execution":{"iopub.status.busy":"2023-06-14T01:28:46.282579Z","iopub.execute_input":"2023-06-14T01:28:46.282935Z","iopub.status.idle":"2023-06-14T01:28:46.667289Z","shell.execute_reply.started":"2023-06-14T01:28:46.282907Z","shell.execute_reply":"2023-06-14T01:28:46.666314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**目标值不平衡**","metadata":{}},{"cell_type":"code","source":"# banner_pos\nprint(data.banner_pos.value_counts())","metadata":{"execution":{"iopub.status.busy":"2023-06-13T01:27:06.659855Z","iopub.execute_input":"2023-06-13T01:27:06.660222Z","iopub.status.idle":"2023-06-13T01:27:07.164610Z","shell.execute_reply.started":"2023-06-13T01:27:06.660177Z","shell.execute_reply":"2023-06-13T01:27:07.163620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='banner_pos',hue='click',data=data)","metadata":{"execution":{"iopub.status.busy":"2023-06-13T01:27:09.875030Z","iopub.execute_input":"2023-06-13T01:27:09.875713Z","iopub.status.idle":"2023-06-13T01:27:15.687876Z","shell.execute_reply.started":"2023-06-13T01:27:09.875681Z","shell.execute_reply":"2023-06-13T01:27:15.686922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**banner_pos并没有显著增加用户的点击次数**","metadata":{}},{"cell_type":"code","source":"sns.countplot(x='hour',hue='click',data=data)","metadata":{"execution":{"iopub.status.busy":"2023-06-13T01:27:18.789903Z","iopub.execute_input":"2023-06-13T01:27:18.792925Z","iopub.status.idle":"2023-06-13T01:27:23.581018Z","shell.execute_reply.started":"2023-06-13T01:27:18.792886Z","shell.execute_reply":"2023-06-13T01:27:23.580096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1 特征工程","metadata":{}},{"cell_type":"markdown","source":"**sparse feature**\n* C1\n* banner_pos\n* site_id\n* site_domain\n* site_category\n* app_id\n* app_domain\n* app_category\n* device_id\n* device_ip\n* device_model\n* device_type\n* device_conn_type\n\n**Dense feature**\n* C14 ~ c21","metadata":{}},{"cell_type":"code","source":"!pip install deepctr","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:59:14.645039Z","iopub.execute_input":"2023-06-14T14:59:14.645362Z","iopub.status.idle":"2023-06-14T14:59:26.302258Z","shell.execute_reply.started":"2023-06-14T14:59:14.645337Z","shell.execute_reply":"2023-06-14T14:59:26.301381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import deepctr\nfrom deepctr.feature_column import SparseFeat, DenseFeat,get_feature_names\nfrom deepctr.models import *","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:59:26.303686Z","iopub.execute_input":"2023-06-14T14:59:26.303974Z","iopub.status.idle":"2023-06-14T14:59:33.947301Z","shell.execute_reply.started":"2023-06-14T14:59:26.303950Z","shell.execute_reply":"2023-06-14T14:59:33.945997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 划分稀疏特征和稠密特征\nsparse_features = data.iloc[::,2:14].columns.values.tolist()\ndense_features = data.iloc[::,15:].columns.values.tolist()\nsparse_features.append('id')\ntarget = ['click']","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:59:38.872950Z","iopub.execute_input":"2023-06-14T14:59:38.873689Z","iopub.status.idle":"2023-06-14T14:59:40.888475Z","shell.execute_reply.started":"2023-06-14T14:59:38.873659Z","shell.execute_reply":"2023-06-14T14:59:40.887188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler,LabelEncoder\nscaler = MinMaxScaler(feature_range=(0,1))  # 数据归一化\nencoder = LabelEncoder()  # 数据编码","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:59:46.308863Z","iopub.execute_input":"2023-06-14T14:59:46.309357Z","iopub.status.idle":"2023-06-14T14:59:46.386308Z","shell.execute_reply.started":"2023-06-14T14:59:46.309325Z","shell.execute_reply":"2023-06-14T14:59:46.384709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for feat in sparse_features:\n    data[feat] = encoder.fit_transform(data[feat])  # 稀疏数据编码\ndata[dense_features] = scaler.fit_transform(data[dense_features])  # 稠密数据归一化","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:59:48.063004Z","iopub.execute_input":"2023-06-14T14:59:48.063338Z","iopub.status.idle":"2023-06-14T15:00:52.407249Z","shell.execute_reply.started":"2023-06-14T14:59:48.063316Z","shell.execute_reply":"2023-06-14T15:00:52.405398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# SparseFeat：将类别特征转换为固定维度的稠密特征\n# DenseFeat：将稠密特征转为向量的形式，并使用transform_fn 函数对其做归一化操作或者其它的线性或非线性变换。\nfixlen_feature_columns = [SparseFeat(feat,data[feat].nunique()) for feat in sparse_features] + [DenseFeat(feat,1) for feat in dense_features]","metadata":{"execution":{"iopub.status.busy":"2023-06-14T15:01:01.303053Z","iopub.execute_input":"2023-06-14T15:01:01.303408Z","iopub.status.idle":"2023-06-14T15:01:02.853377Z","shell.execute_reply.started":"2023-06-14T15:01:01.303383Z","shell.execute_reply":"2023-06-14T15:01:02.851924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fixlen_feature_columns","metadata":{"execution":{"iopub.status.busy":"2023-06-14T15:01:02.855388Z","iopub.execute_input":"2023-06-14T15:01:02.855876Z","iopub.status.idle":"2023-06-14T15:01:02.864612Z","shell.execute_reply.started":"2023-06-14T15:01:02.855846Z","shell.execute_reply":"2023-06-14T15:01:02.863132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dnn_feature_columns = fixlen_feature_columns\nlinear_feature_columns = fixlen_feature_columns","metadata":{"execution":{"iopub.status.busy":"2023-06-14T15:01:04.064716Z","iopub.execute_input":"2023-06-14T15:01:04.065064Z","iopub.status.idle":"2023-06-14T15:01:04.070329Z","shell.execute_reply.started":"2023-06-14T15:01:04.065039Z","shell.execute_reply":"2023-06-14T15:01:04.069331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_names = get_feature_names(linear_feature_columns + dnn_feature_columns)\nfeature_names","metadata":{"execution":{"iopub.status.busy":"2023-06-14T15:01:04.336901Z","iopub.execute_input":"2023-06-14T15:01:04.337291Z","iopub.status.idle":"2023-06-14T15:01:04.408199Z","shell.execute_reply.started":"2023-06-14T15:01:04.337261Z","shell.execute_reply":"2023-06-14T15:01:04.406629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2 模型训练","metadata":{}},{"cell_type":"markdown","source":"## 2.0 分割数据集","metadata":{}},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:34:18.916303Z","iopub.execute_input":"2023-06-13T08:34:18.916948Z","iopub.status.idle":"2023-06-13T08:34:18.948180Z","shell.execute_reply.started":"2023-06-13T08:34:18.916913Z","shell.execute_reply":"2023-06-13T08:34:18.947151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data.shape)","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:34:18.950310Z","iopub.execute_input":"2023-06-13T08:34:18.950737Z","iopub.status.idle":"2023-06-13T08:34:18.956646Z","shell.execute_reply.started":"2023-06-13T08:34:18.950687Z","shell.execute_reply":"2023-06-13T08:34:18.955371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 分割训练集和测试集\nfrom sklearn.model_selection import train_test_split\ntrain_data, test_data = train_test_split(data,test_size = 0.2)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T15:01:07.877386Z","iopub.execute_input":"2023-06-14T15:01:07.878003Z","iopub.status.idle":"2023-06-14T15:01:11.875339Z","shell.execute_reply.started":"2023-06-14T15:01:07.877977Z","shell.execute_reply":"2023-06-14T15:01:11.873684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"对训练数据进行欠采样处理","metadata":{}},{"cell_type":"code","source":"# 获取所有click=0的index\ntrain_data = train_data.reset_index(drop=True)\nindex0 = train_data.index[train_data['click'] == 0].tolist()\nnum = len(train_data[train_data['click']==1])\nkeep_index = random.sample(index0, num)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T15:01:14.836187Z","iopub.execute_input":"2023-06-14T15:01:14.836512Z","iopub.status.idle":"2023-06-14T15:01:17.179004Z","shell.execute_reply.started":"2023-06-14T15:01:14.836487Z","shell.execute_reply":"2023-06-14T15:01:17.178053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keep_index += train_data.index[train_data['click'] == 1].tolist()\nkeep_index_array = np.array(keep_index)\nmask = np.isin(train_data.index, keep_index_array)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T15:01:17.180723Z","iopub.execute_input":"2023-06-14T15:01:17.181074Z","iopub.status.idle":"2023-06-14T15:01:18.505213Z","shell.execute_reply.started":"2023-06-14T15:01:17.181050Z","shell.execute_reply":"2023-06-14T15:01:18.503974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train_data[mask]","metadata":{"execution":{"iopub.status.busy":"2023-06-14T15:01:18.506994Z","iopub.execute_input":"2023-06-14T15:01:18.507294Z","iopub.status.idle":"2023-06-14T15:01:18.721692Z","shell.execute_reply.started":"2023-06-14T15:01:18.507268Z","shell.execute_reply":"2023-06-14T15:01:18.720332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_data[train_data['click']==0]))\nprint(len(train_data[train_data['click']==1]))","metadata":{"execution":{"iopub.status.busy":"2023-06-14T15:01:19.429115Z","iopub.execute_input":"2023-06-14T15:01:19.429462Z","iopub.status.idle":"2023-06-14T15:01:19.606425Z","shell.execute_reply.started":"2023-06-14T15:01:19.429434Z","shell.execute_reply":"2023-06-14T15:01:19.604903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X = {name: train_data[name]for name in feature_names}\ntrain_Y = train_data[target].values","metadata":{"execution":{"iopub.status.busy":"2023-06-14T15:01:21.364851Z","iopub.execute_input":"2023-06-14T15:01:21.365188Z","iopub.status.idle":"2023-06-14T15:01:21.525373Z","shell.execute_reply.started":"2023-06-14T15:01:21.365164Z","shell.execute_reply":"2023-06-14T15:01:21.523911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_X = {name: test_data[name] for name in feature_names}\ntest_Y = test_data[target].values","metadata":{"execution":{"iopub.status.busy":"2023-06-14T15:01:21.651288Z","iopub.execute_input":"2023-06-14T15:01:21.651632Z","iopub.status.idle":"2023-06-14T15:01:21.659405Z","shell.execute_reply.started":"2023-06-14T15:01:21.651606Z","shell.execute_reply":"2023-06-14T15:01:21.658403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import log_loss\nimport torch\nimport torch.nn as nn\ndef model_estimation(predict_Y, label):\n    predict_Y_01 = [1 if item > 0.75 else 0 for item in predict_Y]\n    \n    # Accuracy\n    print('Accuracy:', accuracy_score(test_Y, predict_Y_01))\n    # Binary Cross Entropy Loss\n    predict_y_tensor = torch.Tensor(predict_Y)\n    test_y_BCE = torch.Tensor(test_Y)\n    predict_y_BCE = nn.Sigmoid()(predict_y_tensor)\n    loss = nn.BCELoss()\n    print('Binary Cross Entropy Loss:', loss(predict_y_BCE, test_y_BCE))\n    # LogLoss\n    print('Log Loss:', log_loss(y_true, y_pred))\n    # Precision\n    print('Precision:', precision_score(test_Y, predict_Y_01))\n    # Recall\n    print('Recall:', recall_score(test_Y, predict_Y_01))\n    # F1-score\n    print('F1-score:', f1_score(test_Y, predict_Y_01))\n    # AUC\n    print('AUC:', roc_auc_score(test_Y, predict_Y))\n    \n    # 绘制混淆矩阵\n    cm = confusion_matrix(test_Y, predict_Y_01)\n    sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=['0','1'], yticklabels=['0','1'])\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n    plt.title(label)\n    plt.show()\n    plt.savefig(f'{label}.jpg')","metadata":{"execution":{"iopub.status.busy":"2023-06-14T15:01:23.308384Z","iopub.execute_input":"2023-06-14T15:01:23.308744Z","iopub.status.idle":"2023-06-14T15:01:25.081931Z","shell.execute_reply.started":"2023-06-14T15:01:23.308717Z","shell.execute_reply":"2023-06-14T15:01:25.080154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2.1 FNN","metadata":{}},{"cell_type":"code","source":"FNN_model = FNN(linear_feature_columns, dnn_feature_columns, dnn_dropout=0.5, l2_reg_embedding=0.001, l2_reg_linear=0.001, l2_reg_dnn=0.001)\nFNN_model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['binary_crossentropy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-14T15:01:31.670264Z","iopub.execute_input":"2023-06-14T15:01:31.671398Z","iopub.status.idle":"2023-06-14T15:01:33.119679Z","shell.execute_reply.started":"2023-06-14T15:01:31.671355Z","shell.execute_reply":"2023-06-14T15:01:33.118247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nFNN_history = FNN_model.fit(train_X, train_Y, batch_size=1024, epochs=10, verbose=1, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T15:01:34.350588Z","iopub.execute_input":"2023-06-14T15:01:34.351127Z","iopub.status.idle":"2023-06-14T15:05:23.153228Z","shell.execute_reply.started":"2023-06-14T15:01:34.351100Z","shell.execute_reply":"2023-06-14T15:05:23.151587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FNN_predict_Y = FNN_model.predict(test_X, 1000)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:08:03.784603Z","iopub.execute_input":"2023-06-14T13:08:03.784962Z","iopub.status.idle":"2023-06-14T13:08:14.731255Z","shell.execute_reply.started":"2023-06-14T13:08:03.784933Z","shell.execute_reply":"2023-06-14T13:08:14.730023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_estimation(FNN_predict_Y, 'FNN')","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:08:43.398429Z","iopub.execute_input":"2023-06-14T13:08:43.399513Z","iopub.status.idle":"2023-06-14T13:08:56.164413Z","shell.execute_reply.started":"2023-06-14T13:08:43.399471Z","shell.execute_reply":"2023-06-14T13:08:56.163503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2.2 PNN","metadata":{}},{"cell_type":"code","source":"PNN_model = PNN(dnn_feature_columns, task='binary', dnn_dropout=0.5, l2_reg_embedding=0.001, l2_reg_dnn=0.001)\nPNN_model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['binary_crossentropy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:09:10.128482Z","iopub.execute_input":"2023-06-14T13:09:10.129168Z","iopub.status.idle":"2023-06-14T13:09:10.779258Z","shell.execute_reply.started":"2023-06-14T13:09:10.129134Z","shell.execute_reply":"2023-06-14T13:09:10.778276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nPNN_history = PNN_model.fit(train_X, train_Y, batch_size=1024, epochs=10, verbose=1, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:09:13.357913Z","iopub.execute_input":"2023-06-14T13:09:13.358274Z","iopub.status.idle":"2023-06-14T13:14:38.781659Z","shell.execute_reply.started":"2023-06-14T13:09:13.358241Z","shell.execute_reply":"2023-06-14T13:14:38.780444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PNN_predict_Y = PNN_model.predict(test_X, 1000)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:14:49.675381Z","iopub.execute_input":"2023-06-14T13:14:49.675778Z","iopub.status.idle":"2023-06-14T13:15:00.399432Z","shell.execute_reply.started":"2023-06-14T13:14:49.675745Z","shell.execute_reply":"2023-06-14T13:15:00.398373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_estimation(PNN_predict_Y, 'PNN')","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:15:16.374720Z","iopub.execute_input":"2023-06-14T13:15:16.375091Z","iopub.status.idle":"2023-06-14T13:15:27.520417Z","shell.execute_reply.started":"2023-06-14T13:15:16.375064Z","shell.execute_reply":"2023-06-14T13:15:27.519503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2.3 NFM","metadata":{}},{"cell_type":"code","source":"NFM_model = NFM(linear_feature_columns,dnn_feature_columns, task='binary', dnn_dropout=0.5, l2_reg_embedding=0.001, l2_reg_linear=0.001, l2_reg_dnn=0.001)\nNFM_model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['binary_crossentropy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:16:02.920984Z","iopub.execute_input":"2023-06-14T13:16:02.921378Z","iopub.status.idle":"2023-06-14T13:16:03.410858Z","shell.execute_reply.started":"2023-06-14T13:16:02.921347Z","shell.execute_reply":"2023-06-14T13:16:03.409906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nNFM_history = NFM_model.fit(train_X, train_Y, batch_size=1000, epochs=10, verbose=1, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:16:08.737070Z","iopub.execute_input":"2023-06-14T13:16:08.737430Z","iopub.status.idle":"2023-06-14T13:22:33.841680Z","shell.execute_reply.started":"2023-06-14T13:16:08.737400Z","shell.execute_reply":"2023-06-14T13:22:33.840406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NFM_predict_Y = NFM_model.predict(test_X, 1000)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:30:31.915843Z","iopub.execute_input":"2023-06-14T13:30:31.916197Z","iopub.status.idle":"2023-06-14T13:30:38.982925Z","shell.execute_reply.started":"2023-06-14T13:30:31.916170Z","shell.execute_reply":"2023-06-14T13:30:38.981877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_estimation(NFM_predict_Y, 'NFM')","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:30:38.984776Z","iopub.execute_input":"2023-06-14T13:30:38.985064Z","iopub.status.idle":"2023-06-14T13:30:51.786454Z","shell.execute_reply.started":"2023-06-14T13:30:38.985041Z","shell.execute_reply":"2023-06-14T13:30:51.785518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2.4 AFM","metadata":{}},{"cell_type":"code","source":"del NFM_model","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:31:33.676123Z","iopub.execute_input":"2023-06-14T13:31:33.676477Z","iopub.status.idle":"2023-06-14T13:31:33.680902Z","shell.execute_reply.started":"2023-06-14T13:31:33.676447Z","shell.execute_reply":"2023-06-14T13:31:33.679765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AFM_dnn_feature_columns = [SparseFeat(feat,data[feat].nunique()) for feat in sparse_features]","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:31:38.076433Z","iopub.execute_input":"2023-06-14T13:31:38.077109Z","iopub.status.idle":"2023-06-14T13:31:39.947670Z","shell.execute_reply.started":"2023-06-14T13:31:38.077074Z","shell.execute_reply":"2023-06-14T13:31:39.946649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AFM_model = AFM(linear_feature_columns,AFM_dnn_feature_columns, task='binary', afm_dropout=0.5, l2_reg_embedding=0.001, l2_reg_linear=0.001, l2_reg_att=0.001)\nAFM_model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['binary_crossentropy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:32:59.643114Z","iopub.execute_input":"2023-06-14T13:32:59.643753Z","iopub.status.idle":"2023-06-14T13:33:00.038543Z","shell.execute_reply.started":"2023-06-14T13:32:59.643721Z","shell.execute_reply":"2023-06-14T13:33:00.037642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nAFM_history = AFM_model.fit(train_X, train_Y, batch_size=1024, epochs=10, verbose=1, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:33:03.830247Z","iopub.execute_input":"2023-06-14T13:33:03.830642Z","iopub.status.idle":"2023-06-14T13:40:28.973598Z","shell.execute_reply.started":"2023-06-14T13:33:03.830603Z","shell.execute_reply":"2023-06-14T13:40:28.972365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AFM_predict_Y = AFM_model.predict(test_X, 1000)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:40:28.975783Z","iopub.execute_input":"2023-06-14T13:40:28.976159Z","iopub.status.idle":"2023-06-14T13:40:39.748218Z","shell.execute_reply.started":"2023-06-14T13:40:28.976125Z","shell.execute_reply":"2023-06-14T13:40:39.747207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_estimation(AFM_predict_Y, 'AFM')","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:41:06.029683Z","iopub.execute_input":"2023-06-14T13:41:06.030069Z","iopub.status.idle":"2023-06-14T13:41:17.395453Z","shell.execute_reply.started":"2023-06-14T13:41:06.030041Z","shell.execute_reply":"2023-06-14T13:41:17.394493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2.5 DeepFM","metadata":{}},{"cell_type":"code","source":"del AFM_model","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:41:27.427882Z","iopub.execute_input":"2023-06-14T13:41:27.428231Z","iopub.status.idle":"2023-06-14T13:41:27.432694Z","shell.execute_reply.started":"2023-06-14T13:41:27.428204Z","shell.execute_reply":"2023-06-14T13:41:27.431641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DeepFM_model = DeepFM(linear_feature_columns,dnn_feature_columns, task='binary', dnn_dropout=0.5, l2_reg_embedding=0.001, l2_reg_linear=0.001, l2_reg_dnn=0.001)\nDeepFM_model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['binary_crossentropy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:41:55.553308Z","iopub.execute_input":"2023-06-14T13:41:55.553676Z","iopub.status.idle":"2023-06-14T13:41:56.059001Z","shell.execute_reply.started":"2023-06-14T13:41:55.553645Z","shell.execute_reply":"2023-06-14T13:41:56.058018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nDeepFM_history = DeepFM_model.fit(train_X, train_Y, batch_size=1024, epochs=10, verbose=1, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:42:01.162657Z","iopub.execute_input":"2023-06-14T13:42:01.163012Z","iopub.status.idle":"2023-06-14T13:48:26.446548Z","shell.execute_reply.started":"2023-06-14T13:42:01.162985Z","shell.execute_reply":"2023-06-14T13:48:26.445347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DeepFM_predict_Y = DeepFM_model.predict(test_X, 1000)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:48:26.448466Z","iopub.execute_input":"2023-06-14T13:48:26.448930Z","iopub.status.idle":"2023-06-14T13:48:33.689069Z","shell.execute_reply.started":"2023-06-14T13:48:26.448896Z","shell.execute_reply":"2023-06-14T13:48:33.688077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_estimation(DeepFM_predict_Y, 'DeepFM')","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:48:39.488748Z","iopub.execute_input":"2023-06-14T13:48:39.489104Z","iopub.status.idle":"2023-06-14T13:48:51.468522Z","shell.execute_reply.started":"2023-06-14T13:48:39.489076Z","shell.execute_reply":"2023-06-14T13:48:51.467455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2.6 Wide & Deep","metadata":{}},{"cell_type":"code","source":"del DeepFM_model","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:48:54.099011Z","iopub.execute_input":"2023-06-14T13:48:54.099371Z","iopub.status.idle":"2023-06-14T13:48:54.103872Z","shell.execute_reply.started":"2023-06-14T13:48:54.099341Z","shell.execute_reply":"2023-06-14T13:48:54.102756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"WDL_model = WDL(linear_feature_columns, dnn_feature_columns, task='binary', l2_reg_linear=0.001, l2_reg_embedding=0.001, l2_reg_dnn=0.001, dnn_dropout=0.5)\nWDL_model.compile(\"adam\", \"binary_crossentropy\", metrics=['binary_crossentropy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:50:08.109336Z","iopub.execute_input":"2023-06-14T13:50:08.109723Z","iopub.status.idle":"2023-06-14T13:50:08.504600Z","shell.execute_reply.started":"2023-06-14T13:50:08.109693Z","shell.execute_reply":"2023-06-14T13:50:08.503634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nWDL_history = WDL_model.fit(train_X, train_Y, batch_size=1024, epochs=10, verbose=1, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:50:10.906723Z","iopub.execute_input":"2023-06-14T13:50:10.907087Z","iopub.status.idle":"2023-06-14T13:56:36.005827Z","shell.execute_reply.started":"2023-06-14T13:50:10.907060Z","shell.execute_reply":"2023-06-14T13:56:36.004635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"WDL_predict_Y = WDL_model.predict(test_X, 1000)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:56:46.947959Z","iopub.execute_input":"2023-06-14T13:56:46.948504Z","iopub.status.idle":"2023-06-14T13:56:57.689657Z","shell.execute_reply.started":"2023-06-14T13:56:46.948465Z","shell.execute_reply":"2023-06-14T13:56:57.688535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_estimation(WDL_predict_Y, 'Wide & Deep')","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:56:57.692093Z","iopub.execute_input":"2023-06-14T13:56:57.692814Z","iopub.status.idle":"2023-06-14T13:57:09.101559Z","shell.execute_reply.started":"2023-06-14T13:56:57.692769Z","shell.execute_reply":"2023-06-14T13:57:09.100647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2.7 xDeepFM","metadata":{}},{"cell_type":"code","source":"del WDL_model","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:57:12.291358Z","iopub.execute_input":"2023-06-14T13:57:12.291814Z","iopub.status.idle":"2023-06-14T13:57:12.296812Z","shell.execute_reply.started":"2023-06-14T13:57:12.291776Z","shell.execute_reply":"2023-06-14T13:57:12.295694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xDeepFM_model = xDeepFM(linear_feature_columns, dnn_feature_columns, task='binary', l2_reg_linear=0.001, l2_reg_embedding=0.001, dnn_dropout=0.5)\nxDeepFM_model.compile(\"adam\", \"binary_crossentropy\", metrics=['binary_crossentropy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:58:20.327703Z","iopub.execute_input":"2023-06-14T13:58:20.328067Z","iopub.status.idle":"2023-06-14T13:58:21.088761Z","shell.execute_reply.started":"2023-06-14T13:58:20.328039Z","shell.execute_reply":"2023-06-14T13:58:21.087820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nxDeepFM_history = xDeepFM_model.fit(train_X, train_Y, batch_size=1024, epochs=10, verbose=1, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T13:58:23.406619Z","iopub.execute_input":"2023-06-14T13:58:23.406979Z","iopub.status.idle":"2023-06-14T14:06:49.193826Z","shell.execute_reply.started":"2023-06-14T13:58:23.406952Z","shell.execute_reply":"2023-06-14T14:06:49.192652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xDeepFM_predict_Y = xDeepFM_model.predict(test_X, 1000)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:06:49.195800Z","iopub.execute_input":"2023-06-14T14:06:49.196248Z","iopub.status.idle":"2023-06-14T14:06:57.174022Z","shell.execute_reply.started":"2023-06-14T14:06:49.196211Z","shell.execute_reply":"2023-06-14T14:06:57.172990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_estimation(xDeepFM_predict_Y, 'xDeepFM')","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:07:35.365255Z","iopub.execute_input":"2023-06-14T14:07:35.366062Z","iopub.status.idle":"2023-06-14T14:07:47.945810Z","shell.execute_reply.started":"2023-06-14T14:07:35.366030Z","shell.execute_reply":"2023-06-14T14:07:47.944930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2.8 Deep & Cross","metadata":{}},{"cell_type":"code","source":"del xDeepFM_model","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:07:58.196525Z","iopub.execute_input":"2023-06-14T14:07:58.196935Z","iopub.status.idle":"2023-06-14T14:07:58.201744Z","shell.execute_reply.started":"2023-06-14T14:07:58.196904Z","shell.execute_reply":"2023-06-14T14:07:58.200754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DCN_model = DCN(linear_feature_columns, dnn_feature_columns, task='binary', l2_reg_linear=0.001, l2_reg_embedding=0.001, dnn_dropout=0.5)\nDCN_model.compile(\"adam\", \"binary_crossentropy\", metrics=['binary_crossentropy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:08:34.142278Z","iopub.execute_input":"2023-06-14T14:08:34.142659Z","iopub.status.idle":"2023-06-14T14:08:34.775532Z","shell.execute_reply.started":"2023-06-14T14:08:34.142627Z","shell.execute_reply":"2023-06-14T14:08:34.774635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nDCN_history = DCN_model.fit(train_X, train_Y, batch_size=1024, epochs=10, verbose=1, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:08:39.125441Z","iopub.execute_input":"2023-06-14T14:08:39.126433Z","iopub.status.idle":"2023-06-14T14:15:04.841732Z","shell.execute_reply.started":"2023-06-14T14:08:39.126400Z","shell.execute_reply":"2023-06-14T14:15:04.840043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DCN_predict_Y = DCN_model.predict(test_X, 1000)","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:15:19.821924Z","iopub.execute_input":"2023-06-14T14:15:19.822279Z","iopub.status.idle":"2023-06-14T14:15:28.326048Z","shell.execute_reply.started":"2023-06-14T14:15:19.822252Z","shell.execute_reply":"2023-06-14T14:15:28.325027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_estimation(DCN_predict_Y, 'Deep & Cross')","metadata":{"execution":{"iopub.status.busy":"2023-06-14T14:15:28.329056Z","iopub.execute_input":"2023-06-14T14:15:28.329895Z","iopub.status.idle":"2023-06-14T14:15:39.987538Z","shell.execute_reply.started":"2023-06-14T14:15:28.329858Z","shell.execute_reply":"2023-06-14T14:15:39.986491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}