0

So, I would like to stack couple 2d arrays to vector so it would look like this:

[[[0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]]

 [[0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]]

 [[0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]]]

I can make smth like this:

import numpy as np
a = np.zeros((5, 5), dtype=int)
b = np.zeros((5, 5), dtype=int)
c = np.stack((a, b), 0)
print(c)

To get this:

[[[0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]]

 [[0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]
  [0 0 0 0 0]]]

But I cant figure out how to add third 2d array to such vector or how to create such vector of 2d arrays iteratively in a loop. Append, stack, concat just dont keep the needed shape

So, any suggestions? Thank you!

Conclusion: Thanks to Tom and Mozway we've got two answers

Tom's:

data_x_train = x_train[np.where((y_train==0) | (y_train==1))

Mozway's:

out = np.empty((0,5,5))

while condition:
    # get new array
    a = XXX
    out = np.r_[out, a[None]]
out
5
  • How do you get the first output from the two arrays? Commented Sep 29, 2021 at 9:57
  • I didn't. This vector of len 3 is desirable result that I can't get Commented Sep 29, 2021 at 9:59
  • But, what is the logic to get it from two 2-dimensional arrays? Commented Sep 29, 2021 at 10:00
  • Your question is a bit ambiguous due to the zero-only arrays, if you have n arrays of same shape and want to stack them, do it as a single pass: np.stack((a1, a2, a3, ..., an), 0) Commented Sep 29, 2021 at 10:04
  • I'm trying to get specific 28x28 matrices from fashin_mnist dataset of 60000 pictures. Dataset is in shape of (60000, 28, 28) Commented Sep 29, 2021 at 10:04

2 Answers 2

1

Assuming the following arrays:

a = np.ones((5, 5), dtype=int)
b = np.ones((5, 5), dtype=int)*2
c = np.ones((5, 5), dtype=int)*3

You can stack all at once using:

np.stack((a, b, c), 0)

If you really need to add the arrays iteratively, you can use np.r_:

out = a[None]

for i in (b,c):
    out = np.r_[out, i[None]]

output:

array([[[1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1]],

       [[2, 2, 2, 2, 2],
        [2, 2, 2, 2, 2],
        [2, 2, 2, 2, 2],
        [2, 2, 2, 2, 2],
        [2, 2, 2, 2, 2]],

       [[3, 3, 3, 3, 3],
        [3, 3, 3, 3, 3],
        [3, 3, 3, 3, 3],
        [3, 3, 3, 3, 3],
        [3, 3, 3, 3, 3]]])

edit: if you do not know the arrays in advance

out = np.empty((0,5,5))

while condition:
    # get new array
    a = XXX
    out = np.r_[out, a[None]]
out
Sign up to request clarification or add additional context in comments.

4 Comments

What if I don't know my a, b, c's yet and I'm finding them in a loop by some condition. Like desire = np.array([]); for i in range(1000): if arr1[i] == 1: desire = np.append(arr2[i])
@GlebS I added an alternative
Collect the a,b,c iteratively in a list, and do one stack at the end. This is more efficient, and easier to get right.
@hpaulj I fully agree (see my first option), this was however OP's request to to this iteratively (maybe they need the intermediate result)
1

Do you mean something like:

np.tile(a, (3, 1, 1))

array([[[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]],

       [[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]],

       [[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]]])

Edit: Do you mean something like:

test = np.tile(a, (3000, 1, 1))
filtered_subset = test[[1, 10, 100], :, :]

array([[[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]],

       [[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]],

       [[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]]])

11 Comments

Yeah, but I want to iterate through big set of (60000, 28, 28) shape and add specific (28, 28) shape matrixes to a new vector. So it would be like (12000, 28, 28)
You are going to have to provide a better example, i'm not sure what you mean
I've added what you might mean?
Thank you so much! I'll put your and Mozway's answers to post!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.