{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"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        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-03-15T12:27:33.017072Z","iopub.execute_input":"2023-03-15T12:27:33.01803Z","iopub.status.idle":"2023-03-15T12:27:33.07025Z","shell.execute_reply.started":"2023-03-15T12:27:33.017971Z","shell.execute_reply":"2023-03-15T12:27:33.068989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"导数据，清洗数据","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom tqdm.notebook import tqdm\nimport gc\nimport time\nfrom gensim.models import Word2Vec\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import LabelEncoder, MinMaxScaler, StandardScaler\nfrom sklearn.model_selection import KFold, ShuffleSplit, StratifiedKFold\nfrom sklearn.metrics import roc_auc_score, log_loss\nimport multiprocessing\nimport lightgbm as lgb\npd.set_option('max_columns',1000)\npd.set_option('display.max_info_columns',1000)\n# pd.set_option('display.max_describe_columns',1000)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:27:33.072297Z","iopub.execute_input":"2023-03-15T12:27:33.073343Z","iopub.status.idle":"2023-03-15T12:27:37.596156Z","shell.execute_reply.started":"2023-03-15T12:27:33.073294Z","shell.execute_reply":"2023-03-15T12:27:37.594343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reduce_mem(df):\n    start_mem = df.memory_usage().sum() / 1024 ** 2\n    for col in df.columns:\n        col_type = df[col].dtypes\n        if col_type != object:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)\n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else:\n                    df[col] = df[col].astype(np.float64)\n    end_mem = df.memory_usage().sum() / 1024 ** 2\n    print('{:.2f} Mb, {:.2f} Mb ({:.2f} %)'.format(start_mem, end_mem, 100 * (start_mem - end_mem) / start_mem))\n    gc.collect()\n    return df\n","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:27:37.598124Z","iopub.execute_input":"2023-03-15T12:27:37.598817Z","iopub.status.idle":"2023-03-15T12:27:37.615243Z","shell.execute_reply.started":"2023-03-15T12:27:37.598763Z","shell.execute_reply":"2023-03-15T12:27:37.613353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base='/kaggle/input/deepshare-playground-series2/'","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:27:37.618698Z","iopub.execute_input":"2023-03-15T12:27:37.619922Z","iopub.status.idle":"2023-03-15T12:27:37.629615Z","shell.execute_reply.started":"2023-03-15T12:27:37.619855Z","shell.execute_reply":"2023-03-15T12:27:37.627973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv(base+'train_data.csv')\ntest=pd.read_csv(base+'test_data.csv')\ntrain.shape,test.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:27:37.631959Z","iopub.execute_input":"2023-03-15T12:27:37.632369Z","iopub.status.idle":"2023-03-15T12:28:20.431434Z","shell.execute_reply.started":"2023-03-15T12:27:37.632328Z","shell.execute_reply":"2023-03-15T12:28:20.429603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=pd.concat([train,test],axis=0).sort_values(by=['user_id','ts']).reset_index(drop=True)\n# data=data.fillna(-1)\n# data['user_fea6']=data.groupby('user_id')['user_fea6'].ffill()\n# data['user_fea7']=data.groupby('user_id')['user_fea7'].ffill()\n# data['user_fea8']=data.groupby('user_id')['user_fea8'].ffill()\n# data['user_fea9']=data.groupby('user_id')['user_fea9'].ffill()\ndata['user_item_id']=(data['user_id']+data['item_id'])\ndata['dcount']=data.groupby('user_item_id')['ts'].cumcount()\n# data['datetime64'] = pd.to_datetime(data['ts'])\n# data['year'] = data['datetime64'].dt.year\n# data['quarter'] = data['datetime64'].dt.quarter\n# data['month'] = data['datetime64'].dt.month\n# data['week'] = data['datetime64'].dt.week\n# data['day'] = data['datetime64'].dt.day\n# data['hour'] = data['datetime64'].dt.hour\n# data['minute'] = data['datetime64'].dt.minute\n# data['second'] = data['datetime64'].dt.second\n# data[data['label'].notna()]['datetime64'].describe()","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:28:20.433436Z","iopub.execute_input":"2023-03-15T12:28:20.434345Z","iopub.status.idle":"2023-03-15T12:28:39.261218Z","shell.execute_reply.started":"2023-03-15T12:28:20.434274Z","shell.execute_reply":"2023-03-15T12:28:39.259655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['ts']=(data['ts']-data['ts'].min())*100\ndata['second'] = pd.to_datetime(data['ts']).dt.second\ndata[data['label'].isna()]['second'].describe()","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:28:39.263372Z","iopub.execute_input":"2023-03-15T12:28:39.2638Z","iopub.status.idle":"2023-03-15T12:28:45.369619Z","shell.execute_reply.started":"2023-03-15T12:28:39.263761Z","shell.execute_reply":"2023-03-15T12:28:45.368029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['location']=data['lng'].astype(str)+data['lat'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:28:46.277602Z","iopub.execute_input":"2023-03-15T12:28:46.277984Z","iopub.status.idle":"2023-03-15T12:28:55.745119Z","shell.execute_reply.started":"2023-03-15T12:28:46.277947Z","shell.execute_reply":"2023-03-15T12:28:55.743821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data['location'].value_counts().shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:28:55.747456Z","iopub.execute_input":"2023-03-15T12:28:55.748345Z","iopub.status.idle":"2023-03-15T12:28:55.754152Z","shell.execute_reply.started":"2023-03-15T12:28:55.748294Z","shell.execute_reply":"2023-03-15T12:28:55.752612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cate_cols=['user_id','item_id','user_item_id','location']+[f'user_fea{i}' for i in range(1,6)]+ ['item_fea1']+['user_fea9']\nnum_cols=['label','ts']+[f'user_fea{i}' for i in range(6,9)]","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:28:55.756138Z","iopub.execute_input":"2023-03-15T12:28:55.756613Z","iopub.status.idle":"2023-03-15T12:28:55.776098Z","shell.execute_reply.started":"2023-03-15T12:28:55.756572Z","shell.execute_reply":"2023-03-15T12:28:55.774374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"nunique特征","metadata":{}},{"cell_type":"code","source":"temp_cate=[]\nfor i in tqdm(cate_cols):\n    data[i]=data[i].fillna('-1')\n    data[i]=data[i].factorize()[0]\n#     temp_cate+=[f'{i}_I']\n#     map_dict=dict(zip(data[i].unique(),range(data[i].nunique())))\n    data[f'{i}_I']=data[i].map(data[i].value_counts())    #计算每一列种类出现个数","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:28:55.777901Z","iopub.execute_input":"2023-03-15T12:28:55.779271Z","iopub.status.idle":"2023-03-15T12:29:20.028353Z","shell.execute_reply.started":"2023-03-15T12:28:55.779219Z","shell.execute_reply":"2023-03-15T12:29:20.027112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(cate_cols):\n    tmp = data[[i] + ['ts']].groupby(i)\n    for lag in [1,2,3,4,5,6,8]:\n        data[f'{i}_pre_{lag}']=tmp['ts'].shift(0)-tmp['ts'].shift(lag)\n        data[f'{i}_next_{lag}']=tmp['ts'].shift(-1*lag)-tmp['ts'].shift(0)\n    del tmp\n    gc.collect()\n    data=reduce_mem(data)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:29:20.029914Z","iopub.execute_input":"2023-03-15T12:29:20.030242Z","iopub.status.idle":"2023-03-15T12:32:06.605473Z","shell.execute_reply.started":"2023-03-15T12:29:20.030209Z","shell.execute_reply":"2023-03-15T12:32:06.603825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def emb2(df, f1, f2):\n    emb_size = 8\n    tmp = df.groupby(f1, as_index=False)[f2].agg({'{}_{}_list'.format(f1, f2): list})\n    sentences = tmp['{}_{}_list'.format(f1, f2)].values.tolist()\n    del tmp['{}_{}_list'.format(f1, f2)]\n    for i in range(len(sentences)):\n        sentences[i] = [str(x) for x in sentences[i]]\n    model = Word2Vec(sentences, vector_size=emb_size, window=6, min_count=5, sg=0, hs=0, seed=1, epochs=5)\n    emb_matrix = []\n    for seq in sentences:\n        vec = []\n        for w in seq:\n            if w in model.wv.key_to_index:\n                vec.append(model.wv[w])\n        if len(vec) > 0:\n            emb_matrix.append(np.mean(vec, axis=0))\n        else:\n            emb_matrix.append([0] * emb_size)\n    emb_matrix = np.array(emb_matrix)\n    for i in range(emb_size):\n        tmp['{}_{}_emb_{}'.format(f1, f2, i)] = emb_matrix[:, i]\n\n    word_list = []\n    emb_matrix2 = []\n    for w in model.wv.key_to_index:\n        word_list.append(w)\n        emb_matrix2.append(model.wv[w])\n    emb_matrix2 = np.array(emb_matrix2)\n    tmp2 = pd.DataFrame()\n    tmp2[f2] = np.array(word_list).astype('int')\n    for i in range(emb_size):\n        tmp2['{}_{}_emb_{}'.format(f2, f1, i)] = emb_matrix2[:, i]\n    return tmp, tmp2","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:32:06.607405Z","iopub.execute_input":"2023-03-15T12:32:06.607927Z","iopub.status.idle":"2023-03-15T12:32:06.625052Z","shell.execute_reply.started":"2023-03-15T12:32:06.607866Z","shell.execute_reply":"2023-03-15T12:32:06.623749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('开始构造embedding特征')\n#     print(data[data['label'].isna()].shape)\nemb_cols = [['user_id', 'item_id']]\nsort_df = data.sort_values('second').reset_index(drop=True)\nfor f1, f2 in emb_cols:\n    tmp, tmp2 = emb2(sort_df, f1, f2)\n    temp=data['label']\n    data = data.merge(tmp, on=f1, how='inner').merge(tmp2, on=f2, how='inner').fillna(0)\n    data['label']=temp\n    del tmp,tmp2\n    gc.collect()\ndata=reduce_mem(data)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:32:06.627002Z","iopub.execute_input":"2023-03-15T12:32:06.627864Z","iopub.status.idle":"2023-03-15T12:34:00.859774Z","shell.execute_reply.started":"2023-03-15T12:32:06.627803Z","shell.execute_reply":"2023-03-15T12:34:00.85807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['lastactive_1'] = data.groupby('user_id')['ts'].transform('last')\ndata['lastactive_2'] = data.groupby('user_item_id')['ts'].transform('last')\ndata['lastactive_3'] = data.groupby('item_id')['ts'].transform('last')\n# dt = data.groupby('user_id')['label'].agg('last')\n# raw['lasttarget'] = raw['cfips'].map(dt)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:34:00.86192Z","iopub.execute_input":"2023-03-15T12:34:00.862515Z","iopub.status.idle":"2023-03-15T12:34:01.717771Z","shell.execute_reply.started":"2023-03-15T12:34:00.86243Z","shell.execute_reply":"2023-03-15T12:34:01.716327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=data[data['label'].notna()].reset_index(drop=True)\ntest_df=data[data['label'].isna()].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:34:01.719448Z","iopub.execute_input":"2023-03-15T12:34:01.72002Z","iopub.status.idle":"2023-03-15T12:34:06.797814Z","shell.execute_reply.started":"2023-03-15T12:34:01.71998Z","shell.execute_reply":"2023-03-15T12:34:06.796451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_feature=['label','ID','user_tag','ts','lat','lng','datetime64']\nfeature=[i for i in data.columns if i not in drop_feature] ","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:34:06.799232Z","iopub.execute_input":"2023-03-15T12:34:06.799633Z","iopub.status.idle":"2023-03-15T12:34:06.806349Z","shell.execute_reply.started":"2023-03-15T12:34:06.799593Z","shell.execute_reply":"2023-03-15T12:34:06.804863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:34:06.8084Z","iopub.execute_input":"2023-03-15T12:34:06.809Z","iopub.status.idle":"2023-03-15T12:34:06.845432Z","shell.execute_reply.started":"2023-03-15T12:34:06.80894Z","shell.execute_reply":"2023-03-15T12:34:06.84368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# params = {'num_leaves': 128, #结果对最终效果影响较大，越大值越好，太大会出现过拟合\n#           'objective': 'binary', #定义的目标函数\n#           'max_depth': 8,\n#           'learning_rate': 0.1,\n#           \"min_sum_hessian_in_leaf\": 6,\n#           \"boosting\": \"gbdt\",\n#           \"feature_fraction\": 1,  #提取的特征比率\n#           \"bagging_freq\": 1,\n#           \"bagging_fraction\": 1,\n#           \"bagging_seed\": 11,\n# #           \"lambda_l1\": 0.1,             #l1正则\n#           # 'lambda_l2': 0.001,     #l2正则\n#           \"verbosity\": -1,\n#           \"nthread\": multiprocessing.cpu_count(),                #线程数量，-1表示全部线程，线程越多，运行的速度越快\n#           'metric': {'binary_logloss', 'auc'},  ##评价函数选择\n#           \"random_state\": 2077, #随机数种子，可以防止每次运行的结果不一致\n#           # 'device': 'gpu' ##如果安装的事gpu版本的lightgbm,可以加快运算\n#           }\nparams = {\n    'boosting_type': 'gbdt',\n    'objective': 'binary',\n    'metric': 'auc',\n    'boost_from_average' : True,\n    'train_metric': True, \n    'feature_fraction_seed' : 1,\n    'learning_rate': 0.1,\n    'is_unbalance': False,  #当训练数据是不平衡的，正负样本相差悬殊的时候，可以将这个属性设为true,此时会自动给少的样本赋予更高的权重\n    'num_leaves': 512,  # 一般设为少于2^(max_depth)\n    'max_depth': -1,  #最大的树深，设为-1时表示不限制树的深度\n    'min_child_samples': 15,  # 每个叶子结点最少包含的样本数量，用于正则化，避免过拟合\n    'max_bin': 200,  # 设置连续特征或大量类型的离散特征的bins的数量\n    'subsample': 1,  # Subsample ratio of the training instance.\n    'subsample_freq': 1,  # frequence of subsample, <=0 means no enable\n    'colsample_bytree': 0.5,  # Subsample ratio of columns when constructing each tree.\n    'min_child_weight': 0,  # Minimum sum of instance weight(hessian) needed in a child(leaf)\n    'subsample_for_bin': 200000,  # Number of samples for constructing bin\n    'min_split_gain': 0,  # lambda_l1, lambda_l2 and min_gain_to_split to regularization\n    'reg_alpha': 2.99,  # L1 regularization term on weights\n    'reg_lambda': 1.9,  # L2 regularization term on weights\n    'nthread': 20,\n    'verbose': 0,\n}\n\nn_fold = 5\noof_pre = np.zeros(len(train_df))\ny_pre = np.zeros(len(test_df))\n\nkf = KFold(n_splits=n_fold)\nfor fold_, (trn_idx, val_idx) in enumerate(kf.split(train_df)):\n\n    trn_data = lgb.Dataset(train_df[feature].iloc[trn_idx], label=train_df['label'].iloc[trn_idx])\n    val_data = lgb.Dataset(train_df[feature].iloc[val_idx], label=train_df['label'].iloc[val_idx])\n\n#     clf = lgb.train(params,\n#                     trn_data,\n#                     2000,\n#                     valid_sets=[trn_data, val_data],\n# #                     verbose_eval=50,\n#                     early_stopping_rounds=50)\n\n    # out of fold\n    clf = lgb.train(params,\n                    trn_data,\n                    3000,\n                    valid_sets=[trn_data],\n#                     verbose_eval=50,\n                    early_stopping_rounds=20)\n    oof_pre[val_idx] = clf.predict(train_df[feature].iloc[val_idx], num_iteration=clf.best_iteration)\n    lgb.plot_importance(clf, max_num_features=30)\n    plt.title(\"Featurertances\")\n    plt.show()\n\n    y_pre += clf.predict(test_df[feature], num_iteration=clf.best_iteration) / n_fold","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:41:11.364037Z","iopub.execute_input":"2023-03-15T12:41:11.364494Z","iopub.status.idle":"2023-03-15T12:43:12.089274Z","shell.execute_reply.started":"2023-03-15T12:41:11.36443Z","shell.execute_reply":"2023-03-15T12:43:12.087715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_df = pd.DataFrame()\nres_df['ID'] = test_df['ID']\nres_df['label'] = y_pre\nprint(res_df.head(50))\nres_df.to_csv('/kaggle/working/lgb-baseline.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-15T12:41:10.076021Z","iopub.status.idle":"2023-03-15T12:41:10.077362Z","shell.execute_reply.started":"2023-03-15T12:41:10.077065Z","shell.execute_reply":"2023-03-15T12:41:10.077102Z"},"trusted":true},"execution_count":null,"outputs":[]}]}