I was transforming categorical features using factorize() function which returns a tuple of a cupy array and strings. I assigned the cupy array into a variable named codes. However, I can't seem to get the unique values of codes using codes.unique()

It returns an error message:

AttrubuteError: 'cupy.core.core.ndarray' object has no attribute '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

Appreciate the help and suggestion to make it work

2

Best Answer


CuPy is designed to be highly compatible with NumPy, and in this case note that numpy.ndarray does not have a unique() method either; for both NumPy and CuPy it lives under the main namespace as a regular function (numpy.unique()/cupy.unique()). So it is an invalid usage not specific to CuPy IMHO.

I think you need to call cupy.unique(codes) instead of codes.unique() like you could if it were a regular NumPy array. Docs: https://docs.cupy.dev/en/stable/reference/generated/cupy.unique.html

It was added in this PR: https://github.com/cupy/cupy/pull/1140 which as you may be able to see did not add it as a method on the array type.