Q3

In this question, we'll explore the basics of using NumPy arrays.

A

NumPy arrays are wonderful improvements over native Python lists for many reasons, the biggest of which is its ability to perform "vectorized" operations over entire arrays without having to write loops.

Write a function, difference, which takes two NumPy arrays as arguments and returns their difference (in order of the arguments themselves; if you're getting an AssertionError, try flipping the ordering of the arguments in your function).

  • takes two arguments: both NumPy arrays of floats
  • returns 1 floating point number: the difference of the two vectors (second array from first array).

You will need to check if the arrays are the same length; if not, raise a ValueError.

You cannot use any loops, built-in functions, or NumPy functions.


In [ ]:


In [ ]:
import numpy as np

np.random.seed(578435)
x11 = np.random.random(10)
x12 = np.random.random(10)
d1 = np.array([ 0.24542374,  0.19098998,  0.20645088,  0.49097139, -0.56594091,
       -0.13363814,  0.46859546, -0.32476466, -0.35938731,  0.17459786])
np.testing.assert_allclose(d1, difference(x11, x12))

np.random.seed(85743)
x21 = np.random.random(20)
x22 = np.random.random(20)
d2 = np.array([-0.17964925, -0.57573602,  0.00109792, -0.06535934,  0.51321497,
        0.63854404, -0.17318834,  0.05553455,  0.08780665, -0.12503945,
        0.08794238, -0.53157235, -0.1133253 ,  0.34861933,  0.67987286,
        0.01188672,  0.2099561 , -0.40800005, -0.28166673, -0.35814679])
np.testing.assert_allclose(d2, difference(x21, x22))

try:
    difference(np.array([1, 2, 3]), np.array([4, 5, 6, 7]))
except ValueError:
    assert True
else:
    assert False

B

Write a function, squares, which takes a NumPy array and returns another NumPy array with all its elements squared.

  • takes 1 argument: a NumPy array
  • returns 1 value: a NumPy array where each element is the squared version of the input array

You cannot use any loops, built-in functions, or NumPy functions.


In [ ]:


In [ ]:
import numpy as np

np.random.seed(13735)
x1 = np.random.random(10)
y1 = np.array([ 0.10729775,  0.01234453,  0.37878359,  0.12131263,  0.89916465,
        0.50676134,  0.9927178 ,  0.20673811,  0.88873398,  0.09033156])
np.testing.assert_allclose(y1, squares(x1))

np.random.seed(7853)
x2 = np.random.random(35)
y2 = np.array([  7.70558043e-02,   1.85146792e-01,   6.98666869e-01,
         9.93510847e-02,   1.94026134e-01,   8.43335268e-02,
         1.84097846e-04,   3.74604155e-03,   7.52840504e-03,
         9.34739871e-01,   3.15736597e-01,   6.73512540e-02,
         9.61011706e-02,   7.99394100e-01,   2.18175433e-01,
         4.87808337e-01,   5.36032332e-01,   3.26047002e-01,
         8.86429452e-02,   5.66360150e-01,   9.06164054e-01,
         1.73105310e-01,   5.02681242e-01,   3.07929118e-01,
         7.08507520e-01,   4.95455022e-02,   9.89891434e-02,
         8.94874125e-02,   4.56261817e-01,   9.46454001e-01,
         2.62274636e-01,   1.79655411e-01,   3.81695141e-01,
         5.66890651e-01,   8.03936029e-01])
np.testing.assert_allclose(y2, squares(x2))

C

Write a function, sum_of_elements, which sums up all the elements of a NumPy array and returns it.

  • takes 1 argument: a NumPy array
  • returns 1 floating-point value: the sum of the elements in the NumPy array

You cannot use any loops, but you can use the numpy.sum function.


In [ ]:


In [ ]:
import numpy as np

np.random.seed(7631)
x1 = np.random.random(483)
s1 = 233.48919473752667
np.testing.assert_allclose(s1, sum_of_elements(x1))

np.random.seed(13275)
x2 = np.random.random(23)
s2 = 12.146235770777777
np.testing.assert_allclose(s2, sum_of_elements(x2))

D

You may not have realized it yet, but in the previous three parts, you've implemented almost all of what's needed to compute the distance between two NumPy arrays. All you have to do now is link the code you wrote in the previous three parts together in the right order.

Write a function distance which takes two NumPy arrays and computes their distance.

  • takes 2 arguments: both NumPy arrays of the same length
  • returns 1 number: a non-zero floating point value that is the distance between the two arrays

Remember how Euclidean distance $d$ between two vectors $\vec{a}$ and $\vec{b}$ is calculated:

$$ d(\vec{a}, \vec{b}) = \sqrt{(a_1 - b_1)^2 + (a_2 - b_2)^2 + ... + (a_n - b_n) ^2} $$

where $a_1$ and $b_1$ are the first elements of the arrays $\vec{a}$ and $\vec{b}$; $a_2$ and $b_2$ are the second elements, and so on.

You've already implemented everything except the square root; in addition to that, you just need to arrange the functions you've written in the correct order inside your distance function.

You cannot use any functions aside from those you've already written.


In [ ]:


In [ ]:
import numpy as np
import numpy.linalg as nla

np.random.seed(477582)
x11 = np.random.random(10)
x12 = np.random.random(10)
np.testing.assert_allclose(nla.norm(x11 - x12), distance(x11, x12))

np.random.seed(54782)
x21 = np.random.random(584)
x22 = np.random.random(584)
np.testing.assert_allclose(nla.norm(x21 - x22), distance(x21, x22))

In [ ]: