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

使用imblearn Pipeline时触发TypeError:'Pipeline'类型不可下标访问

问题解决:TypeError: type 'Pipeline' is not subscriptable

错误原因

你错误地将imblearn.pipeline.Pipeline类当成了可下标访问的对象(使用[]),但实际上Pipeline是需要实例化的类,必须通过调用构造函数(使用())并传入步骤列表来创建实例。

修正步骤

  1. 确保正确导入imblearn的Pipeline类:
from imblearn.pipeline import Pipeline
  1. 将创建pipeline的代码中的方括号[]替换为圆括号(),把步骤列表作为参数传入Pipeline构造函数。

修正后的完整代码

from sklearn.preprocessing import OrdinalEncoder, OneHotEncoder, StandardScaler
from sklearn.compose import ColumnTransformer
from imblearn.pipeline import Pipeline
from imblearn.over_sampling import SMOTE
from xgboost import XGBClassifier

transform = [("ord", OrdinalEncoder(), ['job', 'education']),
             ("ohe", OneHotEncoder(), ['contact', 'month', 'poutcome'])]

ct = ColumnTransformer(transform, remainder='passthrough')

# 替换[]为(),调用Pipeline构造函数创建实例
p = Pipeline([('ct', ct),
              ('scaler', StandardScaler()),
              ('smote', SMOTE(random_state=0)),
              ('xgb', XGBClassifier())])

额外说明

  • imblearn的Pipeline兼容sklearn的转换器、模型,同时支持将采样器(如SMOTE)作为流水线步骤,这是它区别于sklearn Pipeline的核心特性。
  • 流水线的每个步骤必须是(步骤名称, 处理对象)的元组,执行顺序严格按照列表中的排列顺序。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 04:29:52