I'm having a 2D array of dummy variables (0 and 1) with the shape of (4432, 35) -> 4432 videos including 35 different customers. Since the videos contain of 1800 frames I want to add a third dimension to this array with 1800 time steps (frames) so that it gets the shape (4432, 35, 1800). So I want Python to multiplicate the zeros and ones in the 2nd dimension 1800 times into the 3rd dimension.
How can I do that?
with an array called array with any 2D shape:
array = [[[j for k in range(1800)] for j in i] for i in array]
This will create a 3rd dimension with 1800 duplicates of the values in the second dimension.
It also seems to make more sense to have a shape (4432, 1800, 35): (video, frame, customers in frame):
array = [[i for k in range(1800)] for i in array]
Related
I have an issue with numpy arrays and I can't understand what I am doing wrong. I need to create a 100x100 matrix with random int (non zero) and the last row should be the combination of all previous rows. Here is my code:
non_zero_m = np.random.randint(0,10,(99,100))
arr = non_zero_m.sum(axis=0)
singular_m = np.concatenate((non_zero_m, arr))
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
I can't understand why python shows that arrays has different dimensions
The problem is that arr is a 1-dimensional array, and you are trying to concatenate it to a matrix (2-dimensional).
Just replace the second line with:
arr = non_zero_m.sum(axis=0).reshape(1, -1)
This reshapes arr to a 2-dimensonal array, such that the first axis has dimension 1 (thus making arr effectively a row vector), and the second axis has the required dimension to keep all of arr's elements (this is the meaning of -1 in this context).
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.
How can I eliminate a dummy dimension in python numpy ndarray?
For example, suppose that A.shape = (0, 1325, 3),
then how can eliminate '0' dimension so that A.shape = (1325,3).
Both 'np.sqeeze(A)' or 'A.reshape(A.shape[1:])' don't work.
You can't eliminate that 0 dimension. A dimension of length 0 is not a "dummy" dimension. It really means length 0. Since the total number of elements in the array (which you can check with a.size) is the product of the shape attribute, an array with shape (0, 1325, 3) contains 0 elements, while an array with shape (1325, 3) contains 3975 elements. If there was a way to eliminate the 0 dimension, where would that data come from?
If your array is supposed to contain data, then you probably need to look at how that array was created in the first place.
I have a numpy array of size 5000x32x32x3. The number 5000 is the number of images and each image is 32x32 in width and height and has 3 color channels.
Now I would like to create a numpy array of shape 5000x3x32x32 in a way that the data is preserved.
What I mean by preserving data is :
There should be 5000 data points in the resulting array
The 2nd dimension (3) of the array correctly determines the color channel i.e all the elements whose 2nd dimension is 0 belong to red channel, whose 2nd dimension is 1 belong to green channel,whose 2nd dimension is 2 belong to blue channel.
Simply reshaping the by np.reshape(data,(5000,3,32,32)) would not work as it would not preserve the channels but just reshape the data into the desired shape.
I think you are looking for a permutation of the axes, numpy.transpose can get this job done:
data = np.transpose( data, (0, 3, 1, 2))
I have a 4D array: array = np.random.rand(3432,1,30,512)
I also have 5 sets of 2D arrays with shape (30,512)
I want to insert these into the 4D structure along axis 1 so that my final shape is (3432,6,30,512) (5 new arrays + the original 1). I need to iteratively insert this set for each of the 3432 elements
Whats the most effective way to do this?
I've tried reshaping the 2D to 4D and then inserting along axis 1. I'm expecting axis 1 to never exceed a size of 6, but the 2D arrays just keep getting added, rather than a set for each of the 3432 elements. I think my problem lies in not fully understanding the obj param for the insert method:
all_data = np.reshape(all_data, (-1, 1, 30, 512))
for i in range(all_data.shape[0]):
num_band = 1
for band in range(5):
temp_trial = np.zeros((30, 512)) # Just an example. values arent actually 0
temp_trial = np.reshape(temp_trial, (1,1,30,512))
all_data = np.insert(all_data, num_band, temp_trial, 1)
num_band += 1
Create an array with the final shape first and insert the elements later:
final = np.zeros((3432,6,30,512))
for i in range(3432): # note, this will take a while
for j in range(6):
final[i, j, :, :] = # insert your array here (np.ones((30, 512)))
or if you actually want to broadcast this over the zeroth axis, assuming each of the 3432 should be the same for each "band":
for i in range(6):
final[:, i, :, :] = # insert your array here (np.ones((30, 512)))
As long as you don't do many loops there is no need to vectorize it