Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

215
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda