Jupyter notebook class01_ipython_notebook.ipynb
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:
This is a markdown cell
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.
Here are two system aliases:
Tab completion:
Tab completion after ( brings up a tooltip with the docstring:
Adding ? opens the docstring in the pager below:
Exceptions are formatted nicely:
---------------------------------------------------------------------------
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:
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:
Beyond a certain point, output will scroll automatically:
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:
Here we go
Sublist
Sublist
There we go
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:
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:
or other languages:
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.
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):
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.
Pandas makes use of this capability to allow DataFrames to be represented as HTML tables.
You can even embed an entire page from another site in an iframe; for example this is today's Wikipedia page for mobile users:
LaTeX
IPython Notebook supports the display of mathematical expressions typeset in LaTeX
EXERCISES: find the error and correct the following statements
EXERCISE: Perceptron
Create a comma-separated values (csv) file with the features (x1, x2) and the target (y)
Open the csv file, parse it and add the data to a list.
Perceptron implementation. w is the list of weights, where w[0] is the value.
The predict function should return 0 if 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 .
Do it until you have no more errors.