我正在使用MlKnn方法,并且能够拟合分类器并通过命令function applyImageClass(image) {
var h = (image) ? image.height() : 0;
if (h > 0) {
var ratio = image.width() / h;
if (ratio === 1) {
image.addClass('square');
} else if (ratio > 1) {
image.addClass('landscape');
} else if (ratio < 1) {
image.addClass('portrait');
}
}
}
$(window).on('load', function() {
// Find all images contained in any element that has any of the tow classes
$('.imagesection, .detailsection').find('img').each(function() {
// Call the function for each one of the matched elements
applyImageClass($(this));
});
});
进行预测。
结果是classifier.predict(Test)
,其中仅包含预测本身。
我不知道如何将这些预测分配给scipy.sparse.lil.lil_matrix
格式的原始数据集Test。
有人可以帮我吗? 谢谢。
答案 0 :(得分:0)
需要知道原始数据集Test的外观。您可以将lil_matrix转换为csr_matrix或完整的numpy.ndarray格式,如下所示:
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