Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook class01_ipython_notebook.ipynb

52 views
Kernel: Python 2 (SageMath)

Learn Python

Laurea Magistrale in Fisica
A.A. 2016/17

Prof. Aldo Zollo

Exercises - IPython Notebook


Run a code cell using Shift-Enter or pressing the "Play" button in the toolbar above:

print 1+2+3
6
for number in [1,3,5,7,9,11,13,15,17]: print(2*number) print("foo")
2 6 10 14 18 22 26 30 34 foo

This is a markdown cell

import time,sys for i in range(10): print(i) time.sleep(0.5)
0 1 2 3 4 5 6 7 8 9

this is a test of markdown this is still a test of markdown this is still a test of markdown

Code is run in a separate process called the IPython Kernel. The Kernel can be interrupted or restarted. Try running the following cell and then hit the "Stop" button in the toolbar above.

import time time.sleep(100)

Here are two system aliases:

Tab completion:

import numpy numpy.random.

Tab completion after ( brings up a tooltip with the docstring:

numpy.random.rand(2,3)

Adding ? opens the docstring in the pager below:

magic?

Exceptions are formatted nicely:

x = 1 y = 4 z = y/(1-x)
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) <ipython-input-4-dc39888fd1d2> in <module>() 1 x = 1 2 y = 4 ----> 3 z = y/(1-x) ZeroDivisionError: integer division or modulo by zero

The %load magic lets you load code from URLs or local files:

%load?
%pylab inline
%load http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/integral_demo.py
#!/usr/bin/env python # implement the example graphs/integral from pyx from pylab import * from matplotlib.patches import Polygon def func(x): return (x-3)*(x-5)*(x-7)+85 ax = subplot(111) a, b = 2, 9 # integral area x = arange(0, 10, 0.01) y = func(x) plot(x, y, linewidth=1) # make the shaded region ix = arange(a, b, 0.01) iy = func(ix) verts = [(a,0)] + list(zip(ix,iy)) + [(b,0)] poly = Polygon(verts, facecolor='0.8', edgecolor='k') ax.add_patch(poly) text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$", horizontalalignment='center', fontsize=20) axis([0,10, 0, 180]) figtext(0.9, 0.05, 'x') figtext(0.1, 0.9, 'y') ax.set_xticks((a,b)) ax.set_xticklabels(('a','b')) ax.set_yticks([]) show()
Image in a Jupyter notebook

To better handle large outputs, the output area can be collapsed. Run the following cell and then single- or double- click on the active area to the left of the output:

for i in range(50): print(i)
(Output Hidden)

Beyond a certain point, output will scroll automatically:

for i in range(500): print(2**i - 1)
(Output Hidden)

Markdown

Text can be added to IPython Notebooks using Markdown cells. Markdown is a popular markup language that is a superset of HTML. Its specification can be found here: http://daringfireball.net/projects/markdown/

You can make text italic or bold. You can build nested itemized or enumerated lists:

  • One

    • Sublist

      • This

    • Sublist - That - The other thing

  • Two

    • Sublist

  • Three

    • Sublist

Now another list:

  1. Here we go

    1. Sublist

    2. Sublist

  2. There we go

  3. Now this

You can add horizontal rules:


Here is a blockquote:

Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!

And shorthand for links:

IPython's website

If you want, you can add headings using Markdown's syntax:

Heading 1

Heading 2

Heading 2.1

Heading 2.2

You can embed code meant for illustration instead of execution in Python:

def f(x): """a docstring""" return x**2

or other languages:

if (i=0; i<n; i++) { printf("hello %d\n", i); x += 4; }

Because Markdown is a superset of HTML you can even add things like HTML tables:

Header 1 Header 2
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2

Rich Display System

To work with images (JPEG, PNG) use the Image class.

from IPython.display import Image Image(url='http://python.org/images/python-logo.gif')

More exotic objects can also be displayed, as long as their representation supports the IPython display protocol. For example, videos hosted externally on YouTube are easy to load (and writing a similar wrapper for other hosted content is trivial):

from IPython.display import YouTubeVideo YouTubeVideo('26wgEsg9Mcc')

Python objects can declare HTML representations that will be displayed in the Notebook. If you have some HTML you want to display, simply use the HTML class.

from IPython.display import HTML s = """<table> <tr> <th>Header 1</th> <th>Header 2</th> </tr> <tr> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> <tr> <td>row 2, cell 1</td> <td>row 2, cell 2</td> </tr> </table>""" h = HTML(s); h

Pandas makes use of this capability to allow DataFrames to be represented as HTML tables.

import pandas
%%file data.csv Date,Open,High,Low,Close,Volume,Adj Close 2012-06-01,569.16,590.00,548.50,584.00,14077000,581.50 2012-05-01,584.90,596.76,522.18,577.73,18827900,575.26 2012-04-02,601.83,644.00,555.00,583.98,28759100,581.48 2012-03-01,548.17,621.45,516.22,599.55,26486000,596.99 2012-02-01,458.41,547.61,453.98,542.44,22001000,540.12 2012-01-03,409.40,458.24,409.00,456.48,12949100,454.53
df = pandas.read_csv('data.csv') df

You can even embed an entire page from another site in an iframe; for example this is today's Wikipedia page for mobile users:

from IPython.display import HTML HTML('

LaTeX

IPython Notebook supports the display of mathematical expressions typeset in LaTeX

F(k)=f(x)e2πikdxF(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx
from IPython.display import Math Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')
from IPython.display import Latex Latex(r"""\begin{eqnarray} \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ \nabla \cdot \vec{\mathbf{B}} & = 0 \end{eqnarray}""")

EXERCISES: find the error and correct the following statements

for i in range(10): print i
print """This is a text with two lines"""
a = 10 print a
a = 10 if a == 10: print a
b = 20 print b
import string print string.lowercase
l = range(10) # print first and last element print l[0], l[9]
def qsort(l): """ Quicksort using list comprehensions """ if l == []: return [] else: pivot = l[0] lesser = qsort([x for x in l[1:] if x < pivot]) greater = qsort([x for x in l[1:] if x >= pivot]) return lesser + [pivot] + greater l = qsort([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3]) for i in range(1,len(l)): if l[i] < l[i-1]: print "FAILED:", l break else: print "SUCCESS:", l

EXERCISE: Perceptron

Create a comma-separated values (csv) file with the features (x1, x2) and the target (y)

%%file data.csv x1,x2,y 0.4946,5.7661,0 4.7206,5.7661,1 1.2888,5.3433,0 4.2898,5.3433,1 1.4293,4.5592,0 4.2286,4.5592,1 1.1921,5.8563,0 3.1454,5.8563,1 1.063,5.7357,0 5.1043,5.7357,1 1.5079,5.8622,0 3.9799,5.8622,1 0.2678,6.9931,0 4.5288,6.9931,1 0.9726,3.6268,0 4.106,3.6268,0 2.5389,3.3884,0 4.7555,3.3884,1 2.473,5.6404,0 4.7977,5.6404,1

Open the csv file, parse it and add the data to a list.

f = open('data.csv') data = [] # consume the header line f.readline() # read and parse the lines for line in f: (x1,x2,y) = line.split(',') x1 = float(x1) x2 = float(x2) y = int(y) data.append((x1,x2,y)) print data plot([x1 for (x1,x2,y) in data if y==0], [x2 for (x1,x2,y) in data if y==0], 'o', mec='r', mfc='r') plot([x1 for (x1,x2,y) in data if y==1], [x2 for (x1,x2,y) in data if y==1], 'o', mec='g', mfc='none')

Perceptron implementation. w is the list of weights, where w[0] is the thresholdthreshold value.

The predict function should return 0 if i=1nwixi<threshold\sum_{i=1}^n{w_ix_i} < threshold and 1 otherwise.

The fit function should update the weights with values able to separate linearly the two classes. You can use this algorithm to implement it:

For each example j in our training set, perform this: If the target value (y) is different from the predicted value (i.e., an error occurred), update the weights with WW+yXW \leftarrow W + yX. Do it until you have no more errors.

w = [0, 0, 0] def predict(x1,x2): # YOUR CODE HERE ypred = w[0]*1+w[1]*x1+w[2]*x2 if ypred>0: return 1 else: return 0 def fit(data): # YOUR CODE HERE while True: error = False for x1, x2, y in data: ytrue = 1 if y>0 else -1 ypred = 1 if predict(x1,x2)>0 else -1 if ytrue != ypred: error = True w[1]=w[1]+ytrue*x1 w[2]=w[2]+ytrue*x2 w[0]=w[0]+ytrue*1 if not error: break fit(data) # Check if the model is correct for (x1, x2, y) in data: if predict(x1,x2) != y: print 'FAIL' break else: print 'SUCCESS!'
def f(x): return -(w[1]*x+w[0])/w[2] #stiamo plottando x2 in funzione di x1 ponendo y=0 da ypred = w[0] + w[1]*x1 + w[2]*x2 x=range(2,6) plot([x1 for (x1,x2,y) in data if y==0], [x2 for (x1,x2,y) in data if y==0], 'o', mec='r', mfc='none') plot([x1 for (x1,x2,y) in data if y==1], [x2 for (x1,x2,y) in data if y==1], 'o', mec='g', mfc='none') plot(x, [f(xi) for xi in x])
x