5

Ok I am a (very) novice Python user, but I am trying to translate a piece of Python code into R, and I have run into a confusing problem with array reshaping.

Lets make some example data:

X1 = np.array([[-0.047, -0.113, 0.155, 0.001],
        [0.039, 0.254, 0.054, 0.201]], dtype=float)

In:X1
Out:
array([[-0.047, -0.113, 0.155, 0.001],
        [0.039, 0.254, 0.054, 0.201]])
In:X1.shape
Out: (2,4)

Ok so I've made a 2D array with 2 rows and 4 columns. I'm happy with this. The confusion arises with this line of code:

X2 = X1.reshape((2, -1, 1))

In: X2
Out:
array([[[-0.047],
        [-0.113],
         [0.155],
         [0.001]],

         [0.039],
         [0.254],
         [0.054],
         [0.201]]])

In: X2.shape
Out: (2, 4, 1)

So I know that I have added an extra dimension (which I think is the 3rd digit 1 in the reshape command), but I don't understand what else this had done. The shape implies it is still got 2 rows and 4 columns, but clearly something else is changed. Again my motivation here is to do the same operation in R, but until I know I understand what I've transformed here I am stuck. (Forgive me if this is an awful question I only started Python yesterday!)

0

2 Answers 2

5

By reshape(2, -1, 1) you have not just added added a new dimension. You have said

* the 1st dimension should be of size 2
* the 3rd dimension should be of size 1
* the 2nd should be whatever remains

so, the only valid option if 4. If you just want to add a new dimension to an existing matrix, you should do something like x[:, np.newaxis, :] (exact usage depends on what you want the output format to be)

Sign up to request clarification or add additional context in comments.

3 Comments

Thats really helpful thank you. In my example does since X1 had dimension (2, 4) and X2 has dimension (2,4,1) - why when I print them does X1 shows the rows with cells side by side, while` X2` shows the individual cells one under another?
Well, printing 3D data in 2D space is confusing indeed. X2 has 3 opening brackets, so the data you get are ordered along the 3rd dimension, not along rows. For example, try printing X2[0, 0, :] the make it clearer.
ok thanks @blue_note - this has been really helpful. Helped me to figure out that the problem is not in these steps, but with later array algebra being handled differently - many thanks!
3

There are 3 different ways of adding a dimension to a 2D array.

You should try different combinations to understand the use of reshape. Try the following:

import numpy as np

X1 = np.array([[-0.047, -0.113, 0.155, 0.001],
        [0.039, 0.254, 0.054, 0.201]], dtype=float)

X2 = X1.reshape((1, 2, -1))
print(X2)

>[[[-0.047 -0.113  0.155  0.001]
   [ 0.039  0.254  0.054  0.201]]]

X3 = X1.reshape((-1, 1, 2))
print(X3)

>[[[-0.047 -0.113]]
  [[ 0.155  0.001]]
  [[ 0.039  0.254]]
  [[ 0.054  0.201]]]

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.