我是靠谱客的博主 俏皮耳机,这篇文章主要介绍python输入数组_python-返回每个输入功能的计数数组,现在分享给大家,希望可以做个参考。

方法1

_, tags, count = np.unique(labels, return_counts=1, return_inverse=1)

sizes = count[tags]

方法#2

标签中带有正数,使用np.bincount则更简单,更有效-

sizes = np.bincount(labels)[labels]

运行时测试

设置具有60,000个唯一正数以及两组长度为100,000和1000,000的定时集.

设置#1:

In [192]: np.random.seed(0)

...: labels = np.random.randint(0,60000,(100000))

In [193]: %%timeit

...: sizes = np.zeros(labels.shape)

...: for num in np.unique(labels):

...: mask = labels == num

...: sizes[mask] = np.count_nonzero(mask)

1 loop, best of 3: 2.32 s per loop

In [194]: %timeit np.bincount(labels)[labels]

1000 loops, best of 3: 376 ?s per loop

In [195]: 2320/0.376 # Speedup figure

Out[195]: 6170.212765957447

设置#2:

In [196]: np.random.seed(0)

...: labels = np.random.randint(0,60000,(1000000))

In [197]: %%timeit

...: sizes = np.zeros(labels.shape)

...: for num in np.unique(labels):

...: mask = labels == num

...: sizes[mask] = np.count_nonzero(mask)

1 loop, best of 3: 43.6 s per loop

In [198]: %timeit np.bincount(labels)[labels]

100 loops, best of 3: 5.15 ms per loop

In [199]: 43600/5.15 # Speedup figure

Out[199]: 8466.019417475727

最后

以上就是俏皮耳机最近收集整理的关于python输入数组_python-返回每个输入功能的计数数组的全部内容,更多相关python输入数组_python-返回每个输入功能内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(124)

评论列表共有 0 条评论

立即
投稿
返回
顶部