python - 如何在循环中连接 np.ndarray?
问题描述
我有一些 ndarray 值是在循环中构造的。我想将这些 ndarray 连接到轴为 0 的单个数组。我怎么能在 python 中做到这一点?这是我的例子
input: 1x2x32x32x64,1x2x32x32x64,1x2x32x32x64,1x2x32x32x64
output 4x2x32x32x64
我做了什么:
import numpy as np
A_concate=np.array([])
for i in range (4):
a_i = np.random.rand(1,2,32,32,64)
print (a_i.shape)
A_concate= np.concatenate(A_concate,a_i, axis=0)
print (A_concate.shape)
错误
Traceback (most recent call last): File "python", line 6, in <module> TypeError: Argument given by name ('axis') and position (2)
在线代码:https ://repl.it/repls/HugeKnownSolidstatedrive
使用 vstack 的第一个解决方案
import numpy as np
A_concate=[]
for i in range (4):
a_i = np.random.rand(1,2,32,32,64)
print (a_i.shape)
A_concate.append(a_i)
A_concate=np.vstack((A_concate))
print (A_concate.shape)
解决方案
这是一个解决方案numpy.vstack
import numpy as np
A_stack = np.random.rand(1,2,32,32,64)
for i in range (3):
a_i = np.random.rand(1,2,32,32,64)
A_stack= np.vstack([A_stack,a_i])
print (A_stack.shape) # Outputs (4, 2, 32, 32, 64)
推荐阅读
- java - Antlr4 解析数
- java - Android Studio java.time.format.DateTimeParseException:从日期选择器中选择日期时无法解析文本
- azure-devops - Azure Devops - 限制特定 WIT 类型的写入权限
- drupal-7 - 如何在视图中显示段落字段中的所有数据(Drupal 7)
- javascript - 在 Laravel 表单中自动进行计算
- mongodb - 我应该怎么做才能对nestjs mongoose进行下一次测试?
- javascript - React 父组件道具无法在子组件中使用 forwardRef 道具访问
- asp.net-core - 在 Visual Studio(ASP.NET CORE 的本地调试会话)中查询 Application Insights 时,如何确保没有丢失遥测数据?
- macos - MacOS Monteray-Update 之后:未找到 DBT 分布
- java - MAC doFinal 为相同的数据给出不同的值