pybrain / pybrain

BSD 3-Clause "New" or "Revised" License
2.85k stars 792 forks source link

classificationPerformance() - TypeError: only length-1 arrays can be converted to Python scalars #182

Open ghost opened 8 years ago

ghost commented 8 years ago

I posted this question on stackoverflow but I think that the problem comes from pybrain.tools.validation.Validator.classificationPerformance()

    @classmethod
    def classificationPerformance(cls, output, target):
        """ Returns the hit rate of the outputs compared to the targets.

            :arg output: array of output values
            :arg target: array of target values
        """
        output = array(output)
        target = array(target)
        assert len(output) == len(target)
        n_correct = sum(output == target)
        return float(n_correct) / float(len(output))

Because:

>>> output == target
array([[False, False, False, False, False, False, False, False, False,
        False, False, False]], dtype=bool)
>>> sum(output == target)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

which means that n_correct is array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) which fill throw the exception at float(n_correct) / ..

This could be avoided if np.sum() would be used instead the build in function sum()

And just as a side node: The values are real values. How can one tell that output should be e.g. x = [int(v > 0.5) for v in x] to have a binary output that gets compared to the binary encoded target?

colceagus commented 8 years ago

Is this going to be fixed in the near future?