Suppose I have a numpy array c constructed as follows:
a = np.zeros((2,4))
b = np.zeros((2,8))
c = np.array([a,b])
I would have expected c.shape to be (2,1) or (2,) but instead it is (2,2). Additionally, what I want to do is concatenate a column vector of ones onto a, but by accessing it through c in the following way:
c0 = c[0] # I would have expected this to be 'a'
np.concatenate((np.ones((c0.shape[0], 1)), c0), axis=1)
This of course doesn't work because c[0] does not equal a as I expected, and I get
ValueError: all the input arrays must have same number of dimensions
I need some way to have an array (or list) of pairs, each pair component being a numpy array, and I need to access the first array in the pair in order to concatenate a column vector of ones to it. My application is machine learning and my data will be coming to me in the format described, but I need to modify the data at the start in order to add a bias element to it.
EDIT: I'm using Python 2.7 and Numpy 1.8.2
ValueError: could not broadcast input array from shape (2,4) into shape (2)when assigningc.c = [a,b]? It is possible to makecan array of object dtype, in which you could store NumPy arrays of arbitrary shape --c = np.empty((2,), dtype='object');c[:] = [a,b]-- , but object arrays do not enjoy any speed benefit over a plain Python list. You might use it for NumPy slicing syntax, but I have yet to see a compelling use case.np.array([a,b])raises the sameValueErrorthat Dux mentioned.c = [a, b]fits the bill.