Exercise #5 - Classes and Objects

Objectives

    • Gain experience in adding methods to classes and testing the classes

    • Gain additional experience with scientific Python

A PairedData class

You are being provided with a Python file, PairedData.py, which contains a PairedData class, much like you've seen in the course notes. In this portion of the exercise, you will add some methods to it.

PairedData.py is available at the end of this document, as well as in

/home/kurs/donmor/Files/Exercise05/

Note that the bottom of the class file has some testing code, and you should first insure that this works when you run from the command line

python PairedData.py

Then, you will add new methods and new testing code at the bottom of the class file.

You should add the following methods

    • y_mean() - the mean value of the y vector

    • mean_bias() - this is simply the average value of your differences. Note that you could calculate this very easily by using the diff_vector() method that already exists. It should be as simple as return self.diff_vector().mean()

  • rmse() - the root-mean-square error - you have already seen this, and, again, you should be able to implement this in a single line (but you don't have to).

    • scatterplot() - I have provided you with an outline, and the appropriate import statements, so you only need to try inserting the code. Recall that there was a scatterplot example in the Scientific Python course notes

I recommend you implement these one at a time, add a test for them at the bottom of the file, then make sure it works before moving on to the next one. Once you are confident that your methods are good, open IPython and try to work with your new class interactively with some simple (or complex) test data. For example:

In [1]: import PairedData

In [2]: import numpy as np

In [8]: xpts = np.random.randn(50)

In [9]: xpts

Out[9]:

array([ 0.04053112, -1.27435613, 1.47470752, 1.19932701, 1.25952902,

0.25824088, -0.02686967, 0.47056267, -0.66380999, -1.43505989,

-0.4288613 , -0.88893093, -0.59167868, 0.00496496, 0.8889448 ,

0.53690377, -1.3713785 , 1.79233722, 1.82320847, 1.71167941,

3.1223232 , 1.17747287, 0.64665918, -1.19943613, -1.05745057,

-2.18998409, 0.075816 , 0.44442808, 0.96898983, -0.46156566,

-0.14537597, -0.48456083, -0.13209807, -0.8012587 , 0.1358326 ,

1.62229708, -0.42540841, 0.09959869, 1.03399069, -0.33690971,

-0.70034966, -0.25425768, -0.33090497, -1.26966407, 0.43068765,

-0.43298085, -0.19576186, -0.24871922, -0.1019111 , -1.04319245])

In [10]: ypts = np.random.randn(50)

In [11]: ypts

Out[11]:

array([-0.46819512, -0.173475 , 2.2639471 , -0.91964678, -0.40067318,

-0.88235761, -0.48883858, 1.01275048, -1.41079517, 0.60510135,

-0.33726024, 0.55315816, -0.67866412, -0.17450488, 1.14260063,

1.74968546, -0.13347751, 0.74728204, 0.47105997, 1.74515724,

0.65440193, 1.23321806, -0.08813341, 0.7439089 , -0.87760374,

0.99798569, 0.5226664 , -0.57195478, 0.07923213, -0.13175932,

1.3985633 , -0.32952676, 0.99329375, -1.20271608, -0.66713415,

1.24090145, 0.11912983, 0.5334144 , 2.43722989, 2.00556566,

0.31896023, 0.75318897, 0.15813992, 1.31874181, -0.71998228,

0.97909065, 0.63839315, 0.83247011, 0.93855297, 1.03820364])

In [12]: mypairs = PairedData.PairedData(xpts, ypts)

In [14]: mypairs.mean_bias()

Out[14]: 0.33685997847677585

In [15]: mypairs.scatterplot()