If I have a numpy array X with X.shape=(m,n)
and a second column vector y with y.shape=(m,1)
, how can I calculate the covariance of each column of X with y wihtout using a for loop? I expect the result to be of shape (m,1)
or (1,m)
.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Assuming that the output is meant to be of shape (1,n)
i.e. a scalar each for covariance
operation for each column of A
with B
and thus for n
columns ending up with n
such scalars, you can use two approaches here that use covariance formula
.
Approach #1: With Broadcasting
np.sum((A - A.mean(0))*(B - B.mean(0)),0)/B.size
Approach #2: With Matrix-multiplication
np.dot((B - B.mean(0)).T,(A - A.mean(0)))/B.size