Python Programming - Array Creation Routines

You can learn about NumPy in Python Programs with Outputs helped you to understand the language better.

Python Programming – Array Creation Routines

NumPy offers a lot of array creation routines for different circumstances. arrange ( ) is one such function based on numerical ranges. It’s often referred to as np. arrange ( ) because np is a widely used abbreviation for NumPy.
This function takes the start index of the array, the end index, and the step size (which is optional) and returns an array object containing evenly spaced values within a given range. The format of the function is as follows:
numpy.arange(start, stop, step, dtype)
The first three parameters determine the range of the values, while the fourth specifies the type of the elements:

  1. the start is the number (integer or decimal) that defines the first value in the array.
  2. the stop is the number that defines the end of the array and is not included in the array.
  3. the step is the number that defines the spacing (difference) between every two consecutive values in the array and defaults to 1.
  4. The dtype specifies the datatype for the Numpy array.

For example,
>>> np.arange ( start=1 , stop = 10 , step = 3 )
array([1, 4, 7])
In the above example, the start is 1; so, the first element of the array is 1. step is 3, i.e., your second value is 1+3, that is 4, while the third value in the array is 4+3, which equals 7.
You can pass start, stop, and step as positional arguments as well:
>>> np.arange(1, 10, 3)
array([1, 4, 7])
You can omit step. In this case, arange() uses its default value of 1. The following two statements are equivalent:
>>> np.arange(start=l, stop=10, step=l)
array ([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.arange(start=l, stop=10)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
The start, stop and step attribute provide the values for a numerical range, start and step values are optional. When only stop value is given, the numer-ical range is generated from zero to stop value with step 1. dtype is the type of the elements of the output array. (See Figure 13.14).

>>> np . arange ( 7 , dtype= float )
array ( [ 0 . , 1 . , 2 . , 3 . , 4 . , 5 . , 6 . ] )
>>> arr1 = np . arange ( 10 , 20 , 2, np.float32 )
>>> arr1
array ( [ 10 . , 12 . , 14 . , 16 . , 18 . ] , dtype = float32 )
>>>I

Remember that the arange method returns an array that starts with the starting index and ends at one index less than the end index.

Let’s Try

>>> np . arange ( 1 , 10 )
>>> np . arange ( start = 0 , stop = 10 , step = 1 )
>>> np . arange ( 0 , 10 , 1 )
>>> np . range ( 10 )

Providing Negative Arguments

If you provide negative values for start or both start and stop , and have a positive step , then arange ( ) will work the same way as with all positive arguments:
>>> np . arange ( -5 , -1 )
array ([ -5 , -4 , -3 , -2 ])
>>> np . arange ( -8 , -2 , 2 )
array ([-8, -6 , -4])
>>> np . arange (-5 , 6, 4 )
array ( [-5 , -1 , 3 ] )

Counting Backwards

Sometimes you want an array with the values decrementing from left to right. In such cases, you can use arange ( ) with a negative value for step ,and with a start greater than stop:
>>> np.arange(5, 1, -1)
array([5, 4, 3, 2])
>>> iip. arange (7, 0, -3)
array([7, 4, 1])

Leave a Reply

Your email address will not be published. Required fields are marked *