Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SciPy instead of GNU Octave

For my lab experiments I write small programs to help with the data analysis. I usually just need basic calculations, means, standard deviation, arbitrary weighted function fitting and plots with errorbars and fitted function.

With GNU Octave, I can do this. I started to read more into the language of it and I start to not like its inconsistencies and that I have to learn yet another language.

So I am thinking about using Python, which I am using for a while now, with SciPy and NumPy. Can I do those things with Python easily or is it more overhead to get the general purpose language Python to do what I intend to do?

like image 557
Martin Ueding Avatar asked Sep 09 '12 21:09

Martin Ueding


1 Answers

Yes, the Python ecosystem makes it a viable platform for everyday data analysis tasks, especially using the IPython interface (but I'll stick to the standard one here.) The "[not having] to learn yet another language" argument is a strong one, IMHO, and is one of the reasons why I tend to use Python for this stuff.

>>> import numpy as np
>>> import scipy.optimize

"I usually just need basic calculations"

>>> x = np.linspace(0, 10, 50)
>>> y = 3*x**2+5+2*np.sin(x)

"means, standard deviation"

>>> y.mean()
106.3687338223809
>>> y.std()
91.395548605660522

"arbitrary weighted function fitting"

>>> def func(x, a, b, c):
...     return a*x**2+b+c*np.sin(x)
... 
>>> ynoisy = y + np.random.normal(0, 0.2, size=len(x))
>>> popt, pcov = scipy.optimize.curve_fit(func, x, ynoisy)
>>> popt
array([ 3.00015527,  4.99421236,  2.03380468])

"plots with error bars and fitted function"

xerr = 0.5
yerr = abs(np.random.normal(0.3, 10.0))
fitted_data = func(x, *popt)

# using the simplified, non-object-oriented interface here
# handy for quick plots

from pylab import *
errorbar(x, ynoisy, xerr=xerr, yerr=yerr, c="green", label="actual data")
plot(x, fitted_data, c="blue", label="fitted function")
xlim(0, 10)
ylim(0, 350)
legend()
xlabel("time since post")
ylabel("coolness of Python")
savefig("cool.png")

sample pic

like image 129
DSM Avatar answered Oct 11 '22 21:10

DSM