如何在Scipy中计算稀疏矩阵的一列的方差? [英] How do I compute the variance of a column of a sparse matrix in Scipy?
问题描述
我有一个大的scipy.sparse.csc_matrix
,并想将其标准化.也就是说,从每个元素中减去列平均值,然后除以列标准偏差(std)i.
I have a large scipy.sparse.csc_matrix
and would like to normalize it. That is subtract the column mean from each element and divide by the column standard deviation (std)i.
scipy.sparse.csc_matrix
具有.mean()
,但是有一种有效的方法来计算方差或标准差吗?
scipy.sparse.csc_matrix
has a .mean()
but is there an efficient way to compute the variance or std?
推荐答案
You can calculate the variance yourself using the mean, with the following formula:
E[X^2] - (E[X])^2
E[X]
代表平均值.因此,要计算E[X^2]
,您必须将csc_matrix
平方,然后使用mean
函数.要获得(E[X])^2
,您只需要对使用常规输入获得的mean
函数的结果求平方即可.
E[X]
stands for the mean. So to calculate E[X^2]
you would have to square the csc_matrix
and then use the mean
function. To get (E[X])^2
you simply need to square the result of the mean
function obtained using the normal input.
这篇关于如何在Scipy中计算稀疏矩阵的一列的方差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!