m<<n 矩阵 (mxn) 的马氏距离
Mahalanobis distance for a matrix (mxn) with m<<n
我有一个 12x202 矩阵(12 个实例,有 202 个特征)。我想计算每 12 个实例之间的马哈拉诺比斯距离,但列数似乎不能大于实例数(行)。 (我在计算 12x11 矩阵的距离时没有问题,但是超过 11 个特征会在 MATLAB 中使用 linkage(X,'ward','mahalanobis');
或 mahal(X,X);
或 pdist2(X,X,'mahalanobis');
导致错误)
如果您查看 matlab documentation 中的 mahal 函数,它会显示:
X and Y must have the same number of columns, but can have different
numbers of rows. X must have more rows than columns.
我不太擅长统计,所以我不确定为什么这个条件很重要,但我想这是出于效率原因,而且 12 个措施的数量太少,所以考虑有更多的措施。
你可以做的是自己计算马哈拉博尼斯距离,在同一个文档中很容易得到公式,而且给出的例子对马哈拉博尼斯距离有更好的计算:
Mahalanobis distance is also called quadratic distance. It measures
the separation of two groups of objects. Suppose we have two groups
with means and , Mahalanobis distance is given by the following
不同组也是如此,不一样。
无论如何你都可以使用这个:
function MD = my_MahalanobisDistance(X, Y)
[nX, mX] = size(X);
[nY, mY] = size(Y);
n = nX + nY;
if(mX ~= mY)
disp('Columns in X must be same as in Y')
else
xDiff = mean(X) - mean(Y);
cX = my_covariance(X);
cY = my_covariance(Y);
pC = nX/n*cX + nY/n*cY;
MD = sqrt(xDiff * inv(pC) * xDiff');
end
协方差:
function C = my_covariance(X)
[n,m] = size(X);
Xc = X -repmat(mean(X),n,1);
C = Xc'* Xc/n;
希望对您有所帮助
我有一个 12x202 矩阵(12 个实例,有 202 个特征)。我想计算每 12 个实例之间的马哈拉诺比斯距离,但列数似乎不能大于实例数(行)。 (我在计算 12x11 矩阵的距离时没有问题,但是超过 11 个特征会在 MATLAB 中使用 linkage(X,'ward','mahalanobis');
或 mahal(X,X);
或 pdist2(X,X,'mahalanobis');
导致错误)
如果您查看 matlab documentation 中的 mahal 函数,它会显示:
X and Y must have the same number of columns, but can have different numbers of rows. X must have more rows than columns.
我不太擅长统计,所以我不确定为什么这个条件很重要,但我想这是出于效率原因,而且 12 个措施的数量太少,所以考虑有更多的措施。
你可以做的是自己计算马哈拉博尼斯距离,在同一个文档中很容易得到公式,而且给出的例子对马哈拉博尼斯距离有更好的计算:
Mahalanobis distance is also called quadratic distance. It measures the separation of two groups of objects. Suppose we have two groups with means and , Mahalanobis distance is given by the following
不同组也是如此,不一样。
无论如何你都可以使用这个:
function MD = my_MahalanobisDistance(X, Y)
[nX, mX] = size(X);
[nY, mY] = size(Y);
n = nX + nY;
if(mX ~= mY)
disp('Columns in X must be same as in Y')
else
xDiff = mean(X) - mean(Y);
cX = my_covariance(X);
cY = my_covariance(Y);
pC = nX/n*cX + nY/n*cY;
MD = sqrt(xDiff * inv(pC) * xDiff');
end
协方差:
function C = my_covariance(X)
[n,m] = size(X);
Xc = X -repmat(mean(X),n,1);
C = Xc'* Xc/n;
希望对您有所帮助