Homework 11 - NumPy
Due Date: December 12, 2023
Problems
- Explain the purpose of the following attributes of a NumPy array:
dtype
,ndim
,shape
,size
. - Describe at least three NumPy methods to create commonly-used arrays and use each one on an example.
- Write a function that takes as a parameter a NumPy array (which could be 1-D or 2-D) and returns the number of entries in the input array that are negative. Do not use any type of loop (no while or for loops). Apply your function to a 1-D and a 2-D sample array.
- The dot product of two vectors \(x=(x_1,x_2,\ldots,x_n)\) and \(y=(y_1,y_2,\ldots,y_n)\), which is usually denoted as \(x \bullet y\), is the number
\[
x\bullet y = x_1 y_1 + x_2y_2 + \cdots + x_n y_n.
\]
Thus, if \(x=(-3,0,-1,2)\) and \(y=(1,4,2,-1)\) then
\[
x\bullet y = (-3)(1) + (0)(4)+(-1)(2) + (2)(-1) = -7.
\]
Write a function called
dot_product
that takes as input two 1-D NumPy arrays, say \(x\) and \(y\), and returns the value of the dot product \(x\bullet y\). Your function should verify that the input arrays are NumPy arrays and should returnNone
and print an error message if one of the arrays fails the validation. Your function should not use any type of loop (no while or for loops). Try your function with the following NumPy arrays:x = np.array([-3, 1, 2, 5, 8, -1, 0]) y = np.array([1, -2, -2, 1, 0, 4, 8])
- Create a function that takes four parameters, say \(n\), \(a\), \(b\), and \(c\), and returns a \(n\times n\) NumPy array, say \(M\), such that the diagonal entries of \(M\) are all equal to \(a\), the entries of \(M\) above the diagonal are equal to \(b\), and the entries of \(M\) below the diagonal are equal to \(c\). For example, a call to your function with the arguments \(n=5\), \(a=4\), \(b=-7\), and \(c=2\) would return the array \[ \begin{bmatrix} 4 & -7 & -7 & -7 & -7 \\ 2 & 4 & -7 & -7 & -7 \\ 2 & 2 & 4 & -7 & -7 \\ 2 & 2 & 2 & 4 & -7 \\ 2 & 2 & 2 & 2 & 4 \end{bmatrix}. \]
Consider the following list of tuples of names that represents friends in a social network:
[('ron', 'tim') , ('bob', 'anna'), ('anna', 'tim'), ('tom', 'kate'), ('anna', 'tom'), ('kate', 'ron') ]
From the list above, we gather that Ron and Tim are friends, Bob and Anna are friends, Anna and Tim are friends, and so on. A way to represent this social network is to use a matrix. To create the matrix, we first need to decide on an ordering of the individuals. For the small social network above, we could set
0 = ron, 1 = tim, 2 = bob, 3 = anna, 4 = tom, 5 = kateTherefore, using integers instead of names, the above social network is
[(0, 1) , (2, 3), (3, 1), (4, 5), (3, 4), (5, 0) ]
Since there are \(n=6\) individuals, we then create a \(6 \times 6\) matrix, call it \(A\), such that if \(i\) and \(j\) are friends then \(A[i, j] =1\) and \(A[j,i]=1\) but if \(i\) and \(j\) are not friends then \(A[i,j] = A[j,i]=0\). Thus, for the above social network the matrix \(A\) is
\[ A = \begin{bmatrix} 0 & 1 & 0 & 0 & 0 & 1 \\ 1 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 1 & 1 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 & 0 & 1 \\ 1 & 0 & 0 & 0 & 1 & 0 \\ \end{bmatrix} \]Create a function called
network_to_matrix
that takes as input a list of 2-element tuples and returns the matrix \(A\) as described above. Apply your function to the following list:[('anna', 'bob'), ('anna', 'bill'), ('ian', 'anna'), ('tim', 'bob'), ('bill', 'tim'), ('ian', 'tim'), ('bill', 'bob'), ('ian', 'bob'), ('ian', 'bill'), ('dawn', 'ian'), ('kate', 'ian')]
Given two 1-D arrays \(x=(x_1,x_2,\ldots,x_n)\) and \(y=(y_1,y_2,\ldots,y_n)\), let us say that \(x\) and \(y\) are equivalent if after sorting \(x\) and \(y\) we obtain the same array. For example, \(x=(-1,1,2,1)\) and \(y=(2,1,1,-1)\) are equivalent because after sorting \(x\) and \(y\) we obtain the array \((-1,1,1,2)\). Write a function that takes as input two NumPy arrays and returns
True
if the input arrays are equivalent andFalse
otherwise. Do not use any for or while loops, and only use NumPy built-in methods.