Signup/Sign In

Python NumPy Array Creation from Numerical Ranges

In this tutorial, we will learn how the NumPy arrays can be created using some given specified numerical ranges.

The Numpy library provides some functions to create an array from the given specified range. These functions are as follows:

  1. numpy.arange

  2. numpy.linspace

  3. numpy.logspace

Now we will discuss the above given functions one by one.

1. Using numpy.arange

This function is used to create an array by using the evenly spaced values over any given interval.

The syntax to use this function is as follows:

numpy.arrange(start, stop, step, dtype)  

Parameters:

The parameters of the above-mentioned function are as follows:

  • start:
    This parameter mainly indicates the starting of an interval. The default value of this parameter is 0.

  • stop:
    This parameter is used to represents the value at which the interval ends excluding this value.

  • step:
    This parameter represents the number by which the interval values change.

  • dtype:
    This parameter indicates the data type of the numpy array items.

Example 1:

import numpy as np 

x = np.arange(15) 
print (x)


[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]

Example 2:

Below we have a code snippet using this function where we will create an array with virtual numbers.

import numpy as np  

arr = np.arange(0,10,2,complex)  
print(arr)  


[0.+0.j 2.+0.j 4.+0.j 6.+0.j 8.+0.j]

2. Using numpy.linspace

This function is similar to the arange() function. But there is a major difference and it is in this function, instead of step size, the number of evenly spaced values between the interval is specified using the num argument.

The syntax to use this function is as follows:

numpy.linspace(start, stop, num, endpoint, retstep, dtype)   

Parameters:

The parameters of the above-mentioned function are as follows:

  • start:
    This parameter is used to represents the starting value of the interval.

  • stop:
    This parameter is used to represents the stopping value of the interval.

  • num:
    This parameter indicates the amount of evenly spaced samples over the interval to be generated. The default value of this parameter is 50.

  • endpoint:
    This parameter's true value is used to indicate that the stopping value is included in the interval.

  • retstep:
    The value of this parameter is a boolean value and it is used to represent the steps and samples between the consecutive numbers.

  • dtype:
    This parameter is used to represent the data type of the array items.

Example 1:

Below we have an example using this function, in which we will get an array with numbers between 20 and 30, and we will have evenly spaced 5 numbers from this range, hence, 20, 22.5, 25, 27.5 and 30 are picked.

import numpy as np  

# start=20, end=30, num=5
arr = np.linspace(20, 30, 5)  
print("The array in this range is ",arr)  


The array in this range is [20. 22.5 25. 27.5 30. ]

Example 2:

Below we have an example where we will find out the value of retstep parameter:

import numpy as np 

x = np.linspace(2,4,5, retstep = True) 
print (x)



(array([2. , 2.5, 3. , 3.5, 4. ]), 0.5)

In the above output, the retstep value is 0.5

3. Using numpy.logspace

This function is used to create an array by using the numbers that are evenly separated on a log scale.

The syntax to use this function is as follows:

numpy.logspace(start, stop, num, endpoint, base, dtype)  

Parameters:

The parameters of the above-mentioned function are as follows:

  • start:
    This parameter is used to represent the starting value of the interval in the base.

  • stop:
    This parameter is used to represent the stopping value of the interval in the base.

  • num:
    This parameter is used to indicate the number of values between the range.

  • endpoint:
    This parameter's value is in boolean. and It is used to make the value represented by stop as the last value of the interval.

  • base:
    This parameter is used to represent the base of the log space.

  • dtype:
    This parameter is used to represent the data type of the array items.

Example 1:

Below we have an example using this function:

import numpy as np  

a = np.logspace(20, 30, num = 5, endpoint = True)  
print("The array will be",a)  


The array will be [1.00000000e+20 3.16227766e+22 1.00000000e+25 3.16227766e+27
1.00000000e+30]

Example 2:

import numpy as np  
arr = np.logspace(5, 30, num = 5,base = 3, endpoint = True)  
print("The array is: ",arr)  


The array is: [2.43000000e+02 2.33138563e+05 2.23677324e+08 2.14600041e+11
2.05891132e+14]

Summary

This tutorial is all about how the NumPy arrays can be created using some given specified numerical ranges. For this purpose we have three functions and we have covered them with their syntax, parameters, and their respective examples.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.