[TOC]
# 求和
~~~
import numpy as np
tang_array = np.array([[1, 2, 3], [4, 5, 6]])
print(tang_array)
# 求和
npSum = np.sum(tang_array)
print(np.sum(tang_array))
~~~
输出
~~~
[[1 2 3]
[4 5 6]]
21
~~~
# 指定要沿着什么轴(维度)求和
## 竖着求和
![](https://box.kancloud.cn/86e180d055b53ae79b79866095aa1dd4_214x122.png)
~~~
import numpy as np
tang_array = np.array([[1, 2, 3], [4, 5, 6]])
# 也可以写成tang_array.sum(axis=0)
npSum = np.sum(tang_array, axis=0)
print(npSum)
~~~
输出
`[5 7 9]`
## 横着求和
![](https://box.kancloud.cn/eae9fe4d0b9f83cb3172b14185250fb4_230x116.png)
~~~
import numpy as np
tang_array = np.array([[1, 2, 3], [4, 5, 6]])
# 也可以写成tang_array.sum(axis=1)
# axis = -1也可以
npSum = np.sum(tang_array, axis=1)
print(npSum)
~~~
输出
`[ 6 15]`
# 乘积
~~~
import numpy as np
tang_array = np.array([[1, 2, 3], [4, 5, 6]])
# 竖着乘
rel = tang_array.prod(axis=0)
print(rel)
# 横着乘,都乘起来不写参数
result = tang_array.prod(axis=1)
print(result)
~~~
输出
~~~
[ 4 10 18]
[ 6 120]
~~~
# 取最小值
~~~
import numpy as np
tang_array = np.array([[1, 2, 3], [4, 5, 6]])
# 全局最小值
rel = tang_array.min()
print(rel)
# 竖着取最小值
Vertically = tang_array.min(axis=0)
print(Vertically)
# 横着最小值
Sideways = tang_array.min(axis=1)
print(Sideways)
~~~
输出
~~~
1
[1 2 3]
[1 4]
~~~
相应最大值是max
**求最小值的索引**
~~~
import numpy as np
tang_array = np.array([[1, 2, 3], [4, 5, 6]])
# 如果要那个维度最小的索引就加参数axis=
argmin = tang_array.argmin()
print(argmin)
~~~
输出
`0`
# 求均值
~~~
import numpy as np
tang_array = np.array([[1, 2, 3], [4, 5, 6]])
# 全局的均值
mean = tang_array.mean()
print(mean)
# 求竖着的均值
array_mean = tang_array.mean(axis=0)
print(array_mean)
~~~
输出
~~~
3.5
[ 2.5 3.5 4.5]
~~~
# 求标准差
![](https://box.kancloud.cn/67ab480a69e165e7ac88522fda3e4d3f_1226x298.png)
~~~
tang_array = np.array([[1, 2, 3], [4, 5, 6]])
# 标准差
std = tang_array.std()
print(std)
~~~
输出
`1.70782512766`
# 求方差
~~~
# 方差
var = tang_array.var()
print(var)
~~~
输出
`2.91666666667`
# 输出限制
~~~
import numpy as np
tang_array = np.array([[1, 2, 3], [4, 5, 6]])
# 小于2的值变为2,大于4的值变为4
clip = tang_array.clip(2, 4)
print(clip)
~~~
输出
~~~
[[2 2 3]
[4 4 4]]
~~~
# 四舍五入
~~~
import numpy as np
tang_array = np.array([[1, 2, 3.1, 4.6, 6.1]])
# 四舍五入
array_round = tang_array.round()
print(array_round)
~~~
输出
~~~
[[ 1. 2. 3. 5. 6.]]
~~~
设置保留精度
~~~
import numpy as np
tang_array = np.array([[1.23, 2.67, 3.12, 4.6, 6.1]])
array_round = tang_array.round(decimals=1)
print(array_round)
~~~
输出
~~~
[[ 1.2 2.7 3.1 4.6 6.1]]
~~~