ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32')
    python/errors and solved 2017. 11. 26. 15:10

    "TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32')"


    If you create an array of numpy and then do an operation.


    >>> a = numpy.array(datingLabels)

    >>> a

    array(['3', '2', '1', '1', '1', '1', '3', '3', '1', '3', '1', '1', '2',

           '1', '1', '1', '1', '1', '2', '3', '2', '1', '2', '3', '2', '3',

           '2', '3', '2', '1', '3', '1', '3', '1', '2', '1', '1', '2', '3',

           '3', '1', '2', '3', '3', '3', '1', '1', '1', '1', '2', '2', '1',

           ...

           '3', '1', '3', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',

           '2', '2', '3', '2', '2', '2', '2', '2', '1', '3', '3', '3'], 

          dtype='|S1')

    >>> 15.0*a


    the following error may appear.


    Traceback (most recent call last):

       File "<stdin>", line 1, in <module>

    TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')


    or


    Traceback (most recent call last):

      File "<stdin>", line 1, in <module>

    TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')


    In this case, dtype is a problem that does not match.

    The data types of numpy are listed below and in the link.

    https://docs.scipy.org/doc/numpy-1.9.1/user/basics.types.html


    To solve the above problem, change the dtype or insert it at the time of creation.


    >>> a = numpy.array(datingLabels, dtype=numpy.int32)

    >>> a

    array([3, 2, 1, 1, 1, 1, 3, 3, 1, 3, 1, 1, 2, 1, 1, 1, 1, 1, 2, 3, 2, 1, 2,

           3, 2, 3, 2, 3, 2, 1, 3, 1, 3, 1, 2, 1, 1, 2, 3, 3, 1, 2, 3, 3, 3, 1,

           1, 1, 1, 2, 2, 1, 3, 2, 2, 2, 2, 3, 1, 2, 1, 2, 2, 2, 2, 2, 3, 2, 3,

           ...

           2, 3, 2, 2, 2, 2, 2, 1, 3, 3, 3], dtype=int32)

    >>> a*15.0

    array([ 45.,  30.,  15.,  15.,  15.,  15.,  45., 45.,  15.,  45.,  15.,

           ...

          45.,  30.,  30.,  30.,  30.,  30.,  15.,  45.,  45.,  45.])


    so, It can be confirmed that the operation is normally performed.

    댓글

Designed by Tistory.