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

标签 python numpy error-handling

我想以[X 1]的形式获取X值。
为此,我使用以下代码:

X = np.array([[value,1] for value in X])
我得到这个警告...
/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
...但是似乎可行。
但是当我随后尝试使用thgis代码获取mb值时:
m, b = np.linalg.lstsq(X,Y)[0]
我收到此错误:
---------------------------------------------------------------------------
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
如何修改我的代码?

最佳答案

我找到了解决方案。不应使用列表理解:

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

Y = df.Y

m,b = np.linalg.lstsq(X,Y)[0]
这对我有用。

关于python - np.linalg.lstsq(X,Y)[0]-TypeError : No loop matching the specified signature and casting was found for ufunc lstsq_n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65476404/

相关文章:

python - 在 Pandas 数据帧中存储 FFT 周期时出现奇怪的问题

php - 跟踪错误行和文件nr的正确方法。在使用自定义错误处理方法的自定义函数中使用debug_backtrace()

python - 如何使用 argwhere 函数按列访问二进制 numpy 数组的索引

python - 对于不同的 CSV 文件获得相同的结果

python - 我可以在没有 for 循环的情况下将函数应用于 Pandas 数据框中的多个列吗?

python - 文件存在于目录中,但正在获取 “No such file or directory: ' I_90-0-109_(90).txt'”

c# - 如果 wpf 应用程序没有响应,则自动重启

error-handling - 如何在Odoo中调试错误消息

python - 请求 : post multipart/form-data

python - 有没有办法等待从当前脚本(使用 subprocess.Propen())调用的另一个 python 脚本直到它完成?