Files
ailearning/docs/da/033.md
2020-10-19 21:08:55 +08:00

518 lines
3.8 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 数组方法
In [1]:
```py
%pylab
```
```py
Using matplotlib backend: Qt4Agg
Populating the interactive namespace from numpy and matplotlib
```
## 求和
In [2]:
```py
a = array([[1,2,3],
[4,5,6]])
```
求所有元素的和:
In [3]:
```py
sum(a)
```
Out[3]:
```py
21
```
指定求和的维度:
沿着第一维求和:
In [4]:
```py
sum(a, axis=0)
```
Out[4]:
```py
array([5, 7, 9])
```
沿着第二维求和:
In [5]:
```py
sum(a, axis=1)
```
Out[5]:
```py
array([ 6, 15])
```
沿着最后一维求和:
In [6]:
```py
sum(a, axis=-1)
```
Out[6]:
```py
array([ 6, 15])
```
或者使用 `sum` 方法:
In [7]:
```py
a.sum()
```
Out[7]:
```py
21
```
In [8]:
```py
a.sum(axis=0)
```
Out[8]:
```py
array([5, 7, 9])
```
In [9]:
```py
a.sum(axis=-1)
```
Out[9]:
```py
array([ 6, 15])
```
## 求积
求所有元素的乘积:
In [10]:
```py
a.prod()
```
Out[10]:
```py
720
```
或者使用函数形式:
In [11]:
```py
prod(a, axis=0)
```
Out[11]:
```py
array([ 4, 10, 18])
```
## 求最大最小值
In [12]:
```py
from numpy.random import rand
a = rand(3, 4)
%precision 3
a
```
Out[12]:
```py
array([[ 0.444, 0.06 , 0.668, 0.02 ],
[ 0.793, 0.302, 0.81 , 0.381],
[ 0.296, 0.182, 0.345, 0.686]])
```
全局最小:
In [13]:
```py
a.min()
```
Out[13]:
```py
0.020
```
沿着某个轴的最小:
In [14]:
```py
a.min(axis=0)
```
Out[14]:
```py
array([ 0.296, 0.06 , 0.345, 0.02 ])
```
全局最大:
In [15]:
```py
a.max()
```
Out[15]:
```py
0.810
```
沿着某个轴的最大:
In [16]:
```py
a.max(axis=-1)
```
Out[16]:
```py
array([ 0.668, 0.81 , 0.686])
```
## 最大最小值的位置
使用 `argmin, argmax` 方法:
In [17]:
```py
a.argmin()
```
Out[17]:
```py
3
```
In [18]:
```py
a.argmin(axis=0)
```
Out[18]:
```py
array([2, 0, 2, 0], dtype=int64)
```
## 均值
可以使用 `mean` 方法:
In [19]:
```py
a = array([[1,2,3],[4,5,6]])
```
In [20]:
```py
a.mean()
```
Out[20]:
```py
3.500
```
In [21]:
```py
a.mean(axis=-1)
```
Out[21]:
```py
array([ 2., 5.])
```
也可以使用 `mean` 函数:
In [22]:
```py
mean(a)
```
Out[22]:
```py
3.500
```
还可以使用 `average` 函数:
In [23]:
```py
average(a, axis = 0)
```
Out[23]:
```py
array([ 2.5, 3.5, 4.5])
```
`average` 函数还支持加权平均:
In [24]:
```py
average(a, axis = 0, weights=[1,2])
```
Out[24]:
```py
array([ 3., 4., 5.])
```
## 标准差
`std` 方法计算标准差:
In [25]:
```py
a.std(axis=1)
```
Out[25]:
```py
array([ 0.816, 0.816])
```
`var` 方法计算方差:
In [26]:
```py
a.var(axis=1)
```
Out[26]:
```py
array([ 0.667, 0.667])
```
或者使用函数:
In [27]:
```py
var(a, axis=1)
```
Out[27]:
```py
array([ 0.667, 0.667])
```
In [28]:
```py
std(a, axis=1)
```
Out[28]:
```py
array([ 0.816, 0.816])
```
## clip 方法
将数值限制在某个范围:
In [29]:
```py
a
```
Out[29]:
```py
array([[1, 2, 3],
[4, 5, 6]])
```
In [30]:
```py
a.clip(3,5)
```
Out[30]:
```py
array([[3, 3, 3],
[4, 5, 5]])
```
小于3的变成3大于5的变成5。
## ptp 方法
计算最大值和最小值之差:
In [31]:
```py
a.ptp(axis=1)
```
Out[31]:
```py
array([2, 2])
```
In [32]:
```py
a.ptp()
```
Out[32]:
```py
5
```
## round 方法
近似,默认到整数:
In [33]:
```py
a = array([1.35, 2.5, 1.5])
```
这里,.5的近似规则为近似到偶数值,可以参考:
[https://en.wikipedia.org/wiki/Rounding#Round_half_to_odd](https://en.wikipedia.org/wiki/Rounding#Round_half_to_odd)
In [34]:
```py
a.round()
```
Out[34]:
```py
array([ 1., 2., 2.])
```
近似到一位小数:
In [35]:
```py
a.round(decimals=1)
```
Out[35]:
```py
array([ 1.4, 2.5, 1.5])
```