question about numpy.polyval

Discussion in 'Python' started by sirvival, Feb 28, 2011.

  1. sirvival

    sirvival Guest

    Hi,
    I have some simulated data of stellar absorption lines.

    What I am trying to is the following:

    I divide my data into chunks (each of the same size).
    Then I let the code find the max y value in one of those chunks.
    I got this working.

    Then I put those value in a two column array (first column has the
    position of the max value in the original data; second column the y
    value at this position).

    Then I use polyfit to fit the data.

    At last I use polyval to get the fit.

    The problem is now since I got about 70 chunks I am not sure how to
    use polyfit to get the fit for the original data.


    My simulated data is a one column array of 300000 data points. I am
    only interested in a fit of values above 160000.
    The data is data_mean.

    My code:

    import numpy as np
    import matplotlib.pyplot as mpl
    import scipy, pyfits

    chunk = 2000
    data_len = len(data_mean)
    num_chunk = data_len/chunk
    start_chunk = 160000
    num_chunk = num_chunk - start_chunk/chunk # I defined num_chunk this
    way so I can change the startvalue
    data_chunk = np.zeros( (num_chunk, chunk))
    data_mean_b = data_mean[start_chunk:len(data_mean)] # for fitting
    purpose later in the code

    for i in range(num_chunk):
    data_chunk = data_mean[start_chunk+i*2000:start_chunk
    +2000+i*2000]

    data_max = np.zeros(num_chunk)

    for i in range(num_chunk):
    data_max = max(data_chunk) # finding the max values inside a
    chunk


    data_max_pos = np.zeros(num_chunk) # the position of the max values

    for i in range(num_chunk):
    for position, item in enumerate(data_mean):
    if item == data_max:
    data_max_pos = position


    data_fin = np.zeros((num_chunk,2))

    for i in range(num_chunk): # final data two columns
    data_fin[i,0] = data_max_pos
    data_fin[i,1] = data_max

    order = 2
    x = np.arange(num_chunk)
    y = data_fin[::,1]
    coeff = np.polyfit(x, y, order)
    fit = np.polyval(coeff,x)

    xa = np.arange(len(data_mean_b))
    fitb = np.zeros((num_chunk,2))


    #end of code

    Now fit does work fine but as len(num_chunk) = 70 it is no use for the
    simulated data.
    So I tried with xa and fitb.

    But this just gives me somethin like this (plot of fita):
    http://img40.imageshack.us/i/web01.png/

    Plot of fit:
    http://img196.imageshack.us/i/web02j.png/


    Thanks
     
    sirvival, Feb 28, 2011
    #1
    1. Advertising

  2. sirvival

    Robert Kern Guest

    On 2/28/11 9:34 AM, sirvival wrote:
    > Hi,
    > I have some simulated data of stellar absorption lines.


    You will want to ask numpy questions on the numpy mailing list:

    http://www.scipy.org/Mailing_Lists

    It would be best if you could make a minimal, self-contained, runnable script
    that demonstrates your problem. I.e. provide x and y arrays that show the
    problem; all the details about chunking and such are just getting in the way.
    Please state what results you expect in addition to the results you got; you
    show plots but don't show exactly what you plotted. I have no idea what you were
    expecting to get. Make sure you use variable names consistently. For example,
    you refer to a "plot of fita", but nothing in your code assigns to "fita".

    Thanks.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Feb 28, 2011
    #2
    1. Advertising

  3. sirvival

    sirvival Guest

    On 28 Feb., 17:15, Robert Kern <> wrote:
    > On 2/28/11 9:34 AM, sirvival wrote:
    >
    > > Hi,
    > > I have some simulated data of stellar absorption lines.

    >
    > You will want to ask numpy questions on the numpy mailing list:
    >
    >    http://www.scipy.org/Mailing_Lists
    >
    > It would be best if you could make a minimal, self-contained, runnable script
    > that demonstrates your problem. I.e. provide x and y arrays that show the
    > problem; all the details about chunking and such are just getting in the way.
    > Please state what results you expect in addition to the results you got; you
    > show plots but don't show exactly what you plotted. I have no idea what you were
    > expecting to get. Make sure you use variable names consistently. For example,
    > you refer to a "plot of fita", but nothing in your code assigns to "fita"..
    >
    > Thanks.
    >
    > --
    > Robert Kern
    >
    > "I have come to believe that the whole world is an enigma, a harmless enigma
    >   that is made terrible by our own mad attempt to interpret it as though it had
    >   an underlying truth."
    >    -- Umberto Eco


    Hi,
    ok I will ask my question (modified with your sugestions) on the numpy
    mailing list.
    I just started using python last week, so I am sure my code looks
    confusion. Sorry about that.


    Thanks
     
    sirvival, Mar 1, 2011
    #3
  4. sirvival

    sirvival Guest

    Hi,
    found the error

    I had x for coeff wrong definded.
    Instead of
    x = np.arange(num_chunk)
    it should be
    x = data_fin[::,0]

    Now it works.
     
    sirvival, Mar 1, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. not telling
    Replies:
    1
    Views:
    1,324
    JustSomeGuy
    Aug 2, 2003
  2. Hiddo

    polyfit / polyval

    Hiddo, Feb 21, 2005, in forum: C++
    Replies:
    1
    Views:
    2,749
    Jack Klein
    Feb 22, 2005
  3. drife
    Replies:
    1
    Views:
    394
    Travis E. Oliphant
    Mar 1, 2006
  4. Duncan Smith
    Replies:
    3
    Views:
    447
    Duncan Smith
    Apr 25, 2007
  5. Replies:
    2
    Views:
    518
    Robert Kern
    Nov 13, 2007
Loading...

Share This Page