numpy 学习笔记
1. np.array([127, 127, 127])
生成一个一维数组 [127, 127, 127]。
参考: https://numpy.org/doc/stable/reference/generated/numpy.array.html
2. a = np.reshape(list(range(24)), (2, 3, 4))
reshape 的第二个参数是维度参数,比如这个是一个三维数组,行数 2 行,列数 3 行,每个单位 4 个元素。
参考: https://numpy.org/doc/stable/reference/generated /numpy.reshape.html?highlight=reshape#numpy.reshape
3. image = np.transpose(image, [2, 0, 1])
矩阵转置,第二个参数是相应的维度变化。 原始的维度是 [0, 1, 2] 其他的都是需要转换的。比如这个 [2, 0, 1] 意思是 把 第三维的数据提取出来作为第一维的开头数据,第一维的数据提取出来放到第二维,第二维的数据放到第三维。
arr = np.arange(16)
matrix = arr.reshape(2, 2, 4)
print("matrix: ", matrix)
matrix_4 = matrix.transpose((2, 0, 1))
print("matrix 4: ", matrix_4)
输出结果为
matrix: [ [ [ 0 1 2 3]
[ 4 5 6 7] ]
[ [ 8 9 10 11]
[12 13 14 15] ] ]
matrix 4: [ [ [ 0 4]
[ 8 12]]
[ [ 1 5]
[ 9 13] ]
[ [ 2 6]
[10 14] ]
[ [ 3 7]
[11 15] ] ]
参考: https://blog.csdn.net/u012762410/article/details/78912667
4. np.expand_dims(a, axis)
第二个参数表示具体再那个地方增加一个维度。
import numpy as np
a = np.reshape(list(range(24)), (2, 3, 4))
a_new = np.expand_dims(a, axis=0)
print('a =', a)
print('a_new =', a_new)
print('a.shape = ', a.shape)
print('a_new.shape = ', a_new.shape)
a = [[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
a_new = [[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]]
a.shape = (2, 3, 4)
a_new.shape = (1, 2, 3, 4)
import numpy as np
a = np.reshape(list(range(24)), (2, 3, 4))
print('a =', a)
print('np.expand_dims(a, axis=1) =', np.expand_dims(a, axis=1))
print('np.expand_dims(a, axis=2) =', np.expand_dims(a, axis=2))
print('np.expand_dims(a, axis=3) =', np.expand_dims(a, axis=3))
print('a.shape = ', a.shape)
print('np.expand_dims(a, axis=1).shape =', np.expand_dims(a, axis=1).shape)
print('np.expand_dims(a, axis=2).shape =', np.expand_dims(a, axis=2).shape)
print('np.expand_dims(a, axis=3).shape =', np.expand_dims(a, axis=3).shape)
a = [[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
np.expand_dims(a, axis=1) = [[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]]
[[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]]
np.expand_dims(a, axis=2) = [[[[ 0 1 2 3]]
[[ 4 5 6 7]]
[[ 8 9 10 11]]]
[[[12 13 14 15]]
[[16 17 18 19]]
[[20 21 22 23]]]]
np.expand_dims(a, axis=3) = [[[[ 0]
[ 1]
[ 2]
[ 3]]
[[ 4]
[ 5]
[ 6]
[ 7]]
[[ 8]
[ 9]
[10]
[11]]]
[[[12]
[13]
[14]
[15]]
[[16]
[17]
[18]
[19]]
[[20]
[21]
[22]
[23]]]]
a.shape = (2, 3, 4)
np.expand_dims(a, axis=1).shape = (2, 1, 3, 4)
np.expand_dims(a, axis=2).shape = (2, 3, 1, 4)
np.expand_dims(a, axis=3).shape = (2, 3, 4, 1)
参考: https://blog.csdn.net/weixin_41560402/article/details/105289015
5. .astype(np.float32)
转换数据类型。
参考: https://blog.csdn.net/qq_34638161/article/details/102853276
6. np.concatenate()
对数列或者矩阵进行合并。
import numpy as np
a=[1,2,3]
b=[4,5,6]
np.concatenate((a,b),axis=0)
array([1, 2, 3, 4, 5, 6])
参考: https://www.jianshu.com/p/a094a954ff61
7. numpy 和 list 互转
numpy -> list: array.tolist()
list -> numpy: numpy.array(list)
参考: https://www.cnblogs.com/WMT-Azura/p/11138084.html
参考数目: https://www.runoob.com/numpy/numpy-array-manipulation.html https://numpy.org/doc/stable/user/c-info.html https://numpy.org.cn/article/advanced/numpy_exercises_for_data_analysis.html#numpy%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%E9%97%AE%E7%AD%94