当前位置:网站首页>[function document] torch Histc and paddle Histogram and numpy.histogram
[function document] torch Histc and paddle Histogram and numpy.histogram
2022-07-28 04:42:00 【Master Fuwen】
1. torch.histc
Excerpt from :
https://pytorch.org/docs/stable/generated/torch.histc.html
torch.histc(input, bins=100, min=0, max=0, *, out=None) → Tensor
be used for Calculate the histogram of tensor
Elements are classified as min and max Between cells of equal width . If min and max all zero , Then use the minimum and maximum values of the data .
Less than min Value and greater than max The element of will be Ignore
Parameters
input(Tensor)—— Input tensor .
bins(int)—— Number of histogram boxes
min(Scalar)—— The lower limit of the range ( Include )
max(Scalar)—— The upper limit of the range ( Include )
out(Tensor , Optional )– Output tensor .
Return value
The histogram is represented by tensor
Return type
Tensor
Example
import torch
input_tensor = torch.tensor([1., 2, 1, 2.5])
res = torch.histc(input_tensor, bins=4, min=0, max=3)
print(res)
tensor([0., 2., 1., 1.])
Example illustration

2. paddle.histogram
and
Excerpt from :
https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/histogram_cn.html#histogram
paddle.histogram(input, bins=100, min=0, max=0)
Calculate the histogram of the input tensor .
With min and max by range The border , Divide them into bins A straight bar , Then the sorted data is divided into straight bars (bins) in .
If min and max All for 0, The maximum and minimum values in the data are used as the boundary .
Parameters
input (Tensor) - Input Tensor. Dimension is multidimensional , The data type is int32、int64、float32 or float64.
bins (int) - Histogram bins( Straight bar ) The number of , The default is 100.
min (int) - range The lower boundary of ( contain ), The default is 0.
max (int) - range The upper boundary of ( contain ), The default is 0.
return
Tensor, The data is int64 type , Dimension for (nbins,).
Code example
import paddle
inputs = paddle.to_tensor([1, 2, 1])
result = paddle.histogram(inputs, bins=4, min=0, max=3)
print(result) # [0, 2, 1, 0]
3. numpy.histogram
hist, bin_edges = numpy.histogram(a, bins=10,
range=None,
normed=None,
weights=None,
density=None)
a Is an array of statistics to be
bins Specify the number of statistical intervals perhaps Directly enter the interval
range Is a length of 2 tuples , Represents the minimum and maximum values of the statistical range , The default value is None, The range of representation is determined by the range of data
(a.min(), a.max())weights Each element of the array is assigned a weight ,histogram() It will sum the weights corresponding to the array in the interval
density by True when , Returns the probability density of each interval ; by False Returns the number of elements in each interval
normed Has been abandoned , Should not be used
Just a few examples :
import numpy as np
hist, bin_edges = np.histogram([1., 2, 1, 2.5], 4, (0, 3))
hist
# Out: array([0, 2, 1, 1], dtype=int64)
bin_edges
# Out: array([0. , 0.75, 1.5 , 2.25, 3. ])
bin_edges yes (0,3) The division of 4 Intervals ,hist Is the number of each interval , And the one above torch.histc and paddle.histogram In the same way
import numpy as np
input_ = np.arange(5)
bins = np.array([0., 0.4, 0.8, 1.9])
hist, bin_edges = np.histogram(input_, bins=bins)
bin_edges
# Out: array([0. , 0.4, 0.8, 1.9])
hist
# Out: array([1, 0, 1], dtype=int64)
Direct will bins This interval is passed into , It can be seen that bin_edges == bins,hist Is the number of corresponding intervals , Elements that exceed the maximum and minimum values are ignored
import numpy as np
input_ = np.random.random(5,) * 5
# Out: array([2.27585698, 0.32795885, 3.16672458, 4.55222666, 3.71125298])
hist, bin_edges = np.histogram(input_, bins=5, range=(0, 6), density=True)
hist
# Out: array([0.16666667 0.16666667 0.16666667 0.33333333 0. ])
hist, bin_edges = np.histogram(input_, bins=5, range=(0, 6), density=False)
hist
# Out: array([1, 1, 1, 2, 0], dtype=int64)
bin_edges
# Out: array([0. , 1.2, 2.4, 3.6, 4.8, 6. ])
It can be seen that density=False when ,hist It's still quantity ,density=True when ,hist Becomes density , After the integral is 1
Finally, take a look at weights How to use parameters :
import numpy as np
input_ = np.array([2.27585698, 0.32795885, 3.16672458, 4.55222666, 3.71125298])
weight = np.array([1, 1, 2, 2, 3])
hist, bin_edges = np.histogram(input_, bins=5, range=(0, 6), weights=weight)
bin_edges
# Out: array([0. , 1.2, 2.4, 3.6, 4.8, 6. ])
hist
# Out: array([1, 1, 2, 5, 0])
weights The parameter directly cancels that each weight is 1 The situation of , therefore weights Of shape It should be related to the input vector shape identical
There is another example in the official documents :
>>> a = np.arange(5)
>>> hist, bin_edges = np.histogram(a, density=True)
>>> hist
array([0.5, 0. , 0.5, 0. , 0. , 0.5, 0. , 0.5, 0. , 0.5])
>>> hist.sum()
2.4999999999999996
>>> np.sum(hist * np.diff(bin_edges))
1.0
To illustrate ,density=True After the integral is 1
边栏推荐
- Practice and thinking of AI standardization engine in pink client
- printf()打印char* str
- 20-Openwrt crond crontab
- Cloud native Devops status survey questionnaire solicitation: kodelurover launched jointly with oschina
- Phpstorm2022 connect to the database
- [函数文档] torch.histc 与 paddle.histogram 与 numpy.histogram
- [II. Mobile web page development] 2D & 3D conversion and animation, mobile terminal layout, responsive layout
- Important SQL server functions - date functions
- Basic knowledge of network security - password (I)
- Performance comparison between set and list
猜你喜欢

Reading of a unified generic framework for aspect based sentimental analysis

MySQL数据库————初识数据库

Mysql database -- first knowledge database

05.01 string

吉利AI面试题【杭州多测师】【杭州多测师_王sir】

高数_第4章__曲线积分

Use Baidu developer tool 4.0 to build a dedicated applet IDE

Rendering process, how the code becomes a page (I)

重要的 SQL Server 函数 - 字符串实用程序

Information system project manager (2022) - key content: Knowledge Management (15)
随机推荐
Ma Yi, Shen Xiangyang, Cao Ying's latest AI overview is hot! It took 3 months to build, netizens: required papers
Warning: file already exists but should not: c:\users\workmai\appdata\local\temp appears when Python packages exe\_ MEI13
【sylar】框架篇-Chapter10-Address 模块
01 node express system framework construction (express generator)
Rendering process, how the code becomes a page (I)
20-Openwrt crond crontab
[Sylar] framework chapter -chapter21- environment variable module
Mac installs mysql5.7 through brew
[Sylar] framework -chapter14 tcpserver module
物联网工业串口转WiFi模块 无线路由WiFi模块的选型
alter和confirm,prompt的区别
Important SQL server functions - date functions
Space complexity calculation super full sorting!! (calculation of hand tearing complexity
[Sylar] framework Chapter 23 summary of module chapter
高数_第4章__曲线积分_习题解法
Advanced architects, 16 common principles of microservice design and Governance
[Sylar] framework chapter -chapter10-address module
Render the data obtained from the database to the table in elementui
[Sylar] framework -chapter24- support business modularization
Elementary level of C language -- while, for, do while