discrete bayes algorithm - Programmers Heaven

Howdy, Stranger!

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

Categories

discrete bayes algorithm

nik812nik812 Posts: 1Member
hello,

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)')
end

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;
else
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;
end
end
end

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);

Regards,
Snigdha

Comments

  • giuggiug Posts: 217Member
    It gives this error because this condition (first row of your function) is satisfied:
    [code]
    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)')
    end[/code]

    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.