“cupy.core.core.ndarray”对象没有“unique”属性

问题描述

我正在使用 factorize() 函数转换分类特征,该函数返回 cupy 数组和字符串的元组。我将 cupy 数组分配给名为 codes 的变量。但是,我似乎无法使用 codes

获得 codes.unique() 的唯一值

它返回一条错误消息:

AttrubuteError: 'cupy.core.core.ndarray' 对象没有属性 'unique'

# use factorize to create continuous integers from a categorical feature (returns a tuple)
codes,uniques = df_train['product_id'].factorize()
codes.unique()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-59-27db0fac06a1> in <module>()
----> 1 codes.unique()

AttributeError: 'cupy.core.core.ndarray' object has no attribute 'unique'

code

感谢帮助和建议使其工作

解决方法

CuPy 被设计为与 NumPy 高度兼容,在这种情况下,请注意 numpy.ndarray 也没有 unique() 方法;对于 NumPy 和 CuPy,它作为常规函数 (numpy.unique()/cupy.unique()) 位于主命名空间下。所以这是一个不特定于 CuPy 恕我直言的无效用法。

,

我认为您需要调用 cupy.unique(codes) 而不是 codes.unique() ,就像如果它是常规 NumPy 数组一样。文档:https://docs.cupy.dev/en/stable/reference/generated/cupy.unique.html

它是在这个 PR 中添加的:https://github.com/cupy/cupy/pull/1140,正如您所看到的,它没有将它添加为数组类型的方法。