|
| 1 | +## Installation of Scipy |
| 2 | + |
| 3 | +You can install scipy using the command: |
| 4 | + |
| 5 | +``` |
| 6 | +$ pip install scipy |
| 7 | +``` |
| 8 | + |
| 9 | +You can also use a Python distribution that already has Scipy installed like Anaconda, or Spyder. |
| 10 | + |
| 11 | +### Importing SciPy |
| 12 | + |
| 13 | +```python |
| 14 | +from scipy import constants |
| 15 | +``` |
| 16 | + |
| 17 | +## Key Features of SciPy |
| 18 | +### 1. Numerical Integration |
| 19 | + |
| 20 | +It helps in computing definite or indefinite integrals of functions |
| 21 | + |
| 22 | +```python |
| 23 | +from scipy import integrate |
| 24 | + |
| 25 | +#Define the function to integrate |
| 26 | +def f(x): |
| 27 | + return x**2 |
| 28 | + |
| 29 | +#Compute definite integral of f from 0 to 1 |
| 30 | +result, error = integrate.quad(f, 0, 1) |
| 31 | +print(result) |
| 32 | +``` |
| 33 | + |
| 34 | +#### Output |
| 35 | + |
| 36 | +``` |
| 37 | +0.33333333333333337 |
| 38 | +``` |
| 39 | + |
| 40 | +### 2. Optimization |
| 41 | + |
| 42 | +It can be used to minimize or maximize functions, here is an example of minimizing roots of an equation |
| 43 | + |
| 44 | +```python |
| 45 | +from scipy.optimize import minimize |
| 46 | + |
| 47 | +# Define an objective function to minimize |
| 48 | +def objective(x): |
| 49 | + return x**2 + 10*np.sin(x) |
| 50 | + |
| 51 | +# Minimize the objective function starting from x=0 |
| 52 | +result = minimize(objective, x0=0) |
| 53 | +print(result.x) |
| 54 | +``` |
| 55 | + |
| 56 | +#### Output |
| 57 | + |
| 58 | +``` |
| 59 | +array([-1.30644012]) |
| 60 | +``` |
| 61 | + |
| 62 | +### 3. Linear Algebra |
| 63 | + |
| 64 | +Solving Linear computations |
| 65 | + |
| 66 | +```python |
| 67 | +from scipy import linalg |
| 68 | +import numpy as np |
| 69 | + |
| 70 | +# Define a square matrix |
| 71 | +A = np.array([[1, 2], [3, 4]]) |
| 72 | + |
| 73 | +# Define a vector |
| 74 | +b = np.array([5, 6]) |
| 75 | + |
| 76 | +# Solve Ax = b for x |
| 77 | +x = linalg.solve(A, b) |
| 78 | +print(x) |
| 79 | +``` |
| 80 | + |
| 81 | +#### Output |
| 82 | + |
| 83 | +``` |
| 84 | +array([-4. , 4.5]) |
| 85 | +``` |
| 86 | + |
| 87 | +### 4. Statistics |
| 88 | + |
| 89 | +Performing statistics functions, like here we'll be distributing the data |
| 90 | + |
| 91 | +```python |
| 92 | +from scipy import stats |
| 93 | +import numpy as np |
| 94 | + |
| 95 | +# Generate random data from a normal distribution |
| 96 | +data = stats.norm.rvs(loc=0, scale=1, size=1000) |
| 97 | + |
| 98 | +# Fit a normal distribution to the data |
| 99 | +mean, std = stats.norm.fit(data) |
| 100 | +``` |
| 101 | + |
| 102 | +### 5. Signal Processing |
| 103 | + |
| 104 | +To process spectral signals, like EEG or MEG |
| 105 | + |
| 106 | +```python |
| 107 | +from scipy import signal |
| 108 | +import numpy as np |
| 109 | + |
| 110 | +# Create a signal (e.g., sine wave) |
| 111 | +t = np.linspace(0, 1, 1000) |
| 112 | +signal = np.sin(2 * np.pi * 5 * t) + 0.5 * np.random.randn(1000) |
| 113 | + |
| 114 | +# Apply a low-pass Butterworth filter |
| 115 | +b, a = signal.butter(4, 0.1, 'low') |
| 116 | +filtered_signal = signal.filtfilt(b, a, signal) |
| 117 | +``` |
| 118 | + |
| 119 | +The various filters applied that are applied here, are a part of signal analysis at a deeper level. |
| 120 | + |
| 121 | +### 6. Sparse Matrix |
| 122 | + |
| 123 | +The word ' sparse 'means less, i.e., the data is mostly unused during some operation or analysis. So, to handle this data, a Sparse Matrix is created |
| 124 | + |
| 125 | +There are two types of Sparse Matrices: |
| 126 | + |
| 127 | +1. CSC: Compressed Sparse Column, it is used for efficient math functions and for column slicing |
| 128 | +2. CSR: Compressed Sparse Row, it is used for fast row slicing |
| 129 | + |
| 130 | +#### In CSC format |
| 131 | + |
| 132 | +```python |
| 133 | +from scipy import sparse |
| 134 | +import numpy as np |
| 135 | + |
| 136 | +data = np.array([[0, 0], [0, 1], [2, 0]]) |
| 137 | + |
| 138 | +row_indices = np.array([1, 2, 1]) |
| 139 | +col_indices = np.array([1, 0, 2]) |
| 140 | +values = np.array([1, 2, 1]) |
| 141 | + |
| 142 | +sparse_matrix_csc = sparse.csc_matrix((values, (row_indices, col_indices))) |
| 143 | +``` |
| 144 | + |
| 145 | +#### In CSR format |
| 146 | + |
| 147 | +```python |
| 148 | +from scipy import sparse |
| 149 | +import numpy as np |
| 150 | + |
| 151 | +data = np.array([[0, 0], [0, 1], [2, 0]]) |
| 152 | +sparse_matrix = sparse.csr_matrix(data) |
| 153 | +``` |
| 154 | + |
| 155 | +### 7. Image Processing |
| 156 | + |
| 157 | +It is used to process the images, like changing dimensions or properties. For example, when you're doing a project on medical imaging, this library is mainly used. |
| 158 | + |
| 159 | +```python |
| 160 | +from scipy import ndimage |
| 161 | +import matplotlib.pyplot as plt |
| 162 | + |
| 163 | +image = plt.imread('path/to/image.jpg') |
| 164 | +plt.imshow(image) |
| 165 | +plt.show() |
| 166 | + |
| 167 | +# Apply Gaussian blur to the image |
| 168 | +blurred_image = ndimage.gaussian_filter(image, sigma=1) |
| 169 | +plt.imshow(blurred_image) |
| 170 | +plt.show() |
| 171 | +``` |
| 172 | + |
| 173 | +The gaussian blur is one of the properties of the ' ndimage ' package in SciPy libraries, it used for better understanding of the image. |
0 commit comments