Я пытаюсь запустить следующее, но сталкиваюсь с ошибкой: 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)
печатают одинаковые значения.