What does the "Reference to non-existent field 'training' " error message mean?
2 次查看(过去 30 天)
显示 更早的评论
hi, can you everyone help me in this error.
the code is:
% Common parameter settings
opts.k = 5; % Number of k in K-nearest neighbor
opts.N = 10; % number of solutions
opts.T = 100; % maximum number of iterations
opts.P = 0.5; % constant
opts.FADs = 0.2; % fish aggregating devices effect
% Divide data into training and validation sets
HO = cvpartition(trainingLabels,'HoldOut',.2);
opts.Model = HO;
% Perform feature selection
FS = jfs('mpa',trainingFeatures,trainingLabels,opts);
% Define index of selected features
sf_idx = FS.sf;
% Accuracy
Acc = jknn(feat(:,sf_idx),trainingLabels,opts);
call function:
function model = jfs(type,feat,label,opts)
switch type
case 'mpa' ; fun = @jMarinePredatorsAlgorithm;
case 'gndo' ; fun = @jGeneralizedNormalDistributionOptimization;
case 'sma' ; fun = @jSlimeMouldAlgorithm;
end
tic;
model = fun(feat,label,opts);
% Computational time
t = toc;
model.t = t;
fprintf('\n Processing Time (s): %f % \n',t); fprintf('\n');
end
call function:
function MPA = jMarinePredatorsAlgorithm(feat,label,opts)
% Parameters
lb = 0;
ub = 1;
thres = 0.5;
beta = 1.5; % levy component
P = 0.5; % constant
FADs = 0.2; % fish aggregating devices effect
if isfield(opts,'N'), N = opts.N; end
if isfield(opts,'T'), max_Iter = opts.T; end
if isfield(opts,'thres'), thres = opts.thres; end
if isfield(opts,'P'), P = opts.P; end
if isfield(opts,'FADs'), FADs = opts.FADs; end
% Objective function
fun = @jFitnessFunction;
% Number of dimensions
dim = size(feat,2);
% Initial (9)
X = zeros(N,dim);
for i = 1:N
for d = 1:dim
X(i,d) = lb + (ub - lb) * rand();
end
end
% Pre
fit = zeros(1,N);
fitG = inf;
curve = inf;
t = 1;
% Iteration
while t <= max_Iter
% Fitness
for i=1:N
fit(i) = fun(feat,label,(X(i,:) > thres),opts);
% Best
if fit(i) < fitG
fitG = fit(i);
Xgb = X(i,:);
end
end
% Memory saving
if t == 1
fitM = fit;
Xmb = X;
end
for i = 1:N
if fitM(i) < fit(i)
fit(i) = fitM(i);
X(i,:) = Xmb(i,:);
end
end
Xmb = X;
fitM = fit;
% Construct elite (10)
Xe = repmat(Xgb,[N 1]);
% Adaptive parameter (14)
CF = (1 - (t / max_Iter)) ^ (2 * (t / max_Iter));
% [First phase] (12)
if t <= max_Iter / 3
for i = 1:N
% Brownian random number
RB = randn(1,dim);
for d = 1:dim
R = rand();
stepsize = RB(d) * (Xe(i,d) - RB(d) * X(i,d));
X(i,d) = X(i,d) + P * R * stepsize;
end
% Boundary
XB = X(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
X(i,:) = XB;
end
% [Second phase] (13-14)
elseif t > max_Iter / 3 && t <= 2 * max_Iter / 3
for i = 1:N
% First half update (13)
if i <= N / 2
% Levy random number
RL = 0.05 * jLevy(beta,dim);
for d = 1:dim
R = rand();
stepsize = RL(d) * (Xe(i,d) - RL(d) * X(i,d));
X(i,d) = X(i,d) + P * R * stepsize;
end
% Another half update (14)
else
% Brownian random number
RB = randn(1,dim);
for d = 1:dim
stepsize = RB(d) * (RB(d) * Xe(i,d) - X(i,d));
X(i,d) = Xe(i,d) + P * CF * stepsize;
end
end
% Boundary
XB = X(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
X(i,:) = XB;
end
% [Third phase] (15)
elseif t > 2 * max_Iter / 3
for i = 1:N
% Levy random number
RL = 0.05 * jLevy(beta,dim);
for d = 1:dim
stepsize = RL(d) * (RL(d) * Xe(i,d) - X(i,d));
X(i,d) = Xe(i,d) + P * CF * stepsize;
end
% Boundary
XB = X(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
X(i,:) = XB;
end
end
% Fitness
for i = 1:N
fit(i) = fun(feat,label,(X(i,:) > thres),opts);
% Best
if fit(i) < fitG
fitG = fit(i);
Xgb = X(i,:);
end
end
% Memory saving
for i = 1:N
if fitM(i) < fit(i)
fit(i) = fitM(i);
X(i,:) = Xmb(i,:);
end
end
Xmb = X;
fitM = fit;
% Eddy formation and FADs effect (16)
if rand() <= FADs
for i = 1:N
% Compute U
U = rand(1,dim) < FADs;
for d = 1:dim
R = rand();
X(i,d) = X(i,d) + CF * (lb + R * (ub - lb)) * U(d);
end
% Boundary
XB = X(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
X(i,:) = XB;
end
else
% Uniform random number [0,1]
r = rand();
% Define two prey randomly
Xr1 = X(randperm(N),:);
Xr2 = X(randperm(N),:);
for i = 1:N
for d = 1:dim
X(i,d) = X(i,d) + (FADs * (1 - r) + r ) * ...
(Xr1(i,d) - Xr2(i,d));
end
% Boundary
XB = X(i,:); XB(XB > ub) = ub; XB(XB < lb) = lb;
X(i,:) = XB;
end
end
% Save
curve(t) = fitG;
fprintf('\nIteration %d Best (MPA)= %f',t,curve(t))
t = t + 1;
end
% Select features based on selected index
Pos = 1:dim;
Sf = Pos((Xgb > thres) == 1);
sFeat = feat(:,Sf);
% Store results
MPA.sf = Sf;
MPA.ff = sFeat;
MPA.nf = length(Sf);
MPA.c = curve;
MPA.f = feat;
MPA.l = label;
end
% Levy distribution
function LF = jLevy(beta,dim)
num = gamma(1 + beta) * sin(pi * beta / 2);
deno = gamma((1 + beta) / 2) * beta * 2 ^ ((beta - 1) / 2);
sigma = (num / deno) ^ (1 / beta);
u = random('Normal',0,sigma,1,dim);
v = random('Normal',0,1,1,dim);
LF = u ./ (abs(v) .^ (1 / beta));
end
9 个评论
Image Analyst
2022-12-26
It really baffles and stuns me that you won't even answer @Walter Roberson's simple, very direct and very explicit question about what you see when you do this in the command window:
which -all cvpartition
Can you explain why you refuse to do that so he can help you?
回答(2 个)
Image Analyst
2022-12-23
Early in the program (line 8) you do this
HO = cvpartition(trainingLabels,'HoldOut',.2);
however you never defined/assigned trainingLabels, so it throws an error:
Unrecognized function or variable 'trainingLabels'.
Error in test7 (line 8)
HO = cvpartition(trainingLabels,'HoldOut',.2);
You need to assign trainingLabels to something.
0 个评论
Image Analyst
2022-12-27
OK, so you seem to have the cvpartition.m file but it's not in the place where mine is. You seem to have the toolbox installed off the root of your E drive instead of under "C:\Program Files\MATLAB\R2022b\toolbox" like we do.
>> which -all cvpartition
C:\Program Files\MATLAB\R2022b\toolbox\stats\stats\cvpartition.m % cvpartition constructor
C:\Program Files\MATLAB\R2022b\toolbox\stats\bigdata\@tall\cvpartition.m % tall method
Do you know why that is?
What does this show for you?
[status, result] = system('set PATH');
executableFolder = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'))
I see
> [status, result] = system('set PATH');
executableFolder = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'))
executableFolder =
'C:\Program Files\MATLAB\R2022b\bin\win64'
And if you go to that 'C:\Program Files\MATLAB\R2022b' folder, do you see a toolbox subfolder there?
If you need immediate help, call the tech support telephone line.
3 个评论
Image Analyst
2022-12-28
It could be due to your using a 7 year old version, r2016a. I suggest you upgrade.
If you need immediate help, call the tech support telephone line.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Big Data Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!