In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
# sensor_code: 4-character string
# position: float
# value: float
samples = np.zeros((6, ), dtype=[('sensor_code', 'S4'), ('position', float), ('value', float)])
In [3]:
samples[0]
Out[3]:
In [4]:
samples.ndim
Out[4]:
In [5]:
samples.shape
Out[5]:
In [6]:
samples.dtype
Out[6]:
In [7]:
samples[:] = [('ALFA', 1, 0.37), ('BETA', 1, 0.11), ('TAU', 1, 0.13),
('ALFA', 1.5, 0.37), ('ALFA', 3, 0.11), ('TAU', 1.2, 0.13)]
In [8]:
samples
Out[8]:
In [9]:
samples['sensor_code']
Out[9]:
In [10]:
samples['position']
Out[10]:
In [11]:
samples[samples['sensor_code'] == 'ALFA']
Out[11]:
In [12]:
x = np.ma.array([1, 2, 3, 4], mask=[0, 1, 0, 1])
In [13]:
x
Out[13]:
In [14]:
y = np.ma.array([1, 2, 3, 4], mask=[0, 1, 1, 1])
In [15]:
y
Out[15]:
In [16]:
x + y
Out[16]:
In [17]:
np.ma.sqrt([1, -1, 2, -2])
Out[17]:
In [18]:
p = np.poly1d([3, 2, -1])
In [19]:
p.roots
Out[19]:
In [20]:
p.order
Out[20]:
In [21]:
x = np.linspace(0, 1, 20)
In [22]:
y = np.cos(x) + 0.3 * np.random.rand(20)
In [23]:
p = np.poly1d(np.polyfit(x, y, 3))
In [24]:
t = np.linspace(0, 1, 200)
In [25]:
plt.plot(x, y, 'o', t, p(t), '-')
Out[25]:
More polynomials
In [26]:
p = np.polynomial.Polynomial([-1, 2, 3]) # coefs in diff order!
In [27]:
t = np.linspace(0, 1, 200)
In [28]:
plt.plot(t, p(t), '-')
Out[28]:
Chebyshev basis
In [29]:
x = np.linspace(-1, 1, 2000)
y = np.cos(x) + 0.3 * np.random.rand(2000)
In [30]:
p = np.polynomial.Chebyshev.fit(x, y, 50)
In [31]:
t = np.linspace(-1, 1, 200)
In [32]:
plt.plot(x, y, 'r.')
Out[32]:
In [33]:
plt.plot(t, p(t), 'k-', lw=3)
Out[33]:
In [34]:
from mpl_toolkits.mplot3d import Axes3D
In [35]:
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot')
Out[35]:
In [36]:
from scipy import io as spio
In [37]:
a = np.ones((3, 3))
b = np.ones(3)
In [38]:
spio.savemat('file.mat', {'a': a, 'b': b})
In [39]:
data = spio.loadmat('file.mat')
In [40]:
# matlab does not represent 1D arrays so data['b'] will be 2D array
data
Out[40]:
In [41]:
from scipy import linalg
In [42]:
arr = np.array([[1, 2], [3, 4]])
In [43]:
# compute the determinant of a square matrix
linalg.det(arr)
Out[43]:
In [44]:
# compute inverse of a square matrix
linalg.inv(arr)
Out[44]:
In [45]:
np.allclose(np.dot(arr, linalg.inv(arr)), np.eye(2))
Out[45]:
In [46]:
# compute singular-value decomposition (SVD)
arr = np.arange(9).reshape((3, 3)) + np.diag([1, 0, 1])
uarr, spec, vharr = linalg.svd(arr)
In [47]:
svd_mat = uarr.dot(np.diag(spec)).dot(vharr)
In [48]:
np.allclose(svd_mat, arr)
Out[48]:
In [49]:
measured_time = np.linspace(0, 1, 10)
noise = (np.random.random(10) * 2 - 1) * 1e-1
measures = np.sin(2 * np.pi * measured_time) + noise
In [50]:
from scipy.interpolate import interp1d
In [51]:
linear_interp = interp1d(measured_time, measures)
In [52]:
cubic_interp = interp1d(measured_time, measures, kind='cubic')
In [53]:
t = np.linspace(0, 1, 50)
plt.plot(measured_time, measures, 'ro', t, linear_interp(t), 'm-', t, cubic_interp(t), 'g--')
Out[53]:
In [54]:
from scipy import optimize
In [55]:
x_data = np.linspace(-5, 5, num=50)
y_data = 2.9 * np.sin(1.5 * x_data) + np.random.normal(size=50)
In [56]:
plt.plot(x_data, y_data, 'o')
Out[56]:
In [57]:
def test_func(x, a, b):
return a * np.sin(b * x)
In [58]:
params, params_covariance = optimize.curve_fit(test_func, x_data, y_data, p0=[2, 2])
In [59]:
print("params", params)
In [60]:
def f(x):
return x**2 + 10. * np.sin(x)
In [61]:
x = np.arange(-10, 10, 0.1)
# global minimum around -1.3 and a local minimum around 3.8
plt.plot(x, f(x))
Out[61]:
In [62]:
# cost 18 functions evaluation
optimize.minimize(f, x0=0)
Out[62]:
In [63]:
# cost 12 functions evaluation
optimize.minimize(f, x0=0, method='L-BFGS-B')
Out[63]:
In [64]:
# Stuck at local minima
optimize.minimize(f, x0=8)
Out[64]:
Use basinhopping() -> combine optimizer with sampling of starting points
In [65]:
optimize.basinhopping(f, 8, 1000)
Out[65]:
Constrainst: We can constrain the variable to the interval (0, 10) using the bounds argument
In [66]:
optimize.minimize(f, x0=1, bounds=((0, 10), ))
Out[66]:
To find a root, i.e a point where f(x) = 0, use scipy.optimize.root()
Note: only one root is found
In [67]:
optimize.root(f, x0=1)
Out[67]:
In [68]:
from scipy import stats
In [69]:
samples = np.random.normal(size=1000)
In [70]:
bins = np.arange(-4, 5)
print(bins)
In [71]:
histogram = np.histogram(samples, bins=bins, normed=True)[0]
In [72]:
histogram
Out[72]:
In [73]:
bins = 0.5 * (bins[1:] + bins[:-1])
print(bins)
In [74]:
pdf = stats.norm.pdf(bins)
In [75]:
plt.plot(bins, histogram)
Out[75]:
In [76]:
plt.plot(bins, pdf)
Out[76]:
In [87]:
waveform_1 = np.load("data/waveform_2.npy")
In [88]:
t = np.arange(len(waveform_1))
In [89]:
plt.plot(t, waveform_1)
Out[89]:
Fitting a waveform with a simple Gaussian model
In [90]:
def model(t, coeffs):
return coeffs[0] + coeffs[1] * np.exp( - ((t-coeffs[2]) / coeffs[3]) ** 2)
In [91]:
x0 = np.array([3, 30, 15, 1], dtype=float)
In [92]:
def residuals(coeffs, y, t):
return y - model(t, coeffs)
In [93]:
from scipy.optimize import leastsq
In [94]:
x, flag = leastsq(residuals, x0, args=(waveform_1, t))
In [95]:
x
Out[95]:
In [96]:
plt.plot(t, waveform_1, t, model(t, x))
Out[96]:
In [ ]: