-1

I am trying to form a 3D array in Python by populating it with 2D arrays. N is a number than varies depending on the file being read. The matrix is forming as 3D but only appears to have 1 'layer' to it when I am expecting it to have N layers. It appears that the N number of 'layers' is not being passed into the formed array.

import numpy as np

#'rot' is a 3D matrix of shape (N,3,3)
a=np.array(rot[:,0,0])
b=np.array(rot[:,0,1])
c=np.array(rot[:,0,2])
d=np.array(rot[:,1,0])
e=np.array(rot[:,1,1])
f=np.array(rot[:,1,2])
g=np.array(rot[:,2,0])
h=np.array(rot[:,2,1])
i=np.array(rot[:,2,2])  

print(a.shape)
#(N,)

#Forming 3D array
arr=np.array([[[a,b,1],
               [d,e,1],
               [g,h,i]]])

print(arr.shape)
#(1,3,3)
5
  • Does this answer your question? Efficiently Creating A Pandas DataFrame From A Numpy 3d array Commented Feb 26, 2020 at 14:57
  • What is the shape that you are expecting? The 1 entries are likely causing a problem since the array now has vectors of length N and integers. Try np.array([1]*N) to get a vector of 1s with the same shape as the other elements. Commented Feb 26, 2020 at 15:18
  • Did you look at arr? Or arr.dtype. Don't just look at shape, especially when the shape is unexpected. Commented Feb 26, 2020 at 18:01
  • The initial size 1 dimension is produced by the outer layer of []. Commented Feb 26, 2020 at 19:41
  • You may need a transpose to put the N dimension first. Commented Feb 26, 2020 at 20:47

2 Answers 2

-1

You don't say exactly what shape you are expecting. The code below will return a 3D array of shape (3, 3, N)

ones_vec = np.array([1] * N)

arr = np.array([[a, b, ones_vec], 
                [d, e, ones_vec], 
                [g, h, h]])
print(arr.shape)
# (3, 3, N)
Sign up to request clarification or add additional context in comments.

Comments

-1

Let's simplify the case, and look at the actual results, not just the shape.

In [327]: a=np.arange(4)  

Make an array from just this array (or others like it)

In [328]: np.array([[[a,a],[a,a]]])                                                            
Out[328]: 
array([[[[0, 1, 2, 3],
         [0, 1, 2, 3]],

        [[0, 1, 2, 3],
         [0, 1, 2, 3]]]])
In [329]: _.shape                                                                              
Out[329]: (1, 2, 2, 4)

Note the shape and dtype (int like a). But when we add a scalar 1 to the mix:

In [330]: np.array([[[a,a,1],[a,a,1]]])                                                        
Out[330]: 
array([[[array([0, 1, 2, 3]), array([0, 1, 2, 3]), 1],
        [array([0, 1, 2, 3]), array([0, 1, 2, 3]), 1]]], dtype=object)
In [331]: _.shape                                                                              
Out[331]: (1, 2, 3)

The dtype has changed. It is now a mix of arrays and the scalar element.

Replacing the scalar with an array that matches a:

In [332]: ones=np.ones_like(a)                                                                 
In [333]: ones                                                                                 
Out[333]: array([1, 1, 1, 1])
In [334]: np.array([[[a,a,ones],[a,a,ones]]])                                                  
Out[334]: 
array([[[[0, 1, 2, 3],
         [0, 1, 2, 3],
         [1, 1, 1, 1]],

        [[0, 1, 2, 3],
         [0, 1, 2, 3],
         [1, 1, 1, 1]]]])
In [335]: _.shape           
Out[335]: (1, 2, 3, 4)

and without the outer layer of []

In [356]: np.array([[a,a,ones],[a,a,ones]])                                                    
Out[356]: 
array([[[0, 1, 2, 3],
        [0, 1, 2, 3],
        [1, 1, 1, 1]],

       [[0, 1, 2, 3],
        [0, 1, 2, 3],
        [1, 1, 1, 1]]])
In [357]: _.shape                                                                              
Out[357]: (2, 3, 4)

If you want the N (here 4) dimension you can transpose

  arr.transpose(2,0,1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.