【问题标题】:XGBClassifier ValueError: operands could not be broadcast together with shapes (2557,) (8,) (2557,)XGBClassifier ValueError: 操作数不能与形状一起广播 (2557,) (8,) (2557,)
【发布时间】:2021-09-12 09:40:55
【问题描述】:

我正在做一个文本分类项目。

在探索不同的分类器时,我遇到了XGBClassifier

我的分类任务是多类。 在尝试对分类器进行评分时,我遇到了上述错误 - 我猜需要进行一些重塑,但我不明白为什么。 对我来说奇怪的是其他分类器工作得很好(即使是这个带有默认参数的分类器)

这是我的代码中的相关部分:

algorithms = [    
    svm.LinearSVC(),  # <<<=== Works    
    linear_model.RidgeClassifier(), # <<<=== Works    
    XGBClassifier(),  # <<<=== Works    
    XGBClassifier(objective='multi:softprob', num_class=len(groups_count_dict), eval_metric='merror')  # <<<=== Not working
]

def train(algorithm, X_train, y_train):
    model = Pipeline([       
        ('vect', transformer),
        ('classifier', OneVsRestClassifier(algorithm))
    ])
    model.fit(X_train, y_train)

    return model

score_dict = {}
algorithm_to_model_dict = {}
for algorithm in algorithms:
    print()
    print(f'trying {algorithm}')
    model = train(algorithm, X_train, y_train)
    score = model.score(X_test, y_test)
    score_dict[algorithm] = int(score * 100)
    algorithm_to_model_dict[algorithm] = model
    
sorted_score_dict = {k: v for k, v in sorted(score_dict.items(), key=lambda item: item[1])}
for classifier, score in sorted_score_dict.items():
    print(f'{classifier.__class__.__name__}: score is {score}%')

这又是错误:

ValueError: operands could not be broadcast together with shapes (2557,) (8,) (2557,)

不确定它是否相关,但我还是会提到它 - 我的 transformer 是这样创建的:

tuples = []
tfidf_kwargs = {'ngram_range': (1, 2), 'stop_words': 'english', 'sublinear_tf': True}
for col in list(features.columns):
    tuples.append((f'vec_{col}', TfidfVectorizer(**tfidf_kwargs), col))

transformer = ColumnTransformer(tuples, remainder='passthrough')

提前致谢

编辑:

添加完整的跟踪:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-576cd62f3df0> in <module>
     84     print(f'trying {algorithm}')
     85     model = train(algorithm, X_train, y_train)
---> 86     score = model.score(X_test, y_test)
     87     score_dict[algorithm] = int(score * 100)
     88     algorithm_to_model_dict[algorithm] = model

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sklearn/utils/metaestimators.py in <lambda>(*args, **kwargs)
    118 
    119         # lambda, but not partial, allows help() to work with update_wrapper
--> 120         out = lambda *args, **kwargs: self.fn(obj, *args, **kwargs)
    121         # update the docstring of the returned function
    122         update_wrapper(out, self.fn)

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sklearn/pipeline.py in score(self, X, y, sample_weight)
    620         if sample_weight is not None:
    621             score_params['sample_weight'] = sample_weight
--> 622         return self.steps[-1][-1].score(Xt, y, **score_params)
    623 
    624     @property

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sklearn/base.py in score(self, X, y, sample_weight)
    498         """
    499         from .metrics import accuracy_score
--> 500         return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
    501 
    502     def _more_tags(self):

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sklearn/multiclass.py in predict(self, X)
    365             for i, e in enumerate(self.estimators_):
    366                 pred = _predict_binary(e, X)
--> 367                 np.maximum(maxima, pred, out=maxima)
    368                 argmaxima[maxima == pred] = i
    369             return self.classes_[argmaxima]

ValueError: operands could not be broadcast together with shapes (2557,) (8,) (2557,) 

打印X_testy_test 的形状得到:(2557, 12) (2557,)

我能够理解(8,) 的来源——它是groups_count_dict 的长度

【问题讨论】:

  • 你能链接堆栈跟踪吗?
  • @CutePoison - 我一定会添加它
  • 在计算score.model,score之前尝试打印X_testy_test的形状,看看它是否在所有模型中都一致。此外,我在文档xgboost.readthedocs.io/en/latest/python/… 中看不到参数num_class
  • @CutePoison 这里是输出:(2557, 12) (2557,)

标签: python machine-learning scikit-learn nlp


【解决方案1】:

原来解决方案是从管道中删除 OneVsRestClassifier 用法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 2017-09-09
    • 2012-10-31
    相关资源
    最近更新 更多