使用MLPClassifier'score'方法时出现“类似于数组的预期数组”错误

时间:2019-04-19 22:01:32

标签: python-3.x scikit-learn

我想使用“ sklearn.neural_network” lib和mnist数据集来训练和测试MLP分类器。当使用“ score:方法并提供有效数据作为数组时,会发生此错误:ValueError:预期的类似数组(数组或非字符串序列),得到了array('B',[7,2,1,0,4, 1,4,9,5,9,0 ...

X和Y训练数据集具有相同的长度。不知道要检查什么才能解决此问题。我正在使用Anaconda / Jupyter Notebook。

代码:

clf = MLPClassifier(hidden_layer_sizes=(100, ), activation='relu', 
              solver='adam', alpha=0.0001, batch_size='auto', 
              learning_rate='constant', learning_rate_init=0.001, 
              power_t=0.5, max_iter=200, shuffle=True, random_state=None, 
              tol=0.0001, verbose=False, warm_start=False, momentum=0.9, 
              nesterovs_momentum=True, early_stopping=False, validation_fraction=0.1, 
              beta_1=0.9, beta_2=0.999, epsilon=1e-08, n_iter_no_change=10)

mndata = MNIST('./samples')
mndata.gz = True

images_tr, labels_tr = mndata.load_training()
images_test, labels_test = mndata.load_testing()
clf.fit(images_tr, labels_tr) 

acc = clf.score(images_test, labels_test)
print(acc)

错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-38-c20e564e6239> in <module>
     18 print(len(images_test))
     19 print(len(labels_test))
---> 20 acc = clf.score(images_test, labels_test)
     21 print(acc)

~\Anaconda3\lib\site-packages\sklearn\base.py in score(self, X, y, sample_weight)
    288         """
    289         from .metrics import accuracy_score
--> 290         return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
    291 
    292 

~\Anaconda3\lib\site-packages\sklearn\metrics\classification.py in accuracy_score(y_true, y_pred, normalize, sample_weight)
    174 
    175     # Compute accuracy for each possible representation
--> 176     y_type, y_true, y_pred = _check_targets(y_true, y_pred)
    177     check_consistent_length(y_true, y_pred, sample_weight)
    178     if y_type.startswith('multilabel'):

~\Anaconda3\lib\site-packages\sklearn\metrics\classification.py in _check_targets(y_true, y_pred)
     70     """
     71     check_consistent_length(y_true, y_pred)
---> 72     type_true = type_of_target(y_true)
     73     type_pred = type_of_target(y_pred)
     74 

~\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py in type_of_target(y)
    241     if not valid:
    242         raise ValueError('Expected array-like (array or non-string sequence), '
--> 243                          'got %r' % y)
    244 
    245     sparseseries = (y.__class__.__name__ == 'SparseSeries')

ValueError: Expected array-like (array or non-string sequence), got array('B', [7, 2, 1, 0, 4, 1, 4, 9, 5, 9, 0 ...

0 个答案:

没有答案