Problem in Navie Bayes theorem code
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dear all
I used the below code on 160x5 file and found it work correctly but now i change the file from 160x5 to 160x17 then it show error. Kindly look into it.
clear all, close all, clc
load('FeatureAngle000Musscle1.mat') % 160x17 size
X = FeaturesAngle000Muscle1(:,1:16);
Y = FeaturesAngle000Muscle1(:,17);
rng(1); % For reproducibility
Mdl2 = fitcnb(X,Y,...
'DistributionNames',{'normal','normal','kernel','kernel' 'normal','normal','kernel','kernel' 'normal','normal','kernel','kernel' 'normal','normal','kernel','kernel'},...
'ClassNames',{'HandGrip','HandOpen','HandRest','WristExtension','WristFlexion'});
Mdl2.DistributionParameters{1,2}
isLabels2 = resubPredict(Mdl2); % it generate the output/response of model
ConfusionMat2 = confusionchart(table2array(Y),isLabels2); % Y need to convert
showing error
Error using ClassificationNaiveBayes/fitNonMNDists (line 222)
A normal distribution cannot be fit for the combination of class HandGrip and predictor LD. The data has zero variance.
Error in ClassificationNaiveBayes (line 104)
this.DistributionParameters = fitNonMNDists(this);
Error in classreg.learning.FitTemplate/fit (line 258)
[varargout{1:nargout}] = this.MakeFitObject(X,Y,W,this.ModelParams,fitArgs{:});
Error in ClassificationNaiveBayes.fit (line 132)
this = fit(temp,X,Y);
Error in fitcnb (line 252)
this = ClassificationNaiveBayes.fit(X,Y,RemainingArgs{:});
0 comentarios
Respuestas (2)
Hiro Yoshino
el 12 de Dic. de 2019
'ClassNames',{'HandGrip','HandOpen','HandRest','WristExtension','WristFlexion'});
the number of class labels is 5 but I guess yours is 17?
Hiro Yoshino
el 12 de Dic. de 2019
I guess one of the distributions does not match what it is, i.e., variance is zero. In this case, Gaussian distributions cannot be fit anyway...
2 comentarios
saurabh kumar
el 26 de Mayo de 2023
The error is due to the wrong distribution in the data sample. It occurs when the variance between the feature value is repeatedly 0 and gaussian distribution will find itself helpless in order to draw a distribution diagram. As for example, look into the attached screen shot. A dataset in my work contained 0s in most of the columns and I was validating the features via Naive Bayes algorithm, I got the same error .....
"A normal distribution cannot be fit for the combination of class 1 and predictor x3. The data has zero variance." .
There are two ways to solve the problem
a) Change your classifier (Use knn or M-SVM instead)
b) Use the following code to reduce your variance
function [odata] = reducevariance(data)
%REDUCEVARIANCE Summary of this function goes here
% Detailed explanation goes here
[rows,cols]=size(data); % find size of the data
odata=[]; % create updated data matrix
colcount=0; % column counter for new data
for j=1:cols
current_col_values=data(:,j); % take current col values
total_samples=numel(current_col_values); % find total number of samples
f=find(current_col_values==0); % find 0s in total samples
percentageofzeros=(numel(f)/total_samples)*100; % find % of existance
if percentageofzeros>20 % if it is greater than 20% remove the col
else
colcount=colcount+1; % else add the col to updated data
odata(1:rows,colcount)=data(1:rows,j);
end
end
end
For any other query, mail me at director.smarttech@gmail.com
Ver también
Categorías
Más información sobre Naive Bayes en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!