Table of Contents
What is NumPy?
NumPy in Python provides a high-performance multidimensional array and basic tools to compute with and manipulate these arrays. NumPy is the library in Python. Provide a tool for integrating C, C++, etc. It is also used to generate random numbers from range and linear algebra.
NumPy Array: Numpy array is an N-dimensional array object which is in the form of rows and columns. We can initialize NumPy arrays from the python list and tuple and access its elements using slicing and perform some operation using the NumPy function.
How to install NumPy?
NumPy is install using pip command.
Step to install NumPy
1. Go to your command prompt
2. Type “pip install numpy”
Import NumPy library
Once the installation is completed go to your IDE ( For example Anaconda Navigator or PyChamp ) launch Jupyter notebook and then import Numpy by typing
“ import numpy ”
If you want to alias NumPy as any short-form then type
“import numpy as np”
Create single dimension array
Pass list into it to create a single dimension array and pass integer value into it.
Example:-
Example:-
# Import NumPy
import numpy as np # Alias numpy as np
# Create Variable and create array
Array = np.array([1,2,3,4,5])
print(" Output of array :- ",Array)
print( " Check the data type of variable :- ",type(Array))
print( "Array stored value data type is :- ",Array.dtype)
print( "Shape of array :- ", Array.shape)
print( "Size of array :-" ,Array.size)
print( "Dimension of array :-", Array.ndim)
Output:-
Output of array :- [1 2 3 4 5]
Check the data type of variable :- <class 'numpy.ndarray'>
Array stored value data type is :- int32
Shape of array :- (5,)
Size of array :- 5
Dimension of array:- 1
Here we used np.array function to create an array, type function to check the data type of variable so it shows the ndarray, dtype function for check the data type of value stored in an array it shows int32, to check the shape, size and dimension of the array we use shape, size and ndim function.
Example 2:- Create a 1-D array store string into a list.
# Import NumPy
import numpy as np # Alias numpy as np
# Create Variable and create array
Array = np.array(['A','B','C'])
print("Output of array :- ",Array)
print( "Check the data type of variable :- ",type(Array))
print( "Array stored value data type is :- ",Array.dtype) # It shows output as unicode # because python stored string as unicode
print( "Shape of array :- ", Array.shape)
print( "Size of array :-" ,Array.size)
print( "Dimension of array :-", Array.ndim)
Output:-
Output of array :- ['A' 'B' 'C']
Check the data type of variable :- <class 'numpy.ndarray'>
Array stored value data type is :- <U1
Shape of array :- (3,)
Size of array :- 3
Dimension of array :- 1
Create multi-dimension array
Pass list into list o create a multi-dimension array and pass integer value into it.
Example 1:- Create a multi-dimension array of integer number.
# Import NumPy
import numpy as np # Alias numpy as np
# Create Variable and use array function to create array
Array = np.array([[1,2,3,4],[5,6,7,8]]) # Here we pass list into it to create 2-D array
print("Output of array :- \n",Array) # \n for next line
print( "Check the data type of variable :- ",type(Array))
print( "Array stored value data type is :- ",Array.dtype)
print( "Shape of array :- ", Array.shape) # It shows output in rows X columns format
print( "Size of array :-" ,Array.size) # It multiply the output of rows X columns (i.e. 2 X 4)
print( "Dimension of array :-", Array.ndim) # Used to show diamention of Array
Output:-
Output of array :-
[[1 2 3 4]
[5 6 7 8]]
Check the data type of variable :- <class 'numpy.ndarray'>
Array stored value data type is :- int32
Shape of array :- (2, 4)
Size of array :- 8
Dimension of array :- 2
Example 2:
# Import NumPy
import numpy as np # Alias numpy as np
# Create Variable and use array function to create array
Arr = np.array([[5,6,7],[8,9,10],[11,12,13]]) # Here we pass list into it to create 2-D array
print("Output of array :- \n",Arr) # \n for next line
print( "Check the data type of variable :- ",type(Arr))
print( "Array stored value data type is :- ",Arr.dtype)
print( "Shape of array :- ", Arr.shape) # It shows output in rows X columns format
print( "Size of array :-" ,Arr.size) # It multiply the output of rows X columns (i.e. 3 X 3)
print( "Dimension of array :-", Arr.ndim) # Used to show diamention of Array
Output:-
Output of array :-
[[ 5 6 7]
[ 8 9 10]
[11 12 13]]
Check the data type of variable :- <class 'numpy.ndarray'>
Array stored value data type is :- int32
Shape of array :- (3, 3)
Size of array :- 9
Dimension of array :- 2
Example 3:- Create 3D Array
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
# Here we pass list into multiple lists it to create 3-D array
print("Output of array :- \n",arr) # \n for next line
print( "Check the data type of variable :- ",type(arr))
print( "Array stored value data type is :- ",arr.dtype)
print( "Shape of array :- ", arr.shape) # It shows output inno of slice, nrows, ncolumns format
print( "Size of array :-" ,arr.size) # It multiply the output of rows X columns (2 X 2 X 3)
print( "Dimension of array :-", arr.ndim) # Used to show diamention of Array
Output:-
Output of array :-
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
Check the data type of variable :- <class 'numpy.ndarray'>
Array stored value data type is :- int32
Shape of array:- (2, 2, 3)
Size of array:- 12
Dimension of array:- 3
Conclusion:
In this blog, you will get knowledge about the python NumPy library how to install the library, create a single dimension, and multi-dimensional array using NumPy and how to identify them.