0

I am working with python, and I Got an array which looks like (750 ,), and it is a matrix with 750 rows and 1 column. but I would like to make it look like (750, 1). to be specific, I would like to do a transform : (750, )--->(750,1). any advice ?

3
  • 2
    Seen np.reshape?... or just arr = arr[:, None] Commented Nov 6, 2017 at 11:17
  • arr = np.expand_dims(arr, -1) Commented Nov 6, 2017 at 11:19
  • 1
    Please post a sample of your actual data and what you have already tried to solve your problem. Commented Nov 6, 2017 at 11:24

1 Answer 1

1

First let's create a (750,) array :

import numpy as np
test_array = np.array(range(750))

test_array.shape 
# Returns (750,)

You can create a new array with the shape you want with the np.ndarray.reshape() method :

new_array = test_array.reshape([750,1])

It is equivalent to

new_array = np.reshape(test_array,[750,1])
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you bobolafrite ! it is exactly what I am look for , and I was stuck in this for one hour. thanks.
Also new_array = test_array[:, np.newaxis] (or, equivalently, new_array = test_array[:, None] as you were already told by Coldspeed) — do yourself a favor and google for "numpy broadcasting" to discover one of the strengths of Numpy...
Yes, this one is interesting too : stackoverflow.com/questions/6627647/… @harold Mark the answer as accepted if you're satisfied with it :)

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.