我目前正在尝试学习Numpy和Python。给出以下数组:
import numpy as np
a = np.array([[1,2],[1,2]])
是否有一个函数返回a
的维度(例如,a是2乘2的数组)?
size()
返回4,这无济于事。
答案 0 :(得分:432)
答案 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