# question about numpy.polyval

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

1. ### sirvivalGuest

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

2. ### Robert KernGuest

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

3. ### sirvivalGuest

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
4. ### sirvivalGuest

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

### 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.