Dimensiones de matriz Numpy
Actualmente estoy tratando de aprender Numpy y Python. Dado el siguiente array:
import numpy as N
a = N.array([[1,2],[1,2]])
¿Hay una función que devuelve las dimensiones de a
(por ejemplo, a es una matriz de 2 por 2)?
size()
devuelve 4 y eso no ayuda mucho.
6 answers
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
2010-06-17 12:59:46
import numpy as np
>>> np.shape(a)
(2,2)
También funciona si la entrada no es una matriz numpy sino una lista de listas
>>> a = [[1,2],[1,2]]
>>> np.shape(a)
(2,2)
O una tupla de tuplas
>>> a = ((1,2),(1,2))
>>> np.shape(a)
(2,2)
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-05-05 23:14:40
Primero:
Por convención, en Python world, el atajo para numpy
es np
, así que:
In [1]: import numpy as np
In [2]: a = np.array([[1,2],[3,4]])
Segundo:
En Numpy, dimensión, eje / ejes, forma son conceptos relacionados y a veces similares:
Dimensión
En Matemáticas/Física, dimensión o dimensionalidad se define informalmente como el número mínimo de coordenadas necesarias para especificar cualquier punto dentro de un espacio. Pero en Numpy , de acuerdo con la numpy doc , es lo mismo que axis / axes:
En Numpy las dimensiones se llaman ejes. El número de ejes es rango.
In [3]: a.ndim # num of dimensions/axes, *Mathematics definition of dimension*
Out[3]: 2
Eje / ejes
La enésima coordenada para indexar un array
en Numpy. Y los arrays multidimensionales pueden tener un índice por eje.
In [4]: a[1,0] # to index `a`, we specific 1 at the first axis and 0 at the second axis.
Out[4]: 3 # which results in 3 (locate at the row 1 and column 0, 0-based index)
Forma
Describe cuántos datos (o el rango) a lo largo de cada eje disponible.
In [5]: a.shape
Out[5]: (2, 2) # both the first and second axis have 2 (columns/rows/pages/blocks/...) data
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-12-08 02:13:25
Puede usar .forma
In: a = np.array([[1,2,3],[4,5,6]])
In: a.shape
Out: (2, 3)
In: a.shape[0] # x axis
Out: 2
In: a.shape[1] # y axis
Out: 3
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
2017-08-25 18:10:30
El método shape
requiere que a
sea un ndarray Numpy. Pero Numpy también puede calcular la forma de iterables de objetos python puros:
np.shape([[1,2],[1,2]])
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
2017-06-15 17:01:43
Puede usar .ndim
para la dimensión y .shape
para conocer la dimensión exacta
var = np.array([[1,2,3,4,5,6], [1,2,3,4,5,6]])
var.ndim
# displays 2
var.shape
# display 6, 2
Puede cambiar la dimensión usando la función .reshape
var = np.array([[1,2,3,4,5,6], [1,2,3,4,5,6]]).reshape(3,4)
var.ndim
#display 2
var.shape
#display 3, 4
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-08-18 12:04:46