Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
Numpy concatenate behaviour. How to concatenate example correctly

I have following multi-dimensional array:

windows = array([[[[[[0., 0.],
           [1., 0.]],

          [[0., 0.],
           [1., 0.]],

          [[0., 0.],
           [1., 0.]]],


         [[[0., 1.],
           [0., 0.]],

          [[0., 1.],
           [0., 0.]],

          [[1., 0.],
           [0., 0.]]],


         [[[1., 0.],
           [0., 0.]],

          [[0., 1.],
           [0., 0.]],

          [[0., 1.],
           [0., 0.]]]]]])
print(windows.shape)
(1, 1, 3, 3, 2, 2) # (n, d, a, b, c, c) w = a * (c*c), h = b * (c*c)

I want to get next resulting array:

mask = array([[
        [[0., 0., 0., 0., 0., 0.],
         [1., 0., 1., 0., 1., 0.],
         [0., 1., 0., 1., 1., 0.],
         [0., 0., 0., 0., 0., 0.],
         [1., 0., 0., 1., 0., 1.],
         [0., 0., 0., 0., 0., 0.]] 
]], dtype=np.float32)
print(mask.shape)
(1, 1, 6, 6) # (n, d, w, h)

Basically, I want to squeeze last 4 dimensions into 2-d matrix so the final shape become (n, d, w, h), in this case (1, 1, 6, 6).

I tried np.concatenate(windows, axis = 2), but it didnt concatenate along 2nd dimension and reduced for some reason first(although I set axis = 2) 'n' dimension.

Additional information:
windows is a result of following code snippet

windows = np.lib.stride_tricks.sliding_window_view(arr, (c, c), axis (-2,-1), writeable = True) # arr.shape == mask.shape
windows = windows[:, :, ::c, ::c] # these are non-overlapping windows of arr with size (c,c)
windows = ... # some modifications of windows

Now I want to build from these windows array with shape of arr.shape, this array called mask in example above. Simple reshape doesn't work because it returns elements in wrong order.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

IIUC, you want to merge dimensions 2+4 and 3+5, an easy way would be to swapaxes 4 and 5 (or -3 and -2), and reshape to (1,1,6,6):

windows.swapaxes(-2,-3).reshape(1,1,6,6)

output:

array([[[[0., 0., 0., 0., 0., 0.],
         [1., 0., 1., 0., 1., 0.],
         [0., 1., 0., 1., 1., 0.],
         [0., 0., 0., 0., 0., 0.],
         [1., 0., 0., 1., 0., 1.],
         [0., 0., 0., 0., 0., 0.]]]])
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda