Numpy数组维度

时间:2010-06-17 12:55:52

标签: python arrays numpy dimensions

我目前正在尝试学习Numpy和Python。给出以下数组:

import numpy as np
a = np.array([[1,2],[1,2]])

是否有一个函数返回a的维度(例如,a是2乘2的数组)?

size()返回4,这无济于事。

8 个答案:

答案 0 :(得分:432)

.shape

  

ndarray。的形状
  数组维度的元组。

因此:

>>> a.shape
(2, 2)

答案 1 :(得分:54)

首先:

按照惯例,在Python世界中,numpy的快捷方式是np,所以:

In [1]: import numpy as np

In [2]: a = np.array([[1,2],[3,4]])

第二

在Numpy中,维度轴/轴形状是相关的,有时也是类似的概念:

尺寸

数学/物理中,维度或维度被非正式地定义为指定空间中任何点所需的最小坐标数。但是在 Numpy 中,根据numpy doc,它与轴/轴相同:

  

在Numpy中,尺寸称为轴。轴数是等级。

In [3]: a.ndim  # num of dimensions/axes, *Mathematics definition of dimension*
Out[3]: 2

轴线/轴

nth 坐标,用于索引Numpy中的array。多维数组每个轴可以有一个索引。

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)

形状

描述每个可用轴上的数据(或范围)。

In [5]: a.shape
Out[5]: (2, 2)  # both the first and second axis have 2 (columns/rows/pages/blocks/...) data

答案 2 :(得分:44)

import numpy as np   
>>> np.shape(a)
(2,2)

如果输入不是numpy数组而是列表列表

,也可以使用
>>> a = [[1,2],[1,2]]
>>> np.shape(a)
(2,2)

或元组元组

>>> a = ((1,2),(1,2))
>>> np.shape(a)
(2,2)

答案 3 :(得分:12)

您可以使用.shape

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

答案 4 :(得分:6)

{ "id": 7936, "location": "Meadowbrook Ct. 4", "locationId": "loc-4", "team1": "DC Premier", "team1Id": 801, "team1Score": "39", "team2": "Severn Elite", "team2Id": 804, "team2Score": "36", "time": "2016-07-09T08:00:00" }, many more ] 方法要求Private Sub Auto_Open() 'variables Dim excelDocPath As String, newfilePath As String, newfileName As String, NewBook As Workbook, MasterBook As Workbook, i As Integer 'initialize strings excelDocPath = ActiveWorkbook.Path & "\FILE_1.xlsx" newfilePath = ActiveWorkbook.Path & "\FOLDER\" 'refresh linked master excel workbook Set MasterBook = Application.Workbooks.Open(excelDocPath, UpdateLinks:=True, ReadOnly:=False) 'create reference to master excel workbook MasterBook.Worksheets("Sheet1").PivotTables("PivotTable").PivotCache.Refresh 'refresh the pivot table source link MasterBook.RefreshAll 'refresh all links MasterBook.Save 'save changes MasterBook.Close 'close workbook 'initialize new file name with current date newfileName = "FILENAME (" & Replace(Date, "/", "-") & ").xlsx" 'copy master doc and save as newfileName FSO.CopyFile excelDocPath, newfilePath & newfileName 'copy temple file and rename accordingly Set NewBook = Application.Workbooks.Open(newfilePath & newfileName, UpdateLinks:=xlUpdateLinksNever, ReadOnly:=False) 'make reference to new workbook NewBook.Activate 'make workbook the active workbook 'loop through workbook connections and delete all For i = ActiveWorkbook.Connections.Count To 1 Step -1 ActiveWorkbook.Connections.Item(i).Delete Next NewBook.Save 'save changes NewBook.Close 'close new workbook End Sub 为Numpy ndarray。但是Numpy也可以计算纯python对象的可迭代形状:

shape

答案 5 :(得分:5)

您可以使用.ndim来标注尺寸,而.shape可以知道确切的尺寸

var = np.array([[1,2,3,4,5,6], [1,2,3,4,5,6]])

var.ndim
# displays 2

var.shape
# display 6, 2

您可以使用.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

答案 6 :(得分:0)

a.shape只是np.info()的有限版本。检查一下:

import numpy as np
a = np.array([[1,2],[1,2]])
np.info(a)

class:  ndarray
shape:  (2, 2)
strides:  (8, 4)
itemsize:  4
aligned:  True
contiguous:  True
fortran:  False
data pointer: 0x27509cf0560
byteorder:  little
byteswap:  False
type: int32

答案 7 :(得分:0)

rows = a.shape[0] # 2 
cols = a.shape[1] # 2
a.shape #(2,2)
a.size # rows * cols = 4