I have an array (of features extracted from an image) with the dimension (1,2048,7,7) and a word embedding (from fastText encoding) with the dimension (1,300). I want to concatenate these arrays to get an array with dimension (1,2348,7,7). How do I go about this?
It is unfortunate that concatenate doesn't do broadcasting. Fortunately, assignment does do broadcasting, so you can just build your result.
result = np.empty((1, 2348, 7, 7))
result[:,:2048] = image
result[:,2048:,:,:] = fast_text_encoding
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.
Suppose my image is 256x256x1. I need to reshape each 8x8x1 block in the image to a vector, which will result in a 32x32x64 tensor, in which each of the 1x1x64 vectors is the reshaped version of the corresponding 8x8 block. How could I do this with numpy or cv2?
You can perform a series of reshapes and transpositions to get the result you want. For example, start by splitting the existing dimensions to add more:
a = a.reshape(32, 8, 32, 8)
The reshape does not need to copy data. Now move the dimensions you want to group to the back:
a = a.transpose(0, 2, 1, 3)
This creates a copy of the array with the blocks you want arranged contiguously. Now you can reshape just the blocks:
a = a.reshape(32, 32, 64)
This reshape won't copy any data either since you are preserving the memory layout.
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
While doing the deeplearning.ai course, on an instant I needed to use numpy.reshape(). However while doing so I was instructed in the course notebook to do it in a specific way.
The purpose was to convert a 4 dimensional vector to a 2 dimensional vector.
//
Instructions:
For convenience, you should now reshape images of shape (num_px, num_px, 3) in a numpy-array of shape (num_px ∗∗ num_px ∗∗ 3, 1). After this, our training (and test) dataset is a numpy-array where each column represents a flattened image. There should be m_train (respectively m_test) columns.
Exercise: Reshape the training and test data sets so that images of size (num_px, num_px, 3) are flattened into single vectors of shape (num_px ∗∗ num_px ∗∗ 3, 1).
A trick when you want to flatten a matrix X of shape (a,b,c,d) to a matrix X_flatten of shape (b∗∗c∗∗d, a) is to use:
X_flatten = X.reshape(X.shape[0], -1).T
(X.T is the transpose of X)
I am unable to understand why the parameters are given in such a way?
Also, while playing with the code, changing '-1' to any any negative integer, didn't change the output.
I am assuming you are working with the MNIST dataset, so you have n images of size mm3 lets assume n to be 100 and m to be 8. So you have 100 RGB-images(3 channels) of size 8*8, thus making your datashape 100,8,8,3. Now you would like to flatten each of the 100 images, so you could either loop through the dataset, and flatten it image by image, or you could reshape it.
You decide to reshape it via:
X.reshape(X.shape[0], -1).T
lets unpack this a bit more, X.shape[0] gives you 100. The shape attribute will return you a tuple of (100,8,8,3) since that is the shape of your dataset and you access its 0th element, that's 100, so you get
X.reshape(100, -1).T
So what this does it that it reshapes the array but makes sure that you still have 100 images, and what -1 states is that you do not care about what shape the result will be reshaped into, so it automatically infers the shape from the original shape. Previously you had a 4-D array of shape 100,8,8,3 but now you want to reshape it into a 2-D array, you specify that 100 should be dimension 0 of the shape, so numpy infers that to reshape it into such a 2-D shape it will have to flatten it, and thus 100,883 is the output shape.
After that you just transpose it
Also, this is what numpy documentation states
The new shape should be compatible with the original shape. If an
integer, then the result will be a 1-D array of that length. One shape
dimension can be -1. In this case, the value is inferred from the
length of the array and remaining dimensions.