gpt4 book ai didi

c# - 求 Math Net 矩阵的协方差

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:42 24 4
gpt4 key购买 nike

我正在使用 MathNet Numerics 矩阵并试图找出矩阵的协方差。

我们如何找到矩阵的协方差?

我们有方法在 Statistics 命名空间下找到两个 IEnumerable 之间的协方差。

http://numerics.mathdotnet.com/api/MathNet.Numerics.Statistics/Statistics.htm

但我不知道如何使用它来查找矩阵。

例如:在 matlab/octave 中

enter image description here

在 C# 中也是如此。我们如何实现??

最佳答案

我认为 Math.NET 中没有任何方法可以模仿 Matlab/Octave 中的 cov 函数,但您可以轻松编写自己的方法:

public static Matrix<double> GetCovarianceMatrix(Matrix<double> matrix)
{
var columnAverages = matrix.ColumnSums() / matrix.RowCount;
var centeredColumns = matrix.EnumerateColumns().Zip(columnAverages, (col, avg) => col - avg);
var centered = DenseMatrix.OfColumnVectors(centeredColumns);
var normalizationFactor = matrix.RowCount == 1 ? 1 : matrix.RowCount - 1;
return centered.TransposeThisAndMultiply(centered) / normalizationFactor;
}

关于c# - 求 Math Net 矩阵的协方差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32256998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com