在numpy中,數組可能具有包含字段的數據類型,類似於電子表格中的列。一個例子是[(a, int), (b, float)]
,其中數組中的每個條目都是一對(int,float)。通常,這些屬性是使用字典查找(例如,arr['a'] and arr['b']
。
記錄數組允許使用以下方式將字段作為數組的成員進行訪問arr.a and arr.b
。 numpy.recarray.var()函數返回沿給定軸的數組元素的方差。
用法: numpy.recarray.var(axis=None, dtype=None, out=None, ddof=0, keepdims=False)
參數:
axis :我們要沿其計算方差的[int或int元組]軸。否則,它將認為arr是平坦的(在所有軸上均有效)。 axis = 0表示沿列的方差,而axis = 1表示沿行的方差。
dtype :[數據類型,可選]計算方差時需要的類型。
out :[ndarray,可選]將結果存儲到的位置。
->如果提供,則必須具有廣播輸入的形狀。
->如果未提供或沒有,則返回新分配的數組。
ddof :[int,可選] Delta自由度”:計算中使用的除數為N-ddof,其中N表示元素數。默認情況下,ddof為零。
keepdims :[布爾,可選]如果將其設置為True,則縮小的軸將保留為尺寸為1的尺寸。使用此選項,結果將針對輸入數組正確廣播。
Return :[ndarray]如果out = None,則返回一個包含方差的新數組;否則,返回對輸出數組的引用。
代碼1:
# Python program explaining
# numpy.recarray.var() method
# importing numpy as geek
import numpy as geek
# creating input array with 2 different field
in_arr = geek.array([[(5.0, 2), (3.0, -4), (6.0, 9)],
[(9.0, 1), (5.0, 4), (-12.0, -7)]],
dtype =[('a', float), ('b', int)])
print ("Input array:", in_arr)
# convert it to a record array,
# using arr.view(np.recarray)
rec_arr = in_arr.view(geek.recarray)
print("Record array of float:", rec_arr.a)
print("Record array of int:", rec_arr.b)
# applying recarray.var methods
# to float record array along axis 1
out_arr = rec_arr.a.var(axis = 1)
print ("Output array containing variance along axis 1:", out_arr)
# applying recarray.var methods
# to float record array along axis 0
out_arr = rec_arr.a.var(axis = 0)
print ("Output array containing variance along axis 0:", out_arr)
# applying recarray.var methods
# to float record array along default axis
out_arr = rec_arr.a.var()
print ("Output array containing variance along default axis:", out_arr)
# applying recarray.var methods
# to int record array along axis 1
out_arr = rec_arr.b.var(axis = 1)
print ("Output array containing variance along axis 1:", out_arr)
# applying recarray.var methods
# to int record array along axis 0
out_arr = rec_arr.b.var(axis = 0)
print ("Output array containing variance along axis 0:", out_arr)
# applying recarray.var methods
# to int record array along default axis
out_arr = rec_arr.b.var()
print ("Output array containing variance along default axis:", out_arr)
輸出:
Input array: [[( 5., 2) ( 3., -4) ( 6., 9)] [( 9., 1) ( 5., 4) (-12., -7)]] Record array of float: [[ 5. 3. 6.] [ 9. 5. -12.]] Record array of int: [[ 2 -4 9] [ 1 4 -7]] Output array containing variance along axis 1: [ 1.55555556 82.88888889] Output array containing variance along axis 0: [ 4. 1. 81.] Output array containing variance along default axis: 46.22222222222222 Output array containing variance along axis 1: [28.22222222 21.55555556] Output array containing variance along axis 0: [ 0.25 16. 64. ] Output array containing variance along default axis: 27.138888888888882
相關用法
- Python numpy.cov()用法及代碼示例
- Python numpy.copyto()用法及代碼示例
- Python Numpy MaskedArray.any()用法及代碼示例
- Python Numpy MaskedArray.var()用法及代碼示例
- Python Numpy MaskedArray.sum()用法及代碼示例
- Python numpy.issubsctype()用法及代碼示例
- Python Numpy MaskedArray.std()用法及代碼示例
- Python Numpy MaskedArray.mean()用法及代碼示例
- Python Numpy recarray.put()用法及代碼示例
- Python Numpy recarray.ptp()用法及代碼示例
- Python Numpy MaskedArray.all()用法及代碼示例
- Python Numpy MaskedArray.dot()用法及代碼示例
- Python Numpy recarray.all()用法及代碼示例
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 Numpy recarray.var() function | Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。