任务:依据电子商务平平台上真实的用户行为记录,利用机器学习相关技术,建立稳健的电商用户购买行为预测模型,预测用户下一个可能会购买的商品。
数据简介
数据整理自一家中等化妆品在线商店公布的网上公开数据集,为该化妆品商店真实的用户交易信息,数据集中每一行表示一个事件,所有的事件都与商品和用户相关,并且用户的点击行为之间是有时间顺序的。数据集中包含了商品和用户的多个属性,例如商品编号、商品类别、用户编号、事件时间等。
数据说明
image.png主要思路
- 对用户id进行分组
- 统计类别、品牌、收藏、加购物车、下单等特征,赋予合理的权重
- 构建时间特征
- 使用lgb的多分类模型进行训练
lgb算法模型预测:
注意:此版本代码lgb版本是2.0.3
import gc
import pandas as pd
from sklearn.preprocessing import LabelEncoder
paths = r'E:\项目文件\CCF\电商用户购买行为预测'
data = pd.read_csv(f'{paths}/train.csv')
submit_example = pd.read_csv(f'{paths}/submit_example.csv')
test = pd.read_csv(f'{paths}/test.csv')
data['user_id'] = data['user_id'].astype('int32')
data['product_id'] = data['product_id'].astype('int32')
data['category_id'] = data['category_id'].astype('int32')
lbe = LabelEncoder()
data['brand'].fillna('0', inplace=True)
data['brand'] = lbe.fit_transform(data['brand'])
data['brand'] = data['brand'].astype('int32')
# data['event_time'] = pd.to_datetime(data['event_time'], format='%Y-%m-%d %H:%M:%S')
data.fillna(0, inplace=True)
gc.collect()
train_X = data
test_data = test
# 构建特征
groups = train_X.groupby('user_id')
temp = groups.size().reset_index().rename(columns={0: 'u1'})
matrix = temp
temp = groups['product_id'].agg([('u2', 'nunique')]).reset_index()
matrix = matrix.merge(temp, on='user_id', how='left')
temp = groups['category_id'].agg([('u3', 'nunique')]).reset_index()
matrix = matrix.merge(temp, on='user_id', how='left')
temp = groups['brand'].agg([('u5', 'nunique')]).reset_index()
# TODO 根据用户购买行为去构建特征
# temp = groups['event_type'].value_counts().unstack().reset_index().rename(
# columns={0: 'u7', 1: 'u8', 2: 'u9', 3: 'u10'})
matrix = matrix.merge(temp, on='user_id', how='left')
label_list = []
for name, group in groups:
product_id = int(group.iloc[-1, 2])
label_list.append([name, product_id])
train_data = matrix.merge(pd.DataFrame(label_list, columns=['user_id', 'label'], dtype=int), on='user_id', how='left')
# 构建特征
groups = test_data.groupby('user_id')
temp = groups.size().reset_index().rename(columns={0: 'u1'})
test_matrix = temp
temp = groups['product_id'].agg([('u2', 'nunique')]).reset_index()
matrix = test_matrix.merge(temp, on='user_id', how='left')
temp = groups['category_id'].agg([('u3', 'nunique')]).reset_index()
matrix = matrix.merge(temp, on='user_id', how='left')
temp = groups['brand'].agg([('u5', 'nunique')]).reset_index()
# TODO 根据用户购买行为去构建特征
# temp = groups['event_type'].value_counts().unstack().reset_index().rename(
# columns={0: 'u7', 1: 'u8', 2: 'u9', 3: 'u10'})
test_data = matrix.merge(temp, on='user_id', how='left')
test_data = test_data.drop(['user_id'], axis=1)
train_X, train_y = train_data.drop(['label', 'user_id'], axis=1), train_data['label']
# train_X.to_csv('train_deal.csv', index=False)
# train_y.to_csv('train_y_deal.csv', index=False)
# test_data.to_csv('test_data.csv', index=False)
# 导入分析库
import lightgbm as lgb
model = lgb.LGBMClassifier(
max_depth=5,
n_estimators=10,
)
model.fit(
train_X,
train_y,
eval_metric='auc',
eval_set=[(train_X, train_y)],
verbose=False,
early_stopping_rounds=5
)
prob = model.predict(test_data)
import numpy as np
np.savetxt(paths + '\\prob1.csv', prob)
submit_example['product_id'] = pd.Series(prob[:, 0])
submit_example.to_csv(paths + r'\\lgb1.csv', index=False)
xgb算法模型预测:
import gc
import pandas as pd
import xgboost as xgb
from sklearn.preprocessing import LabelEncoder
paths = r'E:\项目文件\CCF\电商用户购买行为预测'
data = pd.read_csv(f'{paths}/train.csv')
submit_example = pd.read_csv(f'{paths}/submit_example.csv')
test = pd.read_csv(f'{paths}/test.csv')
data['user_id'] = data['user_id'].astype('int32')
data['product_id'] = data['product_id'].astype('int32')
data['category_id'] = data['category_id'].astype('int32')
lbe = LabelEncoder()
data['brand'].fillna('0', inplace=True)
data['brand'] = lbe.fit_transform(data['brand'])
data['brand'] = data['brand'].astype('int32')
# data['event_time'] = pd.to_datetime(data['event_time'], format='%Y-%m-%d %H:%M:%S')
data.fillna(0, inplace=True)
gc.collect()
train_X = data
test_data = test
# 构建特征
groups = train_X.groupby('user_id')
temp = groups.size().reset_index().rename(columns={0: 'counts'})
matrix = temp
temp = groups['product_id'].agg([('product_count', 'nunique')]).reset_index()
matrix = matrix.merge(temp, on='user_id', how='left')
temp = groups['category_id'].agg([('category_count', 'nunique')]).reset_index()
matrix = matrix.merge(temp, on='user_id', how='left')
temp = groups['brand'].agg([('brand_count', 'nunique')]).reset_index()
matrix = matrix.merge(temp, on='user_id', how='left')
# # 计算用户与商品交互的次数,并添加新的一列count
# temp = groups['event_time'].transform('count')
# matrix = matrix.merge(temp, on='user_id', how='left')
# TODO 根据用户购买行为去构建特征
# temp = groups['event_type'].value_counts().unstack().reset_index().rename(
# columns={0: 'u7', 1: 'u8', 2: 'u9', 3: 'u10'})
label_list = []
for name, group in groups:
product_id = int(group.iloc[-1, 2])
label_list.append([name, product_id])
train_data = matrix.merge(pd.DataFrame(label_list, columns=['user_id', 'label'], dtype=int), on='user_id', how='left')
# 构建特征
groups = test_data.groupby('user_id')
temp = groups.size().reset_index().rename(columns={0: 'counts'})
matrix = temp
temp = groups['product_id'].agg([('product_count', 'nunique')]).reset_index()
matrix = matrix.merge(temp, on='user_id', how='left')
temp = groups['category_id'].agg([('category_count', 'nunique')]).reset_index()
matrix = matrix.merge(temp, on='user_id', how='left')
temp = groups['brand'].agg([('brand_count', 'nunique')]).reset_index()
matrix = matrix.merge(temp, on='user_id', how='left')
# 计算用户与商品交互的次数,并添加新的一列count
# temp = groups['event_time'].transform('count')
# matrix = matrix.merge(temp, on='user_id', how='left')
test_data = matrix.merge(temp, on='user_id', how='left')
# TODO 根据用户购买行为去构建特征
# temp = groups['event_type'].value_counts().unstack().reset_index().rename(
# columns={0: 'u7', 1: 'u8', 2: 'u9', 3: 'u10'})
test_data = test_data.drop(['user_id'], axis=1)
x_train, y_train = train_data.drop(['label', 'user_id'], axis=1), train_data['label']
# X_train.to_csv('train_deal.csv', index=False)
# Y_train.to_csv('train_y_deal.csv', index=False)
# test_data.to_csv('test_data.csv', index=False)
# x_train, x_valid, y_train, y_valid = train_test_split(X_train, Y_train, test_size=.2)
model = xgb.XGBClassifier(
max_depth=8,
n_estimators=1000,
min_child_weight=300,
colsample_bytree=0.8,
subsample=0.8,
eta=0.3,
seed=42
)
model.fit(
x_train,
y_train,
eval_metric='auc',
# eval_set=[(x_train, y_train), (x_valid, y_valid)],
verbose=True,
)
predict_test = model.predict(test_data)
print(predict_test)
import numpy as np
np.savetxt(paths + '\\pred.csv', predict_test)
submit_example['product_id'] = pd.Series(predict_test[:, 0])
submit_example.to_csv(paths + r'\\xgb_best.csv', index=False)
耍花招凑提交的方法,直接默认买最后一条记录,小心被封号
import pandas as pd
paths = r'E:\项目文件\CCF\电商用户购买行为预测'
submit_example = pd.read_csv(f'{paths}/submit_example.csv')
test = pd.read_csv(f'{paths}/test.csv')
# 构建特征
groups = test.groupby('user_id')
label_list = []
for name, group in groups:
product_id = int(group.iloc[-1, 2])
label_list.append([name, product_id])
submit_example = pd.DataFrame(label_list, columns=['user_id', 'product_id'])
submit_example.to_csv(paths + r'\\label_list.csv', index=False)
参考文献,思路都差不多,主要看你怎么构造特征了,加油吧少年
网友评论