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

349
Vistas
Numpy: Indexing over the last axis when you don't know the rank in advance

How can I index the last axis of a Numpy array if I don't know its rank in advance?

Here is what I want to do: Let a be a Numpy array of unknown rank. I want the slice of the last k elements of the last axis.

If a is 1D, I want

b = a[-k:]

If a is 2D, I want

b = a[:, -k:]

If a is 3D, I want

b = a[:, :, -k:]

and so on.

I want this to work regardless of the rank of a (as long as the rank is at least 1).

The fact that I want the last k elements in the example is irrelevant of course, the point is that I want to specify indices for whatever the last axis is when I don't know the rank of an array in advance.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

b = a[..., -k:]

This is mentioned in the docs.

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