0

I am using the MlKnn method and I am able to fit the classifier and to make predictions through the command classifier.predict(Test).

The result is a scipy.sparse.lil.lil_matrix which has only the predictions itself.

I don't understand how to assign these predictions to the original data set Test which is scipy.sparse.csr.csr_matrix format.

Could somebody help me please? Thanks.

Rahul Agarwal
  • 4,034
  • 7
  • 27
  • 51
Logic_Problem_42
  • 229
  • 2
  • 11

1 Answers1

1

Need to know how your original dataset Test looks like. You can convert lil_matrix to csr_matrix or the full numpy.ndarray format like here:

from skmultilearn.adapt import MLkNN
from scipy import sparse
from skmultilearn.dataset import load_dataset
import sklearn.metrics as metrics

X_train, y_train, feature_names, label_names = load_dataset('emotions', 'train')
X_test, y_test, _, _ =load_dataset('emotions', 'test')

clf = MLkNN(k=5)

clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)

print(type(y_pred))  # <class 'scipy.sparse.lil.lil_matrix'>

print(type(y_pred.toarray()))  # <class 'numpy.ndarray'>

y_pred_csr = sparse.csr_matrix(y_pred)

print(type(y_pred_csr))  # <class 'scipy.sparse.csr.csr_matrix'>

accuracy = metrics.accuracy_score(y_test, y_pred)
print(accuracy)  # 0.148
Reveille
  • 4,359
  • 3
  • 23
  • 46