python - 如何通过将最后一个元素复制 3 次将 (z,x,y,1) 形状的 numpy 数组转换为 (z,x,y,3) 形状的 numpy 数组?
问题描述
我想通过复制最后一个元素将 -(z,x,y,1)
形的 numpy 数组变成- 形的 numpy 数组?(z,x,y,3)
例如给出
import numpy as np
# The shape is (1,2,2,1) (that is z=1, x=2, y=2)
a = np.array([[[[1], [2]],[[3], [4]]]])
print(a.shape)
# I want to make it (1,2,2,3) by duplicating the last element 3 times as follow
a = np.array([[[[1,1,1], [2,2,2]],[[3,3,3], [4,4,4]]]])
print(a.shape)
所以给定一个 numpy 数组a
shape ,如何通过复制最后一个元素(z,x,y,1)
使其成为numpy 数组?(z,x,y,3)
解决方案
尝试这个:
def repeat_last(a, n=3):
a.repeat(n, axis=2).reshape(*a.shape[:-1], n)
推荐阅读
- c# - 清洁架构中实体框架的实现
- bootstrap-4 - bootstrap-selectpicker 继续关闭 keydown
- spring - 面对 io.netty.handler.timeout.ReadTimeoutException: null while using server sent events
- android - 没有为 Android 项目生成 Javadoc
- json - http://localhost:3000/api/user/[object%20Object] 处的无效 json 响应正文原因:意外的令牌 < 在 JSON 中的位置 0
- python - Shopee 产品名称中的数据抓取未分别显示其正确名称
- build.gradle - 更新 versionName 和 versionCode 的好习惯
- hibernate - Hibernate StaleObjectStateException 修复
- python - 我怎样才能将其更改为 for 循环?
- gtk - 即使条目已聚焦,如何使 Gtk.SearchEntry 的占位符文本可见?