Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Addition of list and NumPy number

Tags:

python

list

numpy

If you add an integer to a list, you get an error raised by the __add__ function of the list (I suppose):

>>> [1,2,3] + 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "int") to list

If you add a list to a NumPy array, I assume that the __add__ function of the NumPy array converts the list to a NumPy array and adds the lists

>>> np.array([3]) + [1,2,3]
array([4, 5, 6])

But what happens in the following?

>>> [1,2,3] + np.array([3])
array([4, 5, 6])

How does the list know how to handle addition with NumPy arrays?

like image 511
Mads M Pedersen Avatar asked Oct 15 '15 14:10

Mads M Pedersen


People also ask

How do I sum two NumPy arrays?

To add the two arrays together, we will use the numpy. add(arr1,arr2) method. In order to use this method, you have to make sure that the two arrays have the same length. If the lengths of the two arrays are​ not the same, then broadcast the size of the shorter array by adding zero's at extra indexes.

How do I sum an array in NumPy?

The numpy. sum() function is available in the NumPy package of Python. This function is used to compute the sum of all elements, the sum of each row, and the sum of each column of a given array. Essentially, this sum ups the elements of an array, takes the elements within a ndarray, and adds them together.

What do you get if you apply NumPy sum () to a list that contains only Boolean values?

sum receives an array of booleans as its argument, it'll sum each element (count True as 1 and False as 0) and return the outcome. for instance np. sum([True, True, False]) will output 2 :) Hope this helps.

How do I append numbers in NumPy?

NumPy: append() function The append() function is used to append values to the end of an given array. Values are appended to a copy of this array. These values are appended to a copy of arr. It must be of the correct shape (the same shape as arr, excluding axis).


2 Answers

list does not know how to handle addition with NumPy arrays. Even in [1,2,3] + np.array([3]), it's NumPy arrays that handle the addition.

As documented in the data model:

  • For objects x and y, first x.__op__(y) is tried. If this is not implemented or returns NotImplemented, y.__rop__(x) is tried. If this is also not implemented or returns NotImplemented, a TypeError exception is raised. But see the following exception:

  • Exception to the previous item: if the left operand is an instance of a built-in type or a new-style class, and the right operand is an instance of a proper subclass of that type or class and overrides the base’s __rop__() method, the right operand’s __rop__() method is tried before the left operand’s __op__() method.

When you do

[1,2,3] + np.array([3])

what is internally called is

np.array([3]).__radd__([1,2,3])
like image 58
Anand S Kumar Avatar answered Oct 12 '22 11:10

Anand S Kumar


It is because of the __radd__ method of np.array, check out this link : http://www.rafekettler.com/magicmethods.html#numeric (paragraph Reflected arithmetic operators).

In facts, when you try [1,2,3].__add__(np.array([3])), it raises an error, so np.array([3]).__radd__([1,2,3]) is called.

like image 30
Labo Avatar answered Oct 12 '22 13:10

Labo