Python Numpy Assignment.
Budget: ₹600 – ₹1,500 INR
Goals:
• To get hands on experience NumPy o Creating NumPy arrays
o Creating a slice of the array
o Using uFuncs to do computations on array and slices
What to hand in:
• Submit the following to your lab 3 git repo by Feb 7/8 23:59:
1. Main program:
§ Lab3.py
2. Output files:
§ testOutput.txt
• Submit your git submit log to VIU Learn, along with any comments you may have about challenges you
faced
Overview
In this lab you’re going to write a program that:
1. Reads a .csv file into a Python list
2. Extracts data from the Python list into NumPy arrays
3. Uses ufuncs to compute various values from the data in the NumPy array
What to do:
0: Set up
Create a Lab3 directory, and place in it:
• your lab3.py file
• the supplied data files
1: Read in the data file
Read the supplied data file into a Python list using the csv module:
• import the csv module
• in a try-catch statement
o open the supplied file
o use the csv.reader method to read the entire file into a list • Notes:
o Read more about the csv.reader method: https://docs.python.org/3/library/csv.html
2: Copy a Python list to numPy arrays
This is going to take a few intermediate steps. Because numPy arrays can only contain one type, we need to strip out the unneeded elements. We need to strip out a row and a column. Stripping a row is easy, but removing a column of a list requires looping through the list.
We want to remove and save:
• the header row, and remove the timestamp element from this list o save this in its own list
o The resulting list should contain 30 elements
• after the header labels are removed, remove the first column (the timestamps) o You will have to loop through this list. You can treat it as a list of lists.
§ Remove the timestamp value
§ save it in another list, retaining the original order o The resulting list should have 30 columns
At this point you should have:
• a list of header labels
• a list of timestamps
• a 2D list of data points with 30 columns
Create a numpy array of the data.
• Create an intermediate 2D NumPy array by passing the 2D list of data, and indicate that they’re
floating point numbers
• Then copy that numPy array into another where all NaN are replaced with 0.
o Use the numpy.nan_to_num function: https://numpy.org/doc/stable/reference/generated/numpy.nan_to_num.html
At this point it should contain 30 values per row. Now that we have a numpy array we can do things like vertical slicing much more easily.
• For each of the following columns, create a 1D numpy array of the data values: o Air Temperature (C) (column 6)
o Wind Speed (m/s) (column 28)
• Note:
o Usetheslicenotation:
§ myArr[:, 4] will return the column with index 4 as a numpy 1D array
3: Compute values using ufuncs
Write a function that takes in a 1D numpy array of values and a computation type and return the computed value across that array.
• Function should support the following: mean, min, max
• Optionally support other ufuncs
Compute and output the mean, min, max (and other values you wish) for the two measures above (Air temp, wind speed, and wet bulb temp).
Finally, compute the mean temp for the Wet Bulb Temperature (C) (column 22) by calling the numpy mean function on a slice of the full numpy array (not a separately sliced 1D array).
• Note: You can do this two different ways:
o np.mean(my2DArr[:, 10]) will compute the mean of values in column 10 o my2DArr[:, 10].mean() will do the same thing
testing
For this lab, you need only submit the following output:
• output the size, ndim, and shape of the numpy array that contains all data
• provide meaningful and structured output of:
o the mean, max, and min value of the air temp and wind speed
o any other computations you wish to do on these 2 columns of data o the mean value of the wet bulb temp
• To get hands on experience NumPy o Creating NumPy arrays
o Creating a slice of the array
o Using uFuncs to do computations on array and slices
What to hand in:
• Submit the following to your lab 3 git repo by Feb 7/8 23:59:
1. Main program:
§ Lab3.py
2. Output files:
§ testOutput.txt
• Submit your git submit log to VIU Learn, along with any comments you may have about challenges you
faced
Overview
In this lab you’re going to write a program that:
1. Reads a .csv file into a Python list
2. Extracts data from the Python list into NumPy arrays
3. Uses ufuncs to compute various values from the data in the NumPy array
What to do:
0: Set up
Create a Lab3 directory, and place in it:
• your lab3.py file
• the supplied data files
1: Read in the data file
Read the supplied data file into a Python list using the csv module:
• import the csv module
• in a try-catch statement
o open the supplied file
o use the csv.reader method to read the entire file into a list • Notes:
o Read more about the csv.reader method: https://docs.python.org/3/library/csv.html
2: Copy a Python list to numPy arrays
This is going to take a few intermediate steps. Because numPy arrays can only contain one type, we need to strip out the unneeded elements. We need to strip out a row and a column. Stripping a row is easy, but removing a column of a list requires looping through the list.
We want to remove and save:
• the header row, and remove the timestamp element from this list o save this in its own list
o The resulting list should contain 30 elements
• after the header labels are removed, remove the first column (the timestamps) o You will have to loop through this list. You can treat it as a list of lists.
§ Remove the timestamp value
§ save it in another list, retaining the original order o The resulting list should have 30 columns
At this point you should have:
• a list of header labels
• a list of timestamps
• a 2D list of data points with 30 columns
Create a numpy array of the data.
• Create an intermediate 2D NumPy array by passing the 2D list of data, and indicate that they’re
floating point numbers
• Then copy that numPy array into another where all NaN are replaced with 0.
o Use the numpy.nan_to_num function: https://numpy.org/doc/stable/reference/generated/numpy.nan_to_num.html
At this point it should contain 30 values per row. Now that we have a numpy array we can do things like vertical slicing much more easily.
• For each of the following columns, create a 1D numpy array of the data values: o Air Temperature (C) (column 6)
o Wind Speed (m/s) (column 28)
• Note:
o Usetheslicenotation:
§ myArr[:, 4] will return the column with index 4 as a numpy 1D array
3: Compute values using ufuncs
Write a function that takes in a 1D numpy array of values and a computation type and return the computed value across that array.
• Function should support the following: mean, min, max
• Optionally support other ufuncs
Compute and output the mean, min, max (and other values you wish) for the two measures above (Air temp, wind speed, and wet bulb temp).
Finally, compute the mean temp for the Wet Bulb Temperature (C) (column 22) by calling the numpy mean function on a slice of the full numpy array (not a separately sliced 1D array).
• Note: You can do this two different ways:
o np.mean(my2DArr[:, 10]) will compute the mean of values in column 10 o my2DArr[:, 10].mean() will do the same thing
testing
For this lab, you need only submit the following output:
• output the size, ndim, and shape of the numpy array that contains all data
• provide meaningful and structured output of:
o the mean, max, and min value of the air temp and wind speed
o any other computations you wish to do on these 2 columns of data o the mean value of the wet bulb temp