- 返回各维度元组
print(img.shape) - 返回大小
img.size - 返回各维度数据类型
print(img.dtype)
- 数据类型变int8
mask=mask.astype(np.int8)注意int32可变float64 但float64变int32会把小数截断 string_可变float64
NumPy常见操作:
import numpy as np
创建一个一维数组 arr = np.array([1, 2, 3, 4, 5]) print("原始数组:", arr)
对数组进行求和 sum_result = np.sum(arr) print("求和结果:", sum_result)
对数组进行均值计算 mean_result = np.mean(arr) print("均值结果:", mean_result)
对数组进行最大值计算 max_result = np.max(arr) print("最大值结果:", max_result)
对数组进行排序 sort_result = np.sort(arr) print("排序结果:", sort_result)
#创建一个二维数组 arr = np.array([[1, 2, 3], [4, 5, 6]]) print("二维数组:", arr)
#创建一个全为零的数组 zeros_arr = np.zeros((2, 3)) print("全零数组:", zeros_arr)
#创建一个全为一的数组 ones_arr = np.ones((3, 2)) print("全一数组:", ones_arr)
#创建一个随机数组 random_arr = np.random.rand(2, 3) print("随机数组:", random_arr)
arr = np.array([1, 2, 3, 4, 5]) #访问数组的元素 print("第一个元素:", arr[0]) print("最后一个元素:", arr[-1]) print("切片操作:", arr[1:4])
#修改数组的元素 arr[2] = 10 print("修改后的数组:", arr)
#数组形状操作 print("数组形状:", arr.shape)
#数组重塑 reshape_arr = arr.reshape((5, 1)) print("重塑后的数组:", reshape_arr)
数组转置 transpose_arr = arr.T print("转置后的数组:", transpose_arr) 数组计算: python 复制 import numpy as np
arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6])
数组加法 add_result = arr1 + arr2 print("数组加法结果:", add_result)
数组减法 sub_result = arr1 - arr2 print("数组减法结果:", sub_result)
数组乘法 mul_result = arr1 * arr2 print("数组乘法结果:", mul_result)
数组除法 div_result = arr1 / arr2 print("数组除法结果:", div_result)
数组平方 square_result = np.square(arr1) print("数组平方结果:", square_result)
数组平方根 sqrt_result = np.sqrt(arr1) print("数组平方根结果:", sqrt_result)
- 数组拼接:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# 水平拼接
hstack_result = np.hstack((arr1, arr2))
print("水平拼接结果:", hstack_result)
# 垂直拼接
vstack_result = np.vstack((arr1, arr2))
print("垂直拼接结果:", vstack_result)
# 深度拼接
dstack_result = np.dstack((arr1, arr2))
print("深度拼接结果:", dstack_result)
- 数组索引和切片:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# 使用索引获取特定元素
print("索引获取元素:", arr[0, 1])
# 使用切片获取子数组
print("切片获取子数组:", arr[1:, :2])
- 数组统计函数:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# 求和
sum_result = np.sum(arr)
print("求和结果:", sum_result)
# 均值
mean_result = np.mean(arr)
print("均值结果:", mean_result)
# 标准差
std_result = np.std(arr)
print("标准差结果:", std_result)
# 方差
var_result = np.var(arr)
print("方差结果:", var_result)
# 最小值
min_result = np.min(arr)
print("最小值结果:", min_result)
# 最大值
max_result = np.max(arr)
print("最大值结果:", max_result)
- 数组的广播:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# 广播加法
broadcast_add_result = arr1 + arr2
print("广播加法结果:", broadcast_add_result)
# 广播乘法
broadcast_mul_result = arr1 * arr2
print("广播乘法结果:", broadcast_mul_result)
- 数组的逻辑运算:
import numpy as np
arr1 = np.array([True, False, True])
arr2 = np.array([False, True, True])
# 逻辑与
and_result = np.logical_and(arr1, arr2)
print("逻辑与结果:", and_result)
# 逻辑或
or_result = np.logical_or(arr1, arr2)
print("逻辑或结果:", or_result)
# 逻辑非
not_result = np.logical_not(arr1)
print("逻辑非结果:", not_result)
- 数组的矩阵运算:
import numpy as np
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
# 矩阵乘法
matmul_result = np.matmul(matrix1, matrix2)
print("矩阵乘法结果:", matmul_result)
# 矩阵转置
transpose_result = np.transpose(matrix1)
print("矩阵转置结果:", transpose_result)
# 矩阵求逆
inv_result = np.linalg.inv(matrix1)
print("矩阵求逆结果:", inv_result)
- 数组的随机数生成:
import numpy as np
# 生成服从正态分布的随机数
normal_arr = np.random.normal(loc=0, scale=1, size=(3, 3))
print("正态分布随机数:", normal_arr)
# 生成均匀分布的随机数
uniform_arr = np.random.uniform(low=0, high=1, size=(3, 3))
print("均匀分布随机数:", uniform_arr)
# 随机打乱数组
arr = np.array([1, 2, 3, 4, 5])
np.random.shuffle(arr)
print("随机打乱数组:", arr)
- 数组的元素统计:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# 求和
sum_result = np.sum(arr)
print("求和结果:", sum_result)
# 按行求和
row_sum_result = np.sum(arr, axis=1)
print("按行求和结果:", row_sum_result)
# 按列求和
column_sum_result = np.sum(arr, axis=0)
print("按列求和结果:", column_sum_result)
# 最大值
max_result = np.max(arr)
print("最大值结果:", max_result)
# 按行找到最大值
row_max_result = np.max(arr, axis=1)
print("按行找到最大值结果:", row_max_result)
# 按列找到最大值
column_max_result = np.max(arr, axis=0)
print("按列找到最大值结果:", column_max_result)
- 数组的形状操作:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
# 改变数组形状为二维
reshape_result = np.reshape(arr, (2, 3))
print("改变形状后的数组:", reshape_result)
# 展平数组
flatten_result = arr.flatten()
print("展平数组:", flatten_result)
# 转置数组
transpose_result = np.transpose(arr)
print("转置数组:", transpose_result)
- 数组的数学函数:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# 求平方根
sqrt_result = np.sqrt(arr)
print("平方根结果:", sqrt_result)
# 求指数
exp_result = np.exp(arr)
print("指数结果:", exp_result)
# 求对数
log_result = np.log(arr)
print("对数结果:", log_result)
- 数组的排序:
import numpy as np
arr = np.array([3, 1, 5, 2, 4])
# 升序排序
sorted_arr = np.sort(arr)
print("升序排序结果:", sorted_arr)
# 降序排序
reverse_sorted_arr = np.sort(arr)[::-1]
print("降序排序结果:", reverse_sorted_arr)
# 按列排序
arr = np.array([[3, 1, 5], [2, 4, 6]])
column_sorted_arr = np.sort(arr, axis=0)
print("按列排序结果:", column_sorted_arr)
# 按行排序
row_sorted_arr = np.sort(arr, axis=1)
print("按行排序结果:", row_sorted_arr)
- 数组的去重:
import numpy as np
arr = np.array([1, 2, 3, 2, 4, 1])
# 去除重复元素
unique_arr = np.unique(arr)
print("去重结果:", unique_arr)
# 返回去重元素的索引
unique_indices = np.unique(arr, return_index=True)[1]
print("去重元素的索引:", unique_indices)
- 数组的布尔运算:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# 判断数组元素是否大于2
greater_than_2 = arr > 2
print("大于2的元素:", arr[greater_than_2])
# 判断数组元素是否满足条件
condition = (arr > 2) & (arr < 5)
print("满足条件的元素:", arr[condition])
# 使用布尔数组作为索引
bool_index = np.array([True, False, True, False, True])
print("使用布尔数组作为索引:", arr[bool_index])
- 数组的广播:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([[4], [5], [6]])
# 广播数组相加
broadcast_result = arr1 + arr2
print("广播相加的结果:", broadcast_result)
# 广播数组相乘
broadcast_multiply_result = arr1 * arr2
print("广播相乘的结果:", broadcast_multiply_result)
- 数组的数据类型:
import numpy as np
arr = np.array([1, 2, 3])
# 查看数组数据类型
dtype = arr.dtype
print("数组的数据类型:", dtype)
# 修改数组数据类型
arr_float = arr.astype(float)
print("修改数据类型后的数组:", arr_float)
- 数组的索引和切片:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# 获取数组元素
element = arr[2]
print("索引获取数组元素:", element)
# 切片获取子数组
sub_array = arr[1:4]
print("切片获取子数组:", sub_array)
# 布尔数组索引
bool_arr = np.array([True, False, True, False, True])
bool_indexing_result = arr[bool_arr]
print("布尔数组索引结果:", bool_indexing_result)
- 数组的形状操作:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
# 获取数组形状
shape = arr.shape
print("数组形状:", shape)
# 改变数组形状
reshape_arr = arr.reshape(3, 2)
print("改变形状后的数组:", reshape_arr)
# 展平数组
flatten_arr = arr.flatten()
print("展平数组:", flatten_arr)
- 数组的元素操作:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# 数组元素加法
addition_result = arr + 2
print("数组元素加法结果:", addition_result)
# 数组元素乘法
multiplication_result = arr * 3
print("数组元素乘法结果:", multiplication_result)
# 数组元素平方
square_result = np.square(arr)
print("数组元素平方结果:", square_result)
# 数组元素开方
sqrt_result = np.sqrt(arr)
print("数组元素开方结果:", sqrt_result)
- 数组的统计计算:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# 计算数组元素的和
sum_result = np.sum(arr)
print("数组元素的和:", sum_result)
# 计算数组元素的平均值
mean_result = np.mean(arr)
print("数组元素的平均值:", mean_result)
# 计算数组元素的标准差
std_result = np.std(arr)
print("数组元素的标准差:", std_result)
# 计算数组元素的最大值
max_result = np.max(arr)
print("数组元素的最大值:", max_result)
# 计算数组元素的最小值
min_result = np.min(arr)
print("数组元素的最小值:", min_result)
- 数组的逻辑运算:
import numpy as np
arr1 = np.array([True, False, True])
arr2 = np.array([False, True, True])
# 逻辑与
and_result = np.logical_and(arr1, arr2)
print("逻辑与结果:", and_result)
# 逻辑或
or_result = np.logical_or(arr1, arr2)
print("逻辑或结果:", or_result)
# 逻辑非
not_result = np.logical_not(arr1)
print("逻辑非结果:", not_result)
- 数组的广播和矢量化操作:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# 数组的广播相加
broadcast_addition = arr1 + arr2
print("广播相加结果:", broadcast_addition)
# 数组的广播相乘
broadcast_multiplication = arr1 * arr2
print("广播相乘结果:", broadcast_multiplication)
# 数组的矢量化操作
vectorized_operation = np.sin(arr1) + np.cos(arr2)
print("矢量化操作结果:", vectorized_operation)
- 数组的线性代数运算:
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
# 矩阵乘法
matrix_multiplication = np.matmul(arr1, arr2)
print("矩阵乘法结果:", matrix_multiplication)
# 矩阵的转置
matrix_transpose = arr1.T
print("矩阵转置结果:", matrix_transpose)
# 矩阵的逆
matrix_inverse = np.linalg.inv(arr1)
print("矩阵逆结果:", matrix_inverse)
# 矩阵的行列式
matrix_determinant = np.linalg.det(arr1)
print("矩阵行列式:", matrix_determinant)
- 数组的随机数生成:
import numpy as np
# 生成随机整数数组
random_int_array = np.random.randint(low=1, high=10, size=(3, 3))
print("随机整数数组:", random_int_array)
# 生成随机浮点数数组
random_float_array = np.random.rand(2, 2)
print("随机浮点数数组:", random_float_array)
# 生成服从正态分布的随机数数组
random_normal_array = np.random.normal(loc=0, scale=1, size=(3, 3))
print("服从正态分布的随机数数组:", random_normal_array)