首页 > 解决方案 > skmultilearn MLARAM 引发 Keyerror:0

问题描述

我最近发现了 Fernando Benites 的 MLARAM 分类器;埃琳娜·萨波日尼科娃。尝试在一组 11.000 个文档上评估此分类器时。我使用了来自http://scikit.ml/api/skmultilearn.adapt.mlaram.html#skmultilearn.adapt.MLARAM的更新文档并尝试了:

from skmultilearn.adapt import MLARAM

classifier = MLARAM(threshold=0.05, vigilance=0.95)
classifier.fit(x_train, y_train)
predictions = classifier.predict(x_test)
print(f1_score(y_test, predictions, average='micro'))

但这会引发以下错误:

KeyError                                  Traceback (most recent call last)
~\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 0

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-33-d5080bba579e> in <module>
      2 
      3 classifier = MLARAM(threshold=0.05, vigilance=0.95)
----> 4 classifier.fit(x_train, y_train)
      5 predictions = classifier.predict(x_test)
      6 print(f1_score(y_test, predictions, average='micro'))

C:\Program Files\Python37\lib\site-packages\skmultilearn\adapt\mlaram.py in fit(self, X, y)
    165         X = _normalize_input_space(X)
    166 
--> 167         y_0 = _get_label_vector(y, 0)
    168 
    169         if len(self.neurons) == 0:

C:\Program Files\Python37\lib\site-packages\skmultilearn\adapt\mlaram.py in _get_label_vector(y, i)
     35     if issparse(y):
     36         return numpy.squeeze(numpy.asarray(y[i].todense()))
---> 37     return y[i]
     38 
     39 def _concatenate_with_negation(row):

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802                 indexer = [indexer]

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 0

由于有关此分类器的文档和讨论非常稀少,我无法弄清楚问题所在。

还有其他人遇到了 MLARAM 的问题,可以帮我解决这个问题吗?

任何帮助表示赞赏!

标签: python-3.xmultilabel-classificationskmultilearn

解决方案


所以基本上这是 lil_matrix 的问题。使用 lil_matrix 转换对我有用。但是,老实说,我不知道确切的原因是什么。

from skmultilearn.adapt import MLARAM

# The variables have to be transformed before
x_train_MLARAM = lil_matrix(x_train).toarray()
y_train_MLARAM = lil_matrix(y_train).toarray()
x_test_MLARAM = lil_matrix(x_test).toarray()

classifier = MLARAM(threshold=0.05, vigilance=0.95)
classifier.fit(x_train_MLARAM, y_train_MLARAM)
predictions = classifier.predict(x_test_MLARAM)
print(f1_score(y_test, predictions, average='micro'))

可以在此处找到有关 lil_matrix 的更多信息:https ://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.lil_matrix.html


推荐阅读