import numpy as np
a = np.zeros((2, 10))
a[np.arange(2), 4] = 1
print("逐个赋值后的值:", a)
b = np.array([0.34, 1.3, 0.75, -0.23, -1.1])
b = np.ceil(b)
print("向上取整的值:", b)
逐个赋值后的值: [[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]]
向上取整的值: [ 1. 2. 1. -0. -1.]
print('np.repeat()的用法')
t = np.array([[1, 2, 4, 5, 7], [-0.23, 0.45, -1.2, 7, 0.7],
[3.2, 0.44, -0.65, -0.6, 0.5]])
t0 = np.repeat(t, 2, axis=0)
t0_reshape = t0.reshape(t.shape[0], -1)
t1 = np.repeat(t, 2, axis=1)
t1_reshape = t1.reshape(-1, t.shape[1])
print('输入值:', t, t.shape)
print("按第1维度复制矩阵结果:", t0, t0.shape)
print("按第1维度复制并reshape矩阵结果:", t0_reshape, t0_reshape.shape)
np_class = np.ones((3, 20))
t0_reshape = np.concatenate([t0_reshape, np_class], axis=1)
print('拼接后的结果:', t0_reshape, t0_reshape.shape)
print("按第2维度复制矩阵结果:", t1, t1.shape)
print("按第2维度复制并reshape矩阵结果:", t1_reshape, t1_reshape.shape)
np_target = np.zeros((7, 7, 30))
y_idx = np.array([[3], [5], [4]])
x_idx = np.array([[3], [5], [2]])
for i in range(3):
np_target[int(y_idx[i]), int(x_idx[i])] = t0_reshape[i]
print('矩阵直接赋值结果:', np_target, np_target.shape)
np.repeat()的用法
输入值: [[ 1. 2. 4. 5. 7. ]
[-0.23 0.45 -1.2 7. 0.7 ]
[ 3.2 0.44 -0.65 -0.6 0.5 ]] (3, 5)
按第1维度复制矩阵结果: [[ 1. 2. 4. 5. 7. ]
[ 1. 2. 4. 5. 7. ]
[-0.23 0.45 -1.2 7. 0.7 ]
[-0.23 0.45 -1.2 7. 0.7 ]
[ 3.2 0.44 -0.65 -0.6 0.5 ]
[ 3.2 0.44 -0.65 -0.6 0.5 ]] (6, 5)
按第1维度复制并reshape矩阵结果: [[ 1. 2. 4. 5. 7. 1. 2. 4. 5. 7. ]
[-0.23 0.45 -1.2 7. 0.7 -0.23 0.45 -1.2 7. 0.7 ]
[ 3.2 0.44 -0.65 -0.6 0.5 3.2 0.44 -0.65 -0.6 0.5 ]] (3, 10)
拼接后的结果: [[ 1. 2. 4. 5. 7. 1. 2. 4. 5. 7. 1. 1.
1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
1. 1. 1. 1. 1. 1. ]
[-0.23 0.45 -1.2 7. 0.7 -0.23 0.45 -1.2 7. 0.7 1. 1.
1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
1. 1. 1. 1. 1. 1. ]
[ 3.2 0.44 -0.65 -0.6 0.5 3.2 0.44 -0.65 -0.6 0.5 1. 1.
1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
1. 1. 1. 1. 1. 1. ]] (3, 30)
按第2维度复制矩阵结果: [[ 1. 1. 2. 2. 4. 4. 5. 5. 7. 7. ]
[-0.23 -0.23 0.45 0.45 -1.2 -1.2 7. 7. 0.7 0.7 ]
[ 3.2 3.2 0.44 0.44 -0.65 -0.65 -0.6 -0.6 0.5 0.5 ]] (3, 10)
按第2维度复制并reshape矩阵结果: [[ 1. 1. 2. 2. 4. ]
[ 4. 5. 5. 7. 7. ]
[-0.23 -0.23 0.45 0.45 -1.2 ]
[-1.2 7. 7. 0.7 0.7 ]
[ 3.2 3.2 0.44 0.44 -0.65]
[-0.65 -0.6 -0.6 0.5 0.5 ]] (6, 5)
矩阵直接赋值结果: [[[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
...
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]]
[[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
...
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]]
[[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
...
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]]
...
[[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
[ 3.2 0.44 -0.65 ... 1. 1. 1. ]
...
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]]
[[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
...
[ 0. 0. 0. ... 0. 0. 0. ]
[-0.23 0.45 -1.2 ... 1. 1. 1. ]
[ 0. 0. 0. ... 0. 0. 0. ]]
[[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
...
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]]] (7, 7, 30)
矩阵格式:(3,4,2),对应于图片的格式:(heght,width,channge_num)
print('多维矩阵赋值向量')
c = np.zeros((3, 4, 2))
a = np.array(([3, 4], [5, 6]))
c[0, 1] = a[0]
print('赋值到第1块矩阵第2行向量:',c)
c[1, 2] = a[1]
print('赋值到第2块矩阵第3行向量:',c)
c[2, 3] = a[0]
print('赋值到第3块矩阵第4行向量:',c)
多维矩阵赋值向量
赋值到第1块矩阵第2行向量: [[[0. 0.]
[3. 4.]
[0. 0.]
[0. 0.]]
[[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]]
[[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]]]
赋值到第2块矩阵第3行向量: [[[0. 0.]
[3. 4.]
[0. 0.]
[0. 0.]]
[[0. 0.]
[0. 0.]
[5. 6.]
[0. 0.]]
[[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]]]
赋值到第3块矩阵第4行向量: [[[0. 0.]
[3. 4.]
[0. 0.]
[0. 0.]]
[[0. 0.]
[0. 0.]
[5. 6.]
[0. 0.]]
[[0. 0.]
[0. 0.]
[0. 0.]
[3. 4.]]]