我最近发现了Fernando Benites的MLARAM分类器;Elena Sapozhnikova。当尝试在一组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的问题,可以帮我解决这个问题?
感谢任何人的帮助!
发布于 2020-04-11 07:35:03
所以基本上这是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
https://stackoverflow.com/questions/61025190
复制