第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > Feature Engineering 特征工程 3. Feature Generation

Feature Engineering 特征工程 3. Feature Generation

时间:2022-10-31 03:03:03

相关推荐

Feature Engineering 特征工程 3. Feature Generation

文章目录

1. 组合特征2. 过去7天的数据3. 上一个相同类型的项目的时间4. 转换数值特征

learn from /learn/feature-engineering

上一篇:Feature Engineering 特征工程 2. Categorical Encodings

下一篇:Feature Engineering 特征工程 4. Feature Selection

从原始数据创建新特征是改进模型的最佳方法之一

例如,数据有很长连续时间的,我们可以把最近一周的提取出来作为一个新的特征

1. 组合特征

最简单方法之一是组合特征

例如,如果一条记录的国家/地区为"CA",类别为"Music",则可以创建一个新值" CA_Music"

可以从所有分类特征中构建组合特征,也可以使用三个或更多特征进行交互,但是效果往往会变坏

interactions = ks['category']+'_'+ks['country'],像python一样直接相加interactions.head(10)

0 Poetry_GB1 Narrative Film_US2 Narrative Film_US3 Music_US4Film & Video_US5 Restaurants_US6 Food_US7 Drinks_US8 Product Design_US9 Documentary_USdtype: object

将新特征assign进数据

label_enc = LabelEncoder()data_interaction = X.assign(category_country=label_enc.fit_transform(interactions))data_interaction.head()

2. 过去7天的数据

launched = pd.Series(ks.index, index=ks.launched, name="count_7_days").sort_index()# 数据值为索引, 新的索引为建立的时间,新特征名称,按索引(时间)排序launched.head(20)

launched1970-01-01 01:00:00945791970-01-01 01:00:00 3190021970-01-01 01:00:00 2479131970-01-01 01:00:00481471970-01-01 01:00:00753971970-01-01 01:00:0028421970-01-01 01:00:00 273779-04-21 21:02:48 169268-04-23 00:07:53 322000-04-24 21:52:03 138572-04-25 17:36:21 325391-04-27 14:10:39 122662-04-28 13:55:41 213711-04-29 02:04:21 345606-04-29 02:58:50 235255-04-29 04:37:3798954-04-29 05:26:32 342226-04-29 06:43:44 275091-04-29 13:52:03 284115-04-29 22:08:1332898Name: count_7_days, dtype: int64

发现最顶上的7个数据是错误的(时间一样),本节里暂时不去考虑

.rolling('7d'),设置一个窗口

count_7_days = launched.rolling('7d').count()-1 # -1表示不包含当前日期print(count_7_days.head(20))

launched1970-01-01 01:00:000.01970-01-01 01:00:001.01970-01-01 01:00:002.01970-01-01 01:00:003.01970-01-01 01:00:004.01970-01-01 01:00:005.01970-01-01 01:00:006.0-04-21 21:02:480.0-04-23 00:07:531.0-04-24 21:52:032.0-04-25 17:36:213.0-04-27 14:10:394.0-04-28 13:55:415.0-04-29 02:04:215.0-04-29 02:58:506.0-04-29 04:37:377.0-04-29 05:26:328.0-04-29 06:43:449.0-04-29 13:52:03 10.0-04-29 22:08:13 11.0Name: count_7_days, dtype: float64

%matplotlib inlineimport matplotlib.pyplot as pltplt.rcParams['font.sans-serif'] = 'SimHei'plt.plot(count_7_days[7:]);plt.title("最近7天的数据")plt.show()

把新特征数据,reindex后,跟原数据合并

count_7_days.index = launched.valuescount_7_days = count_7_days.reindex(ks.index)count_7_days.head(10)

0 1487.01 .02279.03984.04752.05522.06708.07 1566.08 1048.09975.0Name: count_7_days, dtype: float64

X.join(count_7_days).head(10)join合并

3. 上一个相同类型的项目的时间

比如,电影之类的上映,如果同类型的扎堆了,可能被对手抢占了份额

def time_since_last_project(series):return series.diff().dt.total_seconds()/3600df = ks[['category','launched']].sort_values('launched')# 按时间排序timedeltas = df.groupby('category').transform(time_since_last_project)# 按分类分组,然后调用函数进行转换,算得上一个同类的时间跟自己的间隔是多少小时timedeltas.head(20)

NaN 表示该类型是第一次出现,填上均值或者中位数然后跟其他数据合并之前需要把index调整成一致

timedeltas = timedeltas.fillna(timedeltas.median()).reindex(X.index)timedeltas.head(20)

4. 转换数值特征

Transforming numerical features,一些模型在数据分布是正态分布的时候,工作的很好,所以可以对数据进行开方、取对数转换

plt.hist(ks.goal, range=(0, 100000), bins=50);plt.title('Goal');

plt.hist(np.sqrt(ks.goal), range=(0, 400), bins=50);plt.title('Sqrt(Goal)');

plt.hist(np.log(ks.goal), range=(0, 25), bins=50);plt.title('Log(Goal)');

log 转换对基于树的模型没有什么用,但是对线性模型或者神经网络有用我们需要转成新的特征,然后做一些测试,选择效果最好的转换方法。

上一篇:Feature Engineering 特征工程 2. Categorical Encodings

下一篇:Feature Engineering 特征工程 4. Feature Selection

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。