rob*_*ero 16 python pandas scikit-learn data-science
我正在使用库中的模块Pipeline对我的数据集执行特征工程。ColumnTransformersklearn
数据集最初看起来像这样:
| 日期 | 日期块编号 | 店铺ID | 商品编号 | 商品价格 |
|---|---|---|---|---|
| 2013年1月2日 | 0 | 59 | 22154 | 999.00 |
| 2013年1月3日 | 0 | 25 | 2552 | 899.00 |
| 2013年1月5日 | 0 | 25 | 2552 | 899.00 |
| 2013年1月6日 | 0 | 25 | 2554 | 1709.05 |
| 2013年1月15日 | 0 | 25 | 2555 | 1099.00 |
$> data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2935849 entries, 0 to 2935848
Data columns (total 6 columns):
# Column Dtype
--- ------ -----
0 date object
1 date_block_num object
2 shop_id object
3 item_id object
4 item_price float64
dtypes: float64(2), int64(3), object(1)
memory usage: 134.4+ MB
Run Code Online (Sandbox Code Playgroud)
然后我有以下转变:
num_column_transformer = ColumnTransformer(
transformers=[
("std_scaler", StandardScaler(), make_column_selector(dtype_include=np.number)),
],
remainder="passthrough"
)
num_pipeline = Pipeline(
steps=[
("percent_item_cnt_day_per_shop", PercentOverTotalAttributeWholeAdder(
attribute_percent_name="shop_id",
attribute_total_name="item_cnt_day",
new_attribute_name="%_item_cnt_day_per_shop")
),
("percent_item_cnt_day_per_item", PercentOverTotalAttributeWholeAdder(
attribute_percent_name="item_id",
attribute_total_name="item_cnt_day",
new_attribute_name="%_item_cnt_day_per_item")
),
("percent_sales_per_shop", SalesPerAttributeOverTotalSalesAdder(
attribute_percent_name="shop_id",
new_attribute_name="%_sales_per_shop")
),
("percent_sales_per_item", SalesPerAttributeOverTotalSalesAdder(
attribute_percent_name="item_id",
new_attribute_name="%_sales_per_item")
),
("num_column_transformer", num_column_transformer),
]
)
Run Code Online (Sandbox Code Playgroud)
前四个Transformers创建四个新的不同数值变量,最后一个适用StandardScaler于数据集的所有数值。
执行后,我得到以下数据:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| -0.092652 | -0.765612 | -0.173122 | -0.756606 | -0.379775 | 2013年1月2日 | 0 | 59 | 22154 |
| -0.092652 | 1.557684 | -0.175922 | 1.563224 | -0.394319 | 2013年1月3日 | 0 | 25 | 2552 |
| -0.856351 | 1.557684 | -0.175922 | 1.563224 | -0.394319 | 2013年1月5日 | 0 | 25 | 2552 |
| -0.092652 | 1.557684 | -0.17613 | 1.563224 | -0.396646 | 2013年1月6日 | 0 | 25 | 2554 |
| -0.092652 | 1.557684 | -0.173278 | 1.563224 | -0.380647 | 2013年1月15日 | 0 | 25 | 2555 |
我想要以下输出:
| 日期 | 日期块编号 | 店铺ID | 商品编号 | 商品价格 | %_item_cnt_day_per_shop | %_item_cnt_day_per_item | %_sales_per_shop | %_sales_per_item %_sales_per_item |
|---|---|---|---|---|---|---|---|---|
| 2013年1月2日 | 0 | 59 | 22154 | -0.092652 | -0.765612 | -0.173122 | -0.756606 | -0.379775 |
| 2013年1月3日 | 0 | 25 | 2552 | -0.092652 | 1.557684 | -0.175922 | 1.563224 | -0.394319 |
| 2013年1月5日 | 0 | 25 | 2552 | -0.856351 | 1.557684 | -0.175922 | 1.563224 | -0.394319 |
| 2013年1月6日 | 0 | 25 | 2554 | -0.092652 | 1.557684 | -0.17613 | 1.563224 | -0.396646 |
| 2013年1月15日 | 0 | 25 | 2555 | -0.092652 | 1.557684 | -0.173278 | 1.563224 | -0.380647 |
正如您所看到的,输出中的columns 5、6、7、 和对应于原始数据集中的前四列。8例如,我不知道该item_price特征位于输出表中的哪个位置。
ami*_*ola 20
处理 时需要注意一点,文档ColumnTransformer中报告如下:
转换后的特征矩阵中的列顺序遵循转换器列表中指定列的顺序。
ColumnTransformer这就是你的实例把事情搞砸的原因。事实上,考虑这个类似于您的设置的简化示例:
import numpy as np
import pandas as pd
from sklearn.compose import ColumnTransformer, make_column_selector
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
df = pd.DataFrame({
'date': ['02.01.2013', '03.01.2013', '05.01.2013', '06.01.2013', '15.01.2013'],
'date_block_num': ['0', '0', '0', '0', '0'],
'shop_id': ['59', '25', '25', '25', '25'],
'item_id': ['22514', '2252', '2252', '2254', '2255'],
'item_price': [999.00, 899.00, 899.00, 1709.05, 1099.00]})
ct = ColumnTransformer([
('std_scaler', StandardScaler(), make_column_selector(dtype_include=np.number))],
remainder='passthrough')
pd.DataFrame(ct.fit_transform(df), columns=ct.get_feature_names_out())
Run Code Online (Sandbox Code Playgroud)
您可能会注意到,转换后的数据帧中的第一列结果是数字列,即经过缩放的列(也是转换器列表中的第一列)。
相反,这里有一个示例,说明如何通过在传递所有字符串变量后推迟数值变量的缩放来绕过此类问题,从而确保可以按所需顺序获取列:
ct = ColumnTransformer([
('pass', 'passthrough', make_column_selector(dtype_include=object)),
('std_scaler', StandardScaler(), make_column_selector(dtype_include=np.number))
])
pd.DataFrame(ct.fit_transform(df), columns=ct.get_feature_names_out())
Run Code Online (Sandbox Code Playgroud)
为了完成图片,这里尝试重现您的管道(尽管自定义变压器肯定与您的略有不同):
from sklearn.base import BaseEstimator, TransformerMixin
class PercentOverTotalAttributeWholeAdder(BaseEstimator, TransformerMixin):
def __init__(self, attribute_percent_name='shop_id', new_attribute_name='%_item_cnt_day_per_shop'):
self.attribute_percent_name = attribute_percent_name
self.new_attribute_name = new_attribute_name
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
df[self.new_attribute_name] = df.groupby(by=self.attribute_percent_name)[self.attribute_percent_name].transform('count') / df.shape[0]
return df
ct_pipe = ColumnTransformer([
('pass', 'passthrough', make_column_selector(dtype_include=object)),
('std_scaler', StandardScaler(), make_column_selector(dtype_include=np.number))
], verbose_feature_names_out=False)
pipe = Pipeline([
('percent_item_cnt_day_per_shop', PercentOverTotalAttributeWholeAdder(
attribute_percent_name='shop_id',
new_attribute_name='%_item_cnt_day_per_shop')
),
('percent_item_cnt_day_per_item', PercentOverTotalAttributeWholeAdder(
attribute_percent_name='item_id',
new_attribute_name='%_item_cnt_day_per_item')
),
('column_trans', ct_pipe),
])
pd.DataFrame(pipe.fit_transform(df), columns=pipe[-1].get_feature_names_out())
Run Code Online (Sandbox Code Playgroud)
最后,请注意该verbose_feature_names_out=False参数确保转换后的数据帧的列名称不会显示引用 中不同转换器的前缀ColumnTransformer。
| 归档时间: |
|
| 查看次数: |
6218 次 |
| 最近记录: |