<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Attribution
Step1:  Built-in data structures
Tuples
Step2:  Note that in Python, indexing starts at 0!
Lists
Step3:  Dictionaries ("Hashes")
Step4:  Functions, control structures
Function definition
Step5:  Control structures
If-then-else
Step6:  While
Step7:  For-loops
Step8:  Note that either double or single quotes can be used to delimit strings.
Everything is an object
Functions, too.
Objects have member components (functions and attributes).
Step9:  Defining classes
Step10:  However, note that we can get pretty far without needing classes!
Getting help
When working interactively,
Step11:  help expects the object you need help about. Just instantiate one, if you do not have it!
The ? operator is also useful (only works in IPython)
Step12:  IPython also has a number of so-called "magic" functions
Step13:  Scripts, modules, packages
Naming convention for scripts
Step14:  Modules
Combine common functionality in "modules" (= "libraries")
Same naming convention as for scripts
Step15:  Remember, everything is an object. Access the contents of modules accordingly.
Step16:  Packages for data crunching
Pylab
"pylab" = 
numpy (for "computing") +
matplotlib (for "plotting") +
scipy (for more specific needs, like Fourier transforms, special functions, etc.)
Two ways to use
Step17:  There are many other useful commands built in, e.g. load(), save(), loadtxt(), etc.
Numpy arrays have many useful member components
Step18:  One of the most useful built-in commands is ``shape''
Step19:  Doing computations
To do computations on arrays, numpy as functions like
exp(), log(), cos(), sin()
and operators like
+, -, *, /
These typically operate elementwise.
Other useful matrix operations include
dot(), svd(), eig()
Accessing your data
Step20:  Plotting
Step21:  In a box and whisker plot, the box extends from the lower to upper quartile values of the data, with a line at the median. The whiskers extend from the box to show the range of the data.  Outlier points are those past the end of the whiskers.
Broadcasting and newaxis
How do you add a $2 \times 5$ matrix and a $1 \times 5$ vector?
In Matlab, you would use repmat (or for the pros, bsxfun). You can use Python's repmat equivalent, called tile.
However, Python comes with another possibility, called "broadcasting"
Numpy will always try to copy every dimension in each array as often as it needs to make the dimensions fit
<img width=600px src="files/images/numpy_broadcasting.png">
Step22:  How about a $2 \times 5$ matrix plus a $1 \times 5 \times 3$ tensor?
Step23:  We need to make the number of dimensions match. Solution
Step24:  Style
In general, doing a from module import * is frowned upon. We used it before since it was easy to experiment, but generally you should only import the objects you need. In my own workflow, I start IPython with the ``--pylab'' option, and generally do the following imports
Step25:  Then I use np to access Numpy methods and plt to access plotting methods.
Python has its own style convention called "PEP8". This is a great resource to check out if you are unsure about how to format your code. There are also command-line tools (e.g. pep8) which will verify your code against the standard. Most serious open-source projects will expect you to adhere to the PEP8 standard before contributing code.
Additional resources
Here's a short YouTube video about Numpy arrays
<ASSISTANT_TASK:>
Python Code:
print "hello world"
print "hello world", 1, 2, 1 + 2
a = 1
b = 1
print "hello world", a + b
a = 1
b = "hello"
print "hello world", a + b
b = "world"
print "hello %s" % b
Explanation: Attribution: this notebook is based on slides created by Roland Memisevic. Some of the material related to classes was taken from http://www.stavros.io/tutorials/python/.
<style>
div.box {
    border: solid 1px #CCCCCC;
    background-color: #f9f9f9;
    padding: 4px;
    margin: 4px 4px 0px 4px;
    float:left;
}
</style>
<div class=box>
Running this notebook on your computer (requires IPython to be installed): 
<ul>
<li>Download this notebook (ipynb file) to a folder on your computer.
<li>Open a terminal and ``cd`` to the directory containing this notebook
<li>Start the notebook server: ``jupyter notebook``
<li>It should automatically pop up a browser window. If it doesn't, navigate to http://localhost:8888
</ul>
</div>
Introduction
“Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to combine remarkable power with very clear syntax”, and its standard library is large and comprehensive. Its use of indentation for block delimiters is unique among popular programming languages.” -- Wikipedia
Some features of Python:
Interpreted Language
Strict Syntax
Indentation!
"Batteries Included"
Dynamic types
Mixes imperative, object-oriented, and functional programming elements
There are various implementations of Python available, and several (incompatible) versions. The most common Python 2.x (currently 2.7) is suitable for most of our data processing needs.
The "IPython" interactive shell is highly recommended for any kind of interactive work! Note that here, I'm using the IPython notebook.
"Hello World"
End of explanation
T = (1, 2, 3, "hello")
print T[0]
Explanation: Built-in data structures
Tuples
End of explanation
L = [1, 2, 3, "hello"]
L[0] = "Lists are mutable"
print L[0]
L.append("goodbye")
print L[-1]
Explanation: Note that in Python, indexing starts at 0!
Lists
End of explanation
D = {"a": 1, "b": 2}
print D["a"]
Explanation: Dictionaries ("Hashes")
End of explanation
def timesfour(x):
    return 4 * x
print timesfour(2)
Explanation: Functions, control structures
Function definition
End of explanation
s = "z"
if s == "y":
    print "y"
elif s == "z":
    print "z"
else:
    print "b"
Explanation: Control structures
If-then-else
End of explanation
a = 1.0
s = "hello"
while a != 10.0 and s == "hello":
    a = a + 1.0
print a
Explanation: While
End of explanation
for i in range(3):
    print i
for i in [1, 2, 'x', 3, 4, 'h', 5]:
    print i
Explanation: For-loops
End of explanation
type(timesfour)
# At this point try tab completion on timesfour.
timesfour
Explanation: Note that either double or single quotes can be used to delimit strings.
Everything is an object
Functions, too.
Objects have member components (functions and attributes).
End of explanation
# here "object" specifies the superclass
class MyClass(object):
    common = 10
    # this is a constructor
    def __init__(self):
        self.myvariable = 3
    def myfunction(self, arg1, arg2):
        return self.myvariable
classinstance = MyClass()
# note that arguments passed to myfunction are ignored
print 'classinstance.myfunction(1, 2): %s' % classinstance.myfunction(1, 2)
# This variable is shared by all classes.
classinstance2 = MyClass()
print 'classinstance.common: %s' % classinstance.common
print 'classinstance2.common: %s' % classinstance2.common
# Note how we use the class name instead of the instance.
MyClass.common = 30
print 'classinstance.common: %s' % classinstance.common
print 'classinstance2.common: %s' % classinstance2.common
# This will not update the variable on the class,
# instead it will bind a new object to the old
# variable name.
classinstance.common = 10
print 'classinstance.common: %s' % classinstance.common
print 'classinstance2.common: %s' % classinstance2.common
MyClass.common = 50
# This has not changed, because classinstance.common is
# now an instance variable.
print 'classinstance.common: %s' % classinstance.common
# but this has changed
print 'classinstance2.common: %s' % classinstance2.common
# This class inherits from MyClass. The example
# class above inherits from "object", which makes
# it what's called a "new-style class".
# You can read more about these here: http://stackoverflow.com/a/54873
# Multiple inheritance is declared as:
# class OtherClass(MyClass1, MyClass2, MyClassN)
class OtherClass(MyClass):
    # The "self" argument is passed automatically
    # and refers to the class instance, so you can set
    # instance variables as above, but from inside the class.
    def __init__(self, arg1):
        self.myvariable = 3
        print "arg1: %s" % arg1
classinstance = OtherClass("hello")
print 'classinstance.myfunction(1, 2): %s' % classinstance.myfunction(1, 2)
# This class doesn't have a .test member, but
# we can add one to the instance anyway. Note
# that this will only be a member of classinstance.
classinstance.test = 10
print 'classinstance.test: %s' % classinstance.test
Explanation: Defining classes
End of explanation
a = [1,2,3]
help(a)
Explanation: However, note that we can get pretty far without needing classes!
Getting help
When working interactively,
End of explanation
list?
Explanation: help expects the object you need help about. Just instantiate one, if you do not have it!
The ? operator is also useful (only works in IPython):
End of explanation
%lsmagic
Explanation: IPython also has a number of so-called "magic" functions: e.g. %autoreload, %paste, %debug, %hist, %timeit. Type %magic at a prompt to learn more.
End of explanation
%%writefile mytest.py
for i in xrange(3):
    print "hello %d" % i
%run mytest.py
Explanation: Scripts, modules, packages
Naming convention for scripts: ".py"
python myscript.py
In IPython we can use:
%run myscript.py
End of explanation
import datetime
# imports a single object
from datetime import date
# imports everything (careful, pollutes namespace)
from datetime import *
Explanation: Modules
Combine common functionality in "modules" (= "libraries")
Same naming convention as for scripts: ".py"
Can combine modules in packages
To use a module:
End of explanation
from datetime import datetime
datetime.now()
myobject = datetime.month
print myobject
Explanation: Remember, everything is an object. Access the contents of modules accordingly.
End of explanation
array([1,2,3])
ones((2,3))
zeros((3,2))
eye(4)
# draw from normal distribution with mean 0, std 1
randn(2,2)
Explanation: Packages for data crunching
Pylab
"pylab" = 
numpy (for "computing") +
matplotlib (for "plotting") +
scipy (for more specific needs, like Fourier transforms, special functions, etc.)
Two ways to use:
import pylab from a shell or "from pylab import *" to get direct access to the most important functions and objects
IPython offers the startup flag --pylab to give you an environment that contains all the necessary goodies
I usually alias ipy to ipython --pylab in my shell, e.g. alias ipy="ipython --pylab"
Numpy arrays
The central object for representing data is the numpy array.
It is an n-dimensional generalization of a matrix.
It can hold data of various types, like int, float, string, etc.
The most common use is for representing vectors and matrices filled with numbers (such as floats)
One way to generate an array is to use numpy.array
But we'll see other ways soon
After doing ipython --pylab or from pylab import *; from numpy import * from within Python, we have access to things like:
End of explanation
a = np.array([[1,2,3],[4,5,6]])
print a
print a.T
a.mean(0)
a.mean(1)
a.mean()
a.std(0)
a.max()
Explanation: There are many other useful commands built in, e.g. load(), save(), loadtxt(), etc.
Numpy arrays have many useful member components:
End of explanation
print randn(3,3).shape
print randn(2,5,7,3).shape
print array([1,2,3,4]).shape
Explanation: One of the most useful built-in commands is ``shape'':
End of explanation
a = array([1,2,3])
print a[0]
print a[1]
a = array([[1,2,3],[4,5,6]])
print a[0,0]
print a[1,2]
a = randn(2,3,4)
print a
print a[0,0,0]
print a[0,1,3]
a = array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print a
print a[0, :]  # a "slice"
print a[:, 0]  # another "slice"
print a[1:3, 0]  # another "slice"
print a[1:3, :]  # another "slice" (this is a 2-d block)
Explanation: Doing computations
To do computations on arrays, numpy as functions like
exp(), log(), cos(), sin()
and operators like
+, -, *, /
These typically operate elementwise.
Other useful matrix operations include
dot(), svd(), eig()
Accessing your data
End of explanation
x = arange(0,pi,pi/180)
plot(x, cos(x))
x1 = 1 + 0.1*randn(20)
y1 = 1 + 0.1*randn(20)
x2 = -1 + 0.5*randn(20)
y2 = -1 + 0.5*randn(20)
scatter(x1,y1)
scatter(x2,y2,c='r',marker='x')
xlim(-2,2)
ylim(-2,2)
legend(['class 1', 'class2'], loc='lower right')
subplot(1,2,1)
hist(randn(200),bins=10)
ylabel('counts')
xlabel('bins')
subplot(1,2,2)
boxplot(randn(200))
title('box and whisker plot')
Explanation: Plotting
End of explanation
print (randn(2,5) + randn(1,5)).shape
Explanation: In a box and whisker plot, the box extends from the lower to upper quartile values of the data, with a line at the median. The whiskers extend from the box to show the range of the data.  Outlier points are those past the end of the whiskers.
Broadcasting and newaxis
How do you add a $2 \times 5$ matrix and a $1 \times 5$ vector?
In Matlab, you would use repmat (or for the pros, bsxfun). You can use Python's repmat equivalent, called tile.
However, Python comes with another possibility, called "broadcasting"
Numpy will always try to copy every dimension in each array as often as it needs to make the dimensions fit
<img width=600px src="files/images/numpy_broadcasting.png">
End of explanation
print randn(2,5).shape
print randn(2,5) + randn(1,5,3)
Explanation: How about a $2 \times 5$ matrix plus a $1 \times 5 \times 3$ tensor?
End of explanation
print randn(2,5)[:,:,newaxis].shape
print (randn(2,5)[:,:,newaxis] + randn(1,5,3)).shape
Explanation: We need to make the number of dimensions match. Solution: Numpy's newaxis
End of explanation
import numpy as np
import matplotlib.pyplot as plt
Explanation: Style
In general, doing a from module import * is frowned upon. We used it before since it was easy to experiment, but generally you should only import the objects you need. In my own workflow, I start IPython with the ``--pylab'' option, and generally do the following imports:
End of explanation
from IPython.display import YouTubeVideo
# a short video about using NumPy arrays, from Enthought
YouTubeVideo('vWkb7VahaXQ')
Explanation: Then I use np to access Numpy methods and plt to access plotting methods.
Python has its own style convention called "PEP8". This is a great resource to check out if you are unsure about how to format your code. There are also command-line tools (e.g. pep8) which will verify your code against the standard. Most serious open-source projects will expect you to adhere to the PEP8 standard before contributing code.
Additional resources
Here's a short YouTube video about Numpy arrays:
End of explanation
<END_TASK>