PYTHON
Attached below
CSCI333.01W Assignment 09
Array-Oriented Programming with NumPy
100 points
Deadline: 3/30/
2
020 Tue. by 11:59pm
1. (20 points, each 2 points) True or False questions:
1) (True/False) The NumPy library provides the ndarray data structure (synonym “array”), which is typically much faster than Python lists.
Answer:
2) (True/False) Function array() creates an array from an array or other iterables.
Answer:
3)(True/False) NumPy array can contain different data types in one array.
Answer:
4) (True/False) When one of the operands of an array operator is a scalar, NumPy uses broadcasting to perform the calculation as if the scalar were an array of the same shape as the other operand, containing the scalar value in all its elements.
Answer:
5) (True/False) The array method copy() returns a new array that is a view (shallow copy of the original array.
Answer:
6) (True/False) NumPy ravel() function flattens arrays to 1D and returns view/shallow copy of the original array.
Answer:
7) (True/False) When using reshape() function to change the shape of an array, the new shape does not have to have the same number of elements as the original array.
Answer:
8) (True/False) Compare to Python lists, NumPy array is faster, occupies less memory, and more convenient to use.
Answer:
9) (True/False) The dimension of arrays cannot be changed.
Answer:
10) (True/False) NumPy array elements can be iterated using a for loop.
Answer:
2. (20 points) Multiple choice or Fill in blank questions:
1) (6 points) arr1 = numpy.array([[1,2,3],[4,5,6]]), which of the following create(s) a deep copy, which create(s) a view(shallow) copy, which do(es) not create a copy (has the same id() with the original), list the following a) – f) cases to the corresponding category:
a) arr2 = arr1
b) arr2 = arr1.copy()
c) arr2 = arr1.view()
d) arr2 = arr1.flatten()
e) arr2 = arr1.ravel()
f) arr2 = arr1.T
Answer:
Deep copy:
View (Shallow) copy:
No copy:
2) (2 points) Assume arr = numpy.array([[1,1,5], [2,5,8], [4,12,25], [13,27,30]]), what is the result for print(arr.shape)?
a) (4, 3)
b) (3, 4)
c) (2, 3)
d) (3, 2)
Answer:
3) (2 Points) The position indexes of the elements in NumPy arrays starts with ___
a) 1
b) 0
Answer:
4) (3 points) Assume arr = numpy.array([[1,1,5], [2,5,8], [4,12,25], [13,27,30]]), you want to replace the value 12 to 100, please write statement(s) to change the value:
Answer:
5) (3 points) Assume arr = numpy.array([1,2,3,4,5,6,7,8]), we want to change the dimension of the arr to (2, 2, 2), write statement(s) to change it:
Answer:
6) (4 points) Write statements to create and output 10 evenly spaced numbers for x in 2 by 5 array: 0<= x <= 1
Answer:
3. (25 points) Hand-trace the following code. What is the output, or what error/problem do you observe and why?
1) (5 points)
# create a 2D array from existing data
import numpy as np |
||||
Output: |
2) (5 points)
#Display the number of dimensions and shape of the array
import numpy as np |
3) (5 points)
#Create a 1D array from a list comprehension that produces even integers from 2 through 20
import numpy as np |
4) (5 points)
#use function arange() to create a 1D array and reshape it into a 2D 3-by-4 array
import numpy as np |
5) (5 points)
# create an array of the values from 1 through 5, then use broadcasting to square each value:
import numpy as np |
4. (20 points) Write a program to create an array containing the values 1-15, reshape it into a 3-by-5 array (3 rows, 5 columns), then use indexing and slicing techniques to perform each of the following operations, and print each selection:
a) Select row 0
b) Select column 4
c) Select rows 0 and 1
d) Select columns 2-4
e) Select the element that is in row 1 and column 4
f) Select all elements from rows 1 and 2 that are in columns 0, 2, and 4
(Hint: Rows and Columns of NumPy array start from 0)
Answers:
(18 points, for each sub-question, 2 points for code, 1 point for result)
Write your program here, or copy/paste a screenshot of your Program, as well as your output results:
(2 points) Save the program as “program1.py”. Upload the .py file as part of your submission.
5. (15 points) Write a program,
1) use numpy arange() and reshape() to create, and print the following array:
array([[ 1, 2, 3],
[ 4, 5, 6]])
2) use numpy hstack(), vstack() to create, and print the following array:
array([[ 1, 2, 3, 1, 2, 3],
[ 4, 5, 6, 4, 5, 6],
[ 1, 2, 3, 1, 2, 3],
[ 4, 5, 6, 4, 5, 6]])
3) Multiply the resulting array from step 2) by itself. Print the array.
4) Concatenate resulting arrays from step 2) and step 3), axis = 0. Print the array.
Answers:
(10 points) Write your program here, or copy/paste a screenshot of your Program:
(3 points) Screenshot of the outputs:
(2 points) Save the program as “program2.py”. Upload the .py file as part of your submission.
6. (20 points)
1) (10 points) Write a program to create a 3 by 3 array containing the even numbers from 2 through 18. Create a second 3 by 3 array containing integers from 9 down to 1, then multiply the first array by the second. Show the result.
2) (10 Points) Write a program to create a 2 by 3 array (assume 2 by 3 array name is arr), it contains values of the first six powers of 2, like . Flatten arr array first with method flatten(), then flatten arr with ravel(). In each case, display the resulting arrays then display the original arr array to show that the original array’s shape was unmodified by flatten() or ravel().
(hint: To create the values of the array, one way is to use numpy.arange() to create an array, then use numpy power function to get the “2 to the power of” each array element. Search and review this link (
https://docs.scipy.org/doc/numpy/reference/generated/numpy.power.html
) learn how to use numpy power function)
Answers for 1)
(6 points) Write your program here, or copy/paste a screenshot of your Program:
(2 points) Output result:
(2 point) Save the program as “bonus1.py”. Upload the .py file as part of your submission.
Answers for 2)
(6 points) Write your program here, or copy/paste a screenshot of your Program:
(2 points) Output result:
(2 points) Save the program as “bonus2.py”. Upload the .py file as part of your submission
2