Numpy - Tutorial - Ipynb - Colaboratory
Numpy - Tutorial - Ipynb - Colaboratory
ipynb - Colaboratory
1
import numpy as np
2
a=np.array([1,2,3])
3
print(a)
[1 2 3]
Multi-dimensional Array:
1
a=np.array([(1,2,3),(4,5,6)])
2
print(a)
[[1 2 3]
[4 5 6]]
Create a range
1
np.arange(4)
array([0, 1, 2, 3])
Create Zeros
1
np.zeros(4)
1
np.zeros((2,3))
You can find the dimension of the array, whether it is a two-dimensional array or a single
dimensional array.
1
import numpy as np
2
a = np.array([(1,2,3),(4,5,6)])
3
print(a.ndim)
https://colab.research.google.com/drive/1OX0_25_yerAgFQZd_vxeqRTfHgx9xTbp#scrollTo=Dc6HUTIVJzo0&printMode=true 1/9
9/7/22, 11:32 AM Numpy_Tutorial.ipynb - Colaboratory
Create Ones
1
np.ones((2,2))
array([[1., 1.],
[1., 1.]])
1
import numpy as np
2
a = np.array([(1,2,3)])
3
print(a.itemsize) #So every element occupies 8 byte in the above numpy array.
You can find the data type of the elements that are stored in an array.
1
import numpy as np
2
a = np.array([(1,2,3)])
3
print(a.dtype)
int64
You can find the size and shape of the array using ‘size’ and ‘shape’ function respectively.
1
import numpy as np
2
a = np.array([(1,2,3,4,5,6)])
3
print(a.size)
4
print(a.shape)
(1, 6)
Reshape is when you change the number of rows and columns which gives a new view to an
object.
https://colab.research.google.com/drive/1OX0_25_yerAgFQZd_vxeqRTfHgx9xTbp#scrollTo=Dc6HUTIVJzo0&printMode=true 2/9
9/7/22, 11:32 AM Numpy_Tutorial.ipynb - Colaboratory
1
import numpy as np
2
a = np.array([(8,9,10),(11,12,13)])
3
print(a)
4
a=a.reshape(3,2)
5
print(a)
[[ 8 9 10]
[11 12 13]]
[[ 8 9]
[10 11]
[12 13]]
1
import numpy as np
2
a=np.array([(1,2,3,4),(3,4,5,6)])
3
print(a[0,2])
6
"""Here, the array(1,2,3,4) is your index 0 and (3,4,5,6) is index 1 of the
7
python numpy array. Therefore, we have printed the second element from the
8
zeroth index."""
let’s say we need the 2nd element from the zeroth and first index of the array. Let’s see how you
can perform this operation:
1
import numpy as np
2
a=np.array([(1,2,3,4),(3,4,5,6)])
3
print(a[0:,2])
5
"""Here colon represents all the rows, including zero.
6
Now to get the 2nd element, we’ll call index 2 from both of the
7
rows which gives us the value 3 and 5 respectively."""
https://colab.research.google.com/drive/1OX0_25_yerAgFQZd_vxeqRTfHgx9xTbp#scrollTo=Dc6HUTIVJzo0&printMode=true 3/9
9/7/22, 11:32 AM Numpy_Tutorial.ipynb - Colaboratory
[3 5]
1
import numpy as np
2
a=np.array([(8,9),(10,11),(12,13)])
3
print(a[0:2,1])
5
"""As you can see in the above code, only 9 and 11 gets printed.
6
Now when I have written 0:2, this does not include the second index of the
7
third row of an array. Therefore, only 9 and 11 gets printed else you will
8
get all the elements i.e [9 11 13]."""
[ 9 11]
This is another operation in python numpy which returns evenly spaced numbers over a
specified interval. Consider the below example
1
import numpy as np
2
a=np.linspace(1,3,10)
3
print(a) # it has printed 10 values between 1 to 3.
We have some more operations in numpy such as to find the minimum, maximum as well the
sum of the numpy array.
1
import numpy as np
2
3
a= np.array([1,2,3])
4
print(a.min())
5
print(a.max())
6
print(a.sum())
As you can see in the figure, we have a numpy array 2*3. Here the rows are called as axis 1 and
the columns are called as axis 0. Now you must be wondering what is the use of these axis?
Suppose you want to calculate the sum of all the columns, then you can make use of axis.
https://colab.research.google.com/drive/1OX0_25_yerAgFQZd_vxeqRTfHgx9xTbp#scrollTo=Dc6HUTIVJzo0&printMode=true 4/9
9/7/22, 11:32 AM Numpy_Tutorial.ipynb - Colaboratory
1
#Suppose you want to calculate the sum of all the columns, then you can make use of axi
2
a= np.array([(1,2,3),(3,4,5)])
3
print(a.sum(axis=0))
[4 6 8]
1
import numpy as np
2
a=np.array([(1,2,3),(3,4,5,)])
3
print(np.sqrt(a))
4
print(np.std(a))
[1.73205081 2. 2.23606798]]
1.2909944487358056
Addition Operation
1
import numpy as np
2
x= np.array([(1,2,3),(3,4,5)])
3
y= np.array([(1,2,3),(3,4,5)])
4
print(x+y)
[[ 2 4 6]
[ 6 8 10]]
1
import numpy as np
2
x= np.array([(1,2,3),(3,4,5)])
3
y= np.array([(1,2,3),(3,4,5)])
4
print(x-y)
5
print(x*y)
6
print(x/y)
https://colab.research.google.com/drive/1OX0_25_yerAgFQZd_vxeqRTfHgx9xTbp#scrollTo=Dc6HUTIVJzo0&printMode=true 5/9
9/7/22, 11:32 AM Numpy_Tutorial.ipynb - Colaboratory
[[0 0 0]
[0 0 0]]
[[ 1 4 9]
[ 9 16 25]]
[[1. 1. 1.]
[1. 1. 1.]]
1
import numpy as np
2
x= np.array([(1,2,3),(3,4,5)])
3
y= np.array([(1,2,3),(3,4,5)])
4
print(np.vstack((x,y)))
5
print(np.hstack((x,y)))
[[1 2 3]
[3 4 5]
[1 2 3]
[3 4 5]]
[[1 2 3 1 2 3]
[3 4 5 3 4 5]]
There is one more operation where you can convert one numpy array into a single column i.e
ravel.
1
import numpy as np
2
x= np.array([(1,2,3),(3,4,5)])
3
print(x.ravel())
[1 2 3 3 4 5]
Array operators
1
x=np.arange(4)
2
print(x)
[0 1 2 3]
1
print(x+10)
[10 11 12 13]
https://colab.research.google.com/drive/1OX0_25_yerAgFQZd_vxeqRTfHgx9xTbp#scrollTo=Dc6HUTIVJzo0&printMode=true 6/9
9/7/22, 11:32 AM Numpy_Tutorial.ipynb - Colaboratory
1
print(x*2)
[0 2 4 6]
1
print(x+x)
[0 2 4 6]
1
print(x*x)
[0 1 4 9]
dot product (or more generally matrix multiplication) is done with a function
This function returns the dot product of two arrays. For 2-D vectors, it is the equivalent to matrix
multiplication. For 1-D arrays, it is the inner product of the vectors. For N-dimensional arrays, it
is a sum product over the last axis of a and the second-last axis of b.
1
x.dot(x)
14
1
a = np.array([[1,2],[3,4]])
2
b = np.array([[11,12],[13,14]])
3
np.dot(a,b)
array([[37, 40],
[85, 92]])
1 x = np.diag(np.arange(4))
2 print(x)
[[0 0 0 0]
[0 1 0 0]
[0 0 2 0]
[0 0 0 3]]
1
import numpy as np
2
https://colab.research.google.com/drive/1OX0_25_yerAgFQZd_vxeqRTfHgx9xTbp#scrollTo=Dc6HUTIVJzo0&printMode=true 7/9
9/7/22, 11:32 AM Numpy_Tutorial.ipynb - Colaboratory
3
# matrix creation by array input
4
a = np.matrix([[1, 21, 30],
5
[63 ,434, 3],
6
[54, 54, 56]])
7
8
print("Main Diagonal elements : \n", np.diag(a), "\n")
9
10
print("Diagonal above main diagonal : \n", np.diag(a, 1), "\n")
11
12
print("Diagonal below main diagonal : \n", np.diag(a, -1))
[ 1 434 56]
[21 3]
[63 54]
MATPLOT Tutorial
1 import numpy as np
2 import matplotlib.pyplot as plt
3 x= np.arange(0,3*np.pi,0.1)
4 y=np.sin(x)
5 plt.plot(x,y)
6 plt.show()
1 import numpy as np
2 import matplotlib.pyplot as plt
3 x= np.arange(0,3*np.pi,0.1)
4 y=np.tan(x)
5 plt.plot(x,y)
6 plt.show()
https://colab.research.google.com/drive/1OX0_25_yerAgFQZd_vxeqRTfHgx9xTbp#scrollTo=Dc6HUTIVJzo0&printMode=true 8/9
9/7/22, 11:32 AM Numpy_Tutorial.ipynb - Colaboratory
https://colab.research.google.com/drive/1OX0_25_yerAgFQZd_vxeqRTfHgx9xTbp#scrollTo=Dc6HUTIVJzo0&printMode=true 9/9