Data Processing and Visulisation with Python
Python Exercise 21
Matrix with range in row
Write a Python function to take a positive integer n
as parameter and return a
n
×
n
n\times n
n×n matrix with values in each row ranging from 0 to n-1.
method 1
def matrixRangeRow(m):
import numpy as np
return np.tile([np.arange(m)],(m,1))
method 2
def matrixRangeRow(n):
import numpy as np
return np.array([[i for i in range(n)]]*n)
matrixRangeRow(5)
matrixRangeRow(12)
Matrix with range in column
Write a Python function to take a positive integer n
as parameter and return a
n
×
n
n\times n
n×n matrix with values in each column ranging from 0 to n-1.
method 1
def matrixRangeColumn(n):
import numpy as np
return np.array([[i for i in range(n)]]*n).transpose()
method 2
def matrixRangeColumn(m):
import numpy as np
return np.tile([np.arange(m)],(m,1)).transpose()
matrixRangeColumn(5)
matrixRangeColumn(12)
Sum of multiples of 3 or 5
Write a Python function to take a 1-d numpay array and return the sum of the elements which are multiples of 3 or 5.
method 1
def sum3_5(a):
return sum([i for i in a if i % 3 == 0 or i % 5 == 0])
method 2
def sum3_5(n):
return sum(n[(n%5==0) | (n%3==0)])
import numpy as np
sum3_5(np.array([65, 17, 59, 8, 31, 8, 56, 63, 36, 30, 45, 20, 92, 4, 49, 3, 42, 37, 60, 5, 25, 95, 57, 14, 83, 32, 1, 20, 41, 61]))
sum3_5(np.array([90, 27, 23, 12, 79, 34, 54, 24, 37, 93, 87, 49, 57, 67, 99, 72, 93,
72, 30, 52, 82, 81, 58, 52, 16, 61, 89, 24, 1, 51, 41, 97, 79, 33,
95, 17, 50, 81, 22, 77, 51, 66, 37, 86, 4, 13, 60, 30, 33, 94]))
The m t h m^{th} mth Column
Write a Python function to take a
N
∗
M
N*M
N∗M matrix a
in numpy array and an integer m
(0<=m<M), return the
m
t
h
m^{th}
mth Column.
method 1
def matrixColumn(a,m):
return a[:,m]
method 2
def matrixColumn(n,m):
return np.transpose(n[:,m])
import numpy as np
n = np.random.randint(1,100,(5,3))
n
matrixColumn(n, 2)
n = np.random.randint(1,100,(8,12))
n
matrixColumn(n, 10)
Replace outliers
Write a Python function replaceOutliers(a, ex, value)
to replace the outliers (in numpy array a
), which values are great than ex
, to the value of value
.
def replaceOutliers(a, ex, value):
a[a>ex] = value
return a
import numpy as np
n = np.random.randint(1,100,(5,4))
n
replaceOutliers(n, 70, 70)
n = np.random.randint(1,100,20)
n
replaceOutliers(n, 70, 70)
Swap columns
Write a Python function swapColumns(a, m, n)
to swap the
n
t
h
n^{th}
nth column and the
m
t
h
m^{th}
mth column of the matrix a
. (m
, n
are 0 based)
method 1
def swapColumns(a, m, n):
b = a.copy()
a[:,m] = b[:,n]
a[:,n] = b[:,m]
return a
method 2
def swapColumns(a, m, n):
a[:,[m,n]]=a[:,[n,m]]
return a
import numpy as np
n = np.random.randint(1,100,(5,4))
n
swapColumns(n, 0,2)
n = np.random.randint(1,100,(8,12))
n
swapColumns(n, 2,10)
Norm
Write a Python function norm(a)
to return the norm of numpy array a
.
method 1
import numpy as np
def norm(a):
return np.linalg.norm(a)
method 2
def norm(a):
return np.sqrt((a*a).sum())
import numpy as np
n = np.array([1,2,3,4,5])
n
norm(n)
n = np.random.randint(1,10,(3,4))
n
norm(n)
n = np.random.randint(1,10,(2,3,2))
n
norm(n)
Determinant
def det(a):
return np.linalg.det(a)
import numpy as np
a = np.array([[1,2],[3,4]])
a
det(a)
n = np.random.randint(1,10,(3,3))
n
det(n)
n = np.random.randint(1,20,(10,10))
n
det(n)