特征工程

处理数据

# 全量信息处理,合并数据
all_data =  train_data.append(test_data)
all_data = all_data.merge(user_info, on="user_id", how="left")
all_data.head()
user_id merchant_id label prob age_range gender
0 34176 3906 0.0 NaN 6.0 0.0
1 34176 121 0.0 NaN 6.0 0.0
2 34176 4356 1.0 NaN 6.0 0.0
3 34176 2217 0.0 NaN 6.0 0.0
4 230784 4818 0.0 NaN 0.0 0.0
# wewewwe
del train_data, test_data, user_info
gc.collect()
16896
# 对用户日志按照时间排序
user_log = user_log.sort_values(by=["user_id", "time_stamp"])# 构建表达式(统计用户的历史信息)
list_join_func = lambda x: " ".join(str(i) for i in x)agg_dict = {"item_id": list_join_func,"cat_id": list_join_func,"seller_id": list_join_func,"brand_id": list_join_func,"time_stamp": list_join_func,"action_type": list_join_func
}rename_dict = {"item_id": "item_path","cat_id": "cat_path","seller_id": "seller_path","brand_id": "brand_path","time_stamp": "time_stamp_path","action_type": "action_type_path"
}
# 聚合用户历史购买行为
# user_log.groupby("user_id").agg(agg_dict)
def merge_list(df_ID, join_columns, df_data, agg_dict, rename_dict):"""合并用户信息和日志信息"""# 构建用户历史记录df_data = df_data.groupby("user_id").agg(agg_dict).reset_index().rename(columns=rename_dict)df_ID = df_ID.merge(df_data, on=join_columns, how="left")return df_IDall_date = merge_list(all_data, "user_id", user_log, agg_dict, rename_dict)
# 回收内存和删除不必要的数据
# gc.collect()
# del user_log

定义特征统计函数

定义统计函数

# 统计数据总数
def cnt_(x):try:return len(x.split(" "))except:return -1
# 统计唯一值数量
def nunique_(x):try:return len(set(x.split(" ")))except:return -1
# 统计数据最大值
def max_(x):try:return np.max([float(i) for i in x.split(" ")])except:return -1
# 统计数据最小值
def min_(x):try:return np.min([float(i) for i in x.split(" ")])except:return -1
# 统计数据标准差
def std_(x):try:return np.std([float(i) for i in x.split(" ")])except:return -1
# 统计TopN的数据
def most_n(x, n):try:return Counter(x.split(" ")).most_common(n)[n-1][0] # 名称except:return -1def most_n_cnt(x, n):try:return Counter(x.split(" ")).most_common(n)[n-1][1] # 数量except:return -1

定义调用函数

def user_cnt(df_data, single_col, name):df_data[name] = df_data[single_col].apply(cnt_)return df_data
def user_nunique(df_data, single_col, name):df_data[name] = df_data[single_col].apply(nunique_)return df_data
def user_max(df_data, single_col, name):df_data[name] = df_data[single_col].apply(max_)return df_data
def user_min(df_data, single_col, name):df_data[name] = df_data[single_col].apply(min_)return df_data
def user_std(df_data, single_col, name):df_data[name] = df_data[single_col].apply(std_)return df_data
def user_cnt(df_data, single_col, name):df_data[name] = df_data[single_col].apply(cnt_)return df_data
def user_most_n(df_data, single_col, name, n=1):func = lambda x: most_n(x, n)df_data[name] = df_data[single_col].apply(func)return df_data
def user_most_n_cnt(df_data, single_col, name, n=1):func = lambda x: most_n_cnt(x, n)df_data[name] = df_data[single_col].apply(func)return df_data

提取统计变量(用户特征构造)

统计用户的多样性

# 数据部分采样和全采样
# all_data_test = all_date.sample(2000)
all_data_test = all_date
# 每一位用户所点击的商铺总次数(不去重)
all_data_test = user_cnt(all_data_test, "seller_path", "user_seller_cnt")# 店铺个数(去重)
all_data_test = user_nunique(all_data_test, "seller_path", "user_seller_unique")# 不同种类
all_data_test = user_nunique(all_data_test, "cat_path", "user_cat_unique")# 不同商品
all_data_test = user_nunique(all_data_test, "item_path", "user_item_unique")# 不同品牌
all_data_test = user_nunique(all_data_test, "brand_path", "user_brand_unique")# 活跃天数
all_data_test = user_nunique(all_data_test, "time_stamp_path", "user_time_stamp_unique")# 用户不同行为的种类数目
all_data_test = user_nunique(all_data_test, "action_type_path", "user_action_type_unique")
# 查看构建特征对购买的影响情况
new_feature = ["user_seller_unique", "user_cat_unique", "user_item_unique", "user_brand_unique", "user_time_stamp_unique", "user_action_type_unique"]for feature in new_feature:print("Feature:",feature)print(all_data_test.groupby("label")[feature].agg(["mean", "std"]))
Feature: user_seller_uniquemean        std
label
0.0    35.447798  36.650101
1.0    37.904965  38.137931
Feature: user_cat_uniquemean        std
label
0.0    23.878262  18.266488
1.0    26.464142  19.018367
Feature: user_item_uniquemean         std
label
0.0    81.077354  110.089651
1.0    97.890797  128.547510
Feature: user_brand_uniquemean        std
label
0.0    34.847149  34.815715
1.0    37.365848  36.349934
Feature: user_time_stamp_uniquemean        std
label
0.0    17.391973  14.719989
1.0    19.992289  15.992401
Feature: user_action_type_uniquemean       std
label
0.0    2.634301  0.551990
1.0    2.667503  0.537855

时间类信息获取

# 最早时间
all_data_test = user_min(all_data_test, "time_stamp_path", "user_time_stamp_min")# 最近时间
all_data_test = user_max(all_data_test, "time_stamp_path", "user_time_stamp_max")# 用户点击时间间隔
all_data_test["time_stamp_range"] = all_data_test["user_time_stamp_max"] - all_data_test["user_time_stamp_min"]# 活跃天数的方差,描述用户的活跃的波动情况
all_data_test = user_std(all_data_test, "time_stamp_path", "user_time_stamp_std")

其他基本信息

# 用户最喜欢的店铺
all_data_test = user_most_n(all_data_test, "seller_path", "user_seller_most_1", n=1)# 用户最喜欢的类目
all_data_test = user_most_n(all_data_test, "cat_path", "user_cat_most_1", n=1)# 用户最喜欢的品牌
all_data_test = user_most_n(all_data_test, "brand_path", "user_brand_most_1", n=1)# 用户最常见的行为操作
all_data_test = user_most_n(all_data_test, "action_type_path", "user_action_type_1", n=1)# 统计最喜欢的次数
# 用户最喜欢的店铺
all_data_test = user_most_n_cnt(all_data_test, "seller_path", "user_seller_most_1_cnt", n=1)# 用户最喜欢的类目
all_data_test = user_most_n_cnt(all_data_test, "cat_path", "user_cat_most_1_cnt", n=1)# 用户最喜欢的品牌
all_data_test = user_most_n_cnt(all_data_test, "brand_path", "user_brand_most_1_cnt", n=1)# 用户最常见的行为操作
all_data_test = user_most_n_cnt(all_data_test, "action_type_path", "user_action_type_1_cnt", n=1)

类别特征编码为哑变量

age_range = pd.get_dummies(all_data_test["age_range"], prefix="age")
gender = pd.get_dummies(all_data_test["gender"], prefix="gender")
all_data_test = all_data_test.join(age_range)
all_data_test = all_data_test.join(gender)
all_data_test.columns
Index(['user_id', 'merchant_id', 'label', 'prob', 'age_range', 'gender','item_path', 'cat_path', 'seller_path', 'brand_path', 'time_stamp_path','action_type_path', 'user_seller_cnt', 'user_seller_unique','user_cat_unique', 'user_item_unique', 'user_brand_unique','user_time_stamp_unique', 'user_action_type_unique','user_time_stamp_min', 'user_time_stamp_max', 'time_stamp_range','user_time_stamp_std', 'user_seller_most_1', 'user_cat_most_1','user_brand_most_1', 'user_action_type_1', 'user_seller_most_1_cnt','user_cat_most_1_cnt', 'user_brand_most_1_cnt','user_action_type_1_cnt', 'age_0.0', 'age_1.0', 'age_2.0', 'age_3.0','age_4.0', 'age_5.0', 'age_6.0', 'age_7.0', 'age_8.0', 'gender_0.0','gender_1.0', 'gender_2.0'],dtype='object')
import copydef col_cnt_(df_data, columns_list, action_type):"""统计点击数量"""data_dict = {}try:col_list = copy.deepcopy(columns_list)if action_type != None:col_list += ["action_type_path"]for col in col_list:data_dict[col] = df_data[col].split(" ")path_len = len(data_dict[col])# {"sell_path": ["66", "55", ......]}data_out = []for i_ in range(path_len):data_txt = ""for col_ in columns_list:# 统计点击购买行为if data_dict["action_type_path"][i_]  == action_type:data_txt += "_" + data_dict[col_][i_]data_out.append(data_txt)return len(data_out)        except:return -1

构建用户与商家的交互信息

例如:用户对商铺的点击行为特征,挖掘其是行为对点击情况的影响作用。

用户的点击情况

# 消除重复点击
def col_unique_(df_data, columns_list, action_type):data_dict = {}try:col_list = copy.deepcopy(columns_list)if action_type != None:col_list += ["action_type_path"]for col in col_list:data_dict[col] = df_data[col].split(" ")path_len = len(data_dict[col])# {"sell_path": ["66", "55", ......], "action_type_path":["1", "0", "2", "3", "0",......]}data_out = []for i_ in range(path_len):data_txt = ""for col_ in columns_list:# 统计点击购买行为if data_dict["action_type_path"][i_]  == action_type:data_txt += "_" + data_dict[col_][i_]data_out.append(data_txt)return len(set(data_out))        except:return -1
def user_col_cnt_(df_data, columns_list, action_type, name):df_data[name] = df_data.apply(lambda x:col_cnt_(x, columns_list, action_type), axis=1)return df_datadef user_col_unique_(df_data, columns_list, action_type, name):df_data[name] = df_data.apply(lambda x:col_unique_(x, columns_list, action_type), axis=1)return df_data
# 不同店铺的点击次数
all_data_test = user_col_cnt_(all_data_test, ["seller_path"], "1", "user_cnt_1")
all_data_test = user_col_cnt_(all_data_test, ["seller_path"], "0", "user_cnt_0")
all_data_test = user_col_cnt_(all_data_test, ["seller_path"], "2", "user_cnt_2")
all_data_test = user_col_cnt_(all_data_test, ["seller_path"], "3", "user_cnt_3")
# 店铺唯一值
all_data_test = user_col_unique_(all_data_test, ["seller_path"], "0", "user_unique_0")
all_data_test = user_col_unique_(all_data_test, ["seller_path"], "1", "user_unique_1")
all_data_test = user_col_unique_(all_data_test, ["seller_path"], "2", "user_unique_2")
all_data_test = user_col_unique_(all_data_test, ["seller_path"], "3", "user_unique_3")

统计用户历史对该商品的评分

def user_merchant_mark(df_data, merchant_id, seller_path, action_type_path, action_type):"""统计历史用户的打分情况"""sell_len = len(df_data[seller_path].split(" "))data_dict = {}data_dict[seller_path] = df_data[seller_path].split(" ")data_dict[action_type_path] = df_data[action_type_path].split(" ")# 遍历历史商铺数据访问数据mark = 0for i in range(sell_len):if data_dict[seller_path][i] == str(df_data[merchant_id]):if data_dict[action_type_path][i] == action_type:mark += 1return 0
def user_merchant_mark_all(user_data, merchant_id, seller_path, action_type_path, action_type, name):"""统计所有用户的点击情况"""user_data[name + "_" + action_type] = user_data.apply(lambda x: user_merchant_mark(x, merchant_id, seller_path, action_type_path, action_type), axis=1)return user_data
# 用户针对此商家有多少次 0、1、2、3动作
all_data_test = user_merchant_mark_all(all_data_test,'merchant_id','seller_path','action_type_path','0','user_merchant_action')
all_data_test = user_merchant_mark_all(all_data_test,'merchant_id','seller_path','action_type_path','1','user_merchant_action')
all_data_test = user_merchant_mark_all(all_data_test,'merchant_id','seller_path','action_type_path','2','user_merchant_action')
all_data_test = user_merchant_mark_all(all_data_test,'merchant_id','seller_path','action_type_path','3','user_merchant_action')
all_data_test.columns
Index(['user_id', 'merchant_id', 'label', 'prob', 'age_range', 'gender','item_path', 'cat_path', 'seller_path', 'brand_path', 'time_stamp_path','action_type_path', 'user_seller_cnt', 'user_seller_unique','user_cat_unique', 'user_item_unique', 'user_brand_unique','user_time_stamp_unique', 'user_action_type_unique','user_time_stamp_min', 'user_time_stamp_max', 'time_stamp_range','user_time_stamp_std', 'user_seller_most_1', 'user_cat_most_1','user_brand_most_1', 'user_action_type_1', 'user_seller_most_1_cnt','user_cat_most_1_cnt', 'user_brand_most_1_cnt','user_action_type_1_cnt', 'age_0.0', 'age_1.0', 'age_2.0', 'age_3.0','age_4.0', 'age_5.0', 'age_6.0', 'age_7.0', 'age_8.0', 'gender_0.0','gender_1.0', 'gender_2.0', 'user_cnt_1', 'user_cnt_0', 'user_cnt_2','user_cnt_3', 'user_unique_0', 'user_unique_1', 'user_unique_2','user_unique_3', 'user_merchant_action_0', 'user_merchant_action_1','user_merchant_action_2', 'user_merchant_action_3'],dtype='object')

提取统计变量(商铺特征构造)

# user_log_test = user_log.sample(2000)
user_log_test = user_log
# 构建表达式(统计用户的历史信息)
list_join_func = lambda x: " ".join(str(i) for i in x)
agg_seller_dict = {"item_id": list_join_func,"cat_id": list_join_func,"brand_id": list_join_func,"user_id": list_join_func,"time_stamp": list_join_func,"action_type": list_join_func
}rename_seller_dict = {"item_id": "item_path","cat_id": "cat_path","user_id": "user_path","brand_id": "brand_path","time_stamp": "time_stamp_path","action_type": "action_type_path"
}

商铺基本信息

例如:访问人数、商品种类数目、商铺中的品牌数目

# 构建商品信息表
user_log_seller = user_log_test.groupby("seller_id").agg(agg_seller_dict).reset_index().rename(columns=rename_seller_dict)# 统计商铺被用户点击的总次数
user_log_seller = user_cnt(user_log_seller, "user_path", "seller_user_cnt")# 统计商铺有多少不同消费着(上一步去重)
user_log_seller = user_nunique(user_log_seller, "user_path", "seller_user_unique")# 商铺有多少种类商品
user_log_seller = user_nunique(user_log_seller, "cat_path", "seller_cat_unique")# 商铺有多少不同的商品
user_log_seller = user_nunique(user_log_seller, "item_path", "seller_item_unique")# 商铺有多少不同的品牌
user_log_seller = user_nunique(user_log_seller, "brand_path", "seller_brand_unique")# 商铺被点击的时间相差天数
user_log_seller = user_nunique(user_log_seller, "action_type_path", "seller_action_type_unique")

商铺时间信息

# 最早时间
user_log_seller = user_min(user_log_seller, "time_stamp_path", "seller_time_stamp_min")# 最近时间
user_log_seller = user_max(user_log_seller, "time_stamp_path", "seller_time_stamp_max")# 用户点击时间间隔
user_log_seller["seller_time_stamp_range"] = user_log_seller["seller_time_stamp_max"] - user_log_seller["seller_time_stamp_min"]# 活跃天数的方差,描述商铺被访问时间的活跃的波动情况
user_log_seller = user_std(user_log_seller, "time_stamp_path", "seller_user_time_stamp_std")# 商铺被点击的时间活跃天数(商铺的活跃度)
user_log_seller = user_nunique(user_log_seller, "time_stamp_path", "seller_time_stamp_unique")

商铺被点击信息

# 统计商品被点击情况
def user_action_cnt(df_data,col_action,action_type,name):func = lambda x: len([i for i in x.split(' ') if i == action_type])df_data[name+'_'+action_type] = df_data[col_action].apply(func) return df_data
user_log_seller = user_action_cnt(user_log_seller, "action_type_path", "1", "seller_cnt_1")
user_log_seller = user_action_cnt(user_log_seller, "action_type_path", "0", "seller_cnt_0")
user_log_seller = user_action_cnt(user_log_seller, "action_type_path", "2", "seller_cnt_2")
user_log_seller = user_action_cnt(user_log_seller, "action_type_path", "3", "seller_cnt_3")

合并数据

user_log_seller = user_log_seller.rename(columns={"seller_id": "merchant_id"})
seller_features = [c for c in user_log_seller.columns if c not in ["item_path", "cat_path", "user_path", "brand_path", "time_stamp_path", "action_type_path"]]user_log_seller  = user_log_seller[seller_features]all_data_test = all_data_test.merge(user_log_seller, on="merchant_id", how="left")
all_data_test.columns
Index(['user_id', 'merchant_id', 'label', 'prob', 'age_range', 'gender','item_path', 'cat_path', 'seller_path', 'brand_path', 'time_stamp_path','action_type_path', 'user_seller_cnt', 'user_seller_unique','user_cat_unique', 'user_item_unique', 'user_brand_unique','user_time_stamp_unique', 'user_action_type_unique','user_time_stamp_min', 'user_time_stamp_max', 'time_stamp_range','user_time_stamp_std', 'user_seller_most_1', 'user_cat_most_1','user_brand_most_1', 'user_action_type_1', 'user_seller_most_1_cnt','user_cat_most_1_cnt', 'user_brand_most_1_cnt','user_action_type_1_cnt', 'age_0.0', 'age_1.0', 'age_2.0', 'age_3.0','age_4.0', 'age_5.0', 'age_6.0', 'age_7.0', 'age_8.0', 'gender_0.0','gender_1.0', 'gender_2.0', 'user_cnt_1', 'user_cnt_0', 'user_cnt_2','user_cnt_3', 'user_unique_0', 'user_unique_1', 'user_unique_2','user_unique_3', 'user_merchant_action_0', 'user_merchant_action_1','user_merchant_action_2', 'user_merchant_action_3', 'seller_user_cnt','seller_user_unique', 'seller_cat_unique', 'seller_item_unique','seller_brand_unique', 'seller_action_type_unique','seller_time_stamp_min', 'seller_time_stamp_max','seller_time_stamp_range', 'seller_user_time_stamp_std','seller_time_stamp_unique', 'seller_cnt_1_1', 'seller_cnt_0_0','seller_cnt_2_2', 'seller_cnt_3_3'],dtype='object')

天猫用户重复购买预测之特征工程相关推荐

  1. 天猫用户重复购买预测——特征工程

    天猫用户重复购买预测--特征工程 1.特征工程 1.1 概念 1.2 特征归一化 1.3 类别型特征转换 1.4 高维组合特征的处理 1.5 组合特征 1.6 文本表示模型 2. 赛题特征工程思路 3 ...

  2. 天猫用户重复购买预测——数据探索

    天猫用户重复购买预测--数据探索 1. 理论 1.1 缺失数据处理 1.2 不均衡样本 1.2.1 随机欠采样 1.2.2 随机过采样 1.2.3 基于聚类的过采样方法 1.2.4 SMOTE算法 1 ...

  3. 天猫用户重复购买预测赛题——赛题理解 + 数据探索

    天猫用户重复购买预测赛题--赛题理解 + 数据探索 理论知识 1. 赛题信息 2. 评估指标 AUC 3. 查看数据样例 4. 缺失值查看 5. 查看数据分布 6. 探究影响复购的各种因素 理论知识 ...

  4. 阿里云天池大赛赛题(机器学习)——天猫用户重复购买预测(完整代码)

    目录 赛题背景 全代码 导入包 读取数据(训练数据前10000行,测试数据前100条) 读取全部数据 获取训练和测试数据 切分40%数据用于线下验证 交叉验证:评估估算器性能 F1验证 Shuffle ...

  5. 天猫用户重复购买预测之数据分析

    赛题理解 赛题链接 赛题背景: 商家有时会在特定日期,例如Boxing-day,黑色星期五或是双十一(11月11日)开展大型促销活动或者发放优惠券以吸引消费者,然而很多被吸引来的买家都是一次性消费者, ...

  6. 天猫用户复购预测之特征工程构建1

    导入包 import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns fro ...

  7. 天池竞赛赛题-特征工程-天猫用户重复购买预测解析

    前言 以下是我为大家准备的几个精品专栏,喜欢的小伙伴可自行订阅,你的支持就是我不断更新的动力哟! MATLAB-30天带你从入门到精通 MATLAB深入理解高级教程(附源码) tableau可视化数据 ...

  8. 【BI学习作业13-淘宝定向广告演化与天猫用户复购预测】

    目录 写在前面的话 1.思考题 1.1电商定向广告和搜索广告有怎样的区别,算法模型是否有差别 1.1.1电商定向广告 1.1.2搜索广告 1.2定向广告都有哪些常见的使用模型,包括Attention机 ...

  9. 二手车价格预测task03:特征工程

    二手车价格预测task03:特征工程 1.学习了operator模块operator.itemgetter()函数 2.学习了箱线图 3.了解了特征工程的方法 (内容介绍) 4.敲代码学习,加注解 以 ...

最新文章

  1. 【FFmpeg】ffplay 播放视频命令 ( 播放 | 暂停 | 停止 | 音量控制 | 进度控制 | 音频流 / 视频流 / 字幕流 / 节目切换 )
  2. eigrp ospf 邻居建立过程比较
  3. HDU 5727 Necklace
  4. vmware14安装macos10.12完美可升级10.13
  5. oracle 子表数据变化时主表也会更新_亿信ABI版本重大更新,新增60余个实用新功能,还有一项黑科技...
  6. editthiscookie
  7. 台大李宏毅Machine Learning 2017Fall学习笔记 (13)Semi-supervised Learning
  8. intellij idea 导出可执行jar
  9. 夜深人静写算法(二) - 动态规划
  10. python删除列表第一个,在Python中删除列表的第一个元素
  11. Android名片识别
  12. Kafka源码-发送器Sender类型的的sendProducerData 模版方法
  13. CE实战:修改植物大战僵尸中阳光数值
  14. 万万没想到!TCP/IP 协议会有这么多漏洞
  15. 这些车企在企业微信里,装上高速的“组织引擎”
  16. Codeforces 869 A.The Artful Expedient(博弈论)
  17. vue中使用电子签名
  18. 标准c语言程序的语句都以什么结尾,c程序的执行是从什么开始到什么结束?
  19. 以数据赋能业务,qlik为企业搭建透明绩效管理平台
  20. ARM体系结构与编程_2015.08_P513_完整版PDF电子书下载 带索引书签目录高清版

热门文章

  1. mysql事务6,MySQL6-事务
  2. 前端关系图谱插件_js前端使用jOrgChart插件实现组织架构图的展示
  3. J2EE--自定义mvc增删改查
  4. fiddler证书生成ca证书命令及抓包配置
  5. 水稻广谱与持久抗稻瘟病基因位点Pigm的抗病机制
  6. 在Composure去除掉对体积云和雾的捕获
  7. 重复启动Tomcat时,大概率出现Deploying web application direct
  8. 电商新趋势来临!?解析Dtop 环球嘉年华电商是否值得加入!
  9. 战舰帝国服务器维护,【图片】9月17日更新公告亲爱的司令官:《战舰帝国》于9月17日维护,成功更新后可以获得200个钻石的更新补偿_战舰帝国吧_百度贴吧...
  10. Flutter 仿滴滴出行App,2021最新华为Android校招面试题