π Python programming workbook for machine learning with NumPy and SciPy (2024)
1β£ Join Channel Download:
https://t.iss.one/+MhmkscCzIYQ2MmM8
2β£ Download Book: https://t.iss.one/c/1854405158/1908
π¬ Tags: #numpy #SciPy
β USEFUL CHANNELS FOR YOU βοΈ
1β£ Join Channel Download:
https://t.iss.one/+MhmkscCzIYQ2MmM8
2β£ Download Book: https://t.iss.one/c/1854405158/1908
π¬ Tags: #numpy #SciPy
β USEFUL CHANNELS FOR YOU βοΈ
π7π₯2
Forwarded from Python | Machine Learning | Coding | R
SciPy.pdf
206.4 KB
Unlock the full power of SciPy with my comprehensive cheat sheet!
Master essential functions for:
Function optimization and solving equations
Linear algebra operations
ODE integration and statistical analysis
Signal processing and spatial data manipulation
Data clustering and distance computation ...and much more!
π― BEST DATA SCIENCE CHANNELS ON TELEGRAM π
Master essential functions for:
Function optimization and solving equations
Linear algebra operations
ODE integration and statistical analysis
Signal processing and spatial data manipulation
Data clustering and distance computation ...and much more!
#Python #SciPy #MachineLearning #DataScience #CheatSheet #ArtificialIntelligence #Optimization #LinearAlgebra #SignalProcessing #BigData
Please open Telegram to view this post
VIEW IN TELEGRAM
π5
Topic: Python SciPy β From Easy to Top: Part 1 of 6: Introduction and Basics
---
1. What is SciPy?
β’ SciPy is an open-source Python library used for scientific and technical computing.
β’ Built on top of NumPy, it provides many user-friendly and efficient numerical routines such as routines for numerical integration, optimization, interpolation, eigenvalue problems, algebraic equations, and others.
---
2. Installing SciPy
If you donβt have SciPy installed yet, use:
---
3. Importing SciPy Modules
SciPy is organized into sub-packages for different tasks. Example:
---
4. Key SciPy Sub-packages
β’
β’
β’
β’
β’
β’
---
5. Basic Example: Numerical Integration
Calculate the integral of sin(x) from 0 to pi:
---
6. Basic Example: Root Finding
Find the root of the function f(x) = x^2 - 4:
---
7. SciPy vs NumPy
β’ NumPy focuses on basic array operations and linear algebra.
β’ SciPy extends functionality with advanced scientific algorithms.
---
8. Summary
β’ SciPy is essential for scientific computing in Python.
β’ It contains many specialized sub-packages.
β’ Understanding SciPyβs structure helps solve complex numerical problems easily.
---
Exercise
β’ Calculate the integral of e^(-x^2) from -infinity to +infinity using
β’ Find the root of cos(x) - x = 0 using
---
#Python #SciPy #ScientificComputing #NumericalIntegration #Optimization
https://t.iss.one/DataScienceM
---
1. What is SciPy?
β’ SciPy is an open-source Python library used for scientific and technical computing.
β’ Built on top of NumPy, it provides many user-friendly and efficient numerical routines such as routines for numerical integration, optimization, interpolation, eigenvalue problems, algebraic equations, and others.
---
2. Installing SciPy
If you donβt have SciPy installed yet, use:
pip install scipy
---
3. Importing SciPy Modules
SciPy is organized into sub-packages for different tasks. Example:
import scipy.integrate
import scipy.optimize
import scipy.linalg
---
4. Key SciPy Sub-packages
β’
scipy.integrate β Numerical integration and ODE solvers.β’
scipy.optimize β Optimization and root finding.β’
scipy.linalg β Linear algebra routines (more advanced than NumPyβs).β’
scipy.signal β Signal processing.β’
scipy.fft β Fast Fourier Transforms.β’
scipy.stats β Statistical functions.---
5. Basic Example: Numerical Integration
Calculate the integral of sin(x) from 0 to pi:
import numpy as np
from scipy import integrate
result, error = integrate.quad(np.sin, 0, np.pi)
print("Integral of sin(x) from 0 to pi:", result)
---
6. Basic Example: Root Finding
Find the root of the function f(x) = x^2 - 4:
from scipy import optimize
def f(x):
return x**2 - 4
root = optimize.root_scalar(f, bracket=[0, 3])
print("Root:", root.root)
---
7. SciPy vs NumPy
β’ NumPy focuses on basic array operations and linear algebra.
β’ SciPy extends functionality with advanced scientific algorithms.
---
8. Summary
β’ SciPy is essential for scientific computing in Python.
β’ It contains many specialized sub-packages.
β’ Understanding SciPyβs structure helps solve complex numerical problems easily.
---
Exercise
β’ Calculate the integral of e^(-x^2) from -infinity to +infinity using
scipy.integrate.quad.β’ Find the root of cos(x) - x = 0 using
scipy.optimize.root_scalar.---
#Python #SciPy #ScientificComputing #NumericalIntegration #Optimization
https://t.iss.one/DataScienceM
β€3π₯1
Topic: Python SciPy β From Easy to Top: Part 2 of 6: Numerical Integration and Differentiation
---
1. Numerical Integration Overview
β’ Numerical integration approximates the area under curves when an exact solution is difficult or impossible.
β’ SciPy provides several methods like quad, dblquad, and trapz.
---
2. Using `scipy.integrate.quad`
This function computes the definite integral of a function of one variable.
Example: Integrate cos(x) from 0 to pi divided by 2
---
3. Double Integration with `dblquad`
Integrate a function of two variables over a rectangular region.
Example: Integrate f(x, y) = x times y over x from 0 to 1, y from 0 to 2
---
4. Using the Trapezoidal Rule: `trapz`
Useful for integrating discrete data points.
Example:
---
5. Numerical Differentiation with `derivative`
SciPyβs
Example: Derivative of sin(x) at x equals pi divided by 4
---
6. Limitations of `derivative`
β’
β’ Suitable for simple derivative calculations but not for complex cases.
---
7. Summary
β’
β’
β’
β’
---
Exercise
β’ Compute the integral of e to the power of negative x squared from 0 to 1 using
β’ Calculate the derivative of cos(x) at 0.
β’ Use
---
#Python #SciPy #NumericalIntegration #Differentiation #ScientificComputing
https://t.iss.one/DataScienceM
---
1. Numerical Integration Overview
β’ Numerical integration approximates the area under curves when an exact solution is difficult or impossible.
β’ SciPy provides several methods like quad, dblquad, and trapz.
---
2. Using `scipy.integrate.quad`
This function computes the definite integral of a function of one variable.
Example: Integrate cos(x) from 0 to pi divided by 2
import numpy as np
from scipy import integrate
result, error = integrate.quad(np.cos, 0, np.pi/2)
print("Integral of cos(x) from 0 to pi/2:", result)
---
3. Double Integration with `dblquad`
Integrate a function of two variables over a rectangular region.
Example: Integrate f(x, y) = x times y over x from 0 to 1, y from 0 to 2
def f(x, y):
return x * y
result, error = integrate.dblquad(f, 0, 1, lambda x: 0, lambda x: 2)
print("Double integral result:", result)
---
4. Using the Trapezoidal Rule: `trapz`
Useful for integrating discrete data points.
Example:
import numpy as np
from scipy import integrate
x = np.linspace(0, np.pi, 100)
y = np.sin(x)
area = integrate.trapz(y, x)
print("Approximate integral using trapz:", area)
---
5. Numerical Differentiation with `derivative`
SciPyβs
derivative function approximates the derivative of a function at a point.Example: Derivative of sin(x) at x equals pi divided by 4
from scipy.misc import derivative
import numpy as np
def f(x):
return np.sin(x)
dx = derivative(f, np.pi/4, dx=1e-6)
print("Derivative of sin(x) at pi/4:", dx)
---
6. Limitations of `derivative`
β’
derivative uses finite difference methods, which can be noisy for non-smooth functions.β’ Suitable for simple derivative calculations but not for complex cases.
---
7. Summary
β’
quad is powerful for one-dimensional definite integrals.β’
dblquad handles two-variable integration.β’
trapz approximates integration from sampled data.β’
derivative provides numerical differentiation.---
Exercise
β’ Compute the integral of e to the power of negative x squared from 0 to 1 using
quad.β’ Calculate the derivative of cos(x) at 0.
β’ Use
trapz to approximate the integral of x squared over \[0, 5] using 50 points.---
#Python #SciPy #NumericalIntegration #Differentiation #ScientificComputing
https://t.iss.one/DataScienceM
β€5
Topic: Python SciPy β From Easy to Top: Part 3 of 6: Optimization Basics
---
1. What is Optimization?
β’ Optimization is the process of finding the minimum or maximum of a function.
β’ SciPy provides tools to solve these problems efficiently.
---
2. Using `scipy.optimize.minimize`
This function minimizes a scalar function of one or more variables.
Example: Minimize the function f(x) = (x - 3)^2
---
**3. Minimizing Multivariable Functions**
Example: Minimize f(x, y) = (x - 2)^2 + (y + 3)^2
---
**4. Using Bounds and Constraints**
You can restrict the variables within bounds or constraints.
Example: Minimize f(x) = (x - 3)^2 with x between 0 and 5
---
5. Root Finding with `optimize.root_scalar`
Find a root of a scalar function.
Example: Find root of f(x) = x^3 - 1 between 0 and 2
---
6. Summary
β’ SciPyβs optimization tools help find minima, maxima, and roots.
β’ Supports single and multivariable problems with constraints.
---
Exercise
β’ Minimize the function f(x) = x^4 - 3x^3 + 2 over the range \[-2, 3].
β’ Find the root of f(x) = cos(x) - x near x=1.
---
#Python #SciPy #Optimization #RootFinding #ScientificComputing
https://t.iss.one/DataScienceM
---
1. What is Optimization?
β’ Optimization is the process of finding the minimum or maximum of a function.
β’ SciPy provides tools to solve these problems efficiently.
---
2. Using `scipy.optimize.minimize`
This function minimizes a scalar function of one or more variables.
Example: Minimize the function f(x) = (x - 3)^2
from scipy import optimize
def f(x):
return (x - 3)**2
result = optimize.minimize(f, x0=0)
print("Minimum value:", result.fun)
print("At x =", result.x)
---
**3. Minimizing Multivariable Functions**
Example: Minimize f(x, y) = (x - 2)^2 + (y + 3)^2
def f(vars):
x, y = vars
return (x - 2)**2 + (y + 3)**2
result = optimize.minimize(f, x0=[0, 0])
print("Minimum value:", result.fun)
print("At x, y =", result.x)
---
**4. Using Bounds and Constraints**
You can restrict the variables within bounds or constraints.
Example: Minimize f(x) = (x - 3)^2 with x between 0 and 5
result = optimize.minimize(f, x0=0, bounds=[(0, 5)])
print("Minimum with bounds:", result.fun)
print("At x =", result.x)
---
5. Root Finding with `optimize.root_scalar`
Find a root of a scalar function.
Example: Find root of f(x) = x^3 - 1 between 0 and 2
def f(x):
return x**3 - 1
root = optimize.root_scalar(f, bracket=[0, 2])
print("Root:", root.root)
---
6. Summary
β’ SciPyβs optimization tools help find minima, maxima, and roots.
β’ Supports single and multivariable problems with constraints.
---
Exercise
β’ Minimize the function f(x) = x^4 - 3x^3 + 2 over the range \[-2, 3].
β’ Find the root of f(x) = cos(x) - x near x=1.
---
#Python #SciPy #Optimization #RootFinding #ScientificComputing
https://t.iss.one/DataScienceM
β€3
Topic: Python SciPy β From Easy to Top: Part 4 of 6: Linear Algebra with SciPy
---
1. Introduction to Linear Algebra in SciPy
β’ Linear algebra is fundamental in scientific computing, machine learning, and data science.
β’ SciPy provides advanced linear algebra routines built on top of LAPACK and BLAS libraries.
β’ The main sub-package is
---
2. Basic Matrix Operations
You can create matrices using NumPy arrays:
---
3. Matrix Addition and Multiplication
---
4. Using `scipy.linalg` for Advanced Operations
Import SciPy linear algebra module:
---
5. Matrix Inverse
Calculate the inverse of a matrix (if invertible):
---
6. Determinant
Calculate the determinant:
---
7. Eigenvalues and Eigenvectors
Find eigenvalues and eigenvectors:
---
8. Solving Linear Systems
Solve
---
9. Singular Value Decomposition (SVD)
Decompose matrix A into U, Ξ£, and V^T:
---
10. LU Decomposition
Decompose matrix A into lower and upper triangular matrices:
---
11. QR Decomposition
Factorize A into Q and R matrices:
---
12. Norms of Vectors and Matrices
Calculate different norms:
---
13. Checking if a Matrix is Positive Definite
Try Cholesky decomposition:
---
14. Summary
β’ SciPyβs
β’ Operations include inverse, determinant, eigenvalues, decompositions, and solving linear systems.
β’ These tools are essential for many scientific and engineering problems.
---
Exercise
β’ Compute the eigenvalues and eigenvectors of the matrix \[\[4, 2], \[1, 3]].
β’ Solve the system of equations represented by:
ββ2x + 3y = 8
ββ5x + 4y = 13
β’ Perform SVD on the matrix \[\[1, 0], \[0, -1]] and explain the singular values.
---
#Python #SciPy #LinearAlgebra #SVD #Decomposition #ScientificComputing
https://t.iss.one/DataScienceM
---
1. Introduction to Linear Algebra in SciPy
β’ Linear algebra is fundamental in scientific computing, machine learning, and data science.
β’ SciPy provides advanced linear algebra routines built on top of LAPACK and BLAS libraries.
β’ The main sub-package is
scipy.linalg which extends NumPyβs linear algebra capabilities.---
2. Basic Matrix Operations
You can create matrices using NumPy arrays:
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
---
3. Matrix Addition and Multiplication
# Addition
C = A + B
print("Matrix Addition:\n", C)
# Element-wise Multiplication
D = A * B
print("Element-wise Multiplication:\n", D)
# Matrix Multiplication
E = np.dot(A, B)
print("Matrix Multiplication:\n", E)
---
4. Using `scipy.linalg` for Advanced Operations
Import SciPy linear algebra module:
from scipy import linalg
---
5. Matrix Inverse
Calculate the inverse of a matrix (if invertible):
inv_A = linalg.inv(A)
print("Inverse of A:\n", inv_A)
---
6. Determinant
Calculate the determinant:
det_A = linalg.det(A)
print("Determinant of A:", det_A)
---
7. Eigenvalues and Eigenvectors
Find eigenvalues and eigenvectors:
eigvals, eigvecs = linalg.eig(A)
print("Eigenvalues:\n", eigvals)
print("Eigenvectors:\n", eigvecs)
---
8. Solving Linear Systems
Solve
Ax = b where b is a vector:b = np.array([5, 11])
x = linalg.solve(A, b)
print("Solution x:\n", x)
---
9. Singular Value Decomposition (SVD)
Decompose matrix A into U, Ξ£, and V^T:
U, s, VT = linalg.svd(A)
print("U matrix:\n", U)
print("Singular values:", s)
print("V^T matrix:\n", VT)
---
10. LU Decomposition
Decompose matrix A into lower and upper triangular matrices:
P, L, U = linalg.lu(A)
print("P matrix:\n", P)
print("L matrix:\n", L)
print("U matrix:\n", U)
---
11. QR Decomposition
Factorize A into Q and R matrices:
Q, R = linalg.qr(A)
print("Q matrix:\n", Q)
print("R matrix:\n", R)
---
12. Norms of Vectors and Matrices
Calculate different norms:
# Vector norm
v = np.array([1, -2, 3])
norm_v = linalg.norm(v)
print("Vector norm:", norm_v)
# Matrix norm (Frobenius norm)
norm_A = linalg.norm(A, 'fro')
print("Matrix Frobenius norm:", norm_A)
---
13. Checking if a Matrix is Positive Definite
Try Cholesky decomposition:
try:
L = linalg.cholesky(A)
print("Matrix is positive definite")
except linalg.LinAlgError:
print("Matrix is not positive definite")
---
14. Summary
β’ SciPyβs
linalg module provides extensive linear algebra tools beyond NumPy.β’ Operations include inverse, determinant, eigenvalues, decompositions, and solving linear systems.
β’ These tools are essential for many scientific and engineering problems.
---
Exercise
β’ Compute the eigenvalues and eigenvectors of the matrix \[\[4, 2], \[1, 3]].
β’ Solve the system of equations represented by:
ββ2x + 3y = 8
ββ5x + 4y = 13
β’ Perform SVD on the matrix \[\[1, 0], \[0, -1]] and explain the singular values.
---
#Python #SciPy #LinearAlgebra #SVD #Decomposition #ScientificComputing
https://t.iss.one/DataScienceM
β€8
Topic: Python SciPy β From Easy to Top: Part 5 of 6: Working with SciPy Statistics
---
1. Introduction to `scipy.stats`
β’ The
β’ You can perform tasks like descriptive statistics, hypothesis testing, sampling, and fitting distributions.
---
2. Descriptive Statistics
Use these functions to summarize and describe data characteristics:
---
3. Probability Distributions
SciPy has built-in continuous and discrete distributions such as normal, binomial, Poisson, etc.
Normal Distribution Example
---
4. Hypothesis Testing
One-sample t-test β test if the mean of a sample is equal to a known value:
Interpretation: If the p-value is less than 0.05, reject the null hypothesis.
---
5. Two-sample t-test
Test if two samples come from populations with equal means:
---
6. Chi-Square Test for Independence
Use to test independence between two categorical variables:
---
7. Correlation and Covariance
Measure linear relationship between variables:
Covariance:
---
8. Fitting Distributions to Data
You can fit a distribution to real-world data:
---
9. Sampling from Distributions
Generate random numbers from different distributions:
---
10. Summary
β’
β’ You can compute summaries, perform tests, model distributions, and generate random samples.
---
Exercise
β’ Generate 1000 samples from a normal distribution and compute mean, median, std, and mode.
β’ Test if a sample has a mean significantly different from 5.
β’ Fit a normal distribution to your own dataset and plot the histogram with the fitted PDF curve.
---
#Python #SciPy #Statistics #HypothesisTesting #DataAnalysis
https://t.iss.one/DataScienceM
---
1. Introduction to `scipy.stats`
β’ The
scipy.stats module contains a large number of probability distributions and statistical functions.β’ You can perform tasks like descriptive statistics, hypothesis testing, sampling, and fitting distributions.
---
2. Descriptive Statistics
Use these functions to summarize and describe data characteristics:
from scipy import stats
import numpy as np
data = [2, 4, 4, 4, 5, 5, 7, 9]
mean = np.mean(data)
median = np.median(data)
mode = stats.mode(data, keepdims=True)
std_dev = np.std(data)
print("Mean:", mean)
print("Median:", median)
print("Mode:", mode.mode[0])
print("Standard Deviation:", std_dev)
---
3. Probability Distributions
SciPy has built-in continuous and discrete distributions such as normal, binomial, Poisson, etc.
Normal Distribution Example
from scipy.stats import norm
# PDF at x = 0
print("PDF at 0:", norm.pdf(0, loc=0, scale=1))
# CDF at x = 1
print("CDF at 1:", norm.cdf(1, loc=0, scale=1))
# Generate 5 random numbers
samples = norm.rvs(loc=0, scale=1, size=5)
print("Random Samples:", samples)
---
4. Hypothesis Testing
One-sample t-test β test if the mean of a sample is equal to a known value:
sample = [5.1, 5.3, 5.5, 5.7, 5.9]
t_stat, p_val = stats.ttest_1samp(sample, popmean=5.0)
print("T-statistic:", t_stat)
print("P-value:", p_val)
Interpretation: If the p-value is less than 0.05, reject the null hypothesis.
---
5. Two-sample t-test
Test if two samples come from populations with equal means:
group1 = [20, 22, 19, 24, 25]
group2 = [28, 27, 26, 30, 31]
t_stat, p_val = stats.ttest_ind(group1, group2)
print("T-statistic:", t_stat)
print("P-value:", p_val)
---
6. Chi-Square Test for Independence
Use to test independence between two categorical variables:
# Example contingency table
data = [[10, 20], [20, 40]]
chi2, p, dof, expected = stats.chi2_contingency(data)
print("Chi-square statistic:", chi2)
print("P-value:", p)
---
7. Correlation and Covariance
Measure linear relationship between variables:
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
corr, _ = stats.pearsonr(x, y)
print("Pearson Correlation Coefficient:", corr)
Covariance:
cov_matrix = np.cov(x, y)
print("Covariance Matrix:\n", cov_matrix)
---
8. Fitting Distributions to Data
You can fit a distribution to real-world data:
data = np.random.normal(loc=50, scale=10, size=1000)
params = norm.fit(data) # returns mean and std dev
print("Fitted mean:", params[0])
print("Fitted std dev:", params[1])
---
9. Sampling from Distributions
Generate random numbers from different distributions:
# Binomial distribution
samples = stats.binom.rvs(n=10, p=0.5, size=10)
print("Binomial Samples:", samples)
# Poisson distribution
samples = stats.poisson.rvs(mu=3, size=10)
print("Poisson Samples:", samples)
---
10. Summary
β’
scipy.stats is a powerful tool for statistical analysis.β’ You can compute summaries, perform tests, model distributions, and generate random samples.
---
Exercise
β’ Generate 1000 samples from a normal distribution and compute mean, median, std, and mode.
β’ Test if a sample has a mean significantly different from 5.
β’ Fit a normal distribution to your own dataset and plot the histogram with the fitted PDF curve.
---
#Python #SciPy #Statistics #HypothesisTesting #DataAnalysis
https://t.iss.one/DataScienceM
β€3
Topic: Python SciPy β From Easy to Top: Part 6 of 6: Signal Processing, Interpolation, and Fourier Transforms
---
1. Introduction
SciPy contains powerful tools for signal processing, interpolation, and Fourier transforms. These are essential in fields like image and audio processing, scientific simulations, and data smoothing.
Main submodules covered in this part:
β’
β’
β’
---
### 2. Signal Processing with `scipy.signal`
Filtering a Signal:
Letβs create a noisy sine wave and apply a low-pass filter.
---
Find Peaks in a Signal:
---
### 3. Fourier Transform with `scipy.fft`
The Fourier Transform breaks a signal into its frequency components.
---
### 4. Interpolation with `scipy.interpolate`
Interpolation estimates unknown values between known data points.
---
### 5. 2D Interpolation Example
---
### 6. Summary
β’
β’
β’
These tools are critical for real-time data analysis, image/audio processing, and engineering applications.
---
Exercise
β’ Generate a noisy signal and apply both low-pass and high-pass filters.
β’ Plot the Fourier transform of a composed signal of multiple frequencies.
β’ Perform cubic interpolation on a dataset with missing values and plot both.
---
#Python #SciPy #SignalProcessing #FFT #Interpolation #ScientificComputing
https://t.iss.one/DataScienceM
---
1. Introduction
SciPy contains powerful tools for signal processing, interpolation, and Fourier transforms. These are essential in fields like image and audio processing, scientific simulations, and data smoothing.
Main submodules covered in this part:
β’
scipy.signal β Signal processingβ’
scipy.fft β Fast Fourier Transformβ’
scipy.interpolate β Data interpolation and curve fitting---
### 2. Signal Processing with `scipy.signal`
Filtering a Signal:
Letβs create a noisy sine wave and apply a low-pass filter.
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
# Create a sample signal with noise
t = np.linspace(0, 1.0, 200)
x = np.sin(2 * np.pi * 5 * t) + 0.5 * np.random.randn(200)
# Apply a Butterworth low-pass filter
b, a = signal.butter(3, 0.2)
filtered = signal.filtfilt(b, a, x)
# Plot original and filtered signals
plt.plot(t, x, label="Noisy Signal")
plt.plot(t, filtered, label="Filtered Signal")
plt.legend()
plt.title("Low-pass Filtering with Butterworth")
plt.show()
---
Find Peaks in a Signal:
peaks, _ = signal.find_peaks(x, height=0)
print("Peak Indices:", peaks)
---
### 3. Fourier Transform with `scipy.fft`
The Fourier Transform breaks a signal into its frequency components.
from scipy.fft import fft, fftfreq
# Number of sample points
N = 600
# Sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N, endpoint=False)
y = np.sin(50.0 * 2.0 * np.pi * x) + 0.5 * np.sin(80.0 * 2.0 * np.pi * x)
yf = fft(y)
xf = fftfreq(N, T)[:N//2]
plt.plot(xf, 2.0/N * np.abs(yf[0:N//2]))
plt.grid()
plt.title("Fourier Transform of Signal")
plt.show()
---
### 4. Interpolation with `scipy.interpolate`
Interpolation estimates unknown values between known data points.
from scipy import interpolate
x = np.linspace(0, 10, 10)
y = np.sin(x)
# Create interpolating function
f = interpolate.interp1d(x, y, kind='cubic')
# Interpolate new values
xnew = np.linspace(0, 10, 100)
ynew = f(xnew)
plt.plot(x, y, 'o', label="Data Points")
plt.plot(xnew, ynew, '-', label="Cubic Interpolation")
plt.legend()
plt.title("Interpolation Example")
plt.show()
---
### 5. 2D Interpolation Example
from scipy.interpolate import griddata
# Known points
points = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
values = np.array([0, 1, 1, 0])
# Interpolation grid
grid_x, grid_y = np.mgrid[0:1:100j, 0:1:100j]
grid_z = griddata(points, values, (grid_x, grid_y), method='cubic')
plt.imshow(grid_z.T, extent=(0,1,0,1), origin='lower')
plt.title("2D Cubic Interpolation")
plt.colorbar()
plt.show()
---
### 6. Summary
β’
scipy.signal is used for filtering, finding peaks, convolution, etc.β’
scipy.fft helps analyze signal frequencies.β’
scipy.interpolate estimates unknown values smoothly between data points.These tools are critical for real-time data analysis, image/audio processing, and engineering applications.
---
Exercise
β’ Generate a noisy signal and apply both low-pass and high-pass filters.
β’ Plot the Fourier transform of a composed signal of multiple frequencies.
β’ Perform cubic interpolation on a dataset with missing values and plot both.
---
#Python #SciPy #SignalProcessing #FFT #Interpolation #ScientificComputing
https://t.iss.one/DataScienceM
β€7
π‘ SciPy: Scientific Computing in Python
SciPy is a fundamental library for scientific and technical computing in Python. Built on NumPy, it provides a wide range of user-friendly and efficient numerical routines for tasks like optimization, integration, linear algebra, and statistics.
β’ Optimization:
β’ We provide the function (
β’ The result object (
β’ Numerical Integration:
β’ It returns a tuple containing the integral result and an estimate of the absolute error.
β’ Linear Algebra:
β’
β’ Statistics:
β’
β’ The p-value helps determine if the difference between sample means is statistically significant (a low p-value, e.g., < 0.05, suggests it is).
#SciPy #Python #DataScience #ScientificComputing #Statistics
βββββββββββββββ
By: @DataScienceM β¨
SciPy is a fundamental library for scientific and technical computing in Python. Built on NumPy, it provides a wide range of user-friendly and efficient numerical routines for tasks like optimization, integration, linear algebra, and statistics.
import numpy as np
from scipy.optimize import minimize
# Define a function to minimize: f(x) = (x - 3)^2
def f(x):
return (x - 3)**2
# Find the minimum of the function with an initial guess
res = minimize(f, x0=0)
print(f"Minimum found at x = {res.x[0]:.4f}")
# Output:
# Minimum found at x = 3.0000
β’ Optimization:
scipy.optimize.minimize is used to find the minimum value of a function.β’ We provide the function (
f) and an initial guess (x0=0).β’ The result object (
res) contains the solution in the .x attribute.from scipy.integrate import quad
# Define the function to integrate: f(x) = sin(x)
def integrand(x):
return np.sin(x)
# Integrate sin(x) from 0 to pi
result, error = quad(integrand, 0, np.pi)
print(f"Integral result: {result:.4f}")
print(f"Estimated error: {error:.2e}")
# Output:
# Integral result: 2.0000
# Estimated error: 2.22e-14
β’ Numerical Integration:
scipy.integrate.quad calculates the definite integral of a function over a given interval.β’ It returns a tuple containing the integral result and an estimate of the absolute error.
from scipy.linalg import solve
# Solve the linear system Ax = b
# 3x + 2y = 12
# x - y = 1
A = np.array([[3, 2], [1, -1]])
b = np.array([12, 1])
solution = solve(A, b)
print(f"Solution (x, y): {solution}")
# Output:
# Solution (x, y): [2.8 1.8]
β’ Linear Algebra:
scipy.linalg provides more advanced linear algebra routines than NumPy.β’
solve(A, b) efficiently finds the solution vector x for a system of linear equations defined by a matrix A and a vector b.from scipy import stats
# Create two independent samples
sample1 = np.random.normal(loc=5, scale=2, size=100)
sample2 = np.random.normal(loc=5.5, scale=2, size=100)
# Perform an independent t-test
t_stat, p_value = stats.ttest_ind(sample1, sample2)
print(f"T-statistic: {t_stat:.4f}")
print(f"P-value: {p_value:.4f}")
# Output (will vary):
# T-statistic: -1.7432
# P-value: 0.0829
β’ Statistics:
scipy.stats is a powerful module for statistical analysis.β’
ttest_ind calculates the T-test for the means of two independent samples.β’ The p-value helps determine if the difference between sample means is statistically significant (a low p-value, e.g., < 0.05, suggests it is).
#SciPy #Python #DataScience #ScientificComputing #Statistics
βββββββββββββββ
By: @DataScienceM β¨
β€3
segment = sine_wave[0:51]
windowed_segment = segment * window
VI. Convolution & Correlation
β’ Perform linear convolution.
sig1 = np.repeat([0., 1., 0.], 100)
sig2 = np.repeat([0., 1., 1., 0.], 100)
convolved = signal.convolve(sig1, sig2, mode='same')
β’ Compute cross-correlation.
# Useful for finding delays between signals
correlation = signal.correlate(sig1, sig2, mode='full')
β’ Compute auto-correlation.
# Useful for finding periodicities in a signal
autocorr = signal.correlate(sine_wave, sine_wave, mode='full')
VII. Time-Frequency Analysis
β’ Compute and plot a spectrogram.
f, t_spec, Sxx = signal.spectrogram(chirp_signal, fs)
plt.pcolormesh(t_spec, f, Sxx, shading='gouraud')
plt.show()
β’ Perform Continuous Wavelet Transform (CWT).
widths = np.arange(1, 31)
cwt_matrix = signal.cwt(chirp_signal, signal.ricker, widths)
β’ Perform Hilbert transform to get the analytic signal.
analytic_signal = signal.hilbert(sine_wave)
β’ Calculate instantaneous frequency.
instant_phase = np.unwrap(np.angle(analytic_signal))
instant_freq = (np.diff(instant_phase) / (2.0*np.pi) * fs)
VIII. Feature Extraction
β’ Find peaks in a signal.
peaks, _ = signal.find_peaks(sine_wave, height=0.5)
β’ Find peaks with prominence criteria.
peaks_prom, _ = signal.find_peaks(noisy_signal, prominence=1)
β’ Differentiate a signal (e.g., to find velocity from position).
derivative = np.diff(sine_wave)
β’ Integrate a signal.
from scipy.integrate import cumulative_trapezoid
integral = cumulative_trapezoid(sine_wave, t, initial=0)
β’ Detrend a signal to remove a linear trend.
trend = np.linspace(0, 1, fs)
trended_signal = sine_wave + trend
detrended = signal.detrend(trended_signal)
IX. System Analysis
β’ Define a system via a transfer function (numerator, denominator).
# Example: 2nd order low-pass filter
system = signal.TransferFunction([1], [1, 1, 1])
β’ Compute the step response of a system.
t_step, y_step = signal.step(system)
β’ Compute the impulse response of a system.
t_impulse, y_impulse = signal.impulse(system)
β’ Compute the Bode plot of a system's frequency response.
w, mag, phase = signal.bode(system)
X. Signal Generation from Data
β’ Generate a signal from a function.
t = np.linspace(0, 1, 500)
custom_signal = np.sinc(2 * np.pi * 4 * t)
β’ Convert a list of values to a signal array.
my_data = [0, 1, 2, 3, 2, 1, 0, -1, -2, -1, 0]
data_signal = np.array(my_data)
β’ Read signal data from a WAV file.
from scipy.io import wavfile
samplerate, data = wavfile.read('audio.wav')
β’ Create a pulse train signal.
pulse_train = np.zeros(fs)
pulse_train[::100] = 1 # Impulse every 100 samples
#Python #SignalProcessing #SciPy #NumPy #DSP
βββββββββββββββ
By: @DataScienceM β¨