python 直方图每个bin中的值_如何高效地获取Python中直方图桶的索引?

这篇博客探讨了如何利用numpy库中的random模块生成随机数,并通过digitize和argsort函数实现数据分桶和排序。作者展示了如何计算每个桶内的数据,并给出了使用这些数据进行集群分析的例子,强调了这种方法在数据可视化中的应用。
摘要由CSDN通过智能技术生成

=vals = np.random.random(1e4)

nbins = 100

bins = np.linspace(0, 1, nbins+1)

ind = np.digitize(vals, bins)

new_order = argsort(ind)

ind = ind[new_order]

ordered_vals = vals[new_order]

# slower way of calculating first_hit (first version of this post)

# _,first_hit = unique(ind,return_index=True)

# faster way:

first_hit = searchsorted(ind,arange(1,nbins-1))

first_hit.sort()

#example of using the data:

for j in range(nbins-1):

#I am using a plotting function for your f, to show that they cluster

plot(ordered_vals[first_hit[j]:first_hit[j+1]],'o')

图中显示,垃圾箱实际上是预期的集群:1620

=

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值