|
| 1 | +## Installation of Scipy |
| 2 | +### Install using the command: |
| 3 | +#### C:\Users\Your Name>pip install scipy |
| 4 | + You can also use a Python distribution that already has Scipy installed like Anaconda, or Spyder. |
| 5 | +### Importing SciPy |
| 6 | +#### from scipy import constants |
| 7 | +## Key Features of SciPy |
| 8 | +### 1. Numerical Integration |
| 9 | +#### It helps in computing definite or indefinite integrals of functions |
| 10 | +``` |
| 11 | +from scipy import integrate |
| 12 | +
|
| 13 | +#Define the function to integrate |
| 14 | +def f(x): |
| 15 | + return x**2 |
| 16 | +
|
| 17 | +#Compute definite integral of f from 0 to 1 |
| 18 | +result, error = integrate.quad(f, 0, 1) |
| 19 | +print(result) |
| 20 | +``` |
| 21 | +#### Output |
| 22 | +``` |
| 23 | +0.33333333333333337 |
| 24 | +``` |
| 25 | +### 2. Optimization |
| 26 | +#### It can be used to minimize or maximize functions, here is an example of minimizing roots of an equation |
| 27 | +``` |
| 28 | +from scipy.optimize import minimize |
| 29 | +
|
| 30 | +# Define an objective function to minimize |
| 31 | +def objective(x): |
| 32 | + return x**2 + 10*np.sin(x) |
| 33 | +
|
| 34 | +# Minimize the objective function starting from x=0 |
| 35 | +result = minimize(objective, x0=0) |
| 36 | +print(result.x) |
| 37 | +``` |
| 38 | +#### Output |
| 39 | +``` |
| 40 | +array([-1.30644012]) |
| 41 | +``` |
| 42 | +### 3. Linear Algebra |
| 43 | +#### Solving Linear computations |
| 44 | +``` |
| 45 | +from scipy import linalg |
| 46 | +import numpy as np |
| 47 | +
|
| 48 | +# Define a square matrix |
| 49 | +A = np.array([[1, 2], [3, 4]]) |
| 50 | +
|
| 51 | +# Define a vector |
| 52 | +b = np.array([5, 6]) |
| 53 | +
|
| 54 | +# Solve Ax = b for x |
| 55 | +x = linalg.solve(A, b) |
| 56 | +print(x) |
| 57 | +``` |
| 58 | +#### Output |
| 59 | +``` |
| 60 | +array([-4. , 4.5]) |
| 61 | +``` |
| 62 | +### 4. Statistics |
| 63 | +#### Performing statistics functions, like here we'll be distributing the data |
| 64 | +``` |
| 65 | +from scipy import stats |
| 66 | +import numpy as np |
| 67 | +
|
| 68 | +# Generate random data from a normal distribution |
| 69 | +data = stats.norm.rvs(loc=0, scale=1, size=1000) |
| 70 | +
|
| 71 | +# Fit a normal distribution to the data |
| 72 | +mean, std = stats.norm.fit(data) |
| 73 | +``` |
| 74 | +### 5. Signal Processing |
| 75 | +#### To process spectral signals, like EEG or MEG |
| 76 | +``` |
| 77 | +from scipy import signal |
| 78 | +import numpy as np |
| 79 | +
|
| 80 | +# Create a signal (e.g., sine wave) |
| 81 | +t = np.linspace(0, 1, 1000) |
| 82 | +signal = np.sin(2 * np.pi * 5 * t) + 0.5 * np.random.randn(1000) |
| 83 | +
|
| 84 | +# Apply a low-pass Butterworth filter |
| 85 | +b, a = signal.butter(4, 0.1, 'low') |
| 86 | +filtered_signal = signal.filtfilt(b, a, signal) |
| 87 | +``` |
| 88 | +The various filters applied that are applied here, are a part of signal analysis at a deeper level. |
| 89 | +### 6. Sparse Matrix |
| 90 | +#### 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 |
| 91 | +#### There are two types of Sparse Matrices: |
| 92 | +##### 1. CSC: Compressed Sparse Column, it is used for efficient math functions and for column slicing |
| 93 | +##### 2. CSR: Compressed Sparse Row, it is used for fast row slicing |
| 94 | +#### In CSC format |
| 95 | +``` |
| 96 | +from scipy import sparse |
| 97 | +import numpy as np |
| 98 | +
|
| 99 | +data = np.array([[0, 0], [0, 1], [2, 0]]) |
| 100 | +
|
| 101 | +row_indices = np.array([1, 2, 1]) |
| 102 | +col_indices = np.array([1, 0, 2]) |
| 103 | +values = np.array([1, 2, 1]) |
| 104 | +
|
| 105 | +sparse_matrix_csc = sparse.csc_matrix((values, (row_indices, col_indices))) |
| 106 | +``` |
| 107 | +#### In CSR format |
| 108 | +``` |
| 109 | +from scipy import sparse |
| 110 | +import numpy as np |
| 111 | +
|
| 112 | +data = np.array([[0, 0], [0, 1], [2, 0]]) |
| 113 | +sparse_matrix = sparse.csr_matrix(data) |
| 114 | +``` |
| 115 | +### 7. Image Processing |
| 116 | +#### 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. |
| 117 | +``` |
| 118 | +from scipy import ndimage |
| 119 | +import matplotlib.pyplot as plt |
| 120 | +
|
| 121 | +image = plt.imread('path/to/image.jpg') |
| 122 | +plt.imshow(image) |
| 123 | +plt.show() |
| 124 | +
|
| 125 | +# Apply Gaussian blur to the image |
| 126 | +blurred_image = ndimage.gaussian_filter(image, sigma=1) |
| 127 | +plt.imshow(blurred_image) |
| 128 | +plt.show() |
| 129 | +``` |
| 130 | +#### 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