import time import platform print('Last updated: %s' %time.strftime('%d/%m/%Y')) print('Created using Python', platform.python_version()) %%file hello.py def func_inside_script(x, y): return x + y print('Hello World') %run hello.py func_inside_script(1, 2) %timeit [x**2 for x in range(100)] %timeit -r 5 -n 100 [x**2 for x in range(100)] my_dir = 'new_dir' !mkdir $my_dir !pwd !touch $my_dir'/some.txt' !ls -l './new_dir' !ls -l $my_dir | wc -l def some_func(): var = 'hello world' for i in range(5): print(i) i / 0 return 'finished' %debug some_func() %matplotlib inline import numpy as np from matplotlib import pyplot as plt import math def pdf(x, mu=0, sigma=1): """Calculates the normal distribution's probability density function (PDF). """ term1 = 1.0 / ( math.sqrt(2*np.pi) * sigma ) term2 = np.exp( -0.5 * ( (x-mu)/sigma )**2 ) return term1 * term2 x = np.arange(0, 100, 0.05) pdf1 = pdf(x, mu=5, sigma=2.5**0.5) pdf2 = pdf(x, mu=10, sigma=6**0.5) plt.plot(x, pdf1) plt.plot(x, pdf2) plt.title('Probability Density Functions') plt.ylabel('p(x)') plt.xlabel('random variable x') plt.legend(['pdf1 ~ N($\mu=5$, $\sigma=2.5$)', 'pdf2 ~ N($\mu=10$, $\sigma=6$)'], loc='upper right') plt.ylim([0,0.5]) plt.xlim([0,20]) plt.show() %load_ext cythonmagic %%cython import numpy as np cimport numpy as np cimport cython @cython.boundscheck(False) @cython.wraparound(False) @cython.cdivision(True) cpdef cython_lstsqr(x_ary, y_ary): """ Computes the least-squares solution to a linear matrix equation. """ cdef double x_avg, y_avg, var_x, cov_xy,\ slope, y_interc, temp cdef double[:] x = x_ary # memoryview cdef double[:] y = y_ary cdef unsigned long N, i N = x.shape[0] x_avg = 0 y_avg = 0 for i in range(N): x_avg += x[i] y_avg += y[i] x_avg = x_avg/N y_avg = y_avg/N var_x = 0 cov_xy = 0 for i in range(N): temp = (x[i] - x_avg) var_x += temp**2 cov_xy += temp*(y[i] - y_avg) slope = cov_xy / var_x y_interc = y_avg - slope*x_avg return (slope, y_interc) import numpy as np x_ary = np.array([x_i*np.random.randint(8,12)/10 for x_i in range(100)]) y_ary = np.array([y_i*np.random.randint(10,14)/10 for y_i in range(100)]) cython_lstsqr(x_ary, y_ary) %install_ext https://raw.github.com/mgaitan/fortran_magic/master/fortranmagic.py %load_ext fortranmagic %%fortran SUBROUTINE fortran_lstsqr(ary_x, ary_y, slope, y_interc) ! Computes the least-squares solution to a linear matrix equation. """ IMPLICIT NONE REAL(8), INTENT(in), DIMENSION(:) :: ary_x, ary_y REAL(8), INTENT(out) :: slope, y_interc REAL(8) :: x_avg, y_avg, var_x, cov_xy, temp INTEGER(8) :: N, i N = SIZE(ary_x) x_avg = SUM(ary_x) / N y_avg = SUM(ary_y) / N var_x = 0 cov_xy = 0 DO i = 1, N temp = ary_x(i) - x_avg var_x = var_x + temp**2 cov_xy = cov_xy + (temp*(ary_y(i) - y_avg)) END DO slope = cov_xy / var_x y_interc = y_avg - slope*x_avg END SUBROUTINE fortran_lstsqr import numpy as np x_ary = np.array([x_i*np.random.randint(8,12)/10 for x_i in range(100)]) y_ary = np.array([y_i*np.random.randint(10,14)/10 for y_i in range(100)]) fortran_lstsqr(x_ary, y_ary) %%script perl print 'Hello, World!'; %%perl print 'Hello, World!'; %%ruby puts "Hello, World!" %%bash echo "Hello World!" %%script R --no-save cat("Goodbye, World!\n") def hello_world(): """This is a hello world example function.""" print('Hello, World!') %pdoc hello_world %pdef hello_world %psource math.mean() from math import sqrt