0% found this document useful (0 votes)
10 views

Week 1 exercises-SOLN

Uploaded by

zyl178377
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Week 1 exercises-SOLN

Uploaded by

zyl178377
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Week 1 Exercises

• 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

Populating the interactive namespace from numpy and matplotlib

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]

rng = np.random.default_rng(0) # initialize the random number


generator
a = rng.integers(0, 100, 10)
print(a)

[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.

def dist(X, w):


return sqrt(sum( (X-w)**2,axis=1))

X = np.arange(12).reshape(3, 4)
w = np.array([0, 1, 0, 1])
dist(X, w)

array([ 2.82842712, 10.19803903, 18.11077028])


Exercise 7
• Write a function order_columns(a) which takes as an argument a 2-dimensional numpy
array and returns an array obtained by reordering columns of a according to the sum of
their entries: the first column is the one where the sum of entries is the smallest, the last
column is the one where the sum of entries is the largest.
def order_columns(a):
return a[:,argsort(sum(a,axis=0))]

rng = np.random.default_rng(1) # initialize the random number


generator
a = rng.integers(0, 6, (3,5))
a

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]

rng = np.random.default_rng(0) # initialize the random number


generator
a = rng.integers(0, 100, 10) # array of 10 random integers 0 <= n <
100
a

array([85, 63, 51, 26, 30, 4, 7, 1, 17, 81])

first_smaller(a,50)

26

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy