[SciPy-user] using linalg.lstsq

Mitchell Timin zenguyuno at yahoo.com
Wed May 10 23:30:10 EDT 2006


--- Alan Isaac <aisaac at american.edu> wrote:

> On Wed, 10 May 2006, Mitchell Timin wrote: 
> > It seems I have not explained the problem clearly.  x1, x2 
> > & x3 are scalars, representing x, y & z.
> 
> You need to think of x1, x2, and x3 as vectors.
> E.g., your first "observation" is x1[0], x2[0], x3[0].
> Suppose for example you have this list of four observations:
> (1.0,1.0,3.1),
> (1.0,2.0,4.9),
> (2.0,1.0,4.1),
> (2.0,2.0,5.9)
> Use these to create a data array to do your work with.
> >>> data = N.array( [ (1.0,1.0,3.1), (1.0,2.0,4.9), (2.0,1.0,4.1), (2.0,2.0,5.9) ])
> >>> constant = N.ones((4,1))
> >>> RHS = N.hstack([data[:,0:-1],constant])
> >>> LHS = data[:,-1:]
> >>> N.linalg.lstsq(RHS,LHS)[0]
> array([[ 1. ],
>        [ 1.8],
>        [ 0.3]])

That worked!  I put the above into this program:
-------------------------------------------------------------
import numpy as N

data = N.array( [ (1.0,1.0,3.1), (1.0,2.0,4.9), (2.0,1.0,4.1), (2.0,2.0,5.9) ])
constant = N.ones((4,1))
RHS = N.hstack([data[:,0:-1],constant])
LHS = data[:,-1:]

(X, resids, rank, s) = N.linalg.lstsq(RHS,LHS)

print "X"
print X

print "resids"
print resids

print "rank"
print rank

print "s"
print s
-----------------------------------------------
and it produced this output:
-----------------------------------------------
X
[[ 1. ]
 [ 1.8]
 [ 0.3]]
resids
[  4.07526776e-31]
rank
3
s
[ 4.77752568  1.          0.41862674]
-------------------------------------------------
Thank you, From that I think I can work out how to do the higher dimensional case.

Mitchell Timin

I'm proud of http://ANNEvolve.sourceforge.net.  If you want to write software, or articles, or do testing, or research for ANNEvolve, let me know.

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 




More information about the SciPy-User mailing list