matlab - 错误: chol: input matrix must be positive definite

标签 matlab machine-learning pattern-matching octave naivebayes

如何修复此错误?

mvnpdf.m

% y = mvnpdf(x,mu,Sigma)
% Compute multivariate normal pdf for x given mean mu and covariance matrix 
% sigma.  The dimension of x is d x p, mu is 1 x p and sigma is p x p.

function pdf = mvnpdf(x,mu,sigma)
  [d,p] = size(x);
  % mu can be a scalar, a 1xp vector or a nxp matrix
  if nargin == 1, mu = 0; end
  if all(size(mu) == [1,p]), mu = repmat(mu,[d,1]); end
  if nargin < 3
    pdf = (2*pi)^(-p/2) * exp(-sumsq(x-mu,2)/2);
  else
    r = chol(sigma);
    pdf = (2*pi)^(-p/2) * exp(-sumsq((x-mu)/r,2)/2) / prod(diag(r));
  end

pdfdep.m

function pdfmx = pdfdep(train, test)
% computes probability density for all classes
% assuming feature independence
% train - train set; the first column contains label
%   used to  compute mean and variation for all classes
% test - test set (without labels)
% pdfmx - matrix of probability density for all classes
%   class with label idx is stored in pdfmx(:,idx)

    classnb = rows(unique(train(:,1)));

    pdfmx = ones(rows(test), classnb);

    for cl=1:classnb
        clidx = train(:,1) == cl;

        mu = mean(train(clidx,2:end)); 
        sigma = cov(train(clidx,2:end));

        pdfmx(:,cl) = mvnpdf(test, mu, sigma);
    end

ma​​t.txt

1   2   3   4   5   6   7   8
2   3   4   5   6   7   8   1 
3   4   5   6   7   8   1   2 
4   5   6   7   8   1   2   3
1   8   7   6   5   4   3   2
2   7   6   5   4   3   2   9
3   6   5   4   3   2   9   8 
4   5   4   3   2   9   8   7
1   8   7   6   5   4   3   2
3   6   5   4   3   2   9   8

错误消息:

>> mat2 = mat;
>> pdfdep(mat, mat2)
error: chol: input matrix must be positive definite
error: called from
    mvnpdf at line 13 column 7
    pdfdep at line 20 column 15
>>

最佳答案

这个错误是不言自明的

input matrix must be positive definite

意味着您的矩阵 (sigma) 不是正定的,因此您无法对其进行 cholesky 分解。有很多方法可以很好地估计协方差,当数据退化时(它位于低维流形中),简单地计算经验估计(通过调用 cov 所做的事情)是行不通的。最简单的解决方案之一是使用以下形式的“拉动估计器”:

cov(X) + eps * I

而不是

cov(X)

因此只需更改

sigma = cov(train(clidx,2:end));

合并这个额外的+ eps * I(其中I是适当维度的单位矩阵)。

关于matlab - 错误: chol: input matrix must be positive definite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40438974/

相关文章:

machine-learning - deploy.prototxt 中 dropout 层的用途是什么?

swift - `switch` 类似模式匹配的控制流语句匹配任何和所有真实情况?

java - 单引号检测所需的正则表达式

c - 解决 matlab 函数 : expected '=' , ',' 、 ';' 、 'asm' 或 '__attribute__' 之前 '.' token 中 Simulink C 代码的编译错误

MATLAB 类对象未更新

matlab - 如何在matlab中绘制3d有向图

machine-learning - 什么是弱学习者?

machine-learning - 用于预测文本的二元模型

sql - 与 CLOB 一起使用的类似 UTL_MATCH 的函数

Matlab - 将低通滤波器应用于矢量?