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 ?
1 Answer
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])
3 Comments
harold
Thank you bobolafrite ! it is exactly what I am look for , and I was stuck in this for one hour. thanks.
gboffi
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...bobolafrite
Yes, this one is interesting too : stackoverflow.com/questions/6627647/… @harold Mark the answer as accepted if you're satisfied with it :)
np.reshape?... or justarr = arr[:, None]arr = np.expand_dims(arr, -1)