当前位置:网站首页>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
边栏推荐
- 神经网络入门之模型选择(PyTorch)
- Raspberry pie 4B installs yolov5 to achieve real-time target detection
- Leetcode skimming ---283
- Hou Jie -- STL source code analysis notes
- Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
- Ut2013 learning notes
- Boston house price forecast (tensorflow2.9 practice)
- Ind FHL first week
- Judging the connectivity of undirected graphs by the method of similar Union and set search
- Automatic derivation of introduction to deep learning (pytoch)
猜你喜欢

C project - dormitory management system (1)

Boston house price forecast (tensorflow2.9 practice)

Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation

Weight decay (pytorch)

【SQL】一篇带你掌握SQL数据库的查询与修改相关操作

Mysql5.7 installation and configuration tutorial (Graphic ultra detailed version)

Matrix calculation of Neural Network Introduction (pytoch)

User recommendation preference model based on attention enhanced knowledge perception

深度学习入门之自动求导(Pytorch)

【吐槽&脑洞】关于逛B站时偶然体验的弹幕互动游戏魏蜀吴三国争霸游戏的一些思考
随机推荐
熵值法求权重
Unity学习笔记:个人学习项目《疯狂天才埃德加》纠错文档
Binary search method
Leetcode skimming ---278
Ind wks first week
Leetcode刷题---852
Numpy realizes the classification of iris by perceptron
Leetcode刷题---263
Inverse code of string (Jilin University postgraduate entrance examination question)
Entropy method to calculate weight
[untitled]
Leetcode刷题---283
Drop out (pytoch)
Boston house price forecast (tensorflow2.9 practice)
Leetcode刷题---10
Leetcode刷题---1385
[untitled]
Tensorflow—Neural Style Transfer
丢弃法Dropout(Pytorch)
Configure opencv in QT Creator