0

How can I stack multiple shape=(32, 32, 1) np.arrays to get a shape=(n, 32, 32, 1) np.arrays whitout using lists? The following example I did using lists but I believe there is a way to do that using only numpy.

paddings = tf.constant([[2, 2,], [2, 2], [0, 0]])

arr_1 = np.array(tf.pad(X_train[0], paddings, "CONSTANT")) # arr_1 shape=(32, 32, 1)
arr_2 = np.array(tf.pad(X_train[1], paddings, "CONSTANT")) # arr_2 shape=(32, 32, 1)

matrix = []
matrix.append(arr_1)
matrix.append(arr_2)
matrix = np.array(matrix)

print(type(arr_1)) # matrix shape=(2, 32, 32, 1) but how to do that whitout lists?
  • `np.array`, `np.concatenate` and its `stack derivatives all take a list of arrays. Read the docs! – hpaulj Dec 15 '21 at 02:44
  • You can check (https://stackoverflow.com/questions/29241056/how-does-numpy-newaxis-work-and-when-to-use-it) to cover shape(32,32,1) to (1,32,32,1) and use np.concatenate (https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html) to stack its. Hope it help you. – Viettel Solutions Dec 15 '21 at 03:01
  • @Viettel, `np.stack` does this kind of dimension addition, and then does `concatenate` – hpaulj Dec 15 '21 at 03:39
  • @hpaulj, oh, thank for your infor. – Viettel Solutions Dec 15 '21 at 04:52
  • list contain references, so appending new arrays is relatively fast.It's better than iteratively trying grow an array. An alternative is create the full target array, and assign values. – hpaulj Dec 15 '21 at 08:08

0 Answers0