Solving Python TypeError: ufunc ‘add’ did not contain a loop with signature matching types

Python TypeError: ufunc ‘add’ did not contain a loop with signature matching types is an error which occurs when you apply operations on a list of a different type.

In today’s blog post I am going to present an annoying and confusing python error and explain why this error is taking place and how to fix it, with a set of possible fixes.

Exploring the Python TypeError: ufunc ‘add’ did not contain a loop with signature matching types

This is an error which occurs when you apply operations on a list of a different type.

Make sure your error looks like this, please double check your error message.

                                                                       #
TypeError: ufunc 'add' did not contain a loop with signature matching types ...
                                                                       #

The Method that fixed my issue : Convert the list to float by using np.asarray() or map() and/or other functions.

This error occurs when you work with a list of strings thinking that it is of another type like float for example.

The error is going to take place if you do something like this for example

                                                                       #
return sum(np.asarray(mydata)) / float(len(mydata))
                                                                       #

There are many options to solve this, for example you can use map() which is a python function that allows you to transform and process items in an iterable without using a for loop.

In this case the solution is to convert the list to float via the map() function.

                                                                       #
map(float,mydata)
                                                                       #

Another option is to convert the list to a numpy array of type float by using the dtype=float argument

                                                                       #
return np.asarray(mydata, dtype=float).mean()
                                                                       #

You can do the conversion by using sum() without even using numpy to convert to dtype float

                                                                       #
return sum(np.asarray(mydata, dtype=float)) / float(len(mydata))
                                                                       #

I think the options I presented have the best chance of fixing your issue.

I hope the fix above fixed your problem, good luck with your python projects.

Summing-up : 

The solutions above should be enough to solve your problem, I hope the article helped you get rid of the issue, please keep learning, keep coding and cheers.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/