SciPy instead of GNU Octave

前端 未结 1 1318
执笔经年
执笔经年 2021-02-01 08:54

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 fitt

相关标签:
1条回答
  • 2021-02-01 09:12

    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

    0 讨论(0)
提交回复
热议问题