如何使用scikit MLPClassifer和.score()方法计算准确性?

时间:2019-03-02 21:20:06

标签: python scikit-learn

我试图从Scikit-learn中查找使用MLPClassifier的输出,但无法弄清楚如何安排评分方法以产生准确性。我的输入如下:

test_data_img = (10000, 784) 
test_data_label(10000 ,)
y_pred = (10000, 10) 

运行我的预测时,它会显示一个矩阵,该矩阵在第7列下1秒似乎是恒定的。

根据我研究的一种方法,是使用np.argmax(axis=1),该方法适用于该集合,但是我得到了一个完美的分数,这对于神经网络进行数字分类似乎是不可能的。我已经尝试过直接实现数据,尝试更改score()的输入,但是似乎没有任何东西可以正确产生一个值。谁能解释为什么会这样或需要进行哪些编辑?谢谢!

(由于在每行中产生相同的结果,这种预测方法是否会发生此错误?)

from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
mlp= MLPClassifier(hidden_layer_sizes=(100,),activation='logistic',solver='sgd',batch_size=10,max_iter=30,learning_rate_init=3,learning_rate='constant')
mlp.fit(training_data_img, training_data_label)


y_pred=mlp.predict(test_data_img)
y_pred

output: array([[0, 0, 0, ..., 1, 0, 0],
           [0, 0, 0, ..., 1, 0, 0],
           [0, 0, 0, ..., 1, 0, 0],
           ...,
           [0, 0, 0, ..., 1, 0, 0],
           [0, 0, 0, ..., 1, 0, 0],
           [0, 0, 0, ..., 1, 0, 0]])

s=mlp.score(test_data_img,test_data_label)
    [355  97 406 ... 711 464  75]
   ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-147-a50acc512c2a> in <module>
          1 print(test_data_img.argmax(axis=1))
          2 test_data_img.argmax(axis=1)
    ----> 3 s=mlp.score(test_data_img,test_data_label)

    c:\users\james\appdata\local\programs\python\python36\lib\site-packages\sklearn\base.py in score(self, X, y, sample_weight)
        286         """
        287         from .metrics import accuracy_score
    --> 288         return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
        289 
        290 

    c:\users\james\appdata\local\programs\python\python36\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'):

    c:\users\james\appdata\local\programs\python\python36\lib\site-packages\sklearn\metrics\classification.py in _check_targets(y_true, y_pred)
         79     if len(y_type) > 1:
         80         raise ValueError("Classification metrics can't handle a mix of {0} "
    ---> 81                          "and {1} targets".format(type_true, type_pred))
         82 
         83     # We can't have more than one value on y_type => The set is no more needed

    ValueError: Classification metrics can't handle a mix of multiclass and multilabel-indicator targets

这与score(test_data_img, y_pred)

一起运行
print(test_data_img)
s=mlp.score(test_data_img,y_pred)
s

output: [[0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 ...
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]]
1.0

0 个答案:

没有答案