numpy.cov以及corrcoef

协方差的定义:
Cov(X,Y)=E[(X-E(X))(Y-E(Y))]


#Estimate a covariance matrix, given data and weights.
给定数据和权重,求解协方差矩阵

# Covariance indicates the level to which two variables vary together. If we examine N-dimensional samples, X = [x_1, x_2, ... x_N]^T, then the covariance matrix element C_{ij} is the covariance of x_i and x_j. The element C_{ii} is the variance of x_i.'''
协方差能衡量两个variables的相似度。

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.
作为输入的m必须是一维或者二维的,比如(24)就是二维的。默认情况下,每一行代表一个variables,每一列代表一个observation。cov是以variables为单位进行计算的

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.
如果rowvar是1(默认),那么每行代表一个variables,每列代表一个observations(样本);反之则每行是observations,每列是variables


Returns:    
out : ndarray
# The covariance matrix of the variables.
返回协方差矩阵

具体的内容请参考:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.cov.html

但是,cov算出来的协方差矩阵并不是-1-1之间,为了达到这种效果,可以将矩阵做如下处理:

Cij=CijCiiCjj 

而corrcoef能做到这种效果,实际上corrcoef就是比cov多了上面这一步而已

具体参考请看
https://docs.scipy.org/doc/numpy/reference/generated/numpy.corrcoef.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值