分组数据上的特征工程
现在,您将在上一个练习的基础上,加入一个额外的特征:每台源计算机所使用的唯一协议数量。请注意,对于分组数据,始终可以用这种方式构造特征:以所有分类列的唯一元素数量、以及所有数值列的均值作为起点。与之前一样,flows 已预加载,cross_val_score() 用于衡量准确率,提供了 AdaBoostClassifier(),以及作为 pd 的 pandas 和作为 np 的 numpy。
本练习是课程的一部分
用 Python 设计机器学习工作流
练习说明
- 在给定的分组迭代器上应用
lambda函数,计算每台源计算机使用的唯一协议数量。您可以使用set()将protocol列压缩为唯一值的集合。 - 通过提供索引并将列命名为
protocol,把结果转换为形状正确的数据框。 - 将新的数据框与已有的数据框
X按列拼接。 - 使用
cross_val_score()评估AdaBoostClassifier()在该新数据集上的准确率。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create a feature counting unique protocols per source
protocols = flows.groupby('source_computer').apply(
lambda df: ____)
# Convert this feature into a dataframe, naming the column
protocols_DF = pd.DataFrame(
protocols, index=____, columns=____)
# Now concatenate this feature with the previous dataset, X
X_more = pd.concat([X, ____], axis=____)
# Refit the classifier and report its accuracy
print(____(____(
AdaBoostClassifier(), ____, y)))