Learn SciPy in One Article
SciPy is the most important scientific package in Python. Built on the basis of NumPy and developed in C/C++, SciPy contains a wide range of very powerful mathematical and scientific functions. For this reason, many engineers, data scientists, and researchers use it in their projects. In this tutorial, you will discover some of the most useful modules for performing interpolation, optimization, signal processing, and image processing operations. If these topics seem too complex, don't worry, SciPy is very easy to learn.
1. Signal interpolation with SciPy
Signal interpolation consists in generating a signal with a higher frequency than the original signal, by connecting the different points of the original signal with one of the following methods:
- Linear interpolation (you draw a line between each point)
- Quadratic interpolation (connects points with a polynomial of degree 2)
- Cubic interpolation (connects the points with a polynomial of degree 3)
- Nearest neighbor interpolation (connects points in a staircase)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import numpy as np import matplotlib.pyplot as plt # creating a dataset x = np.linspace( 0 , 10 , 10 ) y = np.sin(x) from scipy.interpolate import interp1d # creating the interpolation function f f = interp1d(x, y, kind = 'cubic' ) # results of the interpolation function f on new data new_x = np.linspace( 0 , 10 , 50 ) result = f(new_x) # visualization with matplotlib plt.scatter(x, y) plt.plot(new_x, result, c = 'r' ) plt.show() |
2. Optimization with SciPy
Optimization is a technique that affects all fields. Everyone wants to optimize a product or process, to maximize its gains, to minimize its risks, etc.
The scipy.optimize module contains many tools dedicated to optimization problems: function minimization, curve fitting, linear programming... Let's have a look at the function minimization
1D Minimisation
Minimizing/maximizing a function is a very common problem in engineering, science, or finance. It consists in finding the global minimum/maximum of a function on a study field. For this, Newton's method or gradient descent are among the most popular techniques. These techniques can all be easily implemented with the optimize.minimize() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import numpy as np import matplotlib.pyplot as plt # Defining a 1 Dimensional function def f (x): return x * * 2 + 15 * np.sin(x) # Visualizing the function x = np.linspace( - 10 , 10 , 100 ) plt.plot(x, f(x)) # Defining a point x0 for the minimization algorithm x0 = - 5 result = optimize.minimize(f, x0 = x0).x # minimization result # Result visualization plt.plot(x, f(x), lw = 3 , zorder = - 1 ) # Function curve plt.scatter(x0, f(x0), s = 200 , marker = '+' , c = 'g' , zorder = 1 , label = 'initial' ) # point initial plt.scatter(result, f(result), s = 100 , c = 'r' , zorder = 1 , label = 'final' ) # point final plt.legend() plt.show() |
2D Minimisation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import numpy as np import matplotlib.pyplot as plt # Defining a 2D function. X is a 2-Dimensional numpy array def f (x): return np.sin(x[ 0 ]) + np.cos(x[ 0 ] + x[ 1 ]) * np.cos(x[ 0 ]) # Generating the function on a 2D space x = np.linspace( - 3 , 3 , 100 ) y = np.linspace( - 3 , 3 , 100 ) x, y = np.meshgrid(x, y) # Placing an initial point x0 at coordinates (0,0) x0 = np.zeros(( 2 , 1 )) # Minimizing the function result = optimize.minimize(f, x0 = x0).x print ( 'the minimum is in the coordinates' , result) # prints the result # Visualizing the result plt.contour(x, y, f(np.array([x, y])), 20 ) # 2D function plt.scatter(x0[ 0 ], x0[ 1 ], marker = '+' , c = 'r' , s = 100 , label = 'initial' ) # Starting point plt.scatter(result[ 0 ], result[ 1 ], c = 'g' , s = 100 , label = 'final' ) # Final point plt.legend() plt.show() |
3. Signal Processing with SciPy
SciPy includes many tools for filtering and processing signals. One of them, the fftpack module, allows to perform Fourier transforms to clean up periodic signals, to know their frequencies, and to perform other related operations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import numpy as np import matplotlib.pyplot as plt # Creating a periodic signal embedded in noise. x = np.linspace( 0 , 30 , 1000 ) y = 3 * np.sin(x) + 2 * np.sin( 5 * x) + np.sin( 10 * x) + np.random.random(x.shape[ 0 ]) * 10 from scipy import fftpack # creating Fourier and Frequency variables, which allow to build the signal spectrum. fourier = fftpack.fft(y) power = np. abs (fourier) # the power variable is created to eliminate negative amplitudes frequences = fftpack.fftfreq(y.size) # spectrum filter with Numpy boolean indexing fourier[power< 400 ] = 0 # Inverse Fourier Transform: generates a new time signal from the filtered spectrum filtered_signal = fftpack.ifft(fourier) # Visualizing the results plt.figure(figsize = ( 12 , 8 )) plt.plot(x, y, lw = 0.5 , label = 'original signal' ) plt.plot(x, filtered_signal, lw = 3 , label = 'filtered signal' ) plt.legend() plt.show() |
Post a Comment