0

What does the 'a' in numpy's numpy.arange method stand for, and how does it differ from a simple range produced by Python's builtin range method (definitionally, not in terms of performance and whatnot)?

I tried looking online for an answer to this, but all I find is tutorials for how to use numpy.arange by GeeksForGeeks and co.

nba
  • 3
  • 3
  • Performance is a big part of it. `range` creates relatively large and expensive python `int` as opposed to `arange` building a contiguous array of hardware level integers. – tdelaney Feb 17 '23 at 16:55
  • 1
    But it depends on what you do next. If you want to iterate individual integers, `range` is better. `arange` would create an ndarray but then when you iterate, it would have to build `numpy.intxx` objects for each. – tdelaney Feb 17 '23 at 16:56
  • Think of `arange` as the function name, not as 'a'+'range'; the 'a' is just part of the name. In Python 2, `range` produced a list; in Py3, it produces a generator like `range` object. If not used in an iteration, you need `list(range(n))`. For simple cases like `np.arange(5)`, the result looks much the same, expect it's a numpy array. The official doc is: https://numpy.org/doc/stable/reference/generated/numpy.arange.html – hpaulj Feb 17 '23 at 17:11
  • 1
    The from the suggested duplicate, https://stackoverflow.com/questions/55102806/why-was-the-name-arange-chosen-for-the-numpy-function, it looks like I previously found a quote from the `numpy` developer. In any case, I think it's more important to think of `arange` as a function by itself, rather than as an extension of `range`. – hpaulj Feb 17 '23 at 17:22
  • Does this answer your question? [Why was the name "arange" chosen for the numpy function?](https://stackoverflow.com/questions/55102806/why-was-the-name-arange-chosen-for-the-numpy-function) – mkrieger1 Feb 17 '23 at 19:32

3 Answers3

2

You can inspect the return types and reason about what it could mean that way:

print(type(range(0,5))) 
import numpy as np  
print(type(np.arange(0,5)))

Which prints:

<class 'range'>
<class 'numpy.ndarray'>

Here's a related question: Why was the name "arange" chosen for the numpy function?

  1. Some people do from numpy import * which would shadow range which causes problems.
  2. Naming the function arrayrange was not chosen because it's too long to type.
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
1

From the previous SO we learn that the 'a' stands, in some sense, for 'array'. arange is a function that returns a numpy array that is similar, at least in simple cases, to the list produced by list(range(...)). From the official arange docs:

For integer arguments the function is roughly equivalent to the Python built-in range, but returns an ndarray rather than a range instance.

In [104]: list(range(-3,10,2))
Out[104]: [-3, -1, 1, 3, 5, 7, 9]

In [105]: np.arange(-3,10,2)
Out[105]: array([-3, -1,  1,  3,  5,  7,  9])

In py3, range by itself is "unevaluated", it's generator like. It's the equivalent of the py2 xrange.

The best "definition" is the official documentation page:

https://numpy.org/doc/stable/reference/generated/numpy.arange.html

But maybe you are wondering when to use one or the other. The simple answer is - if you are doing python level iteration, range is usually better. If you need an array, use arange (or np.linspace as suggested by the docs).

In [106]: [x**2 for x in range(5)]
Out[106]: [0, 1, 4, 9, 16]

In [107]: np.arange(5)**2
Out[107]: array([ 0,  1,  4,  9, 16])

I often use arange to create a example array, as in:

In [108]: np.arange(12).reshape(3,4)
Out[108]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

While it is possible to make an array from a range, e.g. np.array(range(5)), that is relatively slow. np.fromiter(range(5),int) is faster, but still not as good as the direct np.arange.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

The 'a' stands for 'array' in numpy.arange. Numpy.arange is a function that produces an array of sequential numbers within a given interval. It differs from Python's builtin range() function in that it can handle floating-point numbers as well as arbitrary step sizes. Also, the output of numpy.arange is an array of elements instead of a range object.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Juan Melnechuk
  • 461
  • 1
  • 8