Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contruct 3d array in numpy from existing 2d array

Tags:

During preparing data for NumPy calculate. I am curious about way to construct:

myarray.shape => (2,18,18) 

from:

d1.shape => (18,18) d2.shape => (18,18) 

I try to use NumPy command:

hstack([[d1],[d2]]) 

but it looks not work!

like image 722
vernomcrp Avatar asked Nov 13 '09 07:11

vernomcrp


People also ask

How do you create a 3 dimensional NumPy array?

Importing the NumPy package enables us to use the array function in python. To create a three-dimensional array, we pass the object representing x by y by z in python, where x is the nested lists in the object, y is the nested lists inside the x nested lists, and z is the values inside each y nested list.

How do I create a multidimensional array in NumPy?

Creating arrays with more than one dimension In general numpy arrays can have more than one dimension. One way to create such array is to start with a 1-dimensional array and use the numpy reshape() function that rearranges elements of that array into a new shape.

How do you concatenate two 3D arrays in Python?

One way is to use np. dstack which concatenates the arrays along the third axis (d is for depth). You could also use np. concatenate((a, a), axis=2) .


1 Answers

Just doing d3 = array([d1,d2]) seems to work for me:

>>> from numpy import array >>> # ... create d1 and d2 ... >>> d1.shape (18,18) >>> d2.shape (18,18) >>> d3 = array([d1, d2]) >>> d3.shape (2, 18, 18) 
like image 78
Daniel G Avatar answered Sep 19 '22 20:09

Daniel G