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 ?

Christofer Ohlsson
  • 3,097
  • 4
  • 39
  • 56
harold
  • 25
  • 1
  • 5

1 Answers1

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