2012/11/04

python tips: set up IPython Notebook on Ubuntu12.04LTS

set up pythonbrew, which makes it easier to maintain multiple python binaries.
# install curl for downloading pythonbrew package
$ sudo apt-get install curl

# install packages for compiling python2.7.3 with pythonbrew
$ sudo apt-get install build-essential, libbz2-dev, libsqlite3-dev, zlib1g-dev, \
                       libxml2-dev, libxslt1-dev, libreadline5, libreadline-dev, \
                       libgdbm-dev, libgdb-dev, libxml2, libssl-dev, tk-dev, \
                       libgdbm-dev, libexpat1-dev, libncursesw5-dev

# install pythonbrew
$ curl -kLO https://github.com/utahta/pythonbrew/raw/master/pythonbrew-install
$ chmod +x pythonbrew-install
$ ./pythonbrew-install

# edit .bashrc then, 
$ source ${HOME}/.pythonbrew/etc/bashrc

# setup python-2.7.3 with pythonbrew
$ pythonbrew install 2.7.3

# change default python binary
$ pybrew switch 2.7.3
set up virtualenv, which provides virtual pythons' executable environment
# install virtualenv
$ pip virtualenv virtualenvwrapper
$ echo "VIRTUALENVWRAPPER_PYTHON=${HOME}/.pythonbrew/pythons/Python-2.7.3/bin/python" >> ~/.bashrc
$ source ${HOME}/.pythonbrew/pythons/Python-2.7.3/bin/virtualenvwrapper.sh
set up ipython notebook.
# get package for pyqt compile
$ sudo apt-get install libqtcore4
$ sudo apt-get install libqt4-dev

# download SIP package from http://www.riverbankcomputing.co.uk/software/sip/download
# install SIP following README
 
# download PyQt package from http://www.riverbankcomputing.co.uk/software/pyqt/download 
# install SIP following README

# install numpy and scipy
$ sudo apt-get install liblapck3gf liblapack-dev libblas3gf libblas-dev gfortran
$ pip install numpy
$ pip install scipy

# install matplotlib
$ sudo apt-get install libpng12-dev libjpeg8-dev libfreetype6-dev
$ pip install matplotlib

# install pyzmq
$ pip install pyzmq

# install tornado
$ pip install tornado

# install pygments
$ pip install pygmenta

# install pandas
$ pip install pandas

# install sympy
$ pip install sympy

# install nose
$ pip install nose

# install ipython
$ pip install ipython
$ easy_install ipython[test]
$ easy_install ipython[zmq,qtconsole,notebook,test]
$ iptest

# install packages for ipython extension
$ sudo apt-get install r-base-core ruby
$ pip install rpy2
$ pip install rtools

2012/10/30

This is just a test of nbconvert.py

In [1]:
%pylab inline
x = linspace(0, 2*pi)
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline].
For more information, type 'help(pylab)'.
In [2]:
plot(x, sin(x), label=r'$\sin(x)$')
plot(x, cos(x), 'ro', label=r'$\cos(x)$')
title(r'Two familiar function')
Out [2]:
<matplotlib.text.Text at 0x105507cd0>
In [3]:
%load_ext sympyprinting
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nose/plugins/manager.py:418: UserWarning: Module dateutil was already imported from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dateutil/__init__.pyc, but /Users/yaboo/src/matplotlib/lib is being added to sys.path
  import pkg_resources
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nose/plugins/manager.py:418: UserWarning: Module six was already imported from /Users/yaboo/src/matplotlib/lib/six.pyc, but /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages is being added to sys.path
  import pkg_resources
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/extensions/sympyprinting.py:119: UserWarning: The sympyprinting extension in IPython is deprecated, use sympy.interactive.ipythonprinting
  warnings.warn("The sympyprinting extension in IPython is deprecated, "
In [4]:
import sympy as sym
from sympy import *
x, y, z = sym.symbols("x y z")
In [5]:
Rational(3,2)*pi + exp(I*x)/(x**2 + y)
Out [5]:
$$\frac{3}{2} \pi + \frac{e^{\mathbf{\imath} x}}{x^{2} + y}$$
In [6]:
eq = ((x+y)**2 * (x+1))
eq
Out [6]:
$$\left(x + 1\right) \left(x + y\right)^{2}$$
In [7]:
expand(eq)
Out [7]:
$$x^{3} + 2 x^{2} y + x^{2} + x y^{2} + 2 x y + y^{2}$$
In [8]:
diff(cos(x**2)**2 / (1 + x), x)
Out [8]:
$$- 4 \frac{x \sin{\left (x^{2} \right )} \cos{\left (x^{2} \right )}}{x + 1} - \frac{\cos^{2}{\left (x^{2} \right )}}{\left(x + 1\right)^{2}}$$

You can italicize, boldface

  • build
  • list
  • and embed code meant for illustration instead of execution in Python:

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

    or other languages:

    In [9]:
    from IPython.display import Image
    Image(filename=u'/Users/yaboo/Downloads/hoge.jpeg')
    
    Out [9]:
    In [10]:
    from IPython.display import YouTubeVideo
    YouTubeVideo('iwVvqwLDsJo')
    
    Out [10]:
    In [11]:
    %%ruby
    puts "Hello from Ruby #{RUBY_VERSION}"
    
    Hello from Ruby 1.8.7
    
    In [12]:
    %%bash
    echo "hello from $BASH"
    
    hello from /bin/bash
    
    In [13]:
    %load_ext rmagic
    X = np.array([0,1,2,3,4])
    Y = np.array([3,5,4,6,7])
    
    In [14]:
    %%R -i X,Y -o XYcoef
    XYlm = lm(Y~X)
    XYcoef = coef(XYlm)
    print(summary(XYlm))
    par(mfrow=c(2,2))
    plot(XYlm)
    
    Call:
    lm(formula = Y ~ X)
    
    Residuals:
       1    2    3    4    5 
    -0.2  0.9 -1.0  0.1  0.2 
    
    Coefficients:
                Estimate Std. Error t value Pr(>|t|)  
    (Intercept)   3.2000     0.6164   5.191   0.0139 *
    X             0.9000     0.2517   3.576   0.0374 *
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
    
    Residual standard error: 0.7958 on 3 degrees of freedom
    Multiple R-squared:  0.81, Adjusted R-squared: 0.7467 
    F-statistic: 12.79 on 1 and 3 DF,  p-value: 0.03739 
    
    
    In [42]:
    XYcoef
    
    Out [42]:
    [ 3.2  0.9]
    In [16]:
    load_ext cythonmagic
    
    In [17]:
    %%cython -lm
    from libc.math cimport sin
    print 'sin(1)=', sin(1)
    
    sin(1)= 0.841470984808
    

    100