python二维数组去重复_删除numpy数组的重复行

使用^{}-# Perform lex sort and get sorted data

sorted_idx = np.lexsort(data.T)

sorted_data = data[sorted_idx,:]

# Get unique row mask

row_mask = np.append([True],np.any(np.diff(sorted_data,axis=0),1))

# Get unique rows

out = sorted_data[row_mask]

样本运行-In [199]: data

Out[199]:

array([[1, 8, 3, 3, 4],

[1, 8, 9, 9, 4],

[1, 8, 3, 3, 4],

[1, 8, 3, 3, 4],

[1, 8, 0, 3, 4],

[1, 8, 9, 9, 4]])

In [200]: sorted_idx = np.lexsort(data.T)

...: sorted_data = data[sorted_idx,:]

...: row_mask = np.append([True],np.any(np.diff(sorted_data,axis=0),1))

...: out = sorted_data[row_mask]

...:

In [201]: out

Out[201]:

array([[1, 8, 0, 3, 4],

[1, 8, 3, 3, 4],

[1, 8, 9, 9, 4]])

运行时测试-

本节乘以迄今为止提出的解决方案中提出的所有方法。In [34]: data = np.random.randint(0,10,(10000,10))

In [35]: def tuple_based(data):

...: new_array = [tuple(row) for row in data]

...: return np.unique(new_array)

...:

...: def lexsort_based(data):

...: sorted_data = data[np.lexsort(data.T),:]

...: row_mask = np.append([True],np.any(np.diff(sorted_data,axis=0),1))

...: return sorted_data[row_mask]

...:

...: def unique_based(a):

...: a = np.ascontiguousarray(a)

...: unique_a = np.unique(a.view([('', a.dtype)]*a.shape[1]))

...: return unique_a.view(a.dtype).reshape((unique_a.shape[0], a.shape[1]))

...:

In [36]: %timeit tuple_based(data)

10 loops, best of 3: 63.1 ms per loop

In [37]: %timeit lexsort_based(data)

100 loops, best of 3: 8.92 ms per loop

In [38]: %timeit unique_based(data)

10 loops, best of 3: 29.1 ms per loop

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值