【问题标题】:Sklearn: Found input variables with inconsistent numbers of samples:Sklearn:发现样本数量不一致的输入变量:
【发布时间】:2021-03-18 08:38:18
【问题描述】:

我已经建立了一个模型。

est1_pre = ColumnTransformer([('catONEHOT', OneHotEncoder(dtype='int',handle_unknown='ignore'),['Var1'])],remainder='drop')
est2_pre = ColumnTransformer([('BOW', TfidfVectorizer(ngram_range=(1, 3),max_features=1000),['Var2'])],remainder='drop')


    m1= Pipeline([('FeaturePreprocessing', est1_pre),
                              ('clf',alternative)])
    m2= Pipeline([('FeaturePreprocessing', est2_pre),
                              ('clf',alternative)])
    model_combo = StackingClassifier(
         estimators=[('cate',m1),('text',m2)],
         final_estimator=RandomForestClassifier(n_estimators=10,
                                               random_state=42)
     )

我可以使用m1m2 成功拟合和预测。 但是,当我查看model_combo 的组合时 任何调用 .fit/.predict 的尝试都会导致 ValueError: Found input variables with inconsistent numbers of samples:

    model_fitted=model_combo.fit(x_train,y_train)

x_train 包含 Var1Var2 如何适应model_combo?

【问题讨论】:

    标签: machine-learning scikit-learn


    【解决方案1】:

    问题在于 sklearn 文本预处理器(在本例中为 TfidfVectorizer)处理一维数据,而不是像大多数其他预处理器那样处理二维数据。因此矢量化器将其输入视为其的可迭代对象,因此只有一个“文档”。这可以在ColumnTransformer 中通过在列表中指定要对 not 进行操作的列来解决:

    est2_pre = ColumnTransformer([('BOW', TfidfVectorizer(ngram_range=(1, 3),max_features=1000),'Var2')],remainder='drop') 
    

    【讨论】:

      猜你喜欢
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 2021-06-20
      • 2018-06-25
      • 2021-05-06
      • 2020-11-19
      • 2019-12-11
      相关资源
      最近更新 更多