Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

9/7/22, 11:31 AM Numpy_Tutorial.

ipynb - Colaboratory

What are NumPy Arrays?


NumPy is a Python package that stands for ‘Numerical Python’. It is the core library for scientific
computing, which contains a powerful n-dimensional array object. bold text

Single-dimensional Numpy Array


From a given List

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)

array([0., 0., 0., 0.])

1
np.zeros((2,3))

array([[0., 0., 0.],

[0., 0., 0.]])

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.]])

You can calculate the byte size of each element.

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]]

Slicing is basically extracting particular set of elements from an array.

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.

[1. 1.22222222 1.44444444 1.66666667 1.88888889 2.11111111

2.33333333 2.55555556 2.77777778 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]

Square Root & Standard Deviation

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. 1.41421356 1.73205081]

[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.]]

Vertical & Horizontal Stacking


if you want to concatenate two arrays and not just add them, you
can perform it using two ways – vertical stacking and horizontal stacking.

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]

Sclar Addition element wise

1
print(x+10)

[10 11 12 13]

scalar multiplication is element wise

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]

array addition is element wise

1
print(x+x)

[0 2 4 6]

array multiplication is element wise

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]])

numpy.diag(a, k=0) : Extracts and construct a diagonal array Parameters :

a : array_like k : [int, optional, 0 by default]


Diagonal we require; k>0 means diagonal above main
diagonal or vice versa bold text

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))

Main Diagonal elements :

[ 1 434 56]

Diagonal above main diagonal :

[21 3]

Diagonal below main diagonal :

[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

Colab paid products


-
Cancel contracts here

check 0s completed at 9:46 AM

https://colab.research.google.com/drive/1OX0_25_yerAgFQZd_vxeqRTfHgx9xTbp#scrollTo=Dc6HUTIVJzo0&printMode=true 9/9

You might also like