numpy协方差矩阵numpy.cov
numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None)[source]
-
Estimate a covariance matrix, given data and weights.
Covariance indicates the level to which two variables vary together. If we examine N-dimensional samples,
, then the covariance matrix element
is the covariance of
and
. The element
is the variance of
.
See the notes for an outline of the algorithm.
Parameters: m : array_like
A 1-D or 2-D array containing multiple variables and observations. Each row (行) of m represents a variable(变量), and each column(列) a single observation of all those variables(样本). Also see rowvar below.
y : array_like, optional
An additional set of variables and observations. y has the same form as that of m.
rowvar : bool, optional
If rowvar is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations.
bias : bool, optional
Default normalization (False) is by
(N - 1), whereNis the number of observations given (unbiased estimate). If bias is True, then normalization is byN. These values can be overridden by using the keywordddofin numpy versions >= 1.5.ddof : int, optional
If not
Nonethe default value implied by bias is overridden. Note thatddof=1will return the unbiased estimate, even if both fweights and aweights are specified, andddof=0will return the simple average. See the notes for the details. The default value isNone.New in version 1.5.
fweights : array_like, int, optional
1-D array of integer freguency weights; the number of times each observation vector should be repeated.
New in version 1.10.
aweights : array_like, optional
1-D array of observation vector weights. These relative weights are typically large for observations considered “important” and smaller for observations considered less “important”. If
ddof=0the array of weights can be used to assign probabilities to observation vectors.New in version 1.10.
Returns: out : ndarray
The covariance matrix of the variables.
See also
corrcoef- Normalized covariance matrix
Notes
Assume that the observations are in the columns of the observation array m and let
f = fweightsanda = aweightsfor brevity. The steps to compute the weighted covariance are as follows:>>> w = f * a
>>> v1 = np.sum(w)
>>> v2 = np.sum(w * a)
>>> m -= np.sum(m * w, axis=1, keepdims=True) / v1
>>> cov = np.dot(m * w, m.T) * v1 / (v1**2 - ddof * v2)Note that when
a == 1, the normalization factorv1 / (v1**2 - ddof * v2)goes over to1 / (np.sum(f) - ddof)as it should.Examples
Consider two variables,
and
, which correlate perfectly, but in opposite directions:
>>> x = np.array([[0, 2], [1, 1], [2, 0]]).T
>>> x
array([[0, 1, 2],
[2, 1, 0]])Note how
increases while
decreases. The covariance matrix shows this clearly:
>>> np.cov(x)
array([[ 1., -1.],
[-1., 1.]])Note that element
, which shows the correlation between
and
, is negative.
Further, note how x and y are combined:
>>> x = [-2.1, -1, 4.3]
>>> y = [3, 1.1, 0.12]
>>> X = np.stack((x, y), axis=0)
>>> print(np.cov(X))
[[ 11.71 -4.286 ]
[ -4.286 2.14413333]]
>>> print(np.cov(x, y))
[[ 11.71 -4.286 ]
[ -4.286 2.14413333]]
>>> print(np.cov(x))
11.71总结
理解协方差矩阵的关键就在于牢记它的计算是不同维度之间的协方差,而不是不同样本之间。拿到一个样本矩阵,最先要明确的就是一行是一个样本还是一个维度,心中明确整个计算过程就会顺流而下,这么一来就不会迷茫了。
numpy协方差矩阵numpy.cov的更多相关文章
- numpy入门—numpy是什么
numpy是什么?为什么使用numpy 使用numpy库与原生python用于数组计算性能对比
- Python的 numpy中 numpy.ravel() 和numpy.flatten()的区别和使用
两者所要实现的功能是一致的(将多维数组降为一维), 两者的区别在于返回拷贝(copy)还是返回视图(view),numpy.flatten() 返回一份拷贝,对拷贝所做的修改不会影响(reflects ...
- Python 关于数组矩阵变换函数numpy.nonzero(),numpy.multiply()用法
1.numpy.nonzero(condition),返回参数condition(为数组或者矩阵)中非0元素的索引所形成的ndarray数组,同时也可以返回condition中布尔值为True的值索引 ...
- numpy.ravel()/numpy.flatten()/numpy.squeeze()
numpy.ravel(a, order='C') Return a flattened array numpy.chararray.flatten(order='C') Return a copy ...
- 【numpy】新版本中numpy(numpy>1.17.0)中的random模块
numpy是Python中经常要使用的一个库,而其中的random模块经常用来生成一些数组,本文接下来将介绍numpy中random模块的一些使用方法. 首先查看numpy的版本: import nu ...
- NumPy之:NumPy简介教程
目录 简介 安装NumPy Array和List 创建Array Array操作 sort concatenate 统计信息 reshape 增加维度 index和切片 从现有数据中创建Array 算 ...
- numpy入门—Numpy的核心array对象以及创建array的方法
Numpy的核心array对象以及创建array的方法 array对象的背景: Numpy的核心数据结构,就叫做array就是数组,array对象可以是一维数组,也可以是多维数组: Python的Li ...
- 使用numpy实现批量梯度下降的感知机模型
生成多维高斯分布随机样本 生成多维高斯分布所需要的均值向量和方差矩阵 这里使用numpy中的多变量正太分布随机样本生成函数,按照要求设置均值向量和协方差矩阵.以下设置两个辅助函数,用于指定随机变量维度 ...
- python(5):scipy之numpy介绍
python 的scipy 下面的三大库: numpy, matplotlib, pandas scipy 下面还有linalg 等 scipy 中的数据结构主要有三种: ndarray(n维数组), ...
随机推荐
- 小波变换——子带编码,Subband Coding
离散小波变换.多级信号分解,多分辨率分析. Multiresolution Analysis(MRA.多分辨率分析) 子带编码(Subband Coding) 简称SBC. 一种以信号频谱为依据的编码 ...
- while 1要小心
之前判断一个接口的返回,一定约定好了是返回retcode 1或者retcode 0,就用的这个判断,但是接口挂了的时候,一直返回未登录,找了很长时间为什么cpu一直消耗那么高. 使用wihle 1时候 ...
- Android和IOS开发学习路线
图片看上去太小,直接另存为吧 图片来自:http://www.finalshares.com/
- http 返回码 405 解决方案之一
今天做网络请求数据的时候遇到返回码405,当时就傻了~~ 故事是这样的-- 我用post请求访问一个url,服务端数据是一个json的txt文件,理论上直接访问,返回json,然后解析就没事了,可是今 ...
- 【数据分析】Superset 之二 Docker安装初始化
docker search superset amancevice/superset [] Superset on Debian/Python3 [OK] docker pull amancevice ...
- Python 基础进阶
函数的定义 函数的参数 函数的默认参数 函数的变量 函数的返回值 函数的多类型传值 函数的冗余参数 函数的递归调用 匿名函数 高阶函数 内建函数 模块与包 面向对象 类的定义 类的属性 类的内置属性 ...
- 第二十篇:不为客户连接创建子进程的并发回射服务器(poll实现)
前言 在上文中,我使用select函数实现了不为客户连接创建子进程的并发回射服务器( 点此进入 ).但其中有个细节确实有点麻烦,那就是还得设置一个client数组用来标记select监听描述符集中被设 ...
- CDN的那些细枝末节
起源: 原本打算系统看看关于axios的介绍,无意中就看见一句"Using cdn",于是百度一下,"cdn"是什么? 名词解释:CDN CDN的全称是Cont ...
- PHP curl登录 跳过验证码
<?php switch($_GET['do']){ case 'vc': $cookieFile = "./test.tmp"; $url = 'http://localh ...
- .net环境下的缓存技术-转载!
摘要: 介绍缓存的基本概念和常用的缓存技术,给出了各种技术的实现机制的简单介绍和适用范围说明,以及设计缓存方案应该考虑的问题(共17页) 1 概念 1.1 缓存能解决的问题 · 性 ...