COMP1012 Lab4 Solved

30.00 $

Category:

Description

Rate this product

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(xi−x¯)2N−1

Where σ is standard deviation,  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:

  1. open the file.
  2. Initialize 3 list accumulators.
  3. Iterate over the lines of the file in a for loop and accumulate the values in the columns.
  4. Compute the average (either do this inside the loop with a running sum or use the sum function on your lists).

Once you have the mean , 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.81
The mean of column 2 is 499.79, and the standard deviation is 290.16
The mean of column 3 is 5011.12, and the standard deviation is 2874.15

Nested 🐤 lists

As an extra exercise, consider how to write this using nested lists. 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=|x¯−x|σ

Where σ is the standard deviation,  is the mean of the dataset, and x is the data the user provided.

Give me a number : > 50
This 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 🐤 lists

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”.

  • Lab4-jvelzm.zip