Consider the following simple example:
X = numpy.zeros([10, 4]) # 2D array x = numpy.arange(0,10) # 1D array X[:,0] = x # WORKS X[:,0:1] = x # returns ERROR: # ValueError: could not broadcast input array from shape (10) into shape (10,1) X[:,0:1] = (x.reshape(-1, 1)) # WORKS
Can someone explain why numpy has form vectors (N,) and not (N, 1)? What is the best way to cast from a 1D array to a 2D array?
Why do I need it? Because I have code that inserts the result of x into the 2D array x , and the size of x changes from time to time, so I X[:, idx1:idx2] = x works if x also 2D, but not if x is 1D .
python arrays numpy multidimensional-array
Hanan shteingart
source share