lstsq

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

lstsq

Gregor Hagelueken
Hi,

I would like to use lstsq to fit a curve y using a simple linear combination of three spectra (spectrum1-3 see below). Since the program below always crashes, I played around with it and found that it does not crash if I copy the contents of the array "spectrum2" to the array "spectrum3" (using spectrum3[0:36]=spectrum2[0:36]). But as soon as I change one of the numbers in array "spectrum3" it crashes again.

What is the mistake?

Thanks,
Gregor

from numpy import *
from numpy.random import normal
from pylab import *

t = arange(205, 241, 1.0)

#spectrum1
spectrum1=array([-25.35,-30.85,-34.6,-36.5,-36.75,-35.95,-35.2,-34.2,-33.6,-33.55,-33.8,-34.2,-34.2,-34.8,-35.4,-36.25,-36.6,-36.85,-36.55,-35.85,-35.05,-33.55,-31.7,-29.6,-27.15,-24.1,-21.5,-18.95,-16.4,-13.85,-11.45,-9.43,-7.76,-6.24,-4.81,-3.88])
#spectrum2
spectrum2=array([8.08,5.37,1.24,-3.07,-6.02,-8.84,-11.2,-13.05,-14.85,-17.75,-17.5,-18,-18.35,-18.65,-18.15,-17.45,-17.15,-16.3,-15,-13.15,-11.57,-9.76,-7.98,-5.79,-3.73,-2.37,-0.61,0.08,0.15,1.17,0.73,0.77,0.38,0.56,0.28,0.64])
#spectrum3
spectrum3=array([-25.8,-21.45,-18.0,-15.59,-12.82,-10.41,-8.41,-6.58,-4.99,-3.85,-2.62,-1.74,-1.02,-0.5,-0.13,0.17,0.03,-0.08,-0.32,-0.35,-0.48,-0.75,-1.12,-1.31,-1.5,-1.55,-1.17,-1.04,-0.72,-0.46,-0.14,0.21,0.01,0.23,0.03,-0.22])

#make curve y
y=0.8*spectrum1+0.1*spectrum2+0.2*spectrum3+ normal(0.0, 1.0, len(t))

from numpy.linalg import lstsq
Nparam = 3
A=zeros((len(t), Nparam),float)

#helix
A[:,0]=spectrum1
#beta
A[:,1]=spectrum2
#coil
A[:,2]=spectrum3

(p, residuals, rank, s) = lstsq(A,y)

#A=p*A
#fit = A[:,0]+A[:,1]#+A[:,2]
#plot (t, y)
#plot (t, fit)


_______________________________________________
SciPy-user mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: lstsq

Bruce Southey
Gregor Hagelueken wrote:

> Hi,
>
> I would like to use lstsq to fit a curve y using a simple linear
> combination of three spectra (spectrum1-3 see below). Since the
> program below always crashes, I played around with it and found that
> it does not crash if I copy the contents of the array "spectrum2" to
> the array "spectrum3" (using spectrum3[0:36]=spectrum2[0:36]). But as
> soon as I change one of the numbers in array "spectrum3" it crashes again.
>
> What is the mistake?
>
> Thanks,
> Gregor
>
> from numpy import *
> from numpy.random import normal
> from pylab import *
>
> t = arange(205, 241, 1.0)
>
> #spectrum1
> spectrum1=array([-25.35,-30.85,-34.6,-36.5,-36.75,-35.95,-35.2,-34.2,-33.6,-33.55,-33.8,-34.2,-34.2,-34.8,-35.4,-36.25,-36.6,-36.85,-36.55,-35.85,-35.05,-33.55,-31.7,-29.6,-27.15,-24.1,-21.5,-18.95,-16.4,-13.85,-11.45,-9.43,-7.76,-6.24,-4.81,-3.88])
> #spectrum2
> spectrum2=array([8.08,5.37,1.24,-3.07,-6.02,-8.84,-11.2,-13.05,-14.85,-17.75,-17.5,-18,-18.35,-18.65,-18.15,-17.45,-17.15,-16.3,-15,-13.15,-11.57,-9.76,-7.98,-5.79,-3.73,-2.37,-0.61,0.08,0.15,1.17,0.73,0.77,0.38,0.56,0.28,0.64])
> #spectrum3
> spectrum3=array([-25.8,-21.45,-18.0,-15.59,-12.82,-10.41,-8.41,-6.58,-4.99,-3.85,-2.62,-1.74,-1.02,-0.5,-0.13,0.17,0.03,-0.08,-0.32,-0.35,-0.48,-0.75,-1.12,-1.31,-1.5,-1.55,-1.17,-1.04,-0.72,-0.46,-0.14,0.21,0.01,0.23,0.03,-0.22])
>
> #make curve y
> y=0.8*spectrum1+0.1*spectrum2+0.2*spectrum3+ normal(0.0, 1.0, len(t))
>
> from numpy.linalg import lstsq
> Nparam = 3
> A=zeros((len(t), Nparam),float)
>
> #helix
> A[:,0]=spectrum1
> #beta
> A[:,1]=spectrum2
> #coil
> A[:,2]=spectrum3
>
> (p, residuals, rank, s) = lstsq(A,y)
>
> #A=p*A
> #fit = A[:,0]+A[:,1]#+A[:,2]
> #plot (t, y)
> #plot (t, fit)
> ------------------------------------------------------------------------
>
> _______________________________________________
> SciPy-user mailing list
> [hidden email]
> http://projects.scipy.org/mailman/listinfo/scipy-user
>  
Hi,
This works for me on Linux x86_64, Python 2.5 and '1.1.0.dev5133'.
What are you using and what is the actual error message?

Bruce
_______________________________________________
SciPy-user mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: lstsq

Gregor Hagelueken
Bruce Southey wrote:
Gregor Hagelueken wrote:
  
Hi,

I would like to use lstsq to fit a curve y using a simple linear 
combination of three spectra (spectrum1-3 see below). Since the 
program below always crashes, I played around with it and found that 
it does not crash if I copy the contents of the array "spectrum2" to 
the array "spectrum3" (using spectrum3[0:36]=spectrum2[0:36]). But as 
soon as I change one of the numbers in array "spectrum3" it crashes again.

What is the mistake?

Thanks,
Gregor

from numpy import *
from numpy.random import normal
from pylab import *

t = arange(205, 241, 1.0)

#spectrum1
spectrum1=array([-25.35,-30.85,-34.6,-36.5,-36.75,-35.95,-35.2,-34.2,-33.6,-33.55,-33.8,-34.2,-34.2,-34.8,-35.4,-36.25,-36.6,-36.85,-36.55,-35.85,-35.05,-33.55,-31.7,-29.6,-27.15,-24.1,-21.5,-18.95,-16.4,-13.85,-11.45,-9.43,-7.76,-6.24,-4.81,-3.88])
#spectrum2
spectrum2=array([8.08,5.37,1.24,-3.07,-6.02,-8.84,-11.2,-13.05,-14.85,-17.75,-17.5,-18,-18.35,-18.65,-18.15,-17.45,-17.15,-16.3,-15,-13.15,-11.57,-9.76,-7.98,-5.79,-3.73,-2.37,-0.61,0.08,0.15,1.17,0.73,0.77,0.38,0.56,0.28,0.64])
#spectrum3
spectrum3=array([-25.8,-21.45,-18.0,-15.59,-12.82,-10.41,-8.41,-6.58,-4.99,-3.85,-2.62,-1.74,-1.02,-0.5,-0.13,0.17,0.03,-0.08,-0.32,-0.35,-0.48,-0.75,-1.12,-1.31,-1.5,-1.55,-1.17,-1.04,-0.72,-0.46,-0.14,0.21,0.01,0.23,0.03,-0.22])

#make curve y
y=0.8*spectrum1+0.1*spectrum2+0.2*spectrum3+ normal(0.0, 1.0, len(t))

from numpy.linalg import lstsq
Nparam = 3
A=zeros((len(t), Nparam),float)

#helix
A[:,0]=spectrum1
#beta
A[:,1]=spectrum2
#coil
A[:,2]=spectrum3

(p, residuals, rank, s) = lstsq(A,y)

#A=p*A
#fit = A[:,0]+A[:,1]#+A[:,2]
#plot (t, y)
#plot (t, fit)
------------------------------------------------------------------------

_______________________________________________
SciPy-user mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/scipy-user
  
    
Hi,
This works for me on Linux x86_64, Python 2.5 and '1.1.0.dev5133'.
What are you using and what is the actual error message?

Bruce
_______________________________________________
SciPy-user mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/scipy-user
  
Hi Bruce,

I have a Vista PC (Intel Core2Duo) and Python 2.5 installed.

I do not get any error message, it just hangs up, uses 50% of CPU and does not stop again.

Sorry, but I am not sure what you mean by '1.1.0.dev5133'?

Thanks,
Gregor

_______________________________________________
SciPy-user mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: lstsq

Nils Wagner-2
On Thu, 08 May 2008 16:06:10 +0100
  Gregor Hagelueken <[hidden email]> wrote:

> Bruce Southey wrote:
>> Gregor Hagelueken wrote:
>>  
>>> Hi,
>>>
>>> I would like to use lstsq to fit a curve y using a
>>>simple linear
>>> combination of three spectra (spectrum1-3 see below).
>>>Since the
>>> program below always crashes, I played around with it
>>>and found that
>>> it does not crash if I copy the contents of the array
>>>"spectrum2" to
>>> the array "spectrum3" (using
>>>spectrum3[0:36]=spectrum2[0:36]). But as
>>> soon as I change one of the numbers in array "spectrum3"
>>>it crashes again.
>>>
>>> What is the mistake?
>>>
>>> Thanks,
>>> Gregor
>>>
>>> from numpy import *
>>> from numpy.random import normal
>>> from pylab import *
>>>
>>> t = arange(205, 241, 1.0)
>>>
>>> #spectrum1
>>> spectrum1=array([-25.35,-30.85,-34.6,-36.5,-36.75,-35.95,-35.2,-34.2,-33.6,-33.55,-33.8,-34.2,-34.2,-34.8,-35.4,-36.25,-36.6,-36.85,-36.55,-35.85,-35.05,-33.55,-31.7,-29.6,-27.15,-24.1,-21.5,-18.95,-16.4,-13.85,-11.45,-9.43,-7.76,-6.24,-4.81,-3.88])
>>> #spectrum2
>>> spectrum2=array([8.08,5.37,1.24,-3.07,-6.02,-8.84,-11.2,-13.05,-14.85,-17.75,-17.5,-18,-18.35,-18.65,-18.15,-17.45,-17.15,-16.3,-15,-13.15,-11.57,-9.76,-7.98,-5.79,-3.73,-2.37,-0.61,0.08,0.15,1.17,0.73,0.77,0.38,0.56,0.28,0.64])
>>> #spectrum3
>>> spectrum3=array([-25.8,-21.45,-18.0,-15.59,-12.82,-10.41,-8.41,-6.58,-4.99,-3.85,-2.62,-1.74,-1.02,-0.5,-0.13,0.17,0.03,-0.08,-0.32,-0.35,-0.48,-0.75,-1.12,-1.31,-1.5,-1.55,-1.17,-1.04,-0.72,-0.46,-0.14,0.21,0.01,0.23,0.03,-0.22])
>>>
>>> #make curve y
>>> y=0.8*spectrum1+0.1*spectrum2+0.2*spectrum3+ normal(0.0,
>>>1.0, len(t))
>>>
>>> from numpy.linalg import lstsq
>>> Nparam = 3
>>> A=zeros((len(t), Nparam),float)
>>>
>>> #helix
>>> A[:,0]=spectrum1
>>> #beta
>>> A[:,1]=spectrum2
>>> #coil
>>> A[:,2]=spectrum3
>>>
>>> (p, residuals, rank, s) = lstsq(A,y)
>>>
>>> #A=p*A
>>> #fit = A[:,0]+A[:,1]#+A[:,2]
>>> #plot (t, y)
>>> #plot (t, fit)
>>> ------------------------------------------------------------------------
>>>
>>> _______________________________________________
>>> SciPy-user mailing list
>>> [hidden email]
>>> http://projects.scipy.org/mailman/listinfo/scipy-user
>>>  
>>>    
>> Hi,
>> This works for me on Linux x86_64, Python 2.5 and
>>'1.1.0.dev5133'.
>> What are you using and what is the actual error message?
>>
>> Bruce
>> _______________________________________________
>> SciPy-user mailing list
>> [hidden email]
>> http://projects.scipy.org/mailman/listinfo/scipy-user
>>  
> Hi Bruce,
>
> I have a Vista PC (Intel Core2Duo) and Python 2.5
>installed.
>
> I do not get any error message, it just hangs up, uses
>50% of CPU and does not stop again.
>
> Sorry, but I am not sure what you mean by
>'1.1.0.dev5133'?

Python 2.5 (r25:51908, Jan 10 2008, 18:01:52)
[GCC 4.1.2 20061115 (prerelease) (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> import numpy
>>> numpy.__version__
'1.2.0.dev5148'
>>>

Nils


>
> Thanks,
> Gregor

_______________________________________________
SciPy-user mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/scipy-user