Fetch Elements of a certain range from NumPy array in Python
In this tutorial, we will learn how to fetch elements of a certain range from NumPy array in Python with some basic and easy examples. In many situations, you may have to fetch elements over a certain range and NumPy helps us do that very easily.
It provides us 3 methods to fetch elements in Python:
- The array( ) method
- The arange( ) method
- The linspace( ) method
The array( ) method
array( ) method is included in NumPy library which enables us to convert a list into a NumPy array.
The array( ) method takes a list as an object in its argument and converts it to an array.
A simple example to convert a list to an array is shown below.
#importing NumPy as np import numpy as np #declaring a list ls = [1,2,3,4] #converting the list into array arr = np.array(ls)
Output: array([1, 2, 3, 4])
We can also specify an additional argument such as datatype(referred as dtype) in the array( ) method. The default value of dtype is None. As a result, the output array will be the same as the input list.
Some examples to show the use of dtype argument are as shown.
#importing NumPy as np import numpy as np #create a list ls = [1,2,3,4,5,6] #use of array() arr = np.array(ls,dtype=float)
Output: array([1., 2., 3., 4., 5., 6.])
#importing NumPy as np import numpy as np #create a list(floating points) ls = [1.04,3.14,3.14,5.56,8.07] #use of array() arr = np.array(ls,dtype=int)
Output: array([1,3,3,5,8])
We can also pass a list without defining it separately as an object argument.
#importing NumPy as np import numpy as np #using array() arr = np.array([1,2,3,4,5,6,7,8])
Output: array([1,2,3,4,5,6,7,8])
The arange() method
This method is also included in the NumPy library and is one of the most important method available. It takes in 4 parameters as its argument.
Syntax: np.array(start,stop,step,dtype)
The start parameter is always inclusive and the stop parameter is always exclusive. Defining the datatype is optional and left to the user. Deafult step value 1.
example:
#importing NumPy as np import numpy as np #use of arange() arr = np.arange(0,10)
Output: array([0,1,2,3,4,5,6,7,8,9])
For more details and examples on arange( ) method, Click here
The linspace( ) method
linspace( ) is another important method of NumPy used to fetch elements in Python. It is used to generate samples between the start value and the stop value with spacing between them by a number “num”. The default value of num is 50 and it must be non-negative. We can also pass the dtype as an argument which is by default set as None. The dtype should not be set as an integer as loss of data occurs.
Syntax: np.linspace(start,stop,num,dtype)
Unlike arange() method, the stop value and the start values are inclusive. Some examples to show the use of linspace( ) are shown.
#importing NumPy as np import numpy as np #use of linspace() arr = np.linspace(0,10,10)
Output: array([ 0. , 1.11111111, 2.22222222, 3.33333333, 4.44444444, 5.55555556, 6.66666667, 7.77777778, 8.88888889, 10. ])
#importing NumPy as np import numpy as np #use of linspace() #default value of num=50 arr = np.linspace(0,10)
Output: array([ 0. , 0.20408163, 0.40816327, 0.6122449 , 0.81632653, 1.02040816, 1.2244898 , 1.42857143, 1.63265306, 1.83673469, 2.04081633, 2.24489796, 2.44897959, 2.65306122, 2.85714286, 3.06122449, 3.26530612, 3.46938776, 3.67346939, 3.87755102, 4.08163265, 4.28571429, 4.48979592, 4.69387755, 4.89795918, 5.10204082, 5.30612245, 5.51020408, 5.71428571, 5.91836735, 6.12244898, 6.32653061, 6.53061224, 6.73469388, 6.93877551, 7.14285714, 7.34693878, 7.55102041, 7.75510204, 7.95918367, 8.16326531, 8.36734694, 8.57142857, 8.7755102 , 8.97959184, 9.18367347, 9.3877551 , 9.59183673, 9.79591837, 10. ])
Leave a Reply