quiz5_handoutLast Checkpoint: 2 minutes ago(autosaved)
Logout
Python 3
Trusted
•
•
•
•
•
•
•
•
File
Edit
View
Insert
Cell
Kernel
Widgets
Help
Run
CodeMarkdownRaw NBConvertHeadingName:
Instructions:
•
•
•
•
Run the setup cell
Answer each question in the provided empty cell.
Each answer should be only one Python line - plus an occasional print statement.
You are to use only NumPy functions.
Setup
In [ ]:
import numpy as np # standard import
np.set_printoptions(precision=3)
np.random.seed(23)
mat = np.random.ranf((5,6))
print(mat)
Q1
Print the second column of matrix mat created above
In [ ]:
Q2
Print the lower right 2x2 portion of the matrix mat
In [ ]:
Q3
Print the average of the entire matrix mat. Do not use for or while loops.
In [ ]:
Q4
Print the average of the columns of matrix mat. Do not use for or while loops.
In [ ]:
Answer questions 5-7 using numpy.
Do not use for or while loops.
Each of these steps should be one short Python line.
Hint: Explore the NumPy documentation to consider use of the function: "np.arange".
Q5
Create and print a matrix M of zeros that has 2 columns and 6 rows.
In [ ]:
Q6
Fill the first column with (0,1,2,3,4,5) and print M.
In [ ]:
Q7
𝑦=𝑥2−𝑥y=x2−x
𝑥x
The first column is associated with the value of
print M.
. Fill the second column with
and
In [ ]:
Q8
Use a single command to find all of the elements of mat that are greater than 0.7.
In [ ]:
Q9
Use the results from Q8, and in a single command print out the values of the elements in the matrix that
are above 0.7.
In [ ]:
Q10
Print the transpose of matrix mat
In [ ]:
Q11
Print the maximum value of mat
In [ ]:
Q12
Print the locattion of the maximum value of mat
In [ ]:
Hw5
Instructions:
•
•
Run the setup cell
Solve each exercise in the provided empty cell. If you wish you can insert additional cells.
Setup
In [4]:
import numpy as np # standard import
np.set_printoptions(precision=3)
np.random.seed(23)
E1 (5 pts)
Create a vector of 10,000,000 random elements using a function from numpy.random.
Compute the average of this vector using the old-fashioned, slow, for loop method. Show the code and the
result.
In [ ]:
E2 (2 pts)
Compute the average using a numpy function. Show the code and the result.
In [ ]:
E3 (8 pts)
The time.time() function will return the current time in terms of seconds.
In [12]:
import time
help(time.time)
Help on built-in function time in module time:
time(...)
time() -> floating point number
Return the current time in seconds since the Epoch.
Fractions of a second may be present if the system clock provides them.
Hence, it is possible to determine the computational time of a process by capturing the time before and
after the execution of the process.
Write code to compute the computational time for the previous two problems. Re-use vec from above:do
not include the creation of the random vector in your timing.
Compute the ratio of the time to compute the average using Problem 1 to the time it took to compute the
answer using the method in Problem 2. Show your codes, the times for each method, and the ratio.
In [ ]:
E4 (5 pts)
Use the numpy random seed value of 23423. Create three random vectors, each of length 5. These are
𝑎a 𝑏b
𝑐c
named
.
𝑑=𝑎+𝑏×𝑐d=a+b×c
Compute
,
,and
. Show your code and all four vectors.
In [ ]:
E5 (5 pts)
5×55×5
Create a
matrix of random values. Use the seed of 453. Using the random slicing technique, write
a Python command that retrieves values for the following locations in the
(1,3)(1,3) (2,2)(2,2) (3,1)(3,1)
matrix
,
,
.
In [ ]:
E6 (5 pts)
Recall the equation used for an object falling under the influence of gravity,
𝑦2=𝑦1+𝑣1𝑡+12𝑔𝑡2y2=y1+v1t+12gt2
600600
Start with a height of
m. Your task is to compute the height of the object for ten different
experiments. The experiments differ in the amount of time the object
𝑡=(1,2,3,4,5,6,7,8,9,10)t=(1,2,3,4,5,6,7,8,9,10)
𝑦2y2
drops:
experiments, use,
all in seconds. To compute the
values for all
𝑦 2=𝑦 1+12𝑔𝑡 2y→2=y→1+12gt→2
Once the data variables are assigned, this computation can be done in one line of Python. Show your code
and the results.
In [ ]:
E7 (5 pts)
Instantiate numpy arrays representing each of the following:
B=
[1324−3−1][12−334−1]
A=
212541−15−6
[25−114521−6]
y=
2−41
[2−41]
z=
−15−822
[−15−822]
In [ ]:
E8 (5 pts)
Provide numpy code that calculates and prints each of the quantities below.
𝐵𝐴BA
•
𝐴𝐵𝑇ABT
•
𝐴𝑦Ay
•
𝑦𝑇𝑧yTz
•
This is the inner product, or dot product, of y and z
𝑦𝑧𝑇yzT
•
This is the outer product of y an z
In [ ]:
Purchase answer to see full
attachment