The SciPy library is part of the “SciPy ecosystem” that also includes NumPy, Sympy, and Pandas. We will not discuss Sympy but it is a well-developed computer algebra system (CAS) that is also incorporated into several other packages such as SageMath. Its homepage at www.scipy.org has details and documentation.
Importing SciPy Packages
Importing from scipy alone brings only the base packages, which provide a fairly minimal set of tools. Individual packages must be imported explicitly.
from scipy import linalg, optimize
For an imported package, type
help(name) to see a summary of available functions.
Type in the following code to solve a linear system of equations.
import numpy as np from scipy import linalg A = np.array([[1, 2], [3, 4]]) b = np.array([, ]) c=linalg.solve(A, b) print(c)
See examples of simple image processing
here as well as many other sites. Anaconda should have the PIL or Pillow (Python Imaging Library) pre-installed.
Use the SciPy
ndimage to tint a photograph.
- Import numpy and matplotlab.pyplot. Import the scipy misc package
- Extract the sample picture “face” (a raccoon).
- Use matplotlab function imshow to look at the picture. You may still need to use plt.show().
- Use the NumPy array(img) function to get an numpy array. Specify dtype=‘float’.
- Get the shape of the image array.
- Tone down the blue by .9 and the red by .95. The order of color channels in the image is RBG.
- The multiplication will use floats which are not permitted for an image (the pixel values must be integers between 0 and 255). Store the tinted array into a new array with dtype=‘int’. You can do this in one step if you think about it.
- Show the new image. If you use plt.figure() and only one plt.show() you can view the images side by side.
import numpy as np import matplotlib.pyplot as plt from scipy import misc plt.figure() image=misc.face() plt.imshow(image) img_array=np.array(image,dtype='float') print(img_array.shape) retint=np.array(img_array*[0.95,0.9,1.0],dtype='int') plt.figure() plt.imshow(retint) plt.show()
The SciPy reference guide is an invaluable resource for SciPy usage.