Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

discrete bayes algorithm

nik812nik812 Posts: 1Member

below is the code for discrete bayes function and when i try to load dataset with more than 2 rows of data, it shows an error saying that the features are not discrete?...any idea??..plz let me know...thank u

function D = Discrete_Bayes(feat_train, tar_train, cost, region, feat_test)

if (sum(sum(feat_train*10~=floor(feat_train*10))) ~= 0),
error('Features are not discrete (See the definition of discrete in the m-file)')

p0 = length(find(tar_train==0))/length(tar_train);

%Find how the features are distributed
N = 0;
unique_features = [];
counts = [];
for i = 1:size(feat_train,2),
data = feat_train(:,i);
indices = find(sum((data*ones(1,size(feat_train,2))-feat_train).^2)==0);
if isempty(unique_features),
unique_features(:,1) = data;
counts(1,1) = length(find(tar_train(indices) == 0));
counts(2,1) = length(find(tar_train(indices) == 1));
N = 2;
if isempty(find(sum((data*ones(1,size(unique_features,2))-unique_features).^2)==0)),
%Add this feature to the bank
unique_features(:,N) = data;
counts(1,N) = length(find(tar_train(indices) == 0));
counts(2,N) = length(find(tar_train(indices) == 1));
N = N + 1;

Px_given_w = (counts ./ (ones(2,1)* sum(counts)));

Pw_given_x = Px_given_w .* ([p0;1-p0]*ones(1,N-1));
Pw_given_x = Pw_given_x ./ (ones(2,1)*sum(Pw_given_x));

unique_targets = Pw_given_x(2,:) > Pw_given_x(1,:);

D = k_nearest_neighbor(unique_features,unique_targets,1,region);



  • giuggiug Posts: 217Member
    It gives this error because this condition (first row of your function) is satisfied:
    if (sum(sum(feat_train*10~=floor(feat_train*10))) ~= 0),
    error('Features are not discrete (See the definition of discrete in the m-file)')

    This means that in your feat_train matrix there are values with more than one numbers after the point.
    So, 0.2 is admitted, 0.23 is not admitted and gives the error.
Sign In or Register to comment.