cupy.unique() 函数中是否有支持 (axis) 选项的 CuPy 版本?任何解决方法?
Is there a CuPy version supporting (axis) option in cupy.unique() function? Any workaround?
我正在寻找支持轴选项的 numpy.unique() 的 GPU CuPy 对应项。
我有一个 Cupy 二维数组,我需要删除它的重复行。不幸的是,cupy.unique() 函数将数组和 returns 具有唯一值的一维数组展平。我正在寻找像 numpy.unique(arr, axis=0) 这样的函数来解决这个问题,但是 CuPy 还不支持 (axis) 选项
x = cp.array([[1,2,3,4], [4,5,6,7], [1,2,3,4], [10,11,12,13]])
y = cp.unique(x)
y_r = np.unique(cp.asnumpy(x), axis=0)
print('The 2D array:\n', x)
print('Required:\n', y_r, 'But using CuPy')
print('The flattened unique array:\n', y)
print('Error producing line:', cp.unique(x, axis=0))
I expect a 2D array with unique rows but I get a 1D array with unique numbers instead. Any ideas about how to implement this with CuPy or numba?
自 CuPy 版本 8.0.0b2
起,函数 cupy.lexsort
已正确实现。此函数可用作带有轴参数的 cupy.unique
的解决方法(尽管可能不是最有效的)。
假设数组是二维的,并且您想要找到沿轴 0 的唯一元素(否则 transpose/swap 视情况而定):
###################################
# replacement for numpy.unique with option axis=0
###################################
def cupy_unique_axis0(array):
if len(array.shape) != 2:
raise ValueError("Input array must be 2D.")
sortarr = array[cupy.lexsort(array.T[::-1])]
mask = cupy.empty(array.shape[0], dtype=cupy.bool_)
mask[0] = True
mask[1:] = cupy.any(sortarr[1:] != sortarr[:-1], axis=1)
return sortarr[mask]
如果您也想实现 return_stuff 参数,请检查 the original cupy.unique
source code(这是基于的)。我自己不需要那些。
我正在寻找支持轴选项的 numpy.unique() 的 GPU CuPy 对应项。
我有一个 Cupy 二维数组,我需要删除它的重复行。不幸的是,cupy.unique() 函数将数组和 returns 具有唯一值的一维数组展平。我正在寻找像 numpy.unique(arr, axis=0) 这样的函数来解决这个问题,但是 CuPy 还不支持 (axis) 选项
x = cp.array([[1,2,3,4], [4,5,6,7], [1,2,3,4], [10,11,12,13]])
y = cp.unique(x)
y_r = np.unique(cp.asnumpy(x), axis=0)
print('The 2D array:\n', x)
print('Required:\n', y_r, 'But using CuPy')
print('The flattened unique array:\n', y)
print('Error producing line:', cp.unique(x, axis=0))
I expect a 2D array with unique rows but I get a 1D array with unique numbers instead. Any ideas about how to implement this with CuPy or numba?
自 CuPy 版本 8.0.0b2
起,函数 cupy.lexsort
已正确实现。此函数可用作带有轴参数的 cupy.unique
的解决方法(尽管可能不是最有效的)。
假设数组是二维的,并且您想要找到沿轴 0 的唯一元素(否则 transpose/swap 视情况而定):
###################################
# replacement for numpy.unique with option axis=0
###################################
def cupy_unique_axis0(array):
if len(array.shape) != 2:
raise ValueError("Input array must be 2D.")
sortarr = array[cupy.lexsort(array.T[::-1])]
mask = cupy.empty(array.shape[0], dtype=cupy.bool_)
mask[0] = True
mask[1:] = cupy.any(sortarr[1:] != sortarr[:-1], axis=1)
return sortarr[mask]
如果您也想实现 return_stuff 参数,请检查 the original cupy.unique
source code(这是基于的)。我自己不需要那些。