Problem in Navie Bayes theorem code

조회 수: 9 (최근 30일)
Ali Asghar
Ali Asghar 2019년 12월 12일
댓글: saurabh kumar 2023년 5월 26일
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{:});

답변 (2개)

Hiro Yoshino
Hiro Yoshino 2019년 12월 12일
'ClassNames',{'HandGrip','HandOpen','HandRest','WristExtension','WristFlexion'});
the number of class labels is 5 but I guess yours is 17?
  댓글 수: 1
Ali Asghar
Ali Asghar 2019년 12월 12일
In 1-16 Columns there are different features values of emg signals like RMS, VAR etc and in 17th column there is respective output like handopen, handclose etc
In 1-32 rows the 17th columns have Handopen
in 33-64 the the 17th columns have Handclose
etc

댓글을 달려면 로그인하십시오.


Hiro Yoshino
Hiro Yoshino 2019년 12월 12일
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
Ali Asghar
Ali Asghar 2019년 12월 14일
What i can i do now, other than Gaussian...
saurabh kumar
saurabh kumar 2023년 5월 26일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Naive Bayes에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by