Exercise 1: Standard deviation
Open the file lab4.csv
. This file has 3 numbers per-line, separated by commas.
Calculate the mean and standard deviation of each column of the file, using the formula:
=i=1N(xix)2N1
Where is standard deviation, x is the mean of the dataset, N is the number of elements in the dataset, and xi is an element of the dataset at spot i.
You will need to:
open
the file.- Initialize 3
list
accumulators. - Iterate over the lines of the file in a
for
loop and accumulate the values in the columns. - Compute the average (either do this inside the loop with a running sum or use the
sum
function on yourlist
s).
Once you have the mean x, calculate the standard deviation using a second for
loop.
Your output should look like:
The mean of column 1 is 50.30, and the standard deviation is 28.81The mean of column 2 is 499.79, and the standard deviation is 290.16The mean of column 3 is 5011.12, and the standard deviation is 2874.15
Nested 🐤 list
s
As an extra exercise, consider how to write this using nested list
s. You may need to use the range
function if you choose to solve this problem.
Exercise 2: Standard deviations from the mean
Add to your program from the previous part to ask for numerical input from the user. Find how many standard deviations from the mean the input is.
The formula for deviations from the mean is:
deviationsFromMean=|xx|
Where is the standard deviation, x is the mean of the dataset, and x is the data the user provided.
Give me a number : > 50This number is 0000.0104 standard deviations from the mean for column 1.This number is 0001.5501 standard deviations from the mean for column 2.This number is 0001.7261 standard deviations from the mean for column 3.
Nested 🐤 list
s
If you did the extra exercise for above, add to this part an option for the user to choose which column the standard deviation is calculated for.
Optional exercises
Basic if
usage
Change your above code. Use an if
statement to remove negative numbers, creating a standard deviation with only positive numbers (which we will define as greater than or equal to 0).
Evens only
Change your if statement to only accept even numbers and positive. The data are decimal numbers, so we will define even as The 1s place is even.
Reviews
There are no reviews yet.