Skip to main content
留学咨询

辅导案例-9B-Assignment6

By May 15, 2020No Comments

2020/2/4 Assignment6.ipynb – Colaboratory https://colab.research.google.com/drive/1Azz9B-A2HFj_NyQ_vcYcDKBUt4X5tl1d?pli=1&authuser=1#printMode=true 1/6 This notebook is licensed under CC BY-NC 4.0 by Lindsey Kuper. Assignment 6: Variance and Standard Deviation Notebook Format This is a homework notebook. It consists of various types of cells: Text cells: you should read them! Code cells: you should run them, as they may set up the problems that you are asked to solve. Solution cells: These are cells where you should enter a solution. You will see a marker in these should be inserted:     # YOUR CODE HERE Test cells: These cells contain some tests, and are worth some points. You should run the cells you understood the question, and whether the output of your code is produced in the correct for When we grade your notebook, we will run many additional tests in addition to the ones you see here credit by hard-coding the desired output!) Instructions To work on your notebook, we recommend using Colab. Working in Colab has many bene ts: You don’t have to maintain a working Python environment on your own machine; you can work f connection. Colab preserves the revision history, which is useful for many reasons. Your work is automatically saved in Google Drive. In Colab, go to “File > Save a copy in Drive…” and then you’ll have your own copy to work on. Working on Your Notebook Before you turn your nished notebook in, it’s a good idea to sure everything runs as expected. First, f “Runtime > Factory reset runtime”). Then, run all the cells (go to “Runtime > Run all”) in Colab. Submitting Your Notebook 2020/2/4 Assignment6.ipynb – Colaboratory https://colab.research.google.com/drive/1Azz9B-A2HFj_NyQ_vcYcDKBUt4X5tl1d?pli=1&authuser=1#printMode=true 2/6 Submit your work as follows: Download the notebook from Colab, clicking on “File > Download .ipynb”. Upload the resulting le to this Google form. Deadline: 9pm Monday, February 10. You can submit multiple times, and the last submission before the deadline will be used to assign you Make sure you ll in any place that says YOUR CODE HERE or “YOUR ANSWER HERE”, as well as your collaborated with in the below cells. For collaborators, list anyone who (for example) discussed the ge pointed you to useful Python documentation — collaboration of that kind is encouraged. Remember th not allowed. NAME = “” COLLABORATORS = “” After you submit your notebook, your instructor at some point will retreat to a secret hideout, put on s mysterious scripts. These will generate two things: Your grade, which goes into a spreadsheet. Feedback, shared to you as a PDF le on Google Drive. The PDF shows your work, your grade, th and any comments left by the instructor and the TAs. What Happens Next? Make sure to run the following cell to make sure that the Python testing framework, nose , is installed to work.) Testing try:     from nose.tools import assert_equal, assert_almost_equal     from nose.tools import assert_true, assert_false     from nose.tools import assert_not_equal, assert_greater_equal except:     !pip install nose     from nose.tools import assert_equal, assert_almost_equal     from nose.tools import assert_true, assert_false     from nose.tools import assert_not_equal, assert_greater_equal Variance and Standard Deviation 2020/2/4 Assignment6.ipynb – Colaboratory https://colab.research.google.com/drive/1Azz9B-A2HFj_NyQ_vcYcDKBUt4X5tl1d?pli=1&authuser=1#printMode=true 3/6 Problem 1: Computing variance For this problem, you will write a function variance that takes a list whose elements are numbers ( single number. (If you don’t remember how to compute variance, check the lecture notebook; it’s one standard deviation.) You should not use NumPy or any other Python libraries for this problem. For now, worry only about c Passing an empty list to variance should result in a ZeroDivisionError exception being raised, al raise it yourself. def variance(data):     “””Takes a list of numbers and returns their variance.”””     # YOUR CODE HERE     raise NotImplementedError() ### Tests for variance   assert_almost_equal(variance([3, 4, 5, 6]), 1.25) assert_almost_equal(variance([3, 4, 8]), 4.666666666666667) assert_almost_equal(variance([0.0, 0.0, 0, 0]), 0) assert_almost_equal(variance([1.5, 2.5]), 0.25) assert_almost_equal(variance([18, 19, 20, 21, 22]), 2.0) assert_almost_equal(variance([0, 43, 1, 1, 55]), 575.2)   try:     variance([]) except(ZeroDivisionError):     pass   For this problem, you will write a function standard_deviation that takes a list whose elements are returns their standard deviation, a single number. You may call the variance method de ned above may use sqrt from the math library, which we have already imported for you. Passing an empty list to standard_deviation should result in a ZeroDivisionError exception be explicitly raise it yourself. Problem 2: Computing standard deviation from math import sqrt   def standard_deviation(data):     “””Takes a list of numbers and returns their standard deviation.”””     # YOUR CODE HERE 2020/2/4 Assignment6.ipynb – Colaboratory https://colab.research.google.com/drive/1Azz9B-A2HFj_NyQ_vcYcDKBUt4X5tl1d?pli=1&authuser=1#printMode=true 4/6     raise NotImplementedError() ### Tests for standard_deviation   assert_almost_equal(standard_deviation([3, 4, 5, 6]), 1.1180339887499) assert_almost_equal(standard_deviation([3, 4, 8]), 2.1602468994693) assert_almost_equal(standard_deviation([0.0, 0.0, 0, 0]), 0) assert_almost_equal(standard_deviation([1.5, 2.5]), 0.5) assert_almost_equal(standard_deviation([18, 19, 20, 21, 22]), 1.4142135623731) assert_almost_equal(standard_deviation([0, 43, 1, 1, 55]), 23.983327542274)   try:     standard_deviation([]) except(ZeroDivisionError):     pass   Below is the EfficientAveragerator class we saw during lecture, with one new property for you to In EfficientAveragerator , we compute average and standard deviation in an incremental fashion, everything when a new value arrives in our data set. We want to compute variance using the same inc EfficientAveragerator already has everything we need to write such an incremental de nition of v Fill in the de nition of the variance property in the cell below. It should take no more than 1-2 lines o any other fancy library. (Hint: it will look a lot like the de nition of std .) Problem 3: A variance method for the EfficientAveragerator class import numpy as np   class EfficientAveragerator:          def __init__(self):         self.sum_x = 0.         self.sum_x_sq = 0.         self.n = 0              def add(self, x):         # We compute the sum of the x, to compute their average.         self.sum_x += x          # Sum of the x^2, so we can later compute the average of the x^2.          self.sum_x_sq += x * x         self.n += 1              @property     def avg(self):         return self.sum_x / self.n 2020/2/4 Assignment6.ipynb – Colaboratory https://colab.research.google.com/drive/1Azz9B-A2HFj_NyQ_vcYcDKBUt4X5tl1d?pli=1&authuser=1#printMode=true 5/6          @property     def variance(self):         “””Returns the variance of all the data points seen so far.”””         # YOUR CODE HERE         raise NotImplementedError()              @property     def std(self):         average = self.avg # To avoid calling self.avg twice.         return np.sqrt(self.sum_x_sq / self.n – average * average) ### Tests for EfficientAveragerator.variance   ea1 = EfficientAveragerator() ea1.add(3) ea1.add(4) ea1.add(8) assert_almost_equal(ea1.variance, 4.666666666666667)   ea2 = EfficientAveragerator() for num in [0, 43, 1, 1, 55]:     ea2.add(num) assert_almost_equal(ea2.variance, 575.2)   ea
3 = EfficientAveragerator() for _ in range(2000000):     ea3.add(1)     ea3.add(4) assert_almost_equal(ea3.variance, 2.25)   2020/2/4 Assignment6.ipynb – Colaboratory https://colab.research.google.com/drive/1Azz9B-A2HFj_NyQ_vcYcDKBUt4X5tl1d?pli=1&authuser=1#printMode=true 6/6

admin

Author admin

More posts by admin