You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按分组变量为不同行子集拟合缩放器并集成到Pipeline中?

按分组拟合缩放器并集成到Sklearn Pipeline的方法

要实现按分组拟合缩放器(而非全局拟合),同时能集成到Sklearn Pipeline中,最直接的方式是自定义一个符合Sklearn API规范的Transformer,下面是具体实现和用法:

1. 自定义分组缩放器

这个Transformer会按指定分组列,为每个分组单独拟合缩放器,之后用对应分组的缩放器做转换,完全兼容Sklearn的Pipeline流程。

import pandas as pd
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

class GroupScaler(BaseEstimator, TransformerMixin):
    def __init__(self, group_col, scaler=StandardScaler()):
        self.group_col = group_col  # 指定分组列名
        self.scaler = scaler        # 传入要使用的缩放器(可替换为MinMaxScaler等)
        self.group_scalers = {}     # 存储每个分组拟合后的缩放器实例

    def fit(self, X, y=None):
        # 遍历每个分组,单独拟合缩放器
        for group, subset in X.groupby(self.group_col):
            # 提取需要缩放的特征(排除分组列)
            features = subset.drop(columns=[self.group_col])
            # 复制缩放器实例,避免不同分组共享同一实例
            scaler_copy = self.scaler.__class__()
            self.group_scalers[group] = scaler_copy.fit(features)
        return self

    def transform(self, X, y=None):
        scaled_dfs = []
        # 遍历每个分组,用对应缩放器做转换
        for group, subset in X.groupby(self.group_col):
            features = subset.drop(columns=[self.group_col])
            scaled_features = self.group_scalers[group].transform(features)
            # 合并分组列与缩放后的特征,保留原索引
            scaled_df = pd.DataFrame(
                scaled_features, 
                index=subset.index, 
                columns=features.columns
            )
            scaled_df[self.group_col] = subset[self.group_col]
            scaled_dfs.append(scaled_df)
        # 合并所有分组数据,恢复原顺序
        return pd.concat(scaled_dfs).sort_index()

2. 在Pipeline中使用

把自定义的GroupScaler加入Pipeline,和其他Sklearn组件无缝配合:

# 测试数据集
df = pd.DataFrame({'group': [1, 1, 1, 2, 2, 2], 'x': [1,2,3,10,20,30]})

# 创建Pipeline
pipeline = Pipeline([
    ('group_scaler', GroupScaler(group_col='group'))
])

# 拟合并转换数据
df_scaled = pipeline.fit_transform(df)
df_scaled.rename(columns={'x': 'x_scaled'}, inplace=True)
print(df_scaled)

输出结果

x_scaled  group
0 -1.224745      1
1  0.000000      1
2  1.224745      1
3 -1.224745      2
4  0.000000      2
5  1.224745      2

3. 扩展用法

  • 替换缩放器:如果需要用MinMaxScaler,只需初始化时传入scaler=MinMaxScaler()即可
  • 结合其他组件:可以在Pipeline中继续添加模型(比如线性回归、分类器),只需确保transform方法返回的格式符合后续组件要求(若后续是模型,可修改transform方法只返回特征矩阵,去掉分组列)

内容的提问来源于stack exchange,提问作者ascripter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 09:47:45