NumPy arange(): How to Use np.arange() (Summary)
You now know how to use NumPy arange()
. The function np.arange()
is one of the fundamental NumPy routines often used to create instances of NumPy ndarray
. It has four arguments:
start
: the first value of the arraystop
: where the array endsstep
: the increment or decrementdtype
: the type of the elements of the array
You also learned how NumPy arange()
compares with the Python built-in class range
when you’re creating sequences and generating values to iterate over.
You saw that there are other NumPy array creation routines based on numerical ranges, such as linspace()
, logspace()
, meshgrid()
, and so on.
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00
This is just going to be a quick conclusion video to this course on NumPy’s arange()
function. If you remember from the very first video in this course, the arange()
function is a useful way to get evenly spaced ranges of numeric values.
00:15
That means integer or floating-point values returned in a numpy.ndarray
, and you provide the start
, stop
, and step
parameters, and the arange()
function then generates this range of evenly spaced values for you, with an optional dtype
(data type) argument. And those data types, as I talked about, can be essentially various sizes of integer and floating-point numbers, which might allow you faster or more efficient performance in certain applications like machine learning or image processing, where images or sets of data that you’re working with are very large and it might be more efficient to encode them in a smaller data size.
00:56
I talked about the comparison with the inbuilt range
object.
01:01
The arange()
is better than Python’s inbuilt range
for work that requires a lot of manipulation of the resulting sequences. So, comprehensions and performing NumPy operations on a NumPy array will normally be faster than with a range
object, and you get the advantage of working with floating-point numbers, which the inbuilt range
does not allow you to do.
01:22
But the inbuilt range
can be better for looping, especially if early exiting is possible, because a range
generates values lazily, rather than the arange()
function, which generates values all at once into an array.
01:36
I also covered some other NumPy functions like linspace()
or geomspace()
or meshgrid()
, which have similar syntax and output and can be used for really similar purposes, just with slight idiosyncrasies or different philosophies for how to actually generate those array numbers. I hope you found this useful, and if you use NumPy in other work, I strongly suggest that you use arange()
in your work because it shares that kind of characteristic NumPy efficiency and philosophy, which really can help speed up your data science applications and anything that requires heavy number processing.
02:12 So. I hope you enjoyed this course and come back for the next one. Thank you very much.
Chevabecks on Jan. 13, 2022
Perfect, thanks :)
Become a Member to join the conversation.
Glenn Lehman on Nov. 26, 2021
Excellent supplement to the written course! Thank You!