# You must make sure to run all cells in sequence using shift + enter or you might encounter errors
from pykubegrader.initialize import initialize_assignment
responses = initialize_assignment("9_numpy_q", "week_3", "readings", assignment_points = 21.0, assignment_tag = 'week3-readings')
# Initialize Otter
import otter
grader = otter.Notebook("9_numpy_q.ipynb")
โ ๐ Lotl of NumPy ๐งช๐พ#
# Run this block of code by pressing Shift + Enter to display the question
from questions._9_numpy_q import Question1
Question1().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._9_numpy_q import Question2
Question2().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._9_numpy_q import Question3
Question3().show()
Axolotl Counting#
Instructions:#
Imagine you are studying a group of adorable axolotls. Write a Python function using NumPy to count how many axolotls there are in each habitat. You are given a 1D NumPy array where each number represents the number of axolotls observed in different habitats.
Your solution must include:
Import numpy as np
A function
count_axolotls(habitats)
that:Accepts a 1D NumPy array of integers.
uses the numpy
sum
function to count the total number of axolotls in each habitat. Save this to the variablecount
.Returns the total number of axolotls across all habitats.
Test cases to validate the correctness of your implementation.
# import numpy, make sure to use the common abbreviation np
...
# we have provided the function for you
def count_axolotls(habitats):
# Count the number of axolotls in the habitats array
# save the output to the variable count
...
return count
test_input = np.array([1, 2, 3])
print(count_axolotls(test_input)) # expect 6
grader.check("Axolotl-Counting")
Axolotl Habitat Reshaping#
Instructions:#
You have data on axolotls in a 1D array, but you need to organize it into a 2D array to match rows and columns of habitats. Write a Python function using NumPy to reshape the 1D array into a 2D array. Just like axolotls can regrow their limbs, you can reshape the data to fit your needs.
Your solution must include:
import numpy as np
A function
reshape_habitats(axolotl_data, rows, cols)
that:Accepts a 1D NumPy array of integers and reshapes it into a 2D array with the specified number of rows and columns. You should use the built-in method
reshape
. Save this to the variablereshaped
.Returns the reshaped 2D array.
Test cases to validate the correctness of your implementation.
# import numpy, make sure to use the common abbreviation np
...
# We have provided the function for you
# The function takes 3 inputs, and automatically assigns them to the variables rows, cols, and axolotl_data
def reshape_habitats(axolotl_data, rows, cols):
# Reshape the axolotl data into a 2D array with the given number of rows and columns
# save the output to the variable reshaped
...
return reshaped
# Example test case
axolotl_data = np.array([5, 12, 8, 7, 10, 15])
reshaped = reshape_habitats(axolotl_data, 2, 3)
print(reshaped)
grader.check("axolotl-reshaping")
Submitting Assignment#
Please run the following block of code using shift + enter
to submit your assignment, you should see your score.
from pykubegrader.submit.submit_assignment import submit_assignment
submit_assignment("week3-readings", "9_numpy_q")