In this blog we will get the crisp and clear details of Numpy and all the basic concepts that you must know.
Python offers its primitive way of working with an array. However its not a memory optimized thing.
So to achieve efficient memory management and optimization, Data Engineers and Data Scientists use NUMPY package. We can also say numpy as Numerical Python.
First we need to import the Numpy package. It is very basic and it is shown below
import numpy as np
Let us see all the basic concepts one by one that must be known about Numpy
1. To create a one Dimensional array using collection object. Consider the below example , here l is the list and we are converting into a numpy array.
l=[1,2,3,4]
x=np.array(l)
2. To identify the dimension, please follow below code.
x.ndim
The output of above code in "1" as x is in single dimension
3. To find the shape of array, we can use below code that is "shape"
x.shape
The output of above code is (4,) That is , one dimension and 4 elements.
4. Usually we go with for loop when we need to add each element in two list should be added based on the positions. But the advantage of Numpy array is , we no need for loop for that purpose, we can directly add it .
x1=[1,2,3,4]
x2=[2,3,4,5]x3=x1+x2
o/p:
x3=[3,5,7,9]
In the above example consider that xq and x2 are the numpy array. Please consider that we have already converted the list to numpy array using np.array() .
5. Similarly if we perform x1*x2 will give each element multiplication. Therefor to perform matrix multiplication or matric operation we can go with matrix,
matrix1=np.matrix(x1)
Therefore now we can perform matrix1*matrix2 to get the matrix multiplication.
6. Numpy uses concept of Broadcasting:
A single element performs operation in each and every element of array.
7. arrange in numpy:
np.arrange(0,10,2)
o/p:
[0,2,4,6,8]
In the above example we can see (0,10,2) the first integer given in bracket is 0, so the starting number is 0 . And the second value given in the bracket is 10 , so the numbers going to generate must be lesser than 10. And the 3rd number given in bracket is 2, therefore there should be a value gab of 2 between the numbers. So we get 0,2,4,6,8
8. Zeros in numpy:
np.zeros(5)
o/p:
[0.,0.,0.,0.,0.,]
Thus zeros(n) creates zeros as many numbers given as the value of n. From example we can see 5 zeros are created as n=5.
9. Ones in numpy:
np.ones(2,2)
o/p:
[[1. ,1. ],
[1. , 1.]]
Ones are similar to zeros concept , In the example we given (2,2) there for a 2X2 matrix is created with ones.
10. To get content datatype, that is datatype of elements in array :
x.dtype
11. To type cast the elements:
variable2=variable1.astype('int64')
12. Random number Generation :
this will generate random numbers between 0 to 1
np.random.rand(5)
o/p:
[0.013 0.391 0.336 0.42 0.57]
13. To generate random numbers between a fixed range, then go for this,
np.random.randint(1,10,4)
o/p :
[1,8,9,2]
14. An additional information, if you need random normal, that is the result contains mean value as 0 and standard deviation as 1
np.random.randn(10)
15. To create identity matrix ,
np.identity(3)
o/p:
[[1. , 0. , 0.],
[0. , 1. , 0.],
[0. , 0. , 1.]]
I hope this blog will give you some important basic points of Numpy. Please follow our blog by clicking follow button on the top right of the screen.
Thank you !!!
0 Comments