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)
Related
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
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.
I'm trying to do this:
h = [0.2, 0.2, 0.2, 0.2, 0.2]
Y = np.convolve(Y, h, "same")
Y looks like this:
While doing this I get this error:
ValueError: object too deep for desired array
Why is this?
My guess is because somehow the convolve function does not see Y as a 1D array.
The Y array in your screenshot is not a 1D array, it's a 2D array with 300 rows and 1 column, as indicated by its shape being (300, 1).
To remove the extra dimension, you can slice the array as Y[:, 0]. To generally convert an n-dimensional array to 1D, you can use np.reshape(a, a.size).
Another option for converting a 2D array into 1D is flatten() function from numpy.ndarray module, with the difference that it makes a copy of the array.
np.convolve() takes one dimension array. You need to check the input and convert it into 1D.
You can use the np.ravel(), to convert the array to one dimension.
You could try using scipy.ndimage.convolve it allows convolution of multidimensional images. here is the docs
np.convolve needs a flattened array as one of it's inputs, you can use numpy.ndarray.flatten() which is quite fast, find it here.