协方差的定义:
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必须是一维或者二维的,比如(2,4)就是二维的。默认情况下,每一行代表一个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