3

I need to somehow make that array:

[[639 190]
 [ 44 1]
 [ 71 4]
 ...,
 [863 347]
 [870 362]
 [831 359]]

look like this:

[[[639 190]]
 [[ 44 1]]
 [[ 71 4]]
 ...,
 [[863 347]]
 [[870 362]]
 [[831 359]]]

How would I do that? I'm new to numpy and I need it for my scientific experiment.

mx0
7,34312 gold badges57 silver badges57 bronze badges
asked Jul 7, 2017 at 5:51

2 Answers 2

4

Add a new axis with None/np.newaxis -

a[:,None,:] # Or simply a[:,None]

Sample run -

In [222]: a = np.random.randint(0,9,(4,3))
In [223]: a
Out[223]: 
array([[1, 6, 6],
 [4, 4, 5],
 [7, 4, 4],
 [4, 1, 3]])
In [224]: a[:,None]
Out[224]: 
array([[[1, 6, 6]],
 [[4, 4, 5]],
 [[7, 4, 4]],
 [[4, 1, 3]]])
answered Jul 7, 2017 at 5:53
Sign up to request clarification or add additional context in comments.

Comments

3

In addition to newaxis/None mentioned by @Divakar,

np.expand_dims(input_array, axis=1)
answered Jul 7, 2017 at 6:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.