当前位置:网站首页>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
边栏推荐
- Introduction to deep learning linear algebra (pytorch)
- Julia1.0
- Hands on deep learning pytorch version exercise solution -- implementation of 3-2 linear regression from scratch
- Realize an online examination system from zero
- Judging the connectivity of undirected graphs by the method of similar Union and set search
- Leetcode刷题---367
- What useful materials have I learned from when installing QT
- Leetcode刷题---189
- Ut2016 learning notes
- Mysql5.7 installation and configuration tutorial (Graphic ultra detailed version)
猜你喜欢

MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法

侯捷——STL源码剖析 笔记

ThreadLocal principle and usage scenario

A complete answer sheet recognition system

User recommendation preference model based on attention enhanced knowledge perception

一个30岁的测试员无比挣扎的故事,连躺平都是奢望

Hands on deep learning pytorch version exercise solution - 2.3 linear algebra

8、 Transaction control language of MySQL

Numpy realizes the classification of iris by perceptron
![[LZY learning notes -dive into deep learning] math preparation 2.1-2.4](/img/92/955df4a810adff69a1c07208cb624e.jpg)
[LZY learning notes -dive into deep learning] math preparation 2.1-2.4
随机推荐
[SQL] an article takes you to master the operations related to query and modification of SQL database
GAOFAN Weibo app
Ut2012 learning notes
Are there any other high imitation projects
Hands on deep learning pytorch version exercise answer - 2.2 preliminary knowledge / data preprocessing
【吐槽&脑洞】关于逛B站时偶然体验的弹幕互动游戏魏蜀吴三国争霸游戏的一些思考
ThreadLocal原理及使用场景
Mysql5.7 installation and configuration tutorial (Graphic ultra detailed version)
I really want to be a girl. The first step of programming is to wear women's clothes
Ut2016 learning notes
Leetcode刷题---852
Tensorflow—Image segmentation
Leetcode skimming ---263
Leetcode刷题---189
实战篇:Oracle 数据库标准版(SE)转换为企业版(EE)
Drop out (pytoch)
Data classification: support vector machine
Adaptive Propagation Graph Convolutional Network
Jetson TX2 brush machine
Hands on deep learning pytorch version exercise solution - 2.4 calculus