Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are some numpy calls not implemented as methods?

Tags:

python

numpy

I always considered Python as a highly object-oriented programming language. Recently, I've been using numpy a lot and I'm beginning to wonder why a number of things are implemented there as functions only, and not as methods of the numpy.array (or ndarray) object.

If we have a given array a for example, you can do

a = np.array([1, 2, 3])

np.sum(a)
>>> 6
a.sum()
>>> 6

which seems just fine but there are a lot of calls that do not work in the same way as in:

np.amax(a)
>>> 3
a.amax()
>>> AttributeError: 'numpy.ndarray' object has no attribute 'amax'

I find this confusing, unintuitive and I do not see any reason behind it. There might be a good one, though; maybe someone can just enlighten me.

like image 955
inVader Avatar asked Apr 25 '14 09:04

inVader


People also ask

How many NumPy methods are there?

NumPy's operations are divided into three main categories: Fourier Transform and Shape Manipulation, Mathematical and Logical Operations, and Linear Algebra and Random Number Generation.

How do I fix NumPy Ndarray object is not callable?

The Python "TypeError: 'numpy. ndarray' object is not callable" occurs when we try to call an array as if it were a function. To solve the error, make sure to use square brackets when accessing an array element at a specific index, e.g. my_array[0] .

Why is the NumPy library used what are the classes in that library?

NumPy is a Python library used for working with arrays. It also has functions for working in domain of linear algebra, fourier transform, and matrices. NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely.

How do you create a matrix using NumPy in Python?

We can create a matrix in Numpy using functions like array(), ndarray() or matrix(). Matrix function by default creates a specialized 2D array from the given input. The input should be in the form of a string or an array object-like.


1 Answers

When numpy was introduced as a successor to Numeric, a lot of things that were just functions and not methods were added as methods to the ndarray type. At this particular time, having the ability to subclass the array type was a new feature. It was thought that making some of these common functions methods would allow subclasses to do the right thing more easily. For example, having .sum() as a method is useful to allow the masked array type to ignore the masked values; you can write generic code that will work for both plain ndarrays and masked arrays without any branching.

Of course, you can't get rid of the functions. A nice feature of the functions is that they will accept any object that can be coerced into an ndarray, like a list of numbers, which would not have all of the ndarray methods. And the specific list of functions that were added as methods can't be all-inclusive; that's not good OO design, either. There's no need for all of the trig functions to be added as methods, for example.

The current list of methods were mostly chosen early in numpy's development for those that we thought were going to be useful as methods in terms of subclassing or notationally convenient. With more experience under our belt, we have mostly come to the opinion that we added too many methods (seriously, there's no need for .ptp() to be a method) and that subclassing ndarray is typically a bad idea for reasons that I won't go into here.

So take the list of methods as mostly a subset of the list of functions that are available, with the np.amin() and np.amax() as slight renames for the .min() and .max() methods to avoid aliasing the builtins.

like image 158
Robert Kern Avatar answered Oct 25 '22 06:10

Robert Kern