I have many numpy arrays of shape (Ni,227,227,3), where Ni of each array is different.
I want to join them and make array of shape (N1+N2+..+Nk,227,227,3) where k is the number of arrays.
I tried numpy.concatenate and numpy.append but they ask for same dimension in axis 0. I am also confused on what is axis 1 and axis 2 in my arrays.
So, the main problem here was with the one of the arrays of shape (0,) instead of (0,227,227,3).
np.concatenate(alist,axis=0) works.
Related
I have a 2d array of shape (512,512). I need to convert this to shape (512,512,3). All values of 2d dimension will be repeated on other two dims. How can I do this in python?
you can try using np dstack
it would work for your case
np.dstack([a,a,a])
I would use array[..., None].repeat(3, -1)
I have four Numpy arrays of shapes:
(2577, 42)
(2580, 100)
(2580, 236)
(2580, 8)
(2580, 37)
When I try to concatenate all of them do except (2577, 42). I get an error:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 2580 and the array at index 4 has size 2577
The code I am using:
dataset = np.concatenate((onehot_b, num_v, onehot_s, onehot_c, onehot_s), axis=1)
Is there a way to fix this?
The error is prety clear. You Cannot concatenate arrays of different sizes. One possible way out is convert the numpy arrays to lists and append all list lines to you dataset.
Numpy does not allow non-rectangular arrays, meaning that all sub-arrays should have the same dimension along the same axis. In your case, 2577 and 2580, are dimensions along same axis=0 that you are not stacking over (hence not adding them along that axis and they should have same length). If you can change all of them to have same first dimension shape, you can use concatenate. If you insist on stacking them, another way is just stacking arrays rather than their content:
dataset = np.asarray([onehot_b, num_v, onehot_s, onehot_c, onehot_s])
This will create an array of arrays for you.
I want to numpy.sum() my nD arrays (matrix) of shape (2,2,2...n) to 1D arrays of shape (2,).
Basically along the axis=0.
(Sum the first number of each 1d array of shape (2,) and sum the second number of the same arrays into one single resulting 1d array of shape (2,)).
However matrix.sum(axis=0) only seems to work for those of shape (2,2), while I think matrix.sum(axis=(1,2)) works for (2,2,2). But then what about (2,2,2,2) arrays and so on?
The n-dimensions have been confusing me. A generalised solution, along with a brief explanation, would be much appreciated.
EDIT: I've provided an example of the nD arrays I'm trying to sum below. I want to sum along axis=0 to get a (2,) 1D array. numpy.sum(axis=0) seems to only work for the array of shape (2,2)...
#Of shape (2,2):
[[9.99695358e-02 9.99232016e-01]
[9.00030464e-01 7.67983971e-04]].sum(axis=0) seems to work
#Of shape (2,2,2):
[[[2.02737071e-01 7.75883149e-01]
[2.02650032e-08 1.58192237e-02]]
[[7.31718878e-06 1.41793363e-03]
[4.12802168e-03 7.26350831e-06]]].sum(axis=(1,2)) seems to work
#Of shape… (2,2,2,2)
[[[[1.83819962e+00 1.02712560e-02]
[5.05122135e-02 2.80555725e-04]]
[[5.60304309e-07 5.44521143e-04]
[2.41592380e-03 1.49436734e-05]]]
[[[7.04398015e-05 7.66717944e-06]
[1.76843337e-05 1.98868986e-06]]
[[9.74010599e-02 1.12527543e-07]
[2.61427056e-04 2.70778171e-08]]]].sum(axis=?) # What axis? And how to generalise?
What do you think about reshaping x to 2D and summing along the second axis?
x.reshape(x.shape[0], -1).sum(axis=1)
I want to reshape this array: np.array(np.arange(15)) to a 3d array that is built from a 3x3 array and a 3x2 array.
I've tried to do it with the reshape method but it didn't work.
I thought that maybe reshape can get a number of tuples maybe.
a=np.array(np.arange(15)).reshape(1,((3,2),(3,3)))
but I then I saw it cant.
How can I reshape it then? is there a nice way?
a multidimensional array can't have dimensions with different size.
but if you want a tuple you will need to split the array in 2 parts, the first that match in size with the 3x3 array and the second that match the 3x2, at this point you'll have 2 one dimensional array, then reshape them
arr1 = arr1.reshape((3,3))
arr2 = arr2.reshape((3,2))
tuple = arr1, arr2
As I am attempting to use one presented python script, its core part contains the NumPy array summation of a (0,6) array, while I'm coming across a broadcasting error. The basic broadcasting rules of numerical operations on NumPy arrays tell me ValueError will generate when I want to sum a (1000,6) array and a (0,6) array.
print a.shape
#(1000,6)
print b.shape
#(0,6)
a += b + 0.5
As might have been expected, "ValueError: operands could not be broadcast together with shapes (1000,6)(0,6)(1000,6)" reported
I was wondering if there is anyway to fix this? Changing the shape of (0,6) array, or adding an extra dimension?