栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

计算每行出现在numpy.array中的次数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

计算每行出现在numpy.array中的次数

您可以使用另一个问题的答案来获得唯一项目的计数。

使用结构化数组的另一种选择是使用一种void类型的视图,该视图将整行连接到单个项目中:

a = np.array([[1, 1, 1, 0, 0, 0],   [0, 1, 1, 1, 0, 0],   [0, 1, 1, 1, 0, 0],   [1, 1, 1, 0, 0, 0],   [1, 1, 1, 1, 1, 0]])b = np.ascontiguousarray(a).view(np.dtype((np.void, a.dtype.itemsize * a.shape[1])))_, idx = np.unique(b, return_index=True)unique_a = a[idx]>>> unique_aarray([[0, 1, 1, 1, 0, 0],       [1, 1, 1, 0, 0, 0],       [1, 1, 1, 1, 1, 0]])

编辑 添加了np.ascontiguousarray以下@seberg的建议。如果数组不是连续的,这会使方法变慢。

编辑 可以通过执行以下操作来稍微加快上述速度,也许是以清楚为代价的:

unique_a = np.unique(b).view(a.dtype).reshape(-1, a.shape[1])

另外,至少在我的系统上,性能方面与lexsort方法相当,甚至更好:

a = np.random.randint(2, size=(10000, 6))%timeit np.unique(a.view(np.dtype((np.void, a.dtype.itemsize*a.shape[1])))).view(a.dtype).reshape(-1, a.shape[1])100 loops, best of 3: 3.17 ms per loop%timeit ind = np.lexsort(a.T); a[np.concatenate(([True],np.any(a[ind[1:]]!=a[ind[:-1]],axis=1)))]100 loops, best of 3: 5.93 ms per loopa = np.random.randint(2, size=(10000, 100))%timeit np.unique(a.view(np.dtype((np.void, a.dtype.itemsize*a.shape[1])))).view(a.dtype).reshape(-1, a.shape[1])10 loops, best of 3: 29.9 ms per loop%timeit ind = np.lexsort(a.T); a[np.concatenate(([True],np.any(a[ind[1:]]!=a[ind[:-1]],axis=1)))]10 loops, best of 3: 116 ms per loop

在numpy 1.9中,有一个

return_counts
可选的关键字参数,因此您可以简单地执行以下操作:

>>> my_arrayarray([[1, 2, 0, 1, 1, 1],       [1, 2, 0, 1, 1, 1],       [9, 7, 5, 3, 2, 1],       [1, 1, 1, 0, 0, 0],       [1, 2, 0, 1, 1, 1],       [1, 1, 1, 1, 1, 0]])>>> dt = np.dtype((np.void, my_array.dtype.itemsize * my_array.shape[1]))>>> b = np.ascontiguousarray(my_array).view(dt)>>> unq, cnt = np.unique(b, return_counts=True)>>> unq = unq.view(my_array.dtype).reshape(-1, my_array.shape[1])>>> unqarray([[1, 1, 1, 0, 0, 0],       [1, 1, 1, 1, 1, 0],       [1, 2, 0, 1, 1, 1],       [9, 7, 5, 3, 2, 1]])>>> cntarray([1, 1, 3, 1])

在早期版本中,您可以按照以下方式进行操作:

>>> unq, _ = np.unique(b, return_inverse=True)>>> cnt = np.bincount(_)>>> unq = unq.view(my_array.dtype).reshape(-1, my_array.shape[1])>>> unqarray([[1, 1, 1, 0, 0, 0],       [1, 1, 1, 1, 1, 0],       [1, 2, 0, 1, 1, 1],       [9, 7, 5, 3, 2, 1]])>>> cntarray([1, 1, 3, 1])


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/636890.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号