Concatenate 3d and 2d array - python

I have 2 arrays
The first one has this shape
(4133,10000,12)
and the second one has this shape:
(4133,2)
I want to combine those two arrays so I get this shape
(4133,10000,12,2)

Shape of an array along a dimension is NOT the total number of elements. It is the number of elements PER corresponding dimension. Thus, you cannot concatenate arrays of shapes (4133,10000,12) and (4133,2) to have an array of shape (4133,10000,12,2). An easier example to think of is two matrices of shapes (m,n) and (m,k). You cannot concatenate them to have an array of shape (m,n,k).

I don't know what you mean by combine but you can reshape the arrays then let broadcasting kick in. For example:
x = np.empty((10,8,4))
y = np.empty((10,2))
combined = x.reshape((10,8,4,1))*y.reshape((10,1,1,2))
print(combined.shape)
# (10,8,4,2)

Related

What is a 1-D np.array of dimension m?

I have a task and the output should be a "1-D np.array of dimension m" and I don't understand how a 1-D array can have m Dimension, it has 1 per definition ?
The word dimension can mean multiple things, in this case it means the size/length of the singular dimension, i.e. you can say an array has dimensions 2x2.
Therefore, a 1D array of dimension m is equivalent to a list of length m.

can I assign a 3d vector to a 2d matrix

if I have a 2D matrix, and I want to assign a vector [1,1,1] into each cell of my M matrix
vector = np.array([1,1,1])
M= np.zeros((4,4)).astype(np.object)
M[:]=vector.astype(object)
This will obviously give me the error that:
ValueError: could not broadcast input array from shape (2) into shape (3,3)
So is there any method I can store my 3d vector into each cell of my 4x4 M matrix?
Thanks!
I know that if I iterate the ndarray I can do it
for i in range(np.shape(M)[0]):
for j in range(np.shape(M)[1]):
M[i][j]=vector
just wandering whether there's a simple syntax for this
You need to declare what the entries of your matrix should contain with the argument dtype, namely vector.dtype.
This link might help: Numpy - create matrix with rows of vector

How to balance data when they look like a 3-D array?

I've got a numpy_array of size (3275412, 50, 22) which represents my data reshaped for LSTM purposes and I have got a target vector of shape (3275412,).
I want to balance my data so that there is approximately the same number of data with target 0 and 1.
The way I prepared the data makes that I can not do this balancing operation before reshaping.
Firstly, I wanted to apply make_imbalance function (see this link for details) but I can't apply it on a 2-D array (got an error).
My question is : what's the most efficient way to do it for a 3D array ?
My thoughts: I thought about firstly "flatten" my 3-D array to a 2-D array by "concatenating" the second and third dimension (but don't know how so please tell me ??) then apply make_imbalance and then reshape the result to a 3-D array (again, don't know how to do). It seems a little bit tricky however...
So any help would be appreciated, either for an other imbalancing method or for help about reshaping 3D->2D or vice-versa
You can use np.reshape with -1 for unknown dimension size.
data2d = data3d.reshape(data3d.shape[0], -1)
will give you a 2d array of shape (n_samples, n_features)
with the second and the third dimensions merged.
data2d_new, y_new = make_imbalance(data2d, y)
After make_imbalance call, you will get a 2d array with a shape (n_samples_new, n_features), where the number of rows is "unknown" but you know your other two 'feature' dimensions of the original 3d array, so
data3d_new = data2d.reshape(-1, data3d.shape[1], data3d.shape[2])
will give you back the balanced 3d dataset.

change the shape and randomly permutate an array

I have an array, which is of shape (210000,64). I would like to transfer its shape to (210000,64,1) and moreover, I want to generate a new array, which is a random permutation of the original array along the dimension of 210000.

How can I combine my three 2D tensors into a single 3D tensor in tensor flow?

Hello I am a newbie with the tensorflow and currently, I am working with colour Images and it's PCAS.
I have extracted PCAS in a form of "Red","Green" and "Blue" and also computed the weights which are associated with "Red","Green" and "Blue" components.
After doing the all the above stuff I want to combine all three 2D matrices into the single 3D matrix.
For a tensorflow it would be a 3D tensor.
def multi(h0,ppca,mu,i,scope=None):
with tf.variable_scope(scope or"multi"):
return tf.matmul(ppca[:,:,0],h0[i,:,:,0]) + tf.reshape(mu[:,0],[4096,1]) , tf.matmul(ppca[:,:,1],h0[i,:,:,1]) + tf.reshape(mu[:,1],[4096,1]) ,tf.matmul(ppca[:,:,2],h0[i,:,:,2]) + tf.reshape(mu[:,2],[4096,1])
So from the above function, I will get all three different 2D tensors and want to combine those 2D tensors to single 3D tensor which has dimensions [4096,1,3]
How can I do that?
any help is highly appreciated.
You need to concat them like this:
three_d_image = tf.concat(0, [[r], [g], [b]])
This tells tensorflow to concat them along the x dimension and treat each tensor as a matrix.
Doing the same without the additional brackets around the r,g,b tensors will try to concat them to one large 2D matrix
A clean, easy way to do it is using the tf.stack operation (tf.pack in older versions of tensorflow), it concatenats all tensors along a new dimension. If you want your new dimension to be after all previous, you need to set the axis argument to the number of dimensions of your tensors.
three_d_image = tf.stack([r,g,b], axis=2)
one of the solutions is that you can add one more empty dimension to your 2Ds so you will have 3 matrices of 3D dimension [4096,1,1] then you can concat these 3 matrices by axis 2 tf.concat(2,matrices) gives you [4096,1,3]
the second solution can be concat of axis 1, tf.concat(1,matrices) then reshape it to 3D

Categories

Resources