【问题标题】:np.linalg.lstsq(X,Y)[0] - TypeError: No loop matching the specified signature and casting was found for ufunc lstsq_nnp.linalg.lstsq(X,Y)[0] - TypeError: No loop match the specified signature and cast was found for ufunc lstsq_n
【发布时间】:2020-12-28 10:45:03
【问题描述】:

我想以[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

如何修改我的代码?

【问题讨论】:

  • 警告告诉你它正在创建一个对象 dtype 数组。元素不是简单的数字。你看过X 还是它的元素?在其shapedtype?

标签: python numpy error-handling


【解决方案1】:

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

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

Y = df.Y

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

这对我有用。

【讨论】:

    猜你喜欢
    • 2020-04-16
    • 2022-12-01
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    相关资源
    最近更新 更多