NJU1healer的个人博客分享 http://blog.sciencenet.cn/u/NJU1healer

博文

[转载]统计numpy数组中每个值的个数

已有 4093 次阅读 2021-3-29 11:32 |个人分类:Python|系统分类:科研笔记|文章来源:转载

import numpy as np

from collections import Counter

# 方法一

data = np.array([1,1,1,2, 2, 3, 4, 4,4,4])

d = Counter(data)

d_s = sorted(d.items(),key=lambda x:x[1],reverse=True)

print(f'这个值:{d_s[0][0]}出现的次数最多,为{d_s[0][1]}次')

 

# 方法二

mask = np.unique(data)

tmp = []

for v in mask:

    tmp.append(np.sum(data==v))

ts = np.max(tmp)

max_v = mask[np.argmax(tmp)]

print(f'这个值:{max_v}出现的次数最多,为{ts}次')


第一种方法调用时候麻烦,第二种方法相对简单。

参考:https://blog.csdn.net/weixin_39875161/article/details/89740012



https://blog.sciencenet.cn/blog-3428464-1279139.html

上一篇:[转载]深度学习超参数-Minibatch
下一篇:Inception Modules
收藏 IP: 211.162.81.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

全部作者的其他最新博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-16 12:44

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部