TODO 9 Using the dummy_array given below, complete the following indexing/slicing TODOs. Index/slice the first (index 0), third (index 2), and last (index 4 or -1) columns of the array dummy_array. Store the output into the variable column_slice. Hint: The output shape should be a (2, 3)! Index ONLY the first row (think about the corresponding index value) of dummy_array. Store the output into the variable row_slice. Index ONLY the second row and first two columns of dummy_array. Store the output into the variable slice_array. dummy_array = np.arange(10).reshape(2, -1) print(f"dummy_array output: \n {dummy_array}") print(f"dummy_array shape: {dummy_array.shape}") # TODO 9.1 column_slice = print(f"column_slice output: \n {column_slice}") print(f"column_slice shape: {column_slice.shape}") todo_check([ (column_slice.shape == (2, 3), 'column_slice did not return the correct shape of (2, 3)'), (np.all(column_slice == np.array([[0, 2, 4], [5, 7, 9]])), 'column_slice did not return the correct values!') ]) # TODO 9.2 row_slice = print(f"row_slice output: \n {row_slice}") print(f"row_slice shape: {row_slice.shape}") todo_check([ (row_slice.shape == (5,) or row_slice.shape == (1,5), 'row_slice does not have the correct shape of (5,) or (1, 5)' ), (np.all(row_slice == np.array([0, 1, 2, 3, 4])), 'row_slice does not contain the correct values') ]) # TODO 9.3 slice_array = print(f"slice_array output: \n {slice_array}") print(f"slice_array shape: {slice_array.shape}") todo_check([ (slice_array.shape == (2,) or slice_array.shape == (1, 2), 'slice_array does not have the correct shape of (2,) or (1, 2)'), (np.all(slice_array == np.array([5, 6])), 'slice_array does not contain the correct values') ])
TODO 9
Using the dummy_array given below, complete the following indexing/slicing TODOs.
- Index/slice the first (index 0), third (index 2), and last (index 4 or -1) columns of the array dummy_array. Store the output into the variable column_slice.
- Hint: The output shape should be a (2, 3)!
- Index ONLY the first row (think about the corresponding index value) of dummy_array. Store the output into the variable row_slice.
- Index ONLY the second row and first two columns of dummy_array. Store the output into the variable slice_array.
dummy_array = np.arange(10).reshape(2, -1)
print(f"dummy_array output: \n {dummy_array}")
print(f"dummy_array shape: {dummy_array.shape}")
# TODO 9.1
column_slice =
print(f"column_slice output: \n {column_slice}")
print(f"column_slice shape: {column_slice.shape}")
todo_check([
(column_slice.shape == (2, 3), 'column_slice did not return the correct shape of (2, 3)'),
(np.all(column_slice == np.array([[0, 2, 4], [5, 7, 9]])), 'column_slice did not return the correct values!')
])
# TODO 9.2
row_slice =
print(f"row_slice output: \n {row_slice}")
print(f"row_slice shape: {row_slice.shape}")
todo_check([
(row_slice.shape == (5,) or row_slice.shape == (1,5), 'row_slice does not have the correct shape of (5,) or (1, 5)' ),
(np.all(row_slice == np.array([0, 1, 2, 3, 4])), 'row_slice does not contain the correct values')
])
# TODO 9.3
slice_array =
print(f"slice_array output: \n {slice_array}")
print(f"slice_array shape: {slice_array.shape}")
todo_check([
(slice_array.shape == (2,) or slice_array.shape == (1, 2), 'slice_array does not have the correct shape of (2,) or (1, 2)'),
(np.all(slice_array == np.array([5, 6])), 'slice_array does not contain the correct values')
])
Please refer to the following steps for the complete solution to the problem above.
Step by step
Solved in 3 steps with 1 images