How to create a (5,5,4) array from an array of shapes (5,5) in a python numpy array

Asked 2 years ago, Updated 2 years ago, 59 views

There is an array X in python (5,5) similar to the following (the number is appropriate):

array([59,65,57,57,62],
       [96, 81, 83, 83, 96],
       [53, 29, 30, 30, 53],
       [26,  0,  0,  0, 27],
       [20, 1, 5, 0, 19] , dtype=uint8)

X.shape>>>(5,5)

I'd like to combine four of these arrays to create X2 of shape(5,5,4) as shown below, but I don't know how to do it.

array([[59,35,26,54],
        [65, 17, 22, 1],
        [12, 27, 47, 21],
        [14, 17, 37, 41],
        [11, 36, 42, 11]],
        ・
        ・
        ・
        [[20, 25, 10, 3],
        [ 1,  15, 21, 42],
        [ 5,  25, 11, 3],
        [ 0,  4, 10, 23],
        [12, 14, 11, 32]], dtype=uint8)

X2.shape>>(5,5,4)

Can someone tell me how to do this?

That's all.

add
Sorry.
You can arrange the original two-dimensional array as it is in a three-dimensional array, and you can process the shape as (4,5,5).It doesn't matter the order of the shapes.
X2.shape>>>(4,5,5)

python numpy array

2022-09-29 21:20

1 Answers

If it is okay to have a shape (4,5,5), the article on this page will be helpful.
Combine the NumPy array ndarray (concatenate, stack, block, etc.)

You can do it all at once with stack() of numpy.
Suppose you had four two-dimensional arrays: Xa, Xb, Xc, and Xd.

 X2 = np.stack ([Xa, Xb, Xc, Xd], 0)

concrete() if you want to combine one or more of them.
Increasing one by one:

 X3 = np.stack ([Xa, Xb], 0)
X3 = np.concatenate ([X3, [Xc]])
X3 = np.concatenate ([X3, [Xd]])

Combine two 3D arrays

 X4 = np.stack ([Xa, Xb], 0)
X5 = np.stack ([Xc, Xd], 0)
X4 = np.concatenate ([X4, X5])

If you continue, it will look like this

import numpy as np

Xa = np.array([
  [1,2,3,4,5],
  [6,7,8,9,10],
  [11,12,13,14,15],
  [16,17,18,19,20],
  [21,22,23,24,25]
, dtype=np.uint8)

Xb = np.array([
  [51,52,53,54,55],
  [56,57,58,59,60],
  [61,62,63,64,65],
  [66,67,68,69,60],
  [71,72,73,74,75]
, dtype=np.uint8)

Xc = np.array([
  [101,102,103,104,105],
  [106,107,108,109,110],
  [111,112,113,114,115],
  [116,117,118,119,120],
  [121,122,123,124,125]
, dtype=np.uint8)

Xd = np.array([
  [151,152,153,154,155],
  [156,157,158,159,160],
  [161,162,163,164,165],
  [166,167,168,169,160],
  [171,172,173,174,175]
, dtype=np.uint8)

X2 = np.stack ([Xa, Xb, Xc, Xd], 0)
X2.shape

X3 = np.stack ([Xa, Xb], 0)
X3 = np.concatenate ([X3, [Xc]])
X3 = np.concatenate ([X3, [Xd]])
X3.shape

X4 = np.stack ([Xa, Xb], 0)
X5 = np.stack ([Xc, Xd], 0)
X4 = np.concatenate ([X4, X5])
X4.shape

By the way, considering the uint8 data type and the four data types together, it would be like looking for full color data by combining the data for each of the four planes of R, G, B, and A.

I'm not sure if that's true (only 3 RGB versions in English) but this article might help.
Combine 3 separate numpy arrays to an RGB image in Python
Python,NumPy for image processing (read, compute, save)
Separate RGB image color channels to monochromaticize, black-and-white, and color exchange in NumPy: The opposite operation is
How to extract R,G,B values with numpy into separate arrays—This is also in the opposite direction and RGB

OpenCV and PIL may have these methods.


2022-09-29 21:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.