Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminology: Python and Numpy - `iterable` versus `array_like`

Tags:

What is the difference between an iterable and an array_like object in Python programs which use Numpy?

Both iterable and array_like are often seen in Python documentation and they share some similar properties.

I understand that in this context an array_like object should support Numpy type operations like broadcasting, however Numpy arrays area also iterable. Is it correct to say that array_like is an extension (or super-set?) of iterable?

like image 994
dtlussier Avatar asked Nov 21 '11 18:11

dtlussier


People also ask

What does Array_like mean in Python?

Any sequence that can be interpreted as an ndarray. This includes nested lists, tuples, scalars and existing arrays. so even scalars can be taken into account, just like np. array(1024) .

What is the difference between Python and NumPy?

NumPy arrays have a fixed size at creation, unlike Python lists (which can grow dynamically). Changing the size of an ndarray will create a new array and delete the original. The elements in a NumPy array are all required to be of the same data type, and thus will be the same size in memory.

Is NumPy array an iterable?

They are not iterable, but they are accepted. You can also pass int s to numpy. array() , so they are array-like.

What is another name for NumPy arrays?

array(): The Numpy array object in Numpy is called ndarray.


1 Answers

The term "array-like" is indeed only used in NumPy and refers to anything that can be passed as first parameter to numpy.array() to create an array.

The term "iterable" is standard python terminology and refers to anything that can be iterated over (for example using for x in iterable).

Most array-like objects are iterable, with the exception of scalar types.

Many iterables are not array-like -- for example you can't construct a NumPy array from a generator expression using numpy.array(). (You would have to use numpy.fromiter() instead. Nonetheless, a generator expression isn't an "array-like" in the terminology of the NumPy documentation.)

like image 198
Sven Marnach Avatar answered Oct 24 '22 10:10

Sven Marnach