简体   繁体   中英

np.linalg.lstsq(X,Y)[0] - TypeError: No loop matching the specified signature and casting was found for ufunc lstsq_n

I want to get my X values in the shape [X 1] .

For this I am using this code:

X = np.array([[value,1] for value in X])

I get this warning...

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:2: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray

... but it seemed to work.

But when I then tried to get m and b values using thgis code:

m, b = np.linalg.lstsq(X,Y)[0]

I got this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-761e189a9409> in <module>()
----> 1 m, b = np.linalg.lstsq(X,Y)[0]

<__array_function__ internals> in lstsq(*args, **kwargs)

/usr/local/lib/python3.6/dist-packages/numpy/linalg/linalg.py in lstsq(a, b, rcond)
   2304         # lapack can't handle n_rhs = 0 - so allocate the array one larger in that axis
   2305         b = zeros(b.shape[:-2] + (m, n_rhs + 1), dtype=b.dtype)
-> 2306     x, resids, rank, s = gufunc(a, b, rcond, signature=signature, extobj=extobj)
   2307     if m == 0:
   2308         x[...] = 0

TypeError: No loop matching the specified signature and casting was found for ufunc lstsq_n

How can I amend my code?

I found a solution. List comprehension should not be used:

X=np.vstack([df.X,np.ones(len(df.X))]).T

Y = df.Y

m,b = np.linalg.lstsq(X,Y)[0]

This works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM