¿Cómo acceder a la ith columna de una matriz multidimensional NumPy?


Supongamos que tengo:

test = numpy.array([[1, 2], [3, 4], [5, 6]])

test[i] gets me ith line of the array (eg [1, 2]). ¿Cómo puedo acceder a la columna ith? (eg [1, 3, 5]). Además, ¿sería una operación costosa?

Author: nbro, 2010-12-16

6 answers

>>> test[:,0]
array([1, 3, 5])

Del mismo modo,

>>> test[1,:]
array([3, 4])

Le permite acceder a las filas. Esto se trata en la sección 1.4 (Indexación) de la referencia NumPy . Esto es rápido, al menos en mi experiencia. Ciertamente es mucho más rápido que acceder a cada elemento en un bucle.

 479
Author: mtrw,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-20 09:06:10

Y si quieres acceder a más de una columna a la vez puedes hacer:

>>> test = np.arange(9).reshape((3,3))
>>> test
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> test[:,[0,2]]
array([[0, 2],
       [3, 5],
       [6, 8]])
 50
Author: Akavall,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-20 14:05:21
>>> test[:,0]
array([1, 3, 5])

Este comando le da un vector de fila, si solo desea hacer un bucle sobre él, está bien, pero si desea hstack con alguna otra matriz con dimensión 3xN, tendrá

ValueError: todos los arrays de entrada deben tener el mismo número de dimensiones

Mientras que

>>> test[:,[0]]
array([[1],
       [3],
       [5]])

Le da un vector de columna, para que pueda hacer la operación concatenar o hstack.

Por ejemplo

>>> np.hstack((test, test[:,[0]]))
array([[1, 2, 1],
       [3, 4, 3],
       [5, 6, 5]])
 37
Author: Cloud,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-11 15:05:18

También puedes transponer y devolver una fila:

In [4]: test.T[0]
Out[4]: array([1, 3, 5])
 14
Author: Hotschke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-26 09:55:40
>>> test
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

>>> ncol = test.shape[1]
>>> ncol
5L

Luego puede seleccionar la 2a-4a columna de esta manera:

>>> test[0:, 1:(ncol - 1)]
array([[1, 2, 3],
       [6, 7, 8]])
 1
Author: mac,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-21 06:59:24

Para obtener varias columnas independientes, simplemente:

> test[:,[0,2]]

Obtendrá columnas 0 y 2

 0
Author: Brais Perez,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-07 10:43:58