zoukankan      html  css  js  c++  java
  • numpy.bincount正确理解

      今天看了个方法,numpy.bincount首先官网文档:

    numpy.bincount

    numpy.bincount(xweights=Noneminlength=0)

    Count number of occurrences of each value in array of non-negative ints.

    The number of bins (of size 1) is one larger than the largest value in x. If minlength is specified, there will be at least this number of bins in the output array (though it will be longer if necessary, depending on the contents of x). Each bin gives the number of occurrences of its index value in x. If weights is specified the input array is weighted by it, i.e. if a value n is found at position iout[n] += weight[i] instead of out[n] += 1.

    Parameters:
    x array_like, 1 dimension, nonnegative ints

    Input array.

    weights array_like, optional

    Weights, array of the same shape as x.

    minlength int, optional

    A minimum number of bins for the output array.

    New in version 1.6.0.

    Returns:
    out ndarray of ints

    The result of binning the input array. The length of out is equal to np.amax(x)+1.

    Raises:
    ValueError

    If the input is not 1-dimensional, or contains elements with negative values, or if minlength is negative.

    TypeError

    If the type of the input is float or complex.

    大意思是:  一脸懵逼好吧,统计bin在x出现的次数,what is bin?长度是x中最大值的+1,也看了一些博客,有些是这样写的:

    仔细看了看这个例子,还是没看懂,难道是我智商有问题?索引值0->7  哪里看出来的?官网文档也没说呀

    索引0出现1次?x的索引0上数字是0,缺失出现了1次,1出现3次对的,后面索引2也是1应该也是3呀 什么鬼?怎么结果是1???

    最终,我发现了到底是怎么统计的了,这个函数要求x里面的数字是非负正数是有道理的,

    函数返回的是固定的array[0, 1, 2, 3, ... , N]这个数据对应数字在该数组x中出现的次数。

    那么我们来验证一下经典的例子:

    x = np.array([0, 1, 1, 3, 2, 1, 7])

    第一个是看0在x中出现几次,1次;

    1出现几次,3次;

    2出现几次,1次;

    3出现几次,1次;

    4出现几次,0次;

    5出现几次,0次;

    6出现几次,0次;

    7出现几次,1次

    结果:

    np.bincount(x)

    array([0, 1, 1, 0, 1, 0, 1, 1])

    完美,再试一个?

    也是对的,为啥要长度的最大值加一呢,显然是这里从0开始计算出现次数的,所以最后一个是最大值,数组长度要加1了。

     另外一个验证方式,z的数字求和肯定和np.bincount(z).dot(np.arange(max(z)+1))相等

    至于其它参数就不介绍了。

  • 相关阅读:
    kubernetes案例 tomcat+mysql
    elasticsearch+logstash+kibana部署
    elasticsearch集群部署以及head插件安装
    Rhel7.4系统部署cobbler
    部署Hadoop2.0高性能集群
    使用haproxy实现负载均衡集群
    nginx实现动静分离的负载均衡集群
    heartrbeat实现web服务器高可用
    keepalived+lvs
    LVS集群之IP TUN模式以及网站压力测试
  • 原文地址:https://www.cnblogs.com/Kaivenblog/p/10824529.html
Copyright © 2011-2022 走看看