question about numpy.polyval

2011-02-28 Thread sirvival
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 30 data points. I am
only interested in a fit of values above 16.
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 = 16
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[i] = 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[i] = max(data_chunk[i])  # 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[i]:
data_max_pos[i] = 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[i]
   data_fin[i,1] = data_max[i]

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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: question about numpy.polyval

2011-03-01 Thread sirvival
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: question about numpy.polyval

2011-03-01 Thread sirvival
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.
-- 
http://mail.python.org/mailman/listinfo/python-list