In [1]:
# Versão da Linguagem Python
from platform import python_version
print('Versão da Linguagem Python Usada Neste Jupyter Notebook:', python_version())
Para compreender o SciPy é necessário compreender conceitos avançados de Matemática e Estatística, o que está fora do escopo deste treinamento. Caso queira aprender aplicações práticas do pacote em Machine Learning e IA, consulte estes dois cursos aqui na DSA:
Machine Learning: https://www.datascienceacademy.com.br/pages/curso-machine-learning
Programação Paralela em GPU: https://www.datascienceacademy.com.br/pages/curso-programacao-paralela-em-gpu
Pacote | Descrição |
cluster |
Clustering algorithms |
constants |
Mathematical and physical constants |
fftpack |
Fourier transforms |
integrate |
Numerical integration |
interpolate |
Interpolation |
io |
Input and output |
linalg |
Linear algebra |
maxentropy |
Maximum entropy models |
misc |
Miscellaneous |
ndimage |
Multi-dimensional image processing |
odr |
Orthogonal distance regression |
optimize |
Optimization |
signal |
Signal processing |
sparse |
Sparse matrices |
spatial |
Spatial algorithms and data structures |
special |
Special functions |
stats |
Statistical functions |
stsci |
Image processing |
weave |
C/C++ integration |
In [2]:
import scipy
scipy.__version__
Out[2]:
In [3]:
import matplotlib
matplotlib.__version__
Out[3]:
In [4]:
# Função imread foi descontinuada no Scipy
# from scipy import misc
# misc.imread('Matplotlib-Mapa.png')
# Usamos agora a função do pacote imageio
from imageio import imread
imread('Matplotlib-Mapa.png')
# Matplotlib tem uma função similar
import matplotlib.pyplot as plt
plt.imread('Matplotlib-Mapa.png')
Out[4]:
In [5]:
import numpy as np
np.__version__
Out[5]:
In [6]:
from numpy import *
from scipy.integrate import quad, dblquad, tplquad
In [7]:
# Integração
val, abserr = quad(lambda x: exp(-x ** 2), Inf, Inf)
val, abserr
Out[7]:
In [8]:
from scipy.integrate import odeint, ode
In [9]:
from pylab import *
%matplotlib inline
In [10]:
def dy(y, t, zeta, w0):
x, p = y[0], y[1]
dx = p
dp = -2 * zeta * w0 * p - w0**2 * x
return [dx, dp]
y0 = [1.0, 0.0]
t = linspace(0, 10, 1000)
w0 = 2*pi*1.0
y1 = odeint(dy, y0, t, args=(0.0, w0))
y2 = odeint(dy, y0, t, args=(0.2, w0))
y3 = odeint(dy, y0, t, args=(1.0, w0))
y4 = odeint(dy, y0, t, args=(5.0, w0))
fig, ax = subplots()
ax.plot(t, y1[:,0], 'k', label="Não Abafado", linewidth=0.25)
ax.plot(t, y2[:,0], 'r', label="Pouco Abafado")
ax.plot(t, y3[:,0], 'b', label="Criticamente Abafado")
ax.plot(t, y4[:,0], 'g', label="Perigosamente Abafado")
ax.legend();
In [11]:
from scipy.fftpack import *
In [12]:
# Fourier transformation
N = len(t)
dt = t[1]-t[0]
F = fft(y2[:,0])
w = fftfreq(N, dt)
fig, ax = subplots(figsize=(9,3))
ax.plot(w, abs(F));
In [13]:
A = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]])
b = np.array([2, 4, -1])
In [14]:
# Resolvendo um sistema de equações lineares
x = solve(A, b)
x
Out[14]:
In [15]:
A = rand(3,3)
B = rand(3,3)
evals, evecs = eig(A)
evals
Out[15]:
In [16]:
evecs
Out[16]:
In [17]:
svd(A)
Out[17]:
In [18]:
from scipy import optimize
In [19]:
def f(x):
return 4*x**3 + (x-2)**2 + x**4
fig, ax = subplots()
x = linspace(-5, 3, 100)
ax.plot(x, f(x));
In [20]:
x_min = optimize.fmin_bfgs(f, -0.5)
x_min
Out[20]:
In [21]:
from scipy import stats
In [22]:
Y = stats.norm()
x = linspace(-5,5,100)
fig, axes = subplots(3,1, sharex=True)
axes[0].plot(x, Y.pdf(x))
axes[1].plot(x, Y.cdf(x));
axes[2].hist(Y.rvs(size=1000), bins=50);
In [23]:
Y.mean(), Y.std(), Y.var()
Out[23]:
In [24]:
# T-test
t_statistic, p_value = stats.ttest_ind(Y.rvs(size=1000), Y.rvs(size=1000))
t_statistic, p_value
Out[24]: