Python Programming - Array From Numerical Ranges

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

Python Programming – Array From Numerical Ranges

np.arange

arange( ) returns an ndarray object containing evenly spaced values within a given range. The for¬mat of the function is as follows: numpy.arange(start, stop, step, dtype)
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 numerical range is generated from zero to stop value with step 1. The dtype specifies the datatype for the Numpy array.

np.linspace

linspace( ) is used to generate evenly spaced elements between two given limits. The format of the function is as follows:
numpy.linspace(start, stop, num, end-point, retstep, dtype)
The start and stop attributes provide start and end values of the sequence, num, provides the number of evenly spaced samples to be generated. By default, it is 50. The endpoint is True by default, hence the stop value is included in the sequence. If false, it is not included, retstep, if true, return samples and step between the consecutive numbers, dtype is a data type of output ndarray. (See Figure 13.17 and 13.18).

Example
Demo of linespace ( ) function.

import numpy as np
x = np.linspace(10,20,5)
print(x)RUN
>>>
[ 10 . 12.5 15. 17.5 20. ]
>>>

Example
Demo of linsPace ( ) function.

# endpoint set to false
import numpy as np
x = np . linespace ( 10 , 20 , 5 , endpoint = false )
print ( x )
RUN
>>>
[ 10 , 12 , 14 , 16 , 18 ]
>>>

Let’s Try

import NumPy as np
arr1 = np.linspace(2,10,3)
print(arr1)

import numpy as np
arr2 = np.linspace(1,2,4)
print(arr2)

numpy.logspace

logspace ( ) function creates an array by using the numbers that are evenly separated on a log scale. Start and stop endpoints of the scale are indices of the base, usually 10. The format of the function is as follows:
numpy.logspace(start, stop, num, end-point, base, dtype)
The start and stop define the starting and final value of sequence in the base, num defines the number of values between the range, endpoint, is a boolean type value. If true, stop is the last value in the range, base represents the base of log space. Its default is 10. dtype is the data type of the array items.

Example
Demo of logspace ( ) function.

import numpy as np
arr = np.logspace(10, 20, num = 5, endpoint = True)
print(“The array over the given range is “,arr)RUN
>>>
The array over the given range is [1.00000000e+10 3.16227766e+12 1.00000000e+15 3.16227766e+17
1.00000000e+2 0]
>>>

Leave a Reply

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