美文网首页Python学习列表
【机器学习】【图解】numpy.bincount函数详解

【机器学习】【图解】numpy.bincount函数详解

作者: xiandxd | 来源:发表于2019-09-29 13:14 被阅读0次

前言

  • 记录一下机器学习中遇到的numpy.bincount函数

以下代码是在原文档中摘抄的解释说明文字

"""
    bincount(x, weights=None, minlength=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 ``i``, ``out[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.

        .. versionadded:: 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.
"""

1. 它大致说bin的数量比x中的最大值大1,每个bin给出了它的索引值在x中出现的次数。

举例:

# 我们可以看到x中最大的数为7,因此bin的数量为8,那么它的索引值为0->7
x = np.array([0, 1, 1, 3, 2, 1, 7])
# 索引0出现了1次,索引1出现了3次......索引5出现了0次......
np.bincount(x)
#因此,输出结果为:array([1, 3, 1, 1, 0, 0, 0, 1])
图一
# 我们可以看到x中最大的数为7,因此bin的数量为8,那么它的索引值为0->7
x = np.array([7, 6, 2, 1, 4])
# 索引0出现了0次,索引1出现了1次......索引5出现了0次......
np.bincount(x)
#输出结果为:array([0, 1, 1, 0, 1, 0, 1, 1])
图二

2. 若weights参数被指定,x会被它加权;如果值n发现在位置i,那么out[n] += weight[i]而不是out[n] += 1.那么,weights的大小必须与x相同,否则报错。

w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) # weights
# x数组中最大的数为2,因此bin的数量为3,索引为[0,1,2],其对应的值为[1,2,3]
x = np.array([0, 1, 1, 2, 2, 2])
# weights = w
# 索引0 -> w[0] = 0.3
# 索引1 -> w[1] + w[2] = 0.5 + 0.2  = 0.7
# 索引2 -> w[3] + w[4] + w[5] = 0.7 + 1. + (-0.6) = 1.1
np.bincount(x,  weights=w)
# 输出结果为: array([ 0.3,  0.7,  1.1])
图三

3. 若minlength被指定,那么输出数组中bin的数量至少为它指定的数(如果必要的话,bin的数量会更大,这取决于x)

# 我们可以看到x中最大的数为3,因此bin的数量为4,那么它的索引值为0->3
x = np.array([3, 2, 1, 3, 1])
# bin的数量为4,现在我们指定了参数为7,因此现在bin的数量为7,所以现在它的索引值为0->6
np.bincount(x, minlength=7)
# 因此,输出结果为:array([0, 2, 1, 2, 0, 0, 0]
#--------------------#
# 我们可以看到x中最大的数为3,因此bin的数量为4,那么它的索引值为0->3
x = np.array([3, 2, 1, 3, 1])
# 本来bin的数量为4,现在我们指定了参数为1,那么它指定的数量小于原本的数量,因此这个参数失去了作用,索引值还是0->3
np.bincount(x, minlength=1) 
# 因此,输出结果为:array([0, 2, 1, 2])

相关文章

  • 【机器学习】【图解】numpy.bincount函数详解

    前言 记录一下机器学习中遇到的numpy.bincount函数 以下代码是在原文档中摘抄的解释说明文字 1. 它大...

  • 2020-05-11

    详解机器学习中的损失函数https://www.jianshu.com/p/00a405962dca?from=s...

  • 机器学习-损失函数详解

    损失函数(loss function)是用来估量你模型的预测值f(x)与真实值Y的不一致程度,它是一个非负实值函数...

  • uiscrollview

    IOS学习笔记——iOS组件之UIScrollView详解图解UIScrollView的contentOffset...

  • 《图解机器学习》PDF高清完整版-免费下载

    《图解机器学习》PDF高清完整版-免费下载 《图解机器学习》PDF高清完整版-免费下载 下载地址:网盘下载 备用地...

  • 【好书推荐】机器学习书单

    今天推荐的图书专注机器学习主题,一共7本,都是经过实践检验的好书——《机器学习》《图解机器学习》《机器学习实战》《...

  • 饭店流量预测

    lightgbm 模型 修改参数 交叉验证 【机器学习】Cross-Validation(交叉验证)详解. 在机器...

  • 详解机器学习中的损失函数

    1. 前言 我们知道机器学习的三要素是:方法= 模型+策略+算法, 如何从假设空间中选择最优模型,这涉及到我们需要...

  • C/C++的30个冷知识

    数据格式详解 输入输出函数详解 字符串处理函数详解 内存函数详解 类详解 数据格式详解 2^8=256(同样是一个...

  • [图解机器学习] 开篇

    今天在楼下吃了饭,骑了个摩拜单车,来回骑行十公里,去八角借了图书《图解机器学习》。正所谓“书非借不能读也”,所以此...

网友评论

    本文标题:【机器学习】【图解】numpy.bincount函数详解

    本文链接:https://www.haomeiwen.com/subject/zdfmuctx.html