目录
0. 相关文章链接
1. 创建Array数组
2. 基本数值计算
2.1. numpy中的函数
2.2. 数组中的函数
3. 指定维度进行计算
3.1. numpy中的函数
3.2. 数组中的函数
4. 复杂计算
4.1. 统计乘机
4.2. 获取对应值的索引位置
4.3. 求平均值
4.4. 求标准差
4.5. 求方差
4.6. 取界限值
4.7. 取整
0. 相关文章链接
Python文章汇总
1. 创建Array数组
import numpy as np
demo_array = np.array([[1,2,3],[4,5,6]])
demo_array
array([[1, 2, 3],
[4, 5, 6]])
2. 基本数值计算
2.1. numpy中的函数
# 使用 numpy 中的函数进行数值计算
print(demo_array)
print("==================")
print(np.sum(demo_array))
print(np.max(demo_array))
print(np.min(demo_array))
[[1 2 3]
[4 5 6]]
==================
21
6
1
2.2. 数组中的函数
# 直接使用数组的函数进行数值计算
print(demo_array)
print("==================")
print(demo_array.sum())
print(demo_array.max())
print(demo_array.min())
[[1 2 3]
[4 5 6]]
==================
21
6
1
3. 指定维度进行计算
3.1. numpy中的函数
# 指定要进行的操作是沿着什么轴(维度)
# 会根据 shape 中的维度来统计,以二维数组举例,元组的第一位是有多少行(横轴),第二位是有多少列(竖轴)
# 如果指定根据横轴(axis=0)来计算,那计算方式为将同一个竖轴的元素进行聚合(即横轴上有多少个元素,那返回的数组就有多少元素)
# 如果指定根据竖轴(axis=1)来计算,那计算方式为将同一个横轴的元素进行聚合(即竖轴上有多少个元素,那返回的数组就有多少元素)
# 如果axis=-1,即取shape返回的元组中的最后那个
print(demo_array)
print("==================")
print(np.ndim(demo_array))
print(np.shape(demo_array))
print("==================")
print(np.sum(demo_array,axis=0))
print(np.max(demo_array,axis=0))
print(np.min(demo_array,axis=0))
print("==================")
print(np.sum(demo_array,axis=1))
print(np.max(demo_array,axis=1))
print(np.min(demo_array,axis=1))
print("==================")
print(np.sum(demo_array,axis=-1))
print(np.max(demo_array,axis=-1))
print(np.min(demo_array,axis=-1))
[[1 2 3]
[4 5 6]]
==================
2
(2, 3)
==================
[5 7 9]
[4 5 6]
[1 2 3]
==================
[ 6 15]
[3 6]
[1 4]
==================
[ 6 15]
[3 6]
[1 4]
3.2. 数组中的函数
# 跟上述类似,只是使用的是数组中的数值计算方法
print(demo_array)
print("==================")
print(demo_array.ndim)
print(demo_array.shape)
print("==================")
print(demo_array.sum(axis = 0))
print(demo_array.max(axis = 0))
print(demo_array.min(axis = 0))
print("==================")
print(demo_array.sum(axis=1))
print(demo_array.max(axis=1))
print(demo_array.min(axis=1))
print("==================")
print(demo_array.sum(axis=-1))
print(demo_array.max(axis=-1))
print(demo_array.min(axis=-1))
[[1 2 3]
[4 5 6]]
==================
2
(2, 3)
==================
[5 7 9]
[4 5 6]
[1 2 3]
==================
[ 6 15]
[3 6]
[1 4]
==================
[ 6 15]
[3 6]
[1 4]
4. 复杂计算
4.1. 统计乘机
# 求选中范围内数值的乘积
print(demo_array)
print("==================")
print(demo_array.ndim)
print(demo_array.shape)
print("==================")
print(demo_array.prod())
print(demo_array.prod(axis = 0))
print(demo_array.prod(axis = 1))
[[1 2 3]
[4 5 6]]
==================
2
(2, 3)
==================
720
[ 4 10 18]
[ 6 120]
4.2. 获取对应值的索引位置
# 求出对应值的索引位置(多维数值的索引是所有元素放入一起进行计算的)
print(demo_array)
print("==================")
print(demo_array.ndim)
print(demo_array.shape)
print("==================")
print(demo_array.argmin())
print(demo_array.argmin(axis = 0))
print(demo_array.argmin(axis = 1))
print("==================")
print(demo_array.argmax())
print(demo_array.argmax(axis = 0))
print(demo_array.argmax(axis = 1))
[[1 2 3]
[4 5 6]]
==================
2
(2, 3)
==================
0
[0 0 0]
[0 0]
==================
5
[1 1 1]
[2 2]
4.3. 求平均值
# 求平均值
print(demo_array)
print("==================")
print(demo_array.ndim)
print(demo_array.shape)
print("==================")
print(demo_array.sum())
print(demo_array.size)
print(demo_array.mean())
print("==================")
print(demo_array.sum(axis = 0))
print(np.size(demo_array, axis = 0))
print(demo_array.mean(axis = 0))
[[1 2 3]
[4 5 6]]
==================
2
(2, 3)
==================
21
6
3.5
==================
[5 7 9]
2
[2.5 3.5 4.5]
4.4. 求标准差
# 求标准差
print(demo_array)
print("==================")
print(demo_array.ndim)
print(demo_array.shape)
print("==================")
print(demo_array.std())
print(demo_array.std(axis = 0))
print(demo_array.std(axis = 1))
print(demo_array.std(axis = -1))
[[1 2 3]
[4 5 6]]
==================
2
(2, 3)
==================
1.707825127659933
[1.5 1.5 1.5]
[0.81649658 0.81649658]
[0.81649658 0.81649658]
4.5. 求方差
# 求方差
print(demo_array)
print("==================")
print(demo_array.ndim)
print(demo_array.shape)
print("==================")
print(demo_array.var())
print(demo_array.var(axis = 0))
print(demo_array.var(axis = 1))
[[1 2 3]
[4 5 6]]
==================
2
(2, 3)
==================
2.9166666666666665
[2.25 2.25 2.25]
[0.66666667 0.66666667]
4.6. 取界限值
# 界限值,将该数组中超出该界限值的元素强制赋值成最小和最大值
demo_array.clip(2,5)
array([[2, 2, 3],
[4, 5, 5]])
4.7. 取整
demo_array = np.array([1.2,3.56,6.41])
print(demo_array)
# 四舍五入取整,不保留小数
print(demo_array.round())
# 四舍五入,保留一位小数
print(demo_array.round(decimals=1))
[1.2 3.56 6.41]
[1. 4. 6.]
[1.2 3.6 6.4]
注:其他Python相关系列文章链接由此进 -> Python文章汇总