Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Using NumPy's np.arange() Effectively Show
NumPy is the fundamental Python library for numerical computing. Its most important type is an array type called Creating NumPy arrays is important when you’re working with other Python libraries that rely on them, like SciPy, Pandas, Matplotlib, scikit-learn, and more. NumPy is suitable for creating and working with arrays because it offers useful routines, enables performance boosts, and allows you to write concise code. By the end of this article, you’ll know:
Let’s see Return Value and Parameters of np.arange()NumPy You can define the interval of the values contained in an array, space between them, and their type with four parameters of
The first three parameters determine the range of the values, while the fourth specifies the type of the elements:
If You can find more information on the parameters and the return value of Range Arguments of np.arange()The arguments of NumPy The following examples will show you how Providing All Range ArgumentsWhen working with NumPy routines, you have to import NumPy first: >>>
Now, you have NumPy imported and you’re ready to apply Let’s see a first example of how to use NumPy >>>
In this example, Following this pattern, the next value would be You can pass >>>
This code sample is equivalent to, but more concise than the previous one. The value of >>>
This
code sample returns the array with the same values as the previous two. You can get the same result with any value of However, if you make >>>
In this case, you get the array with four elements that includes Notice that
this example creates an array of floating-point numbers, unlike the previous one. That’s because you haven’t defined You can see the graphical representations of these three examples in the figure below:
As you can see from the figure above, the first two examples have three values ( Providing Two Range ArgumentsYou can omit >>>
The second statement is shorter. Using
>>>
This is an intuitive and concise way to invoke Providing One Range ArgumentYou have to provide at least one argument to But what happens if you omit In other words, Let’s see an example where you want to start an array with >>>
These code samples are okay. They work as shown in the previous examples. There’s an even shorter and cleaner, but still intuitive, way to do the same thing. You can just provide a single positional argument: >>>
This is the most usual way to create a NumPy array that starts at zero and has an increment of one. If you try to explicitly provide >>>
You got
the error because Providing Negative ArgumentsIf you provide negative
values for >>>
This behavior is fully consistent with the previous examples. The counting begins with the value of Counting BackwardsSometimes you’ll want an array with the values decrementing from left to right. In such cases, you can use >>>
In this example, notice the following pattern: the obtained array starts
with the value of the first argument and decrements for In the last statement, You can see the graphical representations of this example in the figure below: Again, This time, the arrows show the direction from right to left. That’s because The previous example produces the same result as the following: >>>
However, the variant with the negative value of Getting Empty ArraysThere are several edge cases where you can obtain empty NumPy arrays with If you provide equal values for >>>
This is because counting ends before the value of One of the unusual cases is when >>>
As you can see, these examples result with empty arrays, not with errors. Data Types of np.arange()The
types of the elements in NumPy arrays are an important aspect of using them. When working with You are free to omit >>>
Here, there is one
argument ( The array in the previous example is equivalent to this one: >>>
The argument NumPy offers you several integer fixed-sized dtypes that differ in memory and limits:
If you want other integer types for the elements of your array, then just specify >>>
Now the resulting array has the same values as in the previous case, but the types and sizes of the elements differ. The argument When your argument is a decimal number instead of integer, the dtype will be some NumPy floating-point type, in this case >>>
The values of the elements are the same in the last four examples, but the dtypes differ. Generally, when you provide at least one floating-point argument to >>>
In the examples above, If you specify >>>
The argument When you need a floating-point dtype with lower precision and size (in bytes), you can explicitly specify that: >>>
Using >>>
The difference between the elements of In many cases, you won’t notice this difference. However, sometimes it’s important. For example, TensorFlow uses When Beyond Simple Ranges With np.arange()You can conveniently combine >>>
This is particularly suitable when you want to create a plot in Matplotlib. If you need a multidimensional array, then you can combine >>>
That’s how you can
obtain the Comparison of range and np.arange()Python has a built-in class The main difference between the two is that In addition, their purposes are different! Generally, Parameters and OutputsBoth
You apply these parameters similarly, even in the cases when
However, when working with
Creating SequencesYou can apply However, creating and manipulating NumPy arrays is often faster and more elegant than working with lists or tuples. Let’s compare the performance of creating a >>>
Repeating this code for varying values of
These results might vary, but clearly you can create a NumPy array much faster than a list, except for sequences of very small lengths. (The application often brings additional performance benefits!) This is because NumPy performs many operations, including looping, on the C-level. In addition, NumPy is optimized for working with vectors and avoids some Python-related overhead. Python for LoopsIf you need values to iterate over in a Python
In contrast, For more information about Other Routines Based on Numerical RangesIn addition to
All these functions have their specifics and use cases. You can choose the appropriate one according to your needs. As you already saw, NumPy contains more routines to create instances of Quick SummaryTo use NumPy >>>
Here’s a table with a few examples that summarize how to use NumPy
Don’t forget that you can also influence the memory used for your arrays by specifying NumPy dtypes with the parameter ConclusionYou now know how to use NumPy
You also learned how NumPy You saw that there are other NumPy array creation routines
based on numerical ranges, such as If you have questions or comments, please put them in the comment section below. Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Using NumPy's np.arange() Effectively Apa itu range pada python?range() adalah sebuah fungsi serbaguna yang gunanya untuk menciptakan sebuah list yang terdiri dari angka. Instruksi: Parameter di range() bisa terdiri dari satu hingga tiga parameter. Dan perilaku dari range() berubah sesuai dengan jumlah parameter yang anda berikan.
Apa yang dimaksud dengan NumPy?Numpy singkatan dari Numerik Python adalah Library Python yang digunakan untuk membuat objek kelas array tunggal dan multidimensi. Objek array di NumPy disebut ndarray, yang berguna menyediakan banyak fungsi pendukung yang membuat bekerja dengan ndarray sangat mudah.
Apa itu append pada python?Append dan insert pada dasarnya memiliki fungsi yang sama yaitu untuk menambahkan nilai pada array, perbedaannya hanya pada kondisi penggunaannya. Fungsi append menambahkan nilai array pada urutan akhir. Sedangkan dengan fungsi insert kita bisa menambahkan nilai array pada posisi tertentu.
Apa itu def di Python?def pada bahasa python adalah suatu cara untuk mendifinisikan sebuah method atau fungsi.
|