Machine Learning with Swift
上QQ阅读APP看书,第一时间看更新

IPython notebook crash course

Feel free to skip this section if you're familiar with the Python and Jupyter notebooks.

IPython notebook and its web-based GUI Jupyter are standard tools for data-driven machine learning development. Jupyter is also a handy tool for learning Python and its libraries. You can combine pieces of code with comments in markdown format. You can also execute pieces of code in place, chaining them one after another, and immediately seeing the results of computations. It also allows to embed interactive charts, tables, videos, and other multimedia objects inside the notebook. We will use Jupyter notebooks for writing quick prototypes of our models.

To create a new notebook, run in the Terminal:

> jupyter notebook  

You will see output similar to this:

[I 10:51:23.269 NotebookApp] Serving notebooks from local directory: ...
[I 10:51:23.269 NotebookApp] 0 active kernels 
[I 10:51:23.270 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/?token=3c073db5636e366fd750e661cc597652025fdbf41162c125
[I 10:51:23.270 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). 

Note those long URLs in the output: http://localhost:8888/token=3c073db5636e366fd750e661cc597652025fdbf41162c125.

Copy and paste this address to your browser to open Jupyter.

With Python 3, Jupyter automatically opens a new tab in your default browser's window, with the address http://localhost:8888/tree.

Press the New button, and choose Python 2 in a drop-down menu. This will open a new notebook in a new browser tab.

To stop IPython, you'll need to press Ctrl + C in the Terminal, and enter y when prompted. Don't forget to save your changes in the notebook before quitting.

Let's try something just to get the idea on how it works. In the top cell of the notebook, print import this and press Shift + Enter. You'll see The Zen of Python—a short list of rules every Python programmer should conform to. We will also try to conform to them. The extended version of Python-style guidelines is known as PEP 8, and can be found here: python.org/dev/peps/pep-0008/.

Type into the new cell:

a = 2**32 
b = 64**(1/2.) 
a = a+b 
a 

Then, press Shift + Enter. This calculates 232 + √64, and stores the results into variable a. Unfamiliar operator ** is a power, a and b are variables (no let or var). Typecasting between integer 1 and float 2 happens implicitly. Python is weak-typed, so you can assign float value of variable b to an integer variable a. Jupyter outputs the value of a last line in the cell. Also, note that variables a and b are available now in the next cells.

If you don't know Python, no worries—it's a relatively simple language. For a crash course on Python, please visit:  https://learnxinyminutes.com/docs/python/.

To see how to add and format comments, place your cursor in the new cell, choose from the drop-down menu in the Instruments panel cell, type Markdown, and put some markdown snippet into the cell; for example, the following snippet is a simple text with MathJax-formatted formula and a picture:

# This is a sample text: 
$$Formula = {Numerator over Denominator}$$ 
![]( https://imgs.xkcd.com/comics/conditional_risk.png) 
> Sample text to demonstrate the few markdown feature available to easily create documents. [Packt Hyperlink](http://packtpub.com/)  

You'll get a nicely formatted MathJax formula, an image, and some formatted text. If you want to know more about markdown format, just Google for a markdown tutorial, or a cheat sheet.

You can also execute bash commands from the notebook; just prepend an exclamation mark to them:

In []: 
! ls 
 
Out[]: 
The content of your work folder goes here...