|
| 1 | +# Scipy |
| 2 | + |
| 3 | +Scipy is a powerful Python library used for scientific and technical computing. It builds on NumPy and provides a large number of higher-level functions for mathematical operations, including optimization, integration, interpolation, eigenvalue problems, algebraic equations, and more. |
| 4 | + |
| 5 | +**1. Installation:** |
| 6 | +```python |
| 7 | +python -m pip install scipy |
| 8 | +``` |
| 9 | + |
| 10 | +**2. Import and Usage:** |
| 11 | + |
| 12 | +```python |
| 13 | +import numpy as np |
| 14 | +from scipy import linalg, optimize, integrate, interpolate, stats |
| 15 | +``` |
| 16 | + |
| 17 | +**3. Linear Algebra:** |
| 18 | + |
| 19 | +Scipy has many functions for linear algebra operations |
| 20 | + |
| 21 | +- Solving Linear Equations: |
| 22 | +```python |
| 23 | +import numpy as np |
| 24 | +from scipy import linalg, optimize, integrate, interpolate, stats |
| 25 | +# Solving the system of equations Ax = b |
| 26 | +A = np.array([[3, 2], [1, 2]]) |
| 27 | +b = np.array([2, 0]) |
| 28 | + |
| 29 | +x = linalg.solve(A, b) |
| 30 | +print(x) # Solution to the system |
| 31 | +``` |
| 32 | + |
| 33 | +- Eigenvalues and eigenvectors |
| 34 | +```python |
| 35 | +# Finding eigenvalues and eigenvectors |
| 36 | +import numpy as np |
| 37 | +from scipy import linalg, optimize, integrate, interpolate, stats |
| 38 | + |
| 39 | +A = np.array([[3, 2], [4, 1]]) |
| 40 | + |
| 41 | +eigenvalues, eigenvectors = linalg.eig(A) |
| 42 | +print(eigenvalues) # Eigenvalues |
| 43 | +print(eigenvectors) # Eigenvectors |
| 44 | +``` |
| 45 | + |
| 46 | +**4. Optimization:** |
| 47 | + |
| 48 | +- Finding minima of a function: |
| 49 | +```python |
| 50 | +import numpy as np |
| 51 | +from scipy import linalg, optimize, integrate, interpolate, stats |
| 52 | + |
| 53 | +# Minimizing a scalar function |
| 54 | +def f(x): |
| 55 | + return x**2 + 10*np.sin(x) |
| 56 | + |
| 57 | +result = optimize.minimize(f, x0=0) |
| 58 | +print(result.x) # Minimum value of the function |
| 59 | +``` |
| 60 | + |
| 61 | +- Root Finding |
| 62 | +```python |
| 63 | +import numpy as np |
| 64 | +from scipy import linalg, optimize, integrate, interpolate, stats |
| 65 | + |
| 66 | +# Finding roots of a function |
| 67 | +def f(x): |
| 68 | + return x**2 - 4 |
| 69 | + |
| 70 | +root = optimize.root(f, x0=1) |
| 71 | +print(root.x) # Root of the function |
| 72 | +``` |
| 73 | + |
| 74 | +**5. Interpolation:** |
| 75 | + |
| 76 | +Interpolation can be useful when you want to estimate values between known data points. |
| 77 | + |
| 78 | +```python |
| 79 | +import numpy as np |
| 80 | +from scipy import linalg, optimize, integrate, interpolate, stats |
| 81 | + |
| 82 | +# Interpolating data |
| 83 | +x = np.linspace(0, 10, 10) |
| 84 | +y = np.sin(x) |
| 85 | + |
| 86 | +f = interpolate.interp1d(x, y) |
| 87 | +x_new = np.linspace(0, 10, 50) |
| 88 | +y_new = f(x_new) |
| 89 | + |
| 90 | +print(y_new) # Interpolated values |
| 91 | +``` |
| 92 | + |
| 93 | +**6. Statistics:** |
| 94 | + |
| 95 | +Scipy's `stats` module provides a range of statistical functions. |
| 96 | + |
| 97 | +```python |
| 98 | +import numpy as np |
| 99 | +from scipy import linalg, optimize, integrate, interpolate, stats |
| 100 | + |
| 101 | + |
| 102 | +# Calculating descriptive statistics |
| 103 | +data = np.random.normal(loc=0, scale=1, size=1000) |
| 104 | + |
| 105 | +mean = stats.tmean(data) |
| 106 | +std_dev = stats.tstd(data) |
| 107 | + |
| 108 | +print(mean) # Mean of the data |
| 109 | +print(std_dev) # Standard deviation of the data |
| 110 | +``` |
| 111 | + |
| 112 | + |
| 113 | +**7. Signal Processing:** |
| 114 | + |
| 115 | +Scipy's `signal` module is a powerful tool for signal processing tasks in Python. It provides functions for filtering, spectral analysis, interpolation, and other signal-related operations. |
| 116 | + |
| 117 | +- Filtering: |
| 118 | + |
| 119 | +```python |
| 120 | +from scipy import signal |
| 121 | +import numpy as np |
| 122 | +import matplotlib.pyplot as plt |
| 123 | + |
| 124 | +# Generate a noisy signal |
| 125 | +np.random.seed(0) |
| 126 | +t = np.linspace(0, 1, 1000) |
| 127 | +x = np.sin(2 * np.pi * 10 * t) + np.random.normal(0, 1, t.shape) |
| 128 | + |
| 129 | +# Design a low-pass FIR filter |
| 130 | +b = signal.firwin(30, 0.05) |
| 131 | +x_filtered = signal.lfilter(b, 1, x) |
| 132 | + |
| 133 | +# Plotting |
| 134 | +plt.figure(figsize=(10, 4)) |
| 135 | +plt.plot(t, x, label='Noisy signal') |
| 136 | +plt.plot(t, x_filtered, label='Filtered signal') |
| 137 | +plt.xlabel('Time') |
| 138 | +plt.ylabel('Amplitude') |
| 139 | +plt.legend() |
| 140 | +plt.show() |
| 141 | +``` |
| 142 | + |
| 143 | +Output: |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +- Spectral Analysis: |
| 149 | + |
| 150 | +```python |
| 151 | +from scipy import signal |
| 152 | +import numpy as np |
| 153 | +import matplotlib.pyplot as plt |
| 154 | + |
| 155 | + |
| 156 | +# Compute and plot a spectrogram |
| 157 | +f, t, Sxx = signal.spectrogram(x, fs=1000) |
| 158 | +plt.figure(figsize=(10, 4)) |
| 159 | +plt.pcolormesh(t, f, 10 * np.log10(Sxx), shading='gouraud') |
| 160 | +plt.ylabel('Frequency [Hz]') |
| 161 | +plt.xlabel('Time [sec]') |
| 162 | +plt.colorbar(label='Power [dB]') |
| 163 | +plt.show() |
| 164 | +``` |
| 165 | + |
| 166 | +Output: |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + |
0 commit comments