DeprecationWarning:应使用“ shape”参数代替“ dims”

问题描述

在我的梯度下降实践中,要在3d图形中绘制MSE,请使用以下代码

ij_min = np.unravel_index(indices=plot_cost.argmin(),dims=plot_cost.shape)

ij_min是线性回归中的theta0和theta1值,而plot_cost是MSE数组。

在运行上述命令时,我收到了以下弃用警告:

:2:DeprecationWarning:应使用“形状”参数代替“昏暗”

我对应该使用什么代替dims = plot_cost.shape感到困惑。

有人可以帮忙吗?

解决方法

您已粘贴两次代码。您得到的警告与标题一样吗? = DeprecationWarning:应使用“形状”参数代替“昏暗”

,

DeprecationWarning表示关键字参数dims可用,但不建议使用,因为它将在以后的版本中删除。应该使用dims代替shape。可以这样做:

ij_min = np.unravel_index(indices=plot_cost.argmin(),shape=plot_cost.shape)

help函数是检查Python函数和类的公共文档的有用工具。

help(np.unravel_index)
Help on function unravel_index in module numpy:

unravel_index(...)
    unravel_index(indices,shape,order='C')

    Converts a flat index or array of flat indices into a tuple
    of coordinate arrays.

    Parameters
    ----------
    indices : array_like
        An integer array whose elements are indices into the flattened
        version of an array of dimensions ``shape``. Before version 1.6.0,this function accepted just one index value.
    shape : tuple of ints
        The shape of the array to use for unraveling ``indices``.

        .. versionchanged:: 1.16.0
            Renamed from ``dims`` to ``shape``.

    order : {'C','F'},optional
        Determines whether the indices should be viewed as indexing in
        row-major (C-style) or column-major (Fortran-style) order.

        .. versionadded:: 1.6.0

    Returns
    -------
    unraveled_coords : tuple of ndarray
        Each array in the tuple has the same shape as the ``indices``
        array.

    See Also
    --------
    ravel_multi_index

    Examples
    --------
    >>> np.unravel_index([22,41,37],(7,6))
    (array([3,6,6]),array([4,5,1]))
    >>> np.unravel_index([31,13],6),order='F')
    (array([3,1]))

    >>> np.unravel_index(1621,(6,7,8,9))
    (3,1,4,1)

您会注意到dims在我的numpy版本1.18.1中不可用。 help的输出表明dims在1.16.0版中已被删除。