创建新特征
特征工程也包括实际创建新特征。创建新特征很重要,因为模型的预测准确性依赖于这些特征。在本练习中,您将检查 3 列的属性,这些列在数据中是整数,但表示的是分类值。三列分别是:search_engine_type、product_type 和 advertiser_type。您将为这 3 列,以及 device_id 和 site_id 创建计数特征。这些计数特征表示每个取值对应的点击次数,稍后将用于预测。
pandas 模块已在工作区中以 pd 提供,示例 DataFrame 已加载为 df。
本练习是课程的一部分
用 Python 预测 CTR 的机器学习实战
练习说明
- 打印
feature_list列表中每个特征的总取值数量以及唯一取值数量。 - 使用
.transform(),对new_feature_list中的特征按取值统计点击次数,创建对应的新特征。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Get counts of total and unique values for given features
feature_list = ["search_engine_type", "product_type", "advertiser_type"]
for feature in feature_list:
print(df[feature].____)
print(df[feature].____)
# Define new features as counts
new_feature_list = ['device_id', 'site_id'] + feature_list
for new_feature in new_feature_list:
df[new_feature + '_count'] = df.____(
new_feature)['click'].____("count")
print(df.head(5))