# [SciPy-user] linear_least_squares: OverFlow error or flapack.error

David Grant david.grant at telus.net
Wed Jun 7 01:23:27 CDT 2006

```Massimo,

Check out the attached file. I had trouble doing linear regression back in
2003 using Numeric and scipy and ScientificPython seemed to work best for me.
Things have probably changed...

I ported the file to use matplotlib for the test...it was surprisingly easy to
convert from scipy.gplt to matplotlib.

Dave

On Tuesday 06 June 2006 11:20, massimo sandal wrote:
> Hi,
>
> I'm trying to do a simple linear least squares fit of some data in an
> application.
>
> The relevant code runs about as follows, following closely the example
> found on
> http://mail.python.org/pipermail/python-list/2006-March/331693.html
>
> ---------
> import matplotlib.numerix as nx
>
> contact_x_points=nx.array(x_points[left_bound:right_bound])
> contact_y_points=nx.array(y_points[left_bound:right_bound])
>
> A=nx.ones((len(contact_x_points),2))
> A[:,0]=contact_x_points
> result=nx.linear_algebra.linear_least_squares(A,contact_y_points)
> ---------
>
> ...but when I run, it crashes with:
>
> File "hooke.py", line 202, in find_contact_point
>      result=nx.linear_algebra.linear_least_squares(A,contact_y_points)
>    File "/usr/lib/python2.3/site-packages/Numeric/LinearAlgebra.py",
> line 416, in linear_least_squares
>      nlvl = max( 0, int( math.log( float(min( m,n ))/2. ) ) + 1 )
> OverflowError: math range error
>
>
> I also tried using scipy:
> -----------
> import scipy as sp
>
> contact_x_points=sp.array(x_points[left_bound:right_bound])
> contact_y_points=sp.array(y_points[left_bound:right_bound])
>
> A=sp.ones((len(contact_x_points),2))
> A[:,0]=contact_x_points
> result=sp.linalg.lstsq(A,contact_y_points) -------------
>
> ... with another error:
>
> array_from_pyobj:intent(hide) must have defined dimensions.
> rank=1 dimensions=[ 0 ]
> Traceback:
> [...]
> File "hooke.py", line 202, in find_contact_point
>      result=sp.linalg.lstsq(A, contact_y_points)
>    File "/usr/lib/python2.3/site-packages/scipy/linalg/basic.py", line
> 344, in lstsq
>      overwrite_b = overwrite_b)
> flapack.error: failed in converting hidden `s' of flapack.dgelss to
> C/Fortran array
>
> In my .matplotlibrc the numerix backend is Numeric.
> I'm on Debian Sarge; MPL version is 0.82 ; Scipy is 0.3.2
>
>
> It must be noticed that I fail to declare
>
> A=nx.ones((len.contact_x_points),2),dtype=float)
> as the example should seem to require, because it gives me another error:
>
> TypeError: ones() got an unexpected keyword argument 'dtype'
>
> ...so if this is the problem, please tell me how to correctly pass the
> dtype argument.
>
> Since I'm quite a scipy/numeric newbie I guess there could be some
> obvious blunder and/or more correct way of obtaining my fit, and I'd be
> thankful to anyone pointing me at the solution...
>
> Thanks,
> Massimo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bestFitLine.py
Type: application/x-python
Size: 2139 bytes
Desc: not available
Url : http://www.scipy.net/pipermail/scipy-user/attachments/20060606/2dfe77b7/bestFitLine.bin
```