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
We provide professional writing services to help you score straight A’s by submitting custom written assignments that mirror your guidelines.
Get result-oriented writing and never worry about grades anymore. We follow the highest quality standards to make sure that you get perfect assignments.
Our writers have experience in dealing with papers of every educational level. You can surely rely on the expertise of our qualified professionals.
Your deadline is our threshold for success and we take it very seriously. We make sure you receive your papers before your predefined time.
Someone from our customer support team is always here to respond to your questions. So, hit us up if you have got any ambiguity or concern.
Sit back and relax while we help you out with writing your papers. We have an ultimate policy for keeping your personal and order-related details a secret.
We assure you that your document will be thoroughly checked for plagiarism and grammatical errors as we use highly authentic and licit sources.
Still reluctant about placing an order? Our 100% Moneyback Guarantee backs you up on rare occasions where you aren’t satisfied with the writing.
You don’t have to wait for an update for hours; you can track the progress of your order any time you want. We share the status after each step.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
From brainstorming your paper's outline to perfecting its grammar, we perform every step carefully to make your paper worthy of A grade.
Hire your preferred writer anytime. Simply specify if you want your preferred expert to write your paper and we’ll make that happen.
Get an elaborate and authentic grammar check report with your work to have the grammar goodness sealed in your document.
You can purchase this feature if you want our writers to sum up your paper in the form of a concise and well-articulated summary.
You don’t have to worry about plagiarism anymore. Get a plagiarism report to certify the uniqueness of your work.
Join us for the best experience while seeking writing assistance in your college life. A good grade is all you need to boost up your academic excellence and we are all about it.
We create perfect papers according to the guidelines.
We seamlessly edit out errors from your papers.
We thoroughly read your final draft to identify errors.
Work with ultimate peace of mind because we ensure that your academic work is our responsibility and your grades are a top concern for us!
Dedication. Quality. Commitment. Punctuality
Here is what we have achieved so far. These numbers are evidence that we go the extra mile to make your college journey successful.
We have the most intuitive and minimalistic process so that you can easily place an order. Just follow a few steps to unlock success.
We understand your guidelines first before delivering any writing service. You can discuss your writing needs and we will have them evaluated by our dedicated team.
We write your papers in a standardized way. We complete your work in such a way that it turns out to be a perfect description of your guidelines.
We promise you excellent grades and academic excellence that you always longed for. Our writers stay in touch with you via email.