How to Use Scipy – Python Scientific Library

Matlab is one of the popular software used in academics and labs to solve problems in mathematics, engineering and research domain. But matlab being a commercial software it is often not within reach of most of the students and developers. There are some open source and free equivalent of matlab software like octave, scilab and scipy. Here in this article we’re going to learn about scipy. Scipy library is written for python programming language which internally depends on numpy. With scipy you can do equal computations that you used to do with matlb or mathematica.

Scipy Download

You can download scipy from it’s official download section. Depending on your operating system, you have to download both numpy and scipy package for your version of python. If you’re using old version of python then it’s better to upgrade your python version to recent supported scipy version. Please keep in mind that you need numpy package along with scipy on your operating system.

Using Scipy

In order to work with scipy library you need to import it as subpackage. e.g. take a look at code below.

import scipy as sp

Now you need to use the respective calls to scipy library to do your tasks related to maths or any other specific task like charting etc.

You can also download numpy and matplotlib as it helps you to perform some operation related to scipy. So in order to perform all the scientific calculations, you have to download three packages – numpy, scipy and matplotlib. You can also perform scientific calculations using ipython or interactive python.

Matrices Using Scipy and Numpy

 from numpy import*
a= array([10,20,30,40])
b=array([50,60,70,80])  
c=b-a; # subtraction operation, you can also do addition or multiplication
print c

Generate Random Number

rn=scipy.randn(10)
print rn

2-Dimmensional Array

eye(x,y) – Creates a 2D array size of n x m. Here N= number of 1′s and M= number of elements in 2-dimmensional array.

eye(6,6) # output- 6x6 array with 1 in each row and rest of the elements as 0s.

Help in Scipy

help() method is not useful because it is specific to the python distribution and not to sub packages of python. In order to use scipy help documentation you need to use scipy.info(). In order to get information about any specific math function you need to call it in info(). e.g. scipy.info(“pi”).

You can read more numpy methods here, these are equivalent of matlab/octave. Another source is to get recently updated documentation.

Help and Resources for Scipy

You can get more help on scipy on official documentation page. This page is regularly updated with changes in numpy/scipy. Another place to learn more about scipy is cookbook. You’ll find sample code and usage of scipy in cookbook section.