Data Processing and Visulisation with Python
Python Exercise 20
Reverse vector
Write a Python function to reverse a vector in numpy array (first element becomes last)
def reverseVector(x):
return x[::-1]
import numpy as np
v = np.arange(11, 21)
v
reverseVector(v)
reverseVector(np.arange(100, 121))
Any or all non-zero
Write a Python function anyAllNonZero(nparr, s)
to take a given 1-d numpy array nparr
and a string s
. If s
is “any”, then return True if any element in nparr
is non-zero, return False if all elements are zero. If s
is “all”, then return True if all elements in nparr
are non-zero, return False if any element is zero.
method 1
def anyAllNonZero(nparr,s):
if s == "any":
for i in nparr:
if i != 0:
return True
return False
if s == "all":
for j in nparr:
if j == 0:
return False
return True
method 2
def anyAllNonZero(nparr, s):
if s=='any':
return any(nparr)
if s=='all':
return all(nparr)
method 3
def anyAllNonZero(n,m):
if m=="all":
return((n!=0).all())
if m=="any":
return((n!=0).any())
method 4
def anyAllNonZero(nparr, s):
if s == 'any':
return np.sum(nparr!=0) != 0
if s == 'all':
return np.prod(nparr!=0) != 0
import numpy as np
n1 = np.array([1,2,3,4,5,6])
n2 = np.array([0,1,2,3,4,5])
n3 = np.array([0,0,0,0,0,0])
anyAllNonZero(n1, 'any')
anyAllNonZero(n2, 'any')
anyAllNonZero(n3, 'any')
anyAllNonZero(n1, 'all')
anyAllNonZero(n2, 'all')
anyAllNonZero(n3, 'all')
Create matrix
Write a Python function to take three positive integers m, n, a as parameters, and return an m × n m\times n m×n matrix with elements starting from a, a+1, a+2, … in numpy array.
method 1
def createMatrix(a,b,c):
import numpy as np
return np.mat(np.arange(a*b).reshape(a,b))+c
method 2
def createMatrix(m,n,a):
return np.array(np.arange(a,a+m*n)).reshape(m,n)
createMatrix(3,5,10)
createMatrix(6, 5, 101)
Range in range
Write a Python function rangeInRange(a,b,c,d)
(a,b,c,d are positive integers, and 0<a<c<d<b) to
- generate a numpy array with elements from a (included) to b (excluded), then
- shuffle them, and
- change the sign of elements within [c, d) to negarive.
- return the reault numpy array
Hint : You can use numpy.random.shuffle() to shuffle a numpy array.
method 1
def rangeInRange(a,b,c,d):
import numpy as np
a = np.array([i for i in range(a,c)]+[-i for i in range(c,d)]+[i for i in range(d,b)])
np.random.shuffle(a)
return a
method 2
def rangeInRange(a,b,c,d):
n1 = np.arange(a,b)
n1[c-a:d-a] *= -1
np.random.shuffle(n1)
return n1
method 3
def rangeInRange(a,b,c,d):
from random import shuffle
l=np.arange(a,b)
shuffle(l)
for i in range(len(l)):
if c<=l[i]<d:
l[i]=-l[i]
return l
method 4
def rangeInRange(a,b,c,d):
arr = np.arange(a,b)
np.random.shuffle(arr)
return arr*(np.logical_and(arr>=c,arr<d)*-2+1)
method 5
def rangeInRange(a,b,c,d):
arr = np.arange(a,b)
np.random.shuffle(arr)
arr[(arr>=c)&(arr<d)]*= -1
return arr
rangeInRange(1,20,6,16)
rangeInRange(10,100,20,30)
Digital frame
Write a Python function to take a positive integer n
and return an n*n matrix, in which the elements on the borders will be equal to 1, and inside 0.
method 1
def digitalFrame(n):
import numpy as np
matrix = np.zeros((n,n))
matrix[:,0] = 1
matrix[0,:] = 1
matrix[n-1,:] = 1
matrix[:,n-1] = 1
return matrix
method 2
def digitalFrame(n):
import numpy as np
a = np.ones((n,n))
a[1:-1,1:-1] = 0
return a
method 3
def digitalFrame(n):
x=np.ones((n,n))
x[1:-1,1:-1]=np.zeros((n-2,n-2))
return x
method 4
def digitalFrame(n):
arr = np.zeros((n,n))
arr[(0,n-1),:]=1
arr[:,(0,n-1)]=1
return arr
method 5
def digitalFrame(n):
n1 = np.ones((n,n))
n2 = np.zeros((n-2,n-2))
n1[1:n-1,1:n-1] = n2
return n1
method 6
def checkerBoard(n):
import numpy as np
a = np.array([[0.,1.],[1.,0.]])
if n % 2 == 0:
b = np.tile(a,(n//2,n//2))
else:
c = np.tile(a,(n//2+1,n//2+1))
b = c[:-1,:-1]
return b
digitalFrame(6)
digitalFrame(15)
Diagonal matrix
Write a NumPy function to take a positive integer n
and return an nxn zero matrix with elements on the main diagonal equal to 1, 2, 3, 4, 5…, n.
def diagMatrix(n):
import numpy as np
return np.diag(np.arange(n)+1)
diagMatrix(5)
diagMatrix(10)
Add vector
Write a Python function to take a matrix, a vector and a flag. If the flag is ‘r’, then add the vector to each row of the matrix. If the flag is ‘c’, then add the vector to each column of the matrix. Finaly, return the result matrix. If the flag is neither ‘r’ nor ‘c’, then do nothing.
Note : It is the caller’s responsibility to make sure that the vector match the row or column of the matrix.
import numpy as np
def addVector(a,v,flag):
if flag == "r":
return a+v
if flag == "c":
return a + v[:,np.newaxis]
m = np.arange(1,21).reshape(4,5)
v = np.arange(5)
addVector(m, v, 'r')
v = np.arange(4)
addVector(m, v, 'c')
Checkerboard
Write a Python function to take a positive integer parameter n
and return an
n
×
n
n\times n
n×n matrix and fill it with a checkerboard pattern (check the example output).
method 1
def checkerBoard(n):
n1 = np.zeros((n,n))
for i in range(-n+1,n+1,2):
n2 = np.eye(n,n,k = i)
n1 = n1 + n2
return n1
method 2
def checkerBoard(n):
x=np.zeros((n,n))
for i in range(1,n,2):
y=np.eye(n,n,k=i)
z=np.eye(n,n,k=-i)
x+=y+z
return x
method 3
def checkerBoard(n):
arr=np.ones(shape=(n,n))
arr[::2,::2]=0
arr[1::2,1::2]=0
return arr
method 4
def checkerBoard(n):
arr = np.array([[0,1],[1,0]])
return np.tile(arr,(n,n))[:n,:n]
method 5
def checkerBoard(n):
t = (np.arange(n)%2).reshape(1,n)
t2 = np.transpose(t)
return np.logical_xor(t,t2)*1
method 6
def checkerBoard(n):
t = np.arange(n)%2
return t ^ t[:, np.newaxis]
checkerBoard(10)
checkerBoard(15)