【发布时间】:2019-08-24 09:20:13
【问题描述】:
我正在尝试运行以下命令但遇到错误:ValueError: Lengths must match to compare
from sklearn.feature_selection import chi2
import numpy as np
N = 2
for Product, category_id in sorted(category_to_id.items()):
features_chi2 = chi2(features, labels == category_id)
indices = np.argsort(features_chi2[0])
feature_names = np.array(tfidf.get_feature_names())[indices]
unigrams = [v for v in feature_names if len(v.split(' ')) == 1]
bigrams = [v for v in feature_names if len(v.split(' ')) == 2]
print("# '{}':".format(Product))
print(" . Most correlated unigrams:\n . {}".format('\n . '.join(unigrams[-N:])))
print(" . Most correlated bigrams:\n . {}".format('\n . '.join(bigrams[-N:])))
代码来自https://towardsdatascience.com/multi-class-text-classification-with-scikit-learn-12f1e60e0a9f
输出是:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-45-bbfd1a1f6a1a> in <module>()
3 N = 2
4 for Product, category_id in sorted(category_to_id.items()):
----> 5 features_chi2 = chi2(features, labels == category)
6 indices = np.argsort(features_chi2[0])
7 feature_names = np.array(tfidf.get_feature_names())[indices]
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops.py in wrapper(self, other, axis)
1221 # as it will broadcast
1222 if other.ndim != 0 and len(self) != len(other):
-> 1223 raise ValueError('Lengths must match to compare')
1224
1225 res_values = na_op(self.values, np.asarray(other))
ValueError: Lengths must match to compare
len(features) 和 len(labels) 打印相同的计数。
【问题讨论】:
-
您的回溯在第 5 行中有
labels == category。但在代码中您有labels == category_id。所以你在这里粘贴的代码不是你正在执行的实际代码? -
当然,这是我无法发现的非常简单的错误。我在看
features和labels而不是category/category_id。如果您发布答案,我很乐意接受。
标签: python scikit-learn jupyter-notebook data-science