Week 1 exercises-SOLN
Week 1 exercises-SOLN
• The exercise below should be completed using tools provided by numpy arrays.
• Do not use for or while loops, list comprehensions etc.
• The code in each case can be written in fewer than 10 lines.
#import numpy as np
%pylab inline
Exercise 1
• Write a function border(a) which takes as an argument an 2-dimensional numpy array a
and returns an array obtained by adding a border of zeros to a.
def border(a):
b = zeros( array(shape(a)) + [2,2], dtype=a.dtype )
#b = zeros( array(shape(a)) + [2,2])
b[1:-1,1:-1] = a
return b
a = np.arange(6).reshape(3, 2)
a
array([[0, 1],
[2, 3],
[4, 5]])
border(a)
array([[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 2, 3, 0],
[0, 4, 5, 0],
[0, 0, 0, 0]])
Exercise 2
• Write a function row_split(a, k) which for a given 2-dimensional numpy array a and a
positive integer k returns a tuple of two 2-dimensional numpy arrays: one consisting of
the first k rows of a and the second consisting of the remaining rows.
def row_split(a,k):
b1 = a[:k,:].copy()
b2 = a[k:,:].copy()
return b1,b2
row_split(a,2)
(array([[0, 1],
[2, 3]]),
array([[4, 5]]))
Exercise 3
• Write a function n_smallest(a, n) which given a 1-dimensional numpy array a and an
integer n returns an array with indices of n smallest elements of a
def n_smallest(a, n):
b = argsort(a)
return b[:n]
[85 63 51 26 30 4 7 1 17 81]
n_smallest(a, 3)
array([7, 5, 6])
Exercise 4
• Write function checkers(n) which returns an n × n numpy array consisting of zeros and
ones arranged in a checkerboard pattern. To check that this function works correctly, test
it for both odd and even values of n .
def checkers(n):
x = zeros(8,dtype=bool)
for i in range(int(8/2)):
x[2*i] =1
return (x==x.reshape(n,1))*1
n=8
checkers(n)
array([[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1]])
Exercise 5
• Write a function sub_table(n) which returns an numpy array with the subtraction table of
numbers from 0 to n −1 . The value in the row i and column j of the array should be the
number i− j .
def sub_table(n):
x = arange(n)
return x.reshape(n,1) - x
sub_table(11)
array([[ 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10],
[ 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9],
[ 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8],
[ 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7],
[ 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6],
[ 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5],
[ 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4],
[ 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3],
[ 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2],
[ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1],
[ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]])
Exercise 6
• If ${\bf v}$ and ${\bf w}$ are two vectors with n coordinates, then their Euclidean
distance is given by the formula:
$$d({\bf v},{\bf w}) = \sqrt{\sum_{i=1}^n (v_i -w_i)^2}$$
• Let X be an m ×n numpy array, and let ${\bf w}$ be a 1-dimensional array with n
entries. Write a function dist(X, w) which returns a 1-dimensional array with m
entries, such that the i -th entry is the distance between ${\bf w}$ and the i-th row of
X.
X = np.arange(12).reshape(3, 4)
w = np.array([0, 1, 0, 1])
dist(X, w)
array([[2, 3, 4, 5, 0],
[0, 4, 5, 1, 1],
[5, 2, 1, 4, 1]])
order_columns(a)
array([[0, 2, 3, 4, 5],
[1, 0, 4, 5, 1],
[1, 5, 2, 1, 4]])
Exercise 8
• Write the function first_smaller(a, n) which takes as its argument a 1-dimensional numpy
array a and a number n and returns the first entry of a which is smaller than n . If such
entry does not exist the function should return None.
def first_smaller(a,n):
return a[a<n][0]
first_smaller(a,50)
26