Numpy array in Python : In this tutorial we will study at about Numpy array in Ppython programming language. The Numpy array is the core library function for computing the python language. It will provide the multidimensional array and tools with these arrays. The Numpy array is familiar with MATLAB. Numpy arrays are the grid of values of same type and indexed by tuple of non negative integers. The dimensions of the array are same as the rank of array and have non-negative integers. While creating the Numpy array we use the broadcasting and access or manipulate the arrays. The Numpy is the multidimensional array which as the table of elements and all types of same type and with positive integers. The Numpy array is called as the Ndarray and also alis array. They will help to manipulate the arrays and numeric data use the modules.
Working on windows makes sure that you have added python to PATH and don not forget to install the package such as pip used for open source libraries.
The numpy array is created by three different ways as:-
Sparse:-Used to implement API for sparse arrays that are zeros.
Cupy:-It is used to implement API on the GPU with CUDA.
Dask arrays:-Used to implement API for the multicore workstations.
Parameters |
Shape:- |
Return |
The array of data is given in dtype shape and order Then array objects will be initialized. |
a. One dimensional array:-
Example:-
import numpy as np
array=np.arange (10)
array
The one dimensional array is an array having “arange” that is widely used to create an array.
After passing the value 10 to arrange function the array will create the an array with values from 0 to 9
Output:-
array([0,1,2,3,4,5,6,7,8,9])
b. One dimensional array:-
The output of the one dimensional array is chained with the reshape array function.
array=np.arange (10).reshape (2, 5)
Output:-
array([0,1,2,3,4],[5,6,7,8,9])
Without using the Numpy function you can create the array from python list.
Pass the python list to the array function and create the Numpy array.
Example:-
array=np.array ([1, 2, 3])
array
Output:-
array([1, 2, 3])
Create the list and pass variable name to create the Numpy array.
list= [1, 2, 3]
list
Output:-
[1, 2, 3]
The function is to create and filled with the random values between 0 and 1.
Example:-
np.random.random ((2, 2))
Output:-
array([[0.1, 0.3], [0.3, 0.7]])
The Numpy function will take the 4 parameters as follows:-
Stop:-The number will represent the end of interval.
Start:-The number will represent start of the interval and by default it is zero.
Step:-The number will represent interval between each value in the range and by default value is one.
Dtype:-It is type of output array.
Operators |
Description |
np.array([1,2,4]) |
1d array |
np.array([(1,2,4)]) |
2d array |
np.arrange(start,stop,step) |
Range array |
Array:-
Syntax |
Description |
array. shape |
It has dimensions with the rows and the columns. |
len(array) |
It indicates the length of array. |
array.ndim |
It is number of array dimensions. |
array.dtype |
It has the data type |
array.astype(type) |
Coverts into the data type |
Operators |
Description |
np.linespace(0,1,2,4) |
Add the values between intervals to the array of length. |
np.zeros((1,2)) |
Creating and the array is filled with zeros. |
np.ones((1,2)) |
It will create an array and filled with ones. |
np.random.random((5,5)) |
It will create the random arrays. |
np.empty(2,2) |
It creates an empty array. |
Array mathematics:-
Use the numpy random method to generate the multidimensional array with float between zeros and one.
Example:-
random_arr=np.random ((4, 4))
print(random_arr)
Output:-
[0.7 0.5 0.9 0.02]
[0.8 0.30 0.41 0.64]
[0.3 0.03 0.96 0.84]
[0.1 0.7 0.3 0.3]
Advantages:-
Applications:-
It is used in various applications as follows:-
Used in the image processing.