You are on page 1of 18

Notes:6

NumPy :- Numeric Python


 NumPy stands for ‘Numerical Python’.
 NumPy can be installed by typing following command: pip
install NumPy
 NumPy is used to create arrays in Python. Array is used to store
a fixed set of elements of same data type.
 An array consists of:-
 Element-Each item stored in an array is called an element.
 Index- Numerical position of each element in an array.
Index will be unique for each element
The important characteristics of an array are:
 • Each element of the array is of same data type, though the
values stored in them may be different.
 Eg1:-[1 2 3]
 Eg2:-[1.3 3.4 5.6 6.0]
 The entire array is stored continuous memory locations. This
makes operations on array fast.
 • Each element of the array is identified or referred using the
name of the Array along with the index of that element, which is
unique for each element. The index of an element is an integral
value associated with the element, based on the element’s
position in the array.
 Consider the following array.[100 200 300]
Elements and corresponding index values as follows.
 Element =100 index =0
 Element =200 index =1
 Element =300 index=2
Difference Between List and Array
List Array
List can have elements of All elements of an array are
different data types for of same data type for
example, [1,3.4, ‘hello’, example, an array of floats
‘a@’] may be: [1.2, 5.4, 2.7]
Elements of a list are not Array elements are stored in
stored contiguously in contiguous memory
memory. locations.
List is a part of core Python. Array (ndarray) is a part of
NumPy library

Types of arrays
 1 dimensional array (1D array) / Vector
Eg:-[190 567 890]
 Multi dimensional array or ND array
 2 dimensional(2D) array is the commonly used multi
dimensional array.
 2D array is also called matrices
 Eg:-
[10 20 30
40 50 60
70 80 90]
The following table shows array elements and its
corresponding index values.

10 20 30
[0,0] [0,1] [0,2]

40 50 60
[1,0] [1,1] [1,2]

70 80 90
[2,0] [2,1] [2,2]

# create one dimensional array(vector) with


elements 20,30,40
import numpy as n
arr=n.array([20,30,40])
print(arr)
Output
[20 30 40]

# create one dimensional array(vector) with


elements 20,30,40 using list
import numpy as n
L1=[20,30,40]
arr=n.array(L1)
print(arr)

Output
[20 30 40]

# create two dimensional array(matrix) with


elements [20 30 40
50 60 70]

import numpy as n
a=n.array([[20,30,40],[50,60,70]])
print(a)

Output
[20 30 40
50 60 70]

# create two dimensional array(matrix) with


elements [20 30 40 using list
50 60 70]
import numpy as n
li= [[20,30,40],[50,60,70]]
a=n.array(li)
print(a)

Output
[20 30 40
50 60 70]

Attributes in NumPy
1. ndim – Displays the dimension of an array
eg1:
import numpy as np
a=np.array([10,20,30])
print(a.ndim)

output=1
2. size –Displays number of elements in the array
eg1:
import numpy as np
a=np.array([1,2,3])
print(a.size)

output=3

3. shape- Displays the number of rows and columns of


an array.
eg1:
import numpy as np
a=np.array([[1,2,3],[4,5,6],[5,6,7]])
print(a.shape)

Output= (3,3)

eg2:
import numpy as np
a=np.array([1,2,3,4,5])
print(a.shape)

output:- (5,)

4. dtype- It is used to specify the data type of the array


eg1:-
import numpy as np
a=np.array([11.4,32.5,43.6],dtype=int)
print(a)

output=[11 32 43]

Functions in NumPy
1. zeros – To create arrays with zeros as elements
eg1:-
import numpy as n
a=n.zeros((4,2),dtype=int)
print(a)

output
[0 0
0 0
0 0
0 0]
Eg2:-

Import numpy as n
a=n.zeros(4,dtype=float)
print(a)

output
[0. 0. 0. 0.]
2. ones –To create array with one as element.
eg1:
import numpy as n
a=n.ones((2,2),dtype=int)
print(a)

output
[1 1
1 1]

Eg2:-
import numpy as n
a=n.ones(4,dtype=float)
print(a)
output
[1. 1. 1. 1.]

3. Arange- To create an array with numbers in a given


range and sequence
eg1:-
importnumpy as n
a=n.arange(6)
print(a)

output [0 1 2 3 4 5]

eg2:-
import numpy as n
a=n.arange(2,10,2)
print(a)

output: [2 4 6 8]

eg3:-
import numpy as n
a=n.arange(2,10,2,dtype=float)
print(a)

output:[2. 4. 6. 8.]
eg4:-
import numpy as n
a=n.arange(10,2,-2)
print(a)

output: [10 8 6 4 ]

Array Slicing :-It is used to display the part of an


array.
import numpy as np
a=np.array([-2, 2, 6, 10, 14, 18, 22])
# 0 1 2 3 4 5 6 (index values)
print(a[3:5]) # output:-[10 14]
print(a[0:5]) # output:-[-2 2 6 10 14]
print(a[0:5:2]) # output:-[-2 6 14]
print(a[:5]) # output:-[-2 2 6 10 14]
print(a[: : 1]) # output:-[-2 2 6 10 14 18 22]
# reverse the array
print(a[: : -1] ) # output:-[22 18 14 10 6 2 -2]

Arithmetic Operations on array


1. Addition
Eg1:-
import numpy as np
a=np.array([1,2,3])
b=np.array([4,5,6])
print(a+b)

output:-[5 7 9]

eg2:-
import numpy as np
a=np.array([[1,2,3],[0,1,2]])
b=np.array([[4,5,6],[3,4,5]])
print(a+b)
output:-[5 7 9
3 5 7]

2. Subtraction
Eg1:-
import numpy as np
a=np.array([1,2,3])
b=np.array([4,5,6])
print(a-b)
output:-
[-3 -3 -3]

p=np.array([[1,2,3],[0,1,2]])
q=np.array([[4,5,6],[3,4,5]])
print(p-q)

Output:-
[-3 -3 -3
-3 -3 -3]

3. Multiplication
Eg1:-
import numpy as np
a=np.array([1,2,3])
b=np.array([4,5,6])
print(a*b)
output =[4 10 18]

p=np.array([[1,2,3],[0,1,2]])
q=np.array([[4,5,6],[3,4,5]])
print(p*q)

output
[4 10 18
0 4 10]

4. Matrix multiplication ( @ )
Rule:-The number of columns in the
first matrix is equal to the number of rows in the
second matrix.
Eg:-
[1 2 [6 7 [1*6+2*8 1*7+2*5 [22 17
3 4] 8 5] = 3*6+4*8 3*7+4*5]= 50 41]
import numpy as np
a=np.array([[1,2],[0,1]])
b=np.array([[4,5],[3,4]])
print(a@b)

a=[1 2 b=[4 5 [10 13


0 1] 3 4]= 3 4]

5. Division
Eg1:-
import numpy as np
a=np.array([1,2,3])
b=np.array([4,5,6])
print(a/b)
Output:-
[1/4 2/5 3/6]
=[0.25 0.4 0.5]

p=np.array([[1,2,3],[0,1,2]])
q=np.array([[4,5,6],[3,4,5]])
print(p/q)

Output:-
[1/4 2/5 3/6
0/3 ¼ 2/5]
=[0.25 0.4 0.5
0.0 .25 0.4]
6. Exponent (**)
import numpy as np
a=np.array([[1,2],[3,4]])
print(a**2)

output
[1 4
9 16]

b=np.array([1,2])
print(b**3)

output
[1 8]

7. Modulos- To get remainder (%)


import numpy as np
a=np.array([[6,6],[8,10]])
b=np.array([[5,6],[7,8]])
print(a%b)

Output:-
[6%5 6%6
8%7 10%8]
=[1 0
1 2]
Transpose of matrix – Transposing an array turns rows
into columns.
P=[10 20 30
40 50 60
70 80 90]
Transpose of P=
[10 40 70
20 50 80
30 60 90]

import numpy as np
a=np.array([[6,2],[3,4]])
print(a.transpose())

[6 2
3 4]

output
[6 3
2 4]
Concatenate arrays – To combine arrays
Example 1:
import numpy as n;
a=n.array([2,3,4])
b=n.array([5,6,7])
c=n.concatenate([a,b])
print(c)
# output=[2 3 4 5 6 7]
d=n.cancatenate([b,a])
print(d)
# output=[5 6 7 2 3 4]

Example 2:-
import numpy as n;
a=n.array([[2,3,4],[1,2,3]])
b=n.array([[5,6,7],[6,7,8]])
c=n.concatenate([a,b])
print(c)

a=[2 3 4
1 2 3]
b=[5 6 7
6 7 8]
output:-
[2 3 4
123
5 67
6 7 8]

Example 2:-
import numpy as n;
a=n.array([[10,20],[30,40]])
b=n.array([[50,60],[70,80]])
c=n.concatenate([a,b],axis=0)
print(c)
a=[10 20
30 40]
b=[50 60
70 80]
Output:-
c=[10 20
30 40
50 60
70 80]
Example 3:-
import numpy as n;
a=n.array([[12,13,14],[11,12,13]])
b=n.array([[5,6,7],[6,7,8]])
c=n.concatenate([a,b],axis=1)
print(c)

a=[12 13 14 b=[5 6 7
11 12 13] 6 7 8]
Output
c=[12 13 14 5 6 7
11 12 13 6 7 8]
**************

You might also like