Numpy 的 cov(~)
方法計算給定兩個數組的協方差。
參數
1. x
| array-like
每行代表一個單獨的列數據(即變量)。請參閱下麵的示例以進行說明。
2. y
| array-like
| optional
您無需將數據集指定為 x 中的新行,隻需將其指定為 y
即可。
3. rowvar
| boolean
| optional
如果為 True,則 x 中的每一行代表一個變量。如果為 False,則 x 中的每一列代表一個變量。默認情況下,rowvar=True
。
4. bias
| boolean
| optional
是否計算協方差的有偏估計或無偏估計。默認情況下,bias=False
(樣本協方差)。
5. ddof
| int
| optional
自由度δ。這可以用來修改前麵的分母:
默認情況下,ddof=0
。
6. fweights
| array-like
| optional
每次觀察應重複的次數。請參閱下麵的示例以進行說明。
7. aweights
| array-like
| optional
每個觀測值上放置的一維權重數組。權重越高,觀察結果越重要。
返回值
如果給定一個變量,則返回 a 標量。否則,返回一個 Numpy 數組。
例子
計算樣本協方差
np.cov([2,3,5,6], [3,5,8,12])
array([[ 3.33333333, 7. ],
[ 7. , 15.33333333]])
這與指定二維數組完全相同:
np.cov([[2,3,5,6], [3,5,8,12]])
array([[ 3.33333333, 7. ],
[ 7. , 15.33333333]])
計算總體協方差
np.cov([2,3,5,6], [3,5,8,12], ddof=0)
array([[ 2.5 , 5.25],
[ 5.25, 11.5 ]])
您還可以設置bias=True
:
np.cov([2,3,5,6], [3,5,8,12], bias=True)
array([[ 2.5 , 5.25],
[ 5.25, 11.5 ]])
使用 fweight 重複值
np.cov([2,3,5,6], [3,5,8,12], fweights=[2,1,1,1])
array([[ 3.3 , 6.85],
[ 6.85, 14.7 ]])
這與以下內容完全相同:
np.cov([2,2,3,5,6], [3,3,5,8,12])
array([[ 3.3 , 6.85],
[ 6.85, 14.7 ]])
也就是說,每個數組的第一個值重複兩次,如 fweights
指定的那樣。
相關用法
- Python codecs.decode()用法及代碼示例
- Python collections.somenamedtuple._replace用法及代碼示例
- Python collections.somenamedtuple._asdict用法及代碼示例
- Python compile()用法及代碼示例
- Python NumPy compress方法用法及代碼示例
- Python collections.somenamedtuple._field_defaults用法及代碼示例
- Python codecs.encode()用法及代碼示例
- Python contextlib.AsyncContextDecorator用法及代碼示例
- Python collections.ChainMap用法及代碼示例
- Python contextlib.AsyncExitStack用法及代碼示例
- Python string count()用法及代碼示例
- Python collections.somenamedtuple._make用法及代碼示例
- Python NumPy count_nonzero方法用法及代碼示例
- Python configparser.ConfigParser.readfp用法及代碼示例
- Python code.compile_command()用法及代碼示例
- Python Wand color_matrix()用法及代碼示例
- Python configparser.ConfigParser.BOOLEAN_STATES用法及代碼示例
- Python math copysign()用法及代碼示例
- Python PIL composite()用法及代碼示例
- Python codeop.compile_command用法及代碼示例
- Python contextlib.ExitStack.pop_all用法及代碼示例
- Python contextlib.redirect_stdout用法及代碼示例
- Python collections.Counter用法及代碼示例
- Python configparser.BasicInterpolation用法及代碼示例
- Python configparser.ExtendedInterpolation用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | cov method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。