Part 1 - Working with heights of the US presidents
Execute the cell below to load a NumPy array that contains the heights of the US presidents.
Write one or more statements in the cell below that prints out the heights of the first five presidents.
Write one or more statements in the cell below to print out:
the smallest height
the average height
the maximum height in the
heights
array.
The data in the file doesn't include Presidents Trump (190 cm) and Biden (182 cm). Write one or more statements in the cell below that adds these two values to the end of heights
.
Write one or more statements in the cells below that prints out the heights of the last 10 presidents, which should now include Presidents Trump and Biden.
Write one or more statements in the cell below to create a numpy array named president_heights
. The number of rows in president_heights
should be the same as the number of rows in heights
, and there should be 4 columns. Each value in president_heights
should be initialized to the integer value zero.
Write one or more statements in the cell below to set the values in the first column of president_heights
to be the same as the values in heights
.
Write one or more statements in the cell below to set the values in the second column of president_heights
to be the height of each president in inches. Use the conversion factor 1 cm = 0.393701 in. Store the values in heights
as ints.
Write one or more statements in the cell below to set the values in the third column of president_heights
to be the number of feet for each president, as an integer.
Finally, write one or more statements to set the values in the fourth column of president_heights
to be the number of inches for each president. Use the % operator to get the remainder of dividing the number of inches by 12.
Execute the cell below to print out all the values in the president_heights
array.
Part 2 - working with the 2015-2019 population data
Write one or more statements in the cell below that loads the contents of the JSON file fips_codes.json into a variable named fips_codes_original
.
Write a statement in the cell below that uses a dictionary comprehension to create a dictionary named fips_codes
from the values in the fips_codes_original
dictionary.
The new dictionary's keys should be the result of converting the keys in
fips_codes_original
from strings to intsThe new dictionary's values should be the same as they were in
fips_codes_original
For example, suppose fips_codes_original
contains the keys '01' and '02' with values ['Alabama', 'AL'] and ['Alaska', 'AK']. Then fips_codes
should contain the integer keys 1 and 2 with the same values.
Write 2 statements in the cell below that print out the values in fips_codes
for the keys 1 and 2.
In the cell below, define a function named print_fips_info
that takes 2 arguments:
A dictionary containing named
codes
containing the FIPS codesAn integer named
code
that is a FIPS code for a state
The function should print out the FIPS code, full state name, and abbreviation corresponding to the value of code
. The output should look like exactly like this
Do not hard-code the values 26, Michigan and MI. Instead obtain them from the codes
dictionary using the value of the code
argument.
Write a statement in the cell below that calls the print_fips_info
function, passing fips_codes
and 26
as the values for the arguments.
Write a statement in the cell below that loads the saved NumPy array in the file state_pops_with_region.npy, storing it into an array named pops
. You can reference page 117 (section 4.4) from the course textbook to find information on how to load a saved NumPy array.
Write one or more statements in the cell below that prints out the size of the pops
array, in the format:
where R and C are the actual number of rows and columns.
Write one or more statements in the cell below that prints out contents of the 50th row in pops
, like this:
Use the fips_codes
dictionary to obtain the name of the state. Do not hard-code the year or population in the print statement.
Write one or more statements in the cell below that create an array named midwest_2019
containing the codes and populations for all of the states in the Midwest region in the year 2019. The region number is contained in the 4th column of pops
, and the value for states in the Midwest region is 1. Do not use a for
loop.
Print the value of midwest_2019
after computing it. The output should look like this:
Write code in the cell below that creates and prints a NumPy array named midwest_codes_2019
containing just the FIPS codes for the data in midwest_2019
.
Write one or more statements in the cell below that uses a list comprehension to create a list named midwest_names_2019
containing the full names of the states contained in midwest_codes_2019
. You can get the names from the fips_codes
dictionary. Print the value of midwest_names_2019
.
Study the examples of using argsort
found on pages 478-479 of the textbook carefully. Then write one or more statements in the cell below to print the FIPS code and population data in the midwest_2019
array, sorted in ascending order by population. To do so,
Apply
argsort
to the population column ofmidwest_2019
only to obtain an array of indexes namedsort_indexes
Use
sort_indexes
as an indexer to "rearrange" the rows ofmidwest_2019
, storing the result insorted_by_population
Print just columns 1 and 2 of
sorted_by_population
.
The result should be
Write code in the cell below that prints out the state names and populations for the states in the Midwest region, sorted in descending order by population. Since NumPy arrays can only contain a single data type, I used a list comprehension to create a list of tuples containing (state name, population) pairs from the data in the midwest_2019
array. Then I iterated over that list of tuples to produce the output, which should look like this:
Write one or more statements in the cell below to compute and print out the total population of the Midwest region in 2019 in millions. The output should look like this:
Part 3 - Image processing
As we saw in class, a digital photograph is a 3 dimensional array. The first two dimensions represent the rows and columns of the image, while the 3rd dimension contains color information for each pixel in the image. In color images, the color information contains 3 values, corresponding to intensities of red, green, and blue. In the last part of this assignment, you'll perform some image manipulation operations.
Get started by executing the cell below to import the PIL library and read in an image. PIL stands for Python Imaging Library.
Now write a statement that uses the NumPy copy function to create a copy of the array named img
, storing the result in an array named extra_blue
.
Write statements in the cell below that
Sets the blue (3rd) component of all of the pixels in
extra_blue
to 255.Uses the
Image.fromarray
method to create a newImage
namedextra_blue_img
from theextra_blue
array.Uses the
display
function to displayextra_blue_img
.
Now, in the cell below do something similar as you did for extra_blue
to create a copy of img
named blue_removed
. blue_removed
should have the blue component set to 0. Display the resulting image after creating it.
In the cell below:
Create a copy of
img
namedgray
Use
np.mean
with an appropriateaxis=
argument to compute the average of the RGB values for each pixel, storing the result in an array namedaverage
(what will the shape ofaverage
be?) Thenset the red values in
gray
to the values inaverage
set the green values in
gray
to the values inaverage
set the blue values in
gray
to the values inaverage
Use
Image.fromarray
to create an image namedgray_image
fromgray
Call
display
to display theImage
namedgray_image
.