The purpose of this mini-lab is to practice creating and working with multi-dimensional arrays in Python to process temperature data. This mini-lab is an extension of the Using Arrays minilab.
import numpy as np
This statement will allow us to use the functions in this
module.avgTemp
, medianTemp
,
maxTemp
, minTemp
,
numGreaterThan, and numLessThan
functions
from your Using Arrays minilab.
[[[15, 27, 20, 12, 20, ...] [32, 36, 28, 22, 35, ...]]There is an array element for each year, and within each year, there are 2 arrays; one for low temps and one for high temps. Then within each low/high temp array, there are 31 values.
[[12, 10, 9, 15, 7, ...] [25, 27, 32, 25, 20, ...]]
[[10, 7, 5, -3, 8, ...] [15, 17, 20, 8, 12, ...]]
[[15, 20, 17, 12, 18, ...] [22, 30, 32, 45, 28, ...]]
[[22, 25, 27, 20, 21, ...] [32, 37, 45, 30, 28, ...]]]
As an alternative, the data could be stored in a 5x31x2 size array. With this option, for example, January temps would look something like this:
[[[15, 32] [27, 36] [20, 28] [12, 22] [20, 35] ...]There is an array element for each year, and within each year, there are 31 2-dimensional arrays representing the low/high temps for each day of the month.
[[12, 25] [10, 27] [9, 32] [15, 25] [7, 20] ...]
[[10, 15] [7, 17] [5, 20] [-3. 8] [8, 12] ... ]
[[15, 22] [20, 30] [17, 32] [12, 45] [18, 28] ...]
[[22, 32] [25, 37] [27, 45] [20, 30] [21, 28] ...]]]
Given that the analysis we have previously done on this data was
looking at all of the high temps or all of the low temps for a
given month, it makes more sense to use the first option for the
data we are studying. Create a 5x2x31 array called
temps
with the temps that
you used in the previous minilab.
temps[0][0]
; to access the high temps for that
year, you would type temps[0][1]
. For the second year
of data, you would use temps[1][0]
for the low temps
and temps[1][1]
for the high temps. Print the arrays
of low temps and high temps for your last year of data using
appropriate indices.
for i in range(len(temps)):
print("The low temps for year ", i, " are: ", temps[i][0])
print("The high temps for year ", i, " are: ", temps[i][1])
Copy and test this loop.avgTemp
function twice.mean
function. To find
the average of an array, we would write np.mean(myArray)
. We can
get the average for the low temps of the first year by writing
np.mean(temps[0][0])
. We can get the average of both the low and
high temps at the same time by specifying an axis. We would write
np.mean(temps[0], axis = 1)
. Test this with your array.amin
and amax
, as well as a function,
median
, to find the median value of an array. These functions are
used similarly to the mean
function. Write statements to test these functions on
one month's low and high temps. Compare your results with the results you would
get by passing the same low/high temp arrays into your maxTemp
,
minTemp
, and medianTemp
functions.where
, that can be used to search for specific values. We can use
the search
function to help us compute the number of temps above or
below a certain value. For instance, if we wanted to determine the number of
days with high temps above 90 degrees for the first year of our data, we could
use the following statements:
x = np.where(temps[0][1] > 90)
print(len(x[0]))
Use a loop to print the number of days with high temps above 90 and low temps
below 32 for each year of data.