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

308
Visualizações
Repeat values of an array on both the axes

Say I have this array:

array = np.array([[1,2,3],[4,5,6],[7,8,9]])

Returns:

123
456
789

How should I go about getting it to return something like this?

111222333
111222333
111222333
444555666
444555666
444555666
777888999
777888999
777888999
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You'd have to use np.repeat twice here.

np.repeat(np.repeat(array, 3, axis=1), 3, axis=0)

# [[1 1 1 2 2 2 3 3 3]
#  [1 1 1 2 2 2 3 3 3]
#  [1 1 1 2 2 2 3 3 3]
#  [4 4 4 5 5 5 6 6 6]
#  [4 4 4 5 5 5 6 6 6]
#  [4 4 4 5 5 5 6 6 6]
#  [7 7 7 8 8 8 9 9 9]
#  [7 7 7 8 8 8 9 9 9]
#  [7 7 7 8 8 8 9 9 9]]
over 4 years ago · Santiago Trujillo Relatório

0

For fun (because the nested repeat will be more efficient), you could use einsum on the input array and an array of ones that has extra dimensions to create a multidimensional array with the dimensions in an ideal order to reshape to the expected 2D shape:

np.einsum('ij,ikjl->ikjl', array, np.ones((3,3,3,3))).reshape(9,9)

The generic method being:

i,j = array.shape
k = 3 # extra rows
l = 3 # extra cols
np.einsum('ij,ikjl->ikjl', a, np.ones((i,k,j,l))).reshape(i*k,j*l)

Output:

array([[1, 1, 1, 2, 2, 2, 3, 3, 3],
       [1, 1, 1, 2, 2, 2, 3, 3, 3],
       [1, 1, 1, 2, 2, 2, 3, 3, 3],
       [4, 4, 4, 5, 5, 5, 6, 6, 6],
       [4, 4, 4, 5, 5, 5, 6, 6, 6],
       [4, 4, 4, 5, 5, 5, 6, 6, 6],
       [7, 7, 7, 8, 8, 8, 9, 9, 9],
       [7, 7, 7, 8, 8, 8, 9, 9, 9],
       [7, 7, 7, 8, 8, 8, 9, 9, 9]])

What is however nice with this method, is that it's quite easy to change the order to obtain other patterns or work with higher dimensions.

Example with other patterns:

>>> np.einsum('ij,iklj->iklj', a, np.ones((3,3,3,3))).reshape(9,9)
array([[1, 2, 3, 1, 2, 3, 1, 2, 3],
       [1, 2, 3, 1, 2, 3, 1, 2, 3],
       [1, 2, 3, 1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6, 4, 5, 6],
       [4, 5, 6, 4, 5, 6, 4, 5, 6],
       [4, 5, 6, 4, 5, 6, 4, 5, 6],
       [7, 8, 9, 7, 8, 9, 7, 8, 9],
       [7, 8, 9, 7, 8, 9, 7, 8, 9],
       [7, 8, 9, 7, 8, 9, 7, 8, 9]])

>>> np.einsum('ij,kjil->kjil', a, np.ones((3,3,3,3))).reshape(9,9)
array([[1, 1, 1, 4, 4, 4, 7, 7, 7],
       [2, 2, 2, 5, 5, 5, 8, 8, 8],
       [3, 3, 3, 6, 6, 6, 9, 9, 9],
       [1, 1, 1, 4, 4, 4, 7, 7, 7],
       [2, 2, 2, 5, 5, 5, 8, 8, 8],
       [3, 3, 3, 6, 6, 6, 9, 9, 9],
       [1, 1, 1, 4, 4, 4, 7, 7, 7],
       [2, 2, 2, 5, 5, 5, 8, 8, 8],
       [3, 3, 3, 6, 6, 6, 9, 9, 9]])
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