Using Arrays, Part 2

 


Introduction

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.



Exercises

  1. Create a new file for the statements and functions you will write in this mini-lab. Then save the new file with a name representative of this minilab.
  2. Immediately after your header comments (name, date, program description) for this file, add the following statement to import the NumPy module:
    import numpy as np
    This statement will allow us to use the functions in this module.
  3. Copy the avgTemp, medianTemp, maxTemp, minTemp, numGreaterThan, and numLessThan functions from your Using Arrays minilab.
  4. In the previous minilab, you created 10 separate arrays to hold the high and low temperatures for a given month over the course of 5 different years. That data could have all been stored in a single array. One option would be to have a 5x2x31 size array. With this option, for example, January temps would look something like this:
    [[[15, 27, 20, 12, 20, ...] [32, 36, 28, 22, 35, ...]]
    [[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, ...]]]
    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.

    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] ...]
    [[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] ...]]]
    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.

    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.

  5. Print your array to see what it looks like.
  6. To access the array of low temps for the first year, you would type 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.
  7. The following loop would allow us to print the low and high temps for each year:
    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.
  8. Add two lines of code to this loop to also print the average temperatures for each year. You will need to use your avgTemp function twice.
  9. NumPy has built-in routines for use with statistics. Take a look at the functions that are available: NumPy statistics functions.
    One of the functions that we can use is the 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.
  10. There are also built-in functions to find the minimum and maximum values of an array, called 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.
  11. In a previous minilab, you learned that NumPy has a built-in function, 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.

Submit