在 numpy 中将 3 个数组合并为一个 3D 数组 [英] Combining 3 arrays in one 3D array in numpy

查看:77
本文介绍了在 numpy 中将 3 个数组合并为一个 3D 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 numpy 中的数组,我有一个非常基本的问题,但我找不到快速的方法.我有三个具有相同维度的二维数组 A、B、C.我想将它们转换为一个 3D 数组 (D),其中每个元素都是一个数组

D[column][row] = [A[column][row] B[column][row] c[column][row]]

最好的方法是什么?

解决方案

您可以使用 numpy.dstack:

<预><代码>>>>将 numpy 导入为 np>>>a = np.random.random((11, 13))>>>b = np.random.random((11, 13))>>>c = np.random.random((11, 13))>>>>>>d = np.dstack([a,b,c])>>>>>>d.形状(11, 13, 3)>>>>>>a[1,5], b[1,5], c[1,5](0.92522736614222956, 0.64294050918477097, 0.28230222357027068)>>>d[1,5]数组([0.92522737,0.64294051,0.28230222])

I have a very basic question regarding to arrays in numpy, but I cannot find a fast way to do it. I have three 2D arrays A,B,C with the same dimensions. I want to convert these in one 3D array (D) where each element is an array

D[column][row] = [A[column][row] B[column][row] c[column][row]] 

What is the best way to do it?

解决方案

You can use numpy.dstack:

>>> import numpy as np
>>> a = np.random.random((11, 13))
>>> b = np.random.random((11, 13))
>>> c = np.random.random((11, 13))
>>> 
>>> d = np.dstack([a,b,c])
>>> 
>>> d.shape
(11, 13, 3)
>>> 
>>> a[1,5], b[1,5], c[1,5]
(0.92522736614222956, 0.64294050918477097, 0.28230222357027068)
>>> d[1,5]
array([ 0.92522737,  0.64294051,  0.28230222])

这篇关于在 numpy 中将 3 个数组合并为一个 3D 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆