gpt4 book ai didi

c++ - 为什么LDA中的特征向量和特征值变为零?

转载 作者:行者123 更新时间:2023-12-01 08:50:58 26 4
gpt4 key购买 nike

我想在 OpenCV 中实现快速 PLDA(概率线性判别分析)。在此,LINK快速 PLDA 已在 Matlab 中实现和 Python . PLDA 的一部分是 LDA。我编写了以下用于在 OpenCV 中实现 LDA 的代码:

int LDA_dim = 120;

// Load data

FileStorage fs("newStorageFile.yml", FileStorage::READ);

// Read data

Mat train_data, train_labels;

fs["train_data"] >> train_data;
fs["train_labels"] >> train_labels;

// LDA

if (LDA_dim > 0)
{
LDA lda(LDA_dim);
lda.compute(train_data, train_labels); // compute eigenvectors

Mat eigenvectors = lda.eigenvectors();
}

我已经转换了以上链接中介绍的数据库 .mat.yml .结果是 newStorageFile.yml我上传的 here . train_data有 650 行和 600 列,train_labels 有 650 行和 1 列。我不知道为什么特征向量和特征值变为零!!?请帮助我修复此代码。

最好带上转换数据的代码 .mat.yml :
function matlab2opencv( variable, fileName, flag)

[rows cols] = size(variable);

% Beware of Matlab's linear indexing
variable = variable';

% Write mode as default
if ( ~exist('flag','var') )
flag = 'w';
end

if ( ~exist(fileName,'file') || flag == 'w' )
% New file or write mode specified
file = fopen( fileName, 'w');
fprintf( file, '%%YAML:1.0\n');
else
% Append mode
file = fopen( fileName, 'a');
end

% Write variable header
fprintf( file, ' %s: !!opencv-matrix\n', inputname(1));
fprintf( file, ' rows: %d\n', rows);
fprintf( file, ' cols: %d\n', cols);
fprintf( file, ' dt: f\n');
fprintf( file, ' data: [ ');

% Write variable data
for i=1:rows*cols
fprintf( file, '%.6f', variable(i));
if (i == rows*cols), break, end
fprintf( file, ', ');
if mod(i+1,4) == 0
fprintf( file, '\n ');
end
end

fprintf( file, ']\n');

fclose(file);

编辑 1 ) 我已经用自己生成的一些样本尝试了 LDA:
Mat train_data = (Mat_<double>(3, 3) << 25, 45, 44, 403, 607, 494, 2900, 5900, 2200);
Mat train_labels = (Mat_<int>(3, 1) << 1, 2, 3 );

LDA lda(LDA_dim);

lda.compute(train_data, train_labels); // compute eigenvectors
Mat_<double> eigenvectors = lda.eigenvectors();
Mat_<double> eigenvalues = lda.eigenvalues();
cout << eigenvectors << endl << eigenvalues;

但我必须得到相同的结果:特征值和特征向量变为零:
eigenvector and eigenvalue

最佳答案

正是由于浮点不精确,特征值接近于零。

关于c++ - 为什么LDA中的特征向量和特征值变为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47009542/

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