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 ?
Asked
Active
Viewed 7,190 times
0
-
2Seen `np.reshape`?... or just `arr = arr[:, None]` – cs95 Nov 06 '17 at 11:17
-
`arr = np.expand_dims(arr, -1)` – grovina Nov 06 '17 at 11:19
-
1Please post a sample of your actual data and what you have already tried to solve your problem. – DaveL17 Nov 06 '17 at 11:24
1 Answers
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])

bobolafrite
- 100
- 1
- 11
-
Thank you bobolafrite ! it is exactly what I am look for , and I was stuck in this for one hour. thanks. – harold Nov 06 '17 at 13:03
-
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... – gboffi Nov 06 '17 at 13:40
-
Yes, this one is interesting too : https://stackoverflow.com/questions/6627647/reshaping-a-numpy-array-in-python @harold Mark the answer as accepted if you're satisfied with it :) – bobolafrite Nov 06 '17 at 14:02