how to merge different dimensions arrays in python?

I am analyzing some image represented datasets using keras. I am stuck that I have two different dimensions of images. Please see the snapshot. Features has 14637 images having dimension (10,10,3) and features2 has dimension (10,10,100)

enter image description here

Is there any way that I can merge/concatenate these two data together.?

Upvotes: 5

Views: 8152

Answers (3)

Shajedur Rahman Panna
Shajedur Rahman Panna

Reputation: 59

I think it would be better if you use class.

class your_class:
     array_1 = []
     array_2 = []

 final_array = []

 for x in range(len(your_previous_one_array)):
     temp_class = your_class
     temp_class.array_1 = your_previous_one_array
     temp_class.array_2 = your_previous_two_array
     final_array.append(temp_class)

Upvotes: 0

Neb
Neb

Reputation: 2280

If features and features2 contain the features of the same batch of images, that is features[i] is the same image of features2[i] for each i, then it would make sense to group the features in a single array using the numpy function concatenate():

newArray = np.concatenate((features, features2), axis=3)

Where 3 is the axis along which the arrays will be concatenated. In this case, you'll end up with a new array having dimension (14637, 10, 10, 103).

However, if they refer to completely different batches of images and you would like to merge them on the first axis such that the 14637 images of features2 are placed after the first 14637 image, then, there no way you can end up with an array, since numpy array are structured as matrix, non as a list of objects.

For instance, if you try to execute:

> a = np.array([[0, 1, 2]]) // shape = (1, 3)
> b = np.array([[0, 1]]) // shape = (1, 2)
> c = np.concatenate((a, b), axis=0)

Then, you'll get:

ValueError: all the input array dimensions except for the concatenation axis must match exactly

since you are concatenating along axis = 0 but axis 1's dimensions differ.

Upvotes: 3

msi_gerva
msi_gerva

Reputation: 2078

If dealing with numpy arrays, you should be able to use concatenate method and specify the axis, along which the data should be merged. Basically: np.concatenate((array_a, array_b), axis=2)

Upvotes: 2

Related Questions