Exam 2
What is the output of the following code? arr = np.arange(-2,3) arr > 0 (arr > 0).sum()
2
What is the output of the following slicing operation? arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) arr3d arr3d[1, 0]
Correct output is array([7, 8, 9])
What is the output of arr after the following code is run? import numpy as np arr =np.arange(5) arr_slice = arr[-3:] arr_slice[0] = 123 arr
array([ 0, 1, 123, 3, 4])
what is the result of the following boolean indexing operation? names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe',]) data = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20],[21,22,23,24]]) mask = (names == 'Bob') | (names == 'Will') data[mask]
array([[ 1, 2, 3, 4], [ 9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]])
what does the following indexing code give? arr = np.arange(12).reshape((4, 3)) arr *output given* array([ [ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) arr[[1, 3]][:, [0, 1]]
array([[ 3, 4], [ 9, 10]])
What is the output of the following code? arr = np.array([[-1,1],[-2,2],[-3,3]]) np.where(arr > 0, 2, arr)
array([[-1, 2],[-2, 2],[-3, 2]])
what is the output of the following code? arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) arr2d[2:, :]
array([[7, 8, 9]])
What is the output of the following code? arr = np.array([[1., 2., 3.], [4., 5., 6.]]) arr2 = np.array([[0., 4., 1.], [7., 2., 12.]]) arr2 > arr
array([[False, True, False],[ True, False, True]])
What is the output of the following code? data2 = [[[1, 2, 3, 4], [5, 6, 7, 8]],[1,2,3]] arr2 = np.array(data2) arr2.ndim
1
What is the output of the following code? data = { 'state': ['Ohio', 'Ohio', 'Nevada'], 'year': [2000, 2001, 2001], 'pop': [1, 2, 3] } frame2 = pd.DataFrame(data, columns=['year', 'state', 'pop'], index=['one', 'two', 'three']) (frame2.pop == frame2['pop']).sum()
3
numpy arrays can hold mixed type of data like standard python lists but are much faster
False - standard python lists can hold mixed types. numpy arrays can only
If you want a copy of a slice of an numpy instead of a view, you will need to explicitly copy the array—for example, arr[5:8].copy().
If you want a copy of a slice of an numpy instead of a view, you will need to explicitly copy the array—for example, arr[5:8].copy().
A useful Series feature for many applications is that it automatically aligns by index label in arithmetic operations.
True
For arr2d, is the following statement True or False? arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) arr2d[0, 2] == arr2d[0][2]
True
The following operation is valid if arr is a numpy array having no zeros 1 / arr
True
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe']) np.______(names) array(['Bob', 'Joe', 'Will'], dtype='U4')
unique