Unit-3 PSC
Unit-3 PSC
# – 2
Arrays
# – 3
Arrays
Accessing array elements
We can access the array elements using the respective indices of those elements.
# – 4
Basic Operations on Array
Adding Elements to Array
# – 5
Change elements in Array
import array as arr
numbers = arr.array('i', [1, 2, 3, 5, 7, 10])
# – 6
Finding the length of an array
The length of an array is defined as the number of elements present in an array. It returns an
integer value that is equal to the total number of the elements present in that array.
Syntax
len(array_name)
# – 7
Delete elements from an array
import array as arr
number = arr.array('i', [1, 2, 3, 3, 4])
del number[2] # removing third element
print(number) # Output: array('i', [1, 2, 3, 4])
Output:
# – 8
Remove elements from an array
# – 9
Array Concatenation
We can easily concatenate any two arrays using the + symbol.
Example
# – 10
Array Slicing
# – 11
Array Sorting
# – 12
NumPy v/s Pandas
Developers built pandas on top of NumPy, as a result every task we perform using
pandas also goes through NumPy.
To obtain the benefits of pandas, we need to pay a performance penalty that
some testers say is 100 times slower than NumPy for similar task.
Nowadays computer hardware are powerful enough to take care for the
performance issue, but when speed of execution is essential NumPy is always the
best choice.
We can use pandas to make writing code easier and faster, pandas will reduce
potential coding errors.
Pandas provide rich time-series functionality, data alignment, NA-friendly
statistics, groupby, merge, etc.. methods, if we use NumPy we have to implement
all these methods manually.
So,
if we want performance we should use NumPy,
if we want ease of coding we should use pandas.
# – 13
NumPy
NumPy (Numeric Python) is a Python library to manipulate arrays.
Almost all the libraries in python rely on NumPy as one of their main building block.
NumPy provides functions for domains like Algebra, Fourier transform etc..
NumPy is incredibly fast as it has bindings to C libraries.
Install :
conda install numpy
OR pip install numpy
for Ubuntu
sudo apt install python3-numpy
# – 15
NumPy
# – 16
NumPy Vs. Python Array
The NumPy arrays are convenient as they have the following three
features–
# – 17
NumPy
# – 18
NumPy
There are three methods to import numpy
import numpy
from numpy import *
import numpy as np
# – 19
NumPy
1D Numpy Array using arange() function
Numpy arange() function takes start, end of a range and the interval as arguments
and returns a one-dimensional array.
import numpy as np
Output
# – 20
NumPy
1D Numpy Array using linspace() function
Numpy linspace() functions takes start, end and the number of elements to be
created as arguments and creates a one-dimensional array.
import numpy as np
Output
# – 21
NumPy
2 D Numpy Array
import numpy as np
Output
#create 2*3 numpy array
a = np.array([[5, 25, 4],[10, 22, 13]]) [ 5 25 4]
[10 22 13]]
print(a)
Output:
3 D Numpy Array
import numpy as np [[[1 2 3]
[4 5 6]
[7 8 9]]
[[2 3 1]
a = np.array([[[1, 2, 3],[4, 5, 6],[7, 8, 9]], [5 6 4]
[[2, 3, 1],[5, 6, 4],[8, 9, 7]], [8 9 7]]
numpyarray.py Output
1 import numpy as np <class 'numpy.ndarray'>
2 a= np.array(['indus','Insitute','Ahmedabad']) ['indus' 'Insitute' 'Ahmedabad']
3 print(type(a))
4 print(a)
# – 23
NumPy Array (Cont.)
arange(start,end,step) function will create NumPy array starting from start till end
(not included) with specified steps.
numpyarange.py Output
1 import numpy as np [0 1 2 3 4 5 6 7 8 9]
2 b = np.arange(0,10,1)
3 print(b)
zeros(n) function will return NumPy array of given shape, filled with zeros.
numpyzeros.py Output
1 import numpy as np [0. 0. 0.]
2 c = np.zeros(3)
3 print(c) [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]
4 c1 = np.zeros((3,3)) #have to give as tuple
5 print(c1)
ones(n) function will return NumPy array of given shape, filled with ones.
# – 24
NumPy Array (Cont.)
eye(n) function will create 2-D NumPy array with ones on the diagonal and zeros
elsewhere.
numpyeye.py Output
1 import numpy as np [[1. 0. 0.]
2 b = np.eye(3) [0. 1. 0.]
3 print(b) [0. 0. 1.]]
linspace(start , stop , num) function will return evenly spaced numbers over a
specified interval.
numpylinspace.py
Output
1 import numpy as np [0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]
2 c = np.linspace(0,1,11)
3 print(c)
Note: in arange function we have given start, stop & step, whereas in lispace
function we are giving start,stop & number of elements we want.
# – 25
Array Shape in NumPy
# – 26
Array Reshape in NumPy
# – 27
Array Transpose in NumPy
# – 28
Array Flattern in NumPy
# – 29
NumPy Random
rand(p1,p2….,pn) function will create n-dimensional array with random data
using uniform distrubution, if we do not specify any parameter it will return random
float number.
numpyrand.py Output
1 import numpy as np 0.23937253208490505
2 r1 = np.random.rand()
3 print(r1) [[0.58924723 0.09677878]
4 r2 = np.random.rand(3,2) # no tuple [0.97945337 0.76537675]
5 print(r2) [0.73097381 0.51277276]]
randint(low,high,num) function will create one-dimensional array with num random
integer data between low and high.
numpyrandint.py Output
1 import numpy as np [78 78 17 98 19 26 81 67 23 24]
2 r3 = np.random.randint(1,100,10)
3 print(r3)
We can reshape the array in any shape using reshape method, which we learned
in previous slide.
# – 30
NumPy Random (Cont.)
randn(p1,p2….,pn) function will create n-dimensional array with random data
using standard normal distribution, if we do not specify any parameter it will return
random float number.
numpyrandn.py Output
1 import numpy as np -0.15359861758111037
2 r1 = np.random.randn()
3 print(r1) [[ 0.40967905 -0.21974532]
4 r2 = np.random.randn(3,2) # no tuple [-0.90341482 -0.69779498]
5 print(r2) [ 0.99444948 -1.45308348]]
Note: rand function will generate random number using uniform distribution,
whereas randn function will generate random number using standard normal
distribution.
# – 31
Visualizing the difference between rand & randn
We are going to use matplotlib library to visualize the difference.
matplotdemo.py
1 import numpy as np
2 from matplotlib import pyplot as plt
3 %matplotlib inline
4 samplesize = 100000
5 uniform = np.random.rand(samplesize)
6 normal = np.random.randn(samplesize)
7 plt.hist(uniform,bins=100)
8 plt.title('rand: uniform')
9 plt.show()
10 plt.hist(normal,bins=100)
11 plt.title('randn: normal')
12 plt.show()
# – 32
Aggregations
min() function will return the minimum value from the ndarray, there are two ways
in which we can use min function, example of both ways are given below.
numpymin.py Output
1 import numpy as np Min way1 = 1
2 l = [1,5,3,8,2,3,6,7,5,2,9,11,2,5,3,4,8,9,3,1,9,3] Min way2 = 1
3 a = np.array(l)
4 print('Min way1 = ',a.min())
5 print('Min way2 = ',np.min(a))
max() function will return the maximum value from the ndarray, there are two ways
in which we can use min function, example of both ways are given below.
numpymax.py Output
1 import numpy as np Max way1 = 11
2 l = [1,5,3,8,2,3,6,7,5,2,9,11,2,5,3,4,8,9,3,1,9,3] Max way2 = 11
3 a = np.array(l)
4 print('Max way1 = ',a.max())
5 print('Max way2 = ',np.max(a))
# – 33
Aggregations (Cont.)
NumPy support many aggregation functions such as min, max, argmin, argmax,
sum, mean, std, etc…
numpymin.py Output
1 l = [7,5,3,1,8,2,3,6,11,5,2,9,10,2,5,3,7,8,9,3,1,9,3]
2 a = np.array(l)
3 print('Min = ',a.min()) Min = 1
4 print('ArgMin = ',a.argmin()) ArgMin = 3
5 print('Max = ',a.max()) Max = 11
6 print('ArgMax = ',a.argmax()) ArgMax = 8
7 print('Sum = ',a.sum()) Sum = 122
8 print('Mean = ',a.mean()) Mean = 5.304347826086956
9 print('Std = ',a.std()) Std = 3.042235771223635
# – 34
Using axis argument with aggregate functions
When we apply aggregate functions with multidimensional ndarray, it will apply
aggregate function to all its dimensions (axis).
numpyaxis.py Output
1 import numpy as np sum = 45
2 array2d = np.array([[1,2,3],[4,5,6],[7,8,9]])
3 print('sum = ',array2d.sum())
If we want to get sum of rows or cols we can use axis argument with the aggregate
functions.
numpyaxis.py Output
1 import numpy as np sum (cols) = [12 15 18]
2 array2d = np.array([[1,2,3],[4,5,6],[7,8,9]]) sum (rows) = [6 15 24]
3 print('sum (cols)= ',array2d.sum(axis=0)) #Vertical
4 print('sum (rows)= ',array2d.sum(axis=1)) #Horizontal
# – 35
Single V/S Double bracket notations
There are two ways in which you can access element of multi-dimensional array,
example of both the method is given below
numpybrackets.py
Output
1 arr = np.array([['a','b','c'],['d','e','f'],['g','h','i']])
2 print('double = ',arr[2][1]) # double bracket notaion double = h
single = h
3 print('single = ',arr[2,1]) # single bracket notation
4
Both method is valid and provides exactly the same answer, but single bracket
notation is recommended as in double bracket notation it will create a temporary
sub array of third row and then fetch the second column from it.
Single bracket notation will be easy to read and write while programming.
# – 36
Slicing ndarray
Slicing in python means taking elements from one given index to another given
index.
Similar to Python List, we can use same syntax array[start:end:step] to slice ndarray.
Default start is 0
Default end is length of the array
Default step is 1
numpyslice1d.py
Output
1 import numpy as np
['c' 'd' 'e']
2 arr = np.array(['a','b','c','d','e','f','g','h'])
['a' 'b' 'c' 'd' 'e']
3 print(arr[2:5])
['f' 'g' 'h']
4 print(arr[:5])
['c' 'e' 'g']
5 print(arr[5:])
['h' 'g' 'f' 'e' 'd' 'c' 'b' 'a']
6 print(arr[2:7:2])
7 print(arr[::-1])
# – 37
Array Slicing Example
C-0 C-1 C-2 C-3 C-4
R-0 1 2 3 4 5
R-1 6 7 8 9 10
a = R-2
11 12 13 14 15
R-3
16 17 18 19 20
R-4
21 22 23 24 25
# – 38
Array Slicing Example
import numpy as np
[ 1 2 3 4 5]
#create 5*5 numpy array [ 6 7 8 9 10]
a = np.array([[1, 2, 3, 4, 5],[6, 7, 8, 9, [11 12 13 14 15]
10],[11, 12, 13, 14, 15],[16, 17, 18, 19, [16 17 18 19 20]
20],[21, 22, 23, 24, 25]])
[21 22 23 24 25]]
print(a)
print("a[2][3]=",a[2][3]) a[2][3]= 14
print("a[2,3]=",a[2,3]) a[2,3]= 14
print("a[2]=",a[2]) a[2]= [11 12 13 14 15]
print("a[0:2] =",a[0:2])
a[0:2] = [[ 1 2 3 4 5]
[ 6 7 8 9 10]]
# – 39
Array Slicing Example
# – 41
Warning : Array Slicing is mutable !
When we slice an array and apply some operation on them, it will also make
changes in original array, as it will not create a copy of a array while slicing.
Example,
numpyslice1d.py Output
1 import numpy as np Original Array = [2 2 2 4 5]
2 arr = np.array([1,2,3,4,5]) Sliced Array = [2 2 2]
3 arrsliced = arr[0:3]
4
5 arrsliced[:] = 2 # Broadcasting
6
7 print('Original Array = ', arr)
8 print('Sliced Array = ',arrsliced)
# – 42
NumPy Arithmetic Operations
numpyop.py Output
1 import numpy as np Addition Scalar = [[3 4 5]
2 arr1 = np.array([[1,2,3],[1,2,3],[1,2,3]]) [3 4 5]
3 arr2 = np.array([[4,5,6],[4,5,6],[4,5,6]]) [3 4 5]]
Addition Matrix = [[5 7 9]
4
[5 7 9]
5 arradd1 = arr1 + 2 # addition of matrix with scalar [5 7 9]]
6 arradd2 = arr1 + arr2 # addition of two matrices Substraction Scalar = [[-1 0 1]
7 print('Addition Scalar = ', arradd1) [-1 0 1]
8 print('Addition Matrix = ', arradd2) [-1 0 1]]
9 Substraction Matrix = [[-3 -3 -3]
10 arrsub1 = arr1 - 2 # substraction of matrix with scalar [-3 -3 -3]
arrsub2 = arr1 - arr2 # substraction of two matrices [-3 -3 -3]]
Division Scalar = [[0.5 1. 1.5]
11 print('Substraction Scalar = ', arrsub1)
[0.5 1. 1.5]
12 print('Substraction Matrix = ', arrsub2) [0.5 1. 1.5]]
13 arrdiv1 = arr1 / 2 # substraction of matrix with scalar Division Matrix = [[0.25 0.4 0.5 ]
14 arrdiv2 = arr1 / arr2 # substraction of two matrices [0.25 0.4 0.5 ]
print('Division Scalar = ', arrdiv1) [0.25 0.4 0.5 ]]
15 print('Division Matrix = ', arrdiv2)
16
17
# – 43
NumPy Arithmetic Operations (Cont.)
numpyop.py Output
1 import numpy as np Multiply Scalar = [[2 4 6]
2 arrmul1 = arr1 * 2 # multiply matrix with scalar [2 4 6]
3 arrmul2 = arr1 * arr2 # multiply two matrices [2 4 6]]
Multiply Matrix = [[ 4 10 18]
4 print('Multiply Scalar = ', arrmul1)
[ 4 10 18]
5 #Note : its not metrix multiplication* [ 4 10 18]]
6 print('Multiply Matrix = ', arrmul2) Matrix Multiplication = [[24 30 36]
7 # In order to do matrix multiplication [24 30 36]
8 arrmatmul = np.matmul(arr1,arr2) [24 30 36]]
9 print('Matrix Multiplication = ',arrmatmul) Dot = [[24 30 36]
10 # OR [24 30 36]
arrdot = arr1.dot(arr2) [24 30 36]]
Python 3.5+ support = [[24 30 36]
11 print('Dot = ',arrdot)
[24 30 36]
12 # OR [24 30 36]]
13 arrpy3dot5plus = arr1 @ arr2
14 print('Python 3.5+ support = ',arrpy3dot5plus)
# – 44
Sorting Array
The sort() function returns a sorted copy of the input array.
syntax Parameters
import numpy as np arr = array to sort (inplace)
# arr = our ndarray axis = axis to sort (default=0)
np.sort(arr,axis,kind,order) kind = kind of algo to use
# OR arr.sort() (‘quicksort’ <- default, ‘mergesort’, ‘heapsort’)
order = on which field we want to sort (if
multiple fields)
Example :
numpysort.py Output
1 import numpy as np Before Sorting = ['Darshan' 'Rajkot' 'Insitute'
2 arr = np.array(['Darshan','Rajkot','Insitute','of','Engineering']) 'of' 'Engineering']
print("Before Sorting = ", arr) After Sorting = ['Darshan' 'Engineering'
arr.sort() # or np.sort(arr) 'Insitute' 'Rajkot' 'of']
3 print("After Sorting = ",arr)
4
5
# – 45
Sort Array Example
numpysort2.py Output
1 import numpy as np [(b'ABC', 300) (b'Darshan', 200) (b'XYZ',
2 dt = np.dtype([('name', 'S10'),('age', int)]) 100)]
3 arr2 = np.array([('Darshan',200),('ABC',300),('XYZ',100)],dtype=dt)
arr2.sort(order='name')
print(arr2)
4
5
# – 46
Conditional Selection
Similar to arithmetic operations when we apply any comparison operator to Numpy
Array, then it will be applied to each element in the array and a new bool Numpy
Array will be created with values True or False.
numpycond1.py Output
1 import numpy as np [25 17 24 15 17 97 42 10 67 22]
2 arr = np.random.randint(1,100,10) [False False False False False True False
3 print(arr) False True False]
4 boolArr = arr > 50
5 print(boolArr)
numpycond2.py Output
1 import numpy as np All = [31 94 25 70 23 9 11 77 48 11]
2 arr = np.random.randint(1,100,10) Filtered = [94 70 77]
3 print("All = ",arr)
4 boolArr = arr > 50
5 print("Filtered = ", arr[boolArr])
# – 47
Numpy Linear Algebra
# – 48
Numpy Linear Algebra
# – 49
Textfile To Matrix
To import Text files into Numpy Arrays, we have two functions in Numpy:
import numpy as np
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]] 50
# –
Textfile To Matrix
Example 2: Importing text file into NumPy array by skipping first row
import numpy as np
Output :
[['2' 'Bunty']
['3' 'Tinku']
['4' 'Rina']]
# – 51
Textfile To Matrix
Example 3: Importing only the first column(Names) of text file into numpy arrays
import numpy as np
Ankit
Bunty
Tinku
Rina
Rajesh
# – 52
Textfile To Matrix
Method 2 : numpy.genfromtxt()
Example 3: Importing only the first column(Names) of text file into numpy arrays
import numpy as np
Output :
# – 53
# – 54
# – 55
# – 56
# – 57
# – 58
# – 59
# – 60
# – 61
# – 62