Tools
Be Pythonic
Speed
In [1]:
import IPython
IPython.__version__
Out[1]:
In [2]:
from IPython.display import YouTubeVideo
YouTubeVideo("05fA_DXgW-Y")
Out[2]:
In [3]:
import numpy
numpy.__version__
Out[3]:
"NumPy is an extension to the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large library of high-level mathematical functions to operate on these arrays.
Using NumPy in Python gives functionality comparable to MATLAB ..."</i>, Wikipedia
In [4]:
from IPython.display import YouTubeVideo
YouTubeVideo("1zmV8lZsHF4")
Out[4]:
In [5]:
import matplotlib
matplotlib.__version__
Out[5]:
matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy.
There is also a procedural "pylab" interface ..., designed to closely resemble that of MATLAB.</i>
In [6]:
from IPython.display import YouTubeVideo
YouTubeVideo("MKucn8NtVeI")
Out[6]:
In [7]:
import scipy
scipy.__version__
Out[7]:
"SciPy contains modules for optimization, linear algebra, integration, interpolation, special functions, FFT, signal and image processing, ODE solvers and other tasks common in science and engineering.", Wikipedia
"Pandas is a software library written for the Python programming language for data manipulation and analysis. In particular, it offers data structures and operations for manipulating numerical tables and time series.", Wikpedia
In [8]:
from IPython.display import YouTubeVideo
YouTubeVideo('0CFFTJUZ2dc')
Out[8]:
"SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible." SymPy
In [9]:
from IPython.display import YouTubeVideo
YouTubeVideo('Lgp442bibDM')
Out[9]:
In [10]:
import h5py
h5py.__version__
Out[10]:
"HDF5 is a data model, library, and file format for storing and managing data. It supports an unlimited variety of datatypes, and is designed for flexible and efficient I/O and for high volume and complex data. HDF5 is portable and is extensible, allowing applications to evolve in their use of HDF5. The HDF5 Technology suite includes tools and applications for managing, manipulating, viewing, and analyzing data in the HDF5 format.", HDF Group
In [11]:
from IPython.display import YouTubeVideo
YouTubeVideo('nddj5OA8LJo')
Out[11]:
In [12]:
import Cython
Cython.__version__
Out[12]:
"Cython is a compiled language that generates CPython extension modules. These extension modules can then be loaded and used by regular Python code using the import statement.
It works by producing a standard Python module. However, the behavior differs from standard Python in that the module code, originally written in Python, is translated into C. While the resulting code is fast, ..." </i>, Wikipedia
In [13]:
from IPython.display import YouTubeVideo
YouTubeVideo('gMvkiQ-gOW8')
Out[13]:
In [3]:
# Import necassary packages
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Set up matpltolib for nice plotting in the notebook
%matplotlib notebook
plt.style.use('seaborn-notebook')
In [4]:
x = np.linspace(0, 4 * np.pi, 200)
rad = x / np.pi
plt.figure(figsize=(12, 3))
line, = plt.plot(rad, 2 * np.sin(x))
plt.ylim(-5, 5)
plt.grid()
plt.tight_layout()
In [5]:
from ipywidgets import interact
@interact(a=(1, 4), f=(0.1, 2, 0.1), phi=(0, 2, 0.1))
def update(a=2, f = 1, phi=0):
line.set_ydata(a * np.sin((x + phi * np.pi) / f))
In [23]:
x = np.linspace(0, 4 * np.pi, 200)
rad = x / np.pi
#Create some noise
noise = 0.75 * np.random.randn(x.size)
# Define differnt harmonic functions
y0 = 1.0 * np.sin(x + 0) + noise
y1 = 1.5 * np.sin(x + np.pi / 2) + noise
y2 = 2.5 * np.sin(x + np.pi) + noise
# Plot everything
fig, axs = plt.subplots(3 , 1, figsize=(12, 6))
axs[0].plot(rad, y0, 'b.')
axs[0].set_xticks([])
axs[1].plot(rad, y1, 'g.')
axs[1].set_xticks([])
axs[2].plot(rad, y2, 'k.')
axs[2].set_xlabel('x / 2$\pi$')
for ax in axs:
ax.set_ylim(-5.5, 5.5)
plt.tight_layout(h_pad=0)
In [24]:
# Define the fit function
def sin(x, a, phi):
return a * np.sin(x + phi)
# Find the fit parameters
(a0, phi0), *err = curve_fit(sin, x, y0)
(a1, phi1), *err = curve_fit(sin, x, y1)
(a2, phi2), *err = curve_fit(sin, x, y2)
# Plot fits into subplots
axs[0].plot(rad, sin(x, a0, phi0), 'r--', lw=3, label='${:.2f} \cdot Sin(x + {:.2f}\pi$)'.format(a0, phi0 / np.pi))
axs[1].plot(rad, sin(x, a1, phi1), 'r--', lw=3, label='${:.2f} \cdot Sin(x + {:.2f}\pi$)'.format(a1, phi1 / np.pi))
axs[2].plot(rad, sin(x, a2, phi2), 'r--', lw=3, label='${:.2f} \cdot Sin(x + {:.2f}\pi$)'.format(a2, phi2 / np.pi))
for ax in axs:
ax.legend(loc=4)