Obtain the GitHub repository you will use to complete the homework
assignment, which contains the starter Jupyter notebook file
homework1.ipynb
. The notebook template provides space for you to
answer each question. Your notebook should run without error when you
select Restart Kernel and Run All
Cells:
When you’re done, save your file, then stage, commit, and push (upload) it to GitHub, and then follow the instructions in the How to submit section.
-
Create variables that are assigned the following values:
-
A list called
ages
with values 19, 21, 21, and 20. -
A list called
names
with three values “Ruth,” “Callixte,” and “Talishia.”
-
-
Define a function named
sine_compute
that takesx
as input and returns the value of the mathematical expression . Evaluatesine_compute
when . -
Assign 1 to a variable named
d
. Then, in a loop that executes 10 times, update the value ofd
to be double what it was before the previous iteration. Before executing the loop, determine the final value by hand so you can check your work (write this out in a Markdown block). Then test your code, and typeprint(d)
to display d’s final value to check your work. -
The following code computes a distance and a time for several steps in a loop and prints the results:
import math # only needed if math hasn't been imported yet dist = 0 for i in range(1, 8): dist += 2.25 time = (24.5 - math.sqrt(600.25 - 19.6 * dist)) / 9.8 print("For distance {0}, time = {1}".format(dist, time))
Copy this code into your notebook and run it to see the output. Next, modify the code so that you do not need to initialize
dist
withdist = 0
, but will still get the same results. -
Define a function called
ln_compute
that returns the value of the mathematical expression . Then, write a loop that creates a variablek
that takes on integer values from 1 through 8. Inside the loop, print the value ofk
andln_compute(k)
in the style of theprint()
statement used in question 4. -
Create the following
numpy
arrays and assign them each to a unique variable. Only the first exercise can be created manually, the rest must use function(s) to generate the sequence (in some instances, you may need to generate sequences individually and join them afterwards).-
Manually create a
numpy
array with the numbers 47, 35, 22, and 10. -
Generate a
numpy
array with the numbers 1 through 12. -
Generate a
numpy
array with the numbers 4, 8, 12, 16, …, 84. -
Generate a
numpy
array with eleven 3’s followed by eleven 4’s followed by twelve 5’s. -
Generate a
numpy
array with the numbers 7, 6, 5, …, 1, 19, 2, 4, 6, 8, …, 30.
-
-
The code snippet below generates the
temperatures
matrix:import numpy as np # only if numpy hasn't been imported t = [57, 64, 88, 81, 61, 88, 86, 76, 63, 89, 70, 76] temperatures = np.reshape(t, newshape=(3, 4))
Slice the
numpy
array to return each of the following. Remember, Python starts indexing from0
, meaning row 1 is index 0!-
The element at row 3, column 3.
-
The element at row 2, column 1.
-
The entire 3rd row.
-
The entire 2nd column.
-
Columns 2 through 4 of rows 1 and 3.
-
-
The code snippet below generates the
air_pressure
matrix:import numpy as np # only if numpy hasn't been imported air_pressure = np.ones((5, 5, 3)) air_pressure[:, :, 1] = 0.99 air_pressure[:, :, 2] = 0.98
“Height” is what we call the third index. Slice the
numpy
array to return each of the following. Remember, Python starts indexing from0
, meaning row 1 is index 0!-
The element at row 3, column 3 at height 3.
-
The element at row 4, column 2 at height 1.
-
The entire 3rd row for all columns and heights.
-
All rows and columns for the lowest height.
-
All heights for row 4, columns 2 through 5.
-
-
Perform the following vector operations
-
With one assignment statement, make
my_matrix
be a 2-by-4 matrix of all zeros. -
With one assignment statement, make the first row of
my_matrix
be the sequence of positive integers 1, 3, 5, 7. -
Return the product of 3 by every element of
my_matrix
without changingmy_matrix
. -
Return the square root of every element of
my_matrix
without changingmy_matrix
(hint,numpy
has its own version of square root). -
Add 2 to every element of
my_matrix
, changing the value ofmy_matrix
to hold those increased numbers.
-
-
Use list comprehensions to a “list of lists” named
pairs_list
containing a column of x-values, which are positive integers from 1 through 9, and a second column with values of , where the is taken from the first column. -
Create a new file named
rectangle.py
in the same directory as your Jupyter notebook. In this file, define a function calledcircumference
that returns the circumference of a rectangle with parameters for length and width,l
andw
, respectively. Save the file. Then, in your Jupyter notebook, import the file you created and test the function you just defined by having it return the circumference of a rectangle with dimensions 3 and 4.2, respectively. -
Compute the following matrix-vector exercises using
numpy
:-
Generate a 4-by-2 matrix
mA
, where thei - j
element is the sum ofi
andj
. For example, after formingmA
,mA[3, 2]
should be 5, which is 3 + 2. -
Generate a 4-by-2 matrix
mO
of all ones. -
Give matrix sum of
mA
andmO
. -
Define a one-dimensional array
u
with elements 2 and 7. -
Define a one-dimensional array
v
with elements 5 and 3. -
Compute the dot product of
u
andv
. -
Generate a 2-by-3 matrix
mB
, where thei - j
element is the difference ofi
andj
,i - j
. -
Give the matrix product of
mA
andmB
.
-
-
Use matplotlib to plot the function from -3 to 3, where is stepped through in increments of 0.1. Label the x axis as
t
and the y axis as . -
Load the provided
iris.csv
file into Pandas and write code that reproduces the following plot:Hint: For some relevant examples, take a look at https://scipython.com/book/chapter-7-matplotlib/examples/.
-
Load the provided
gapminder_all.csv
file into Pandas. Determine which countries saw the largest increase and the largest decrease in life expectancy between 1987 and 1992.
To lock in your submission time, export your notebook to PDF and upload the PDF file to the assignment posting on Blackboard.
In addition, be sure to save, commit, and push your final result so that everything is synchronized to GitHub. I may want to inspect your source files directly and run your notebook, so it’s very important that the files in your homework repository match what I see in the PDF export uploaded to Blackboard.