当前位置:网站首页>Numpy quick start (IV) -- random sampling and general functions
Numpy quick start (IV) -- random sampling and general functions
2022-07-03 10:40:00 【serity】
Catalog
One 、 Random sampling (np.random modular )
function | effect |
---|---|
np.random.random(size=None) | size yes Integers or Integer tuple ; Generate [0, 1) Random number between |
np.random.uniform(a=0.0, b=1.0, size=None) | Generate [a, b) Upper Uniform distribution The random number |
np.random.randn(d0, d1, …, dn) | d0, d1, …, dn Control shape ; Generate Standard normal The random number |
np.random.randint(a, b=None, size=None) | Generate [a, b) Random on Integers ; When b When omitted , Generate [0, a) Random integers over |
np.random.rand(d0, d1, …, dn) | And random.random The effect is the same , It's just size No longer a tuple |
np.random.normal(mu=0.0, sigma=1.0, size=none) | Generate Normal distribution The random number |
1.1 np.random.random()
print(np.random.random())
# 0.7244562837682348
print(np.random.random(2))
# [0.22283098 0.72975332]
print(np.random.random((2, 2)))
# [[0.30166809 0.39634144]
# [0.75755076 0.9186064 ]]
1.2 np.random.uniform()
print(np.random.uniform(2, 4))
# 2.8381002639487756
print(np.random.uniform(2, 4, (2, 2)))
# [[2.66832969 3.97791269]
# [3.28302481 2.77316893]]
1.3 np.random.randn()
print(np.random.randn())
# 0.07619658670655587
print(np.random.randn(1))
# [0.15591832]
print(np.random.randn(2, 2))
# [[ 1.80289612 1.26520523]
# [-0.18691585 -1.07256013]]
1.4 np.random.randint()
print(np.random.randint(1, 10))
# 2
print(np.random.randint(1, 10, (2, 2)))
# [[2 7]
# [2 3]]
1.5 np.random.rand()
And random.random comparison , More recommended random.rand.
print(np.random.rand())
# 0.4294526669056693
print(np.random.rand(2, 3))
# [[0.98298448 0.93969596 0.1592919 ]
# [0.4399961 0.52320823 0.5306809 ]]
1.6 np.random.normal()
print(np.random.normal(0, 1, (2, 3)))
# [[-1.42867251 1.07133406 -0.41702297]
# [ 0.83092417 0.17981133 2.15503384]]
print(np.random.normal(2, 100, (2, 3)))
# [[ -8.0837793 161.39657097 35.2570548 ]
# [ 72.32855794 51.32771614 10.86325406]]
Two 、 Common mathematics / Statistical function
Common mathematical functions :
function | effect |
---|---|
np.sin、np.cos | Trigonometric functions |
np.sqrt | square root |
np.exp | Exponential function |
np.log、np.log10、np.log2 | Logarithmic function |
np.abs | The absolute value |
np.sum | Sum up |
np.prod | quadrature |
Commonly used statistical functions :
function | effect |
---|---|
np.median | Calculate the median |
np.mean | Calculate average |
np.std | Calculate the standard deviation |
np.var | Calculate variance |
np.corrcoef | Calculate the correlation coefficient |
3、 ... and 、 Common logic functions
3.1 Truth test
function | effect |
---|---|
np.all(a, axis=None) | Judge array a Along the axis Whether the value in the direction All for True;axis by None Will judge whether the value along all directions is True |
np.any(a, axis=None) | Judge array a Along the axis Whether the value in the direction At least one for True |
3.1.1 np.all()
A = np.array([[True, False],[True, True]])
print(np.all(A))
print(np.all(A, axis=0))
print(np.all(A, axis=1))
# False
# [ True False]
# [False True]
print(np.all([1, 1]))
print(np.all([1, 0]))
print(np.all([0, 0]))
# True
# False
# False
3.1.2 np.any()
A = np.array([[True, False],[True, False]])
print(np.any(A))
print(np.any(A, axis=0))
print(np.any(A, axis=1))
# True
# [ True False]
# [ True True]
print(np.any([1, 1]))
print(np.any([1, 0]))
print(np.any([0, 0]))
# True
# True
# False
3.2 Comparison function
function | effect |
---|---|
np.array_equal(a1, a2) | If the array a1 and a2 Of The same shape And one of them The elements are the same , Then return to True, Otherwise return to False |
np.allclose(a, b, rtol=1e-5, atol=1e-8) | rtol yes relative Tolerance ,atol yes absolute Tolerance ; If the array a and b Within tolerance By element equal , Then return to True, Otherwise return to False |
3.2.1 np.array_equal()
print(np.array_equal([1, 2], [1, 2]))
print(np.array_equal(np.array([1, 2]), np.array([1, 2])))
print(np.array_equal([1, 2], [1, 2, 3]))
print(np.array_equal([1, 2], [1, 4]))
# True
# True
# False
# False
3.2.2 np.allclose()
If the array a and b According to the elements, the following inequality is satisfied :
∣ a − b ∣ ≤ a t o l + r t o l ⋅ ∣ b ∣ |a-b|\leq atol \, + \, rtol\, \cdot\, |b| ∣a−b∣≤atol+rtol⋅∣b∣
be allclose Function will return True. It can be seen that , The above inequality is asymmetric , So we can come to the conclusion that :
allclose(a, b)
andallclose(b, a)
In rare cases, they are not equal .
print(np.allclose([1e10, 1e-7], [1.00001e10, 1e-8]))
print(np.allclose([1e10, 1e-8], [1.00001e10, 1e-9]))
print(np.allclose([1e10, 1e-8], [1.0001e10, 1e-9]))
# False
# True
# False
边栏推荐
- C project - dormitory management system (1)
- Ut2015 learning notes
- Handwritten digit recognition: CNN alexnet
- An open source OA office automation system
- 【吐槽&脑洞】关于逛B站时偶然体验的弹幕互动游戏魏蜀吴三国争霸游戏的一些思考
- Leetcode skimming ---1
- Class-Variant Margin Normalized Softmax Loss for Deep Face Recognition
- Jetson TX2 brush machine
- Ut2012 learning notes
- Drop out (pytoch)
猜你喜欢
丢弃法Dropout(Pytorch)
安装yolov3(Anaconda)
Leetcode skimming ---367
8、 Transaction control language of MySQL
Hands on deep learning pytorch version exercise solution-3.3 simple implementation of linear regression
C#项目-寝室管理系统(1)
Unity学习笔记:联网游戏Pixel Adventure 1学习过程&纠错心得
2018 y7000 upgrade hard disk + migrate and upgrade black apple
Stroke prediction: Bayesian
GAOFAN Weibo app
随机推荐
Leetcode skimming ---367
Jetson TX2 brush machine
ThreadLocal principle and usage scenario
Ut2016 learning notes
Mysql5.7 installation and configuration tutorial (Graphic ultra detailed version)
Yolov5 creates and trains its own data set to realize mask wearing detection
神经网络入门之预备知识(PyTorch)
Binary search method
Raspberry pie 4B installs yolov5 to achieve real-time target detection
Leetcode刷题---10
八、MySQL之事务控制语言
Ind yff first week
[LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
丢弃法Dropout(Pytorch)
Handwritten digit recognition: CNN alexnet
Leetcode刷题---977
Leetcode刷题---263
A super cool background permission management system
MySQL reports an error "expression 1 of select list is not in group by claim and contains nonaggre" solution
Model evaluation and selection