python np.ceil()和np.repeat(),图像通道赋值的用法

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.]]]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智能学习者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值