朴素贝叶斯和条件概率计算

时间:2013-11-02 16:29:15

标签: matlab classification probability bayesian-networks

这里是我的情况,我知道一些概率论,我知道贝叶斯定理等等。但是把它放到matlab中我就失去了如何计算条件。

我正在做的是虹膜数据集的分类,这个:

    5.1000    3.5000    1.4000    0.2000    1.0000
    4.9000    3.0000    1.4000    0.2000    1.0000
    4.7000    3.2000    1.3000    0.2000    1.0000
    4.6000    3.1000    1.5000    0.2000    1.0000
    5.0000    3.6000    1.4000    0.2000    1.0000
    5.4000    3.9000    1.7000    0.4000    1.0000
    4.6000    3.4000    1.4000    0.3000    1.0000
    5.0000    3.4000    1.5000    0.2000    1.0000
    4.4000    2.9000    1.4000    0.2000    1.0000
    4.9000    3.1000    1.5000    0.1000    1.0000
    5.4000    3.7000    1.5000    0.2000    1.0000
    4.8000    3.4000    1.6000    0.2000    1.0000
    4.8000    3.0000    1.4000    0.1000    1.0000
    4.3000    3.0000    1.1000    0.1000    1.0000
    5.8000    4.0000    1.2000    0.2000    1.0000
    5.7000    4.4000    1.5000    0.4000    1.0000
    5.4000    3.9000    1.3000    0.4000    1.0000
    5.1000    3.5000    1.4000    0.3000    1.0000
    5.7000    3.8000    1.7000    0.3000    1.0000
    5.1000    3.8000    1.5000    0.3000    1.0000
    5.4000    3.4000    1.7000    0.2000    1.0000
    5.1000    3.7000    1.5000    0.4000    1.0000
    4.6000    3.6000    1.0000    0.2000    1.0000
    5.1000    3.3000    1.7000    0.5000    1.0000
    4.8000    3.4000    1.9000    0.2000    1.0000
    5.0000    3.0000    1.6000    0.2000    1.0000
    5.0000    3.4000    1.6000    0.4000    1.0000
    5.2000    3.5000    1.5000    0.2000    1.0000
    5.2000    3.4000    1.4000    0.2000    1.0000
    4.7000    3.2000    1.6000    0.2000    1.0000
    4.8000    3.1000    1.6000    0.2000    1.0000
    5.4000    3.4000    1.5000    0.4000    1.0000
    5.2000    4.1000    1.5000    0.1000    1.0000
    5.5000    4.2000    1.4000    0.2000    1.0000
    4.9000    3.1000    1.5000    0.1000    1.0000
    5.0000    3.2000    1.2000    0.2000    1.0000
    5.5000    3.5000    1.3000    0.2000    1.0000
    4.9000    3.1000    1.5000    0.1000    1.0000
    4.4000    3.0000    1.3000    0.2000    1.0000
    5.1000    3.4000    1.5000    0.2000    1.0000
    5.0000    3.5000    1.3000    0.3000    1.0000
    4.5000    2.3000    1.3000    0.3000    1.0000
    4.4000    3.2000    1.3000    0.2000    1.0000
    5.0000    3.5000    1.6000    0.6000    1.0000
    5.1000    3.8000    1.9000    0.4000    1.0000
    4.8000    3.0000    1.4000    0.3000    1.0000
    5.1000    3.8000    1.6000    0.2000    1.0000
    4.6000    3.2000    1.4000    0.2000    1.0000
    5.3000    3.7000    1.5000    0.2000    1.0000
    5.0000    3.3000    1.4000    0.2000    1.0000
    7.0000    3.2000    4.7000    1.4000    2.0000
    6.4000    3.2000    4.5000    1.5000    2.0000
    6.9000    3.1000    4.9000    1.5000    2.0000
    5.5000    2.3000    4.0000    1.3000    2.0000
    6.5000    2.8000    4.6000    1.5000    2.0000
    5.7000    2.8000    4.5000    1.3000    2.0000
    6.3000    3.3000    4.7000    1.6000    2.0000
    4.9000    2.4000    3.3000    1.0000    2.0000
    6.6000    2.9000    4.6000    1.3000    2.0000
    5.2000    2.7000    3.9000    1.4000    2.0000
    5.0000    2.0000    3.5000    1.0000    2.0000
    5.9000    3.0000    4.2000    1.5000    2.0000
    6.0000    2.2000    4.0000    1.0000    2.0000
    6.1000    2.9000    4.7000    1.4000    2.0000
    5.6000    2.9000    3.6000    1.3000    2.0000
    6.7000    3.1000    4.4000    1.4000    2.0000
    5.6000    3.0000    4.5000    1.5000    2.0000
    5.8000    2.7000    4.1000    1.0000    2.0000
    6.2000    2.2000    4.5000    1.5000    2.0000
    5.6000    2.5000    3.9000    1.1000    2.0000
    5.9000    3.2000    4.8000    1.8000    2.0000
    6.1000    2.8000    4.0000    1.3000    2.0000
    6.3000    2.5000    4.9000    1.5000    2.0000
    6.1000    2.8000    4.7000    1.2000    2.0000
    6.4000    2.9000    4.3000    1.3000    2.0000
    6.6000    3.0000    4.4000    1.4000    2.0000
    6.8000    2.8000    4.8000    1.4000    2.0000
    6.7000    3.0000    5.0000    1.7000    2.0000
    6.0000    2.9000    4.5000    1.5000    2.0000
    5.7000    2.6000    3.5000    1.0000    2.0000
    5.5000    2.4000    3.8000    1.1000    2.0000
    5.5000    2.4000    3.7000    1.0000    2.0000
    5.8000    2.7000    3.9000    1.2000    2.0000
    6.0000    2.7000    5.1000    1.6000    2.0000
    5.4000    3.0000    4.5000    1.5000    2.0000
    6.0000    3.4000    4.5000    1.6000    2.0000
    6.7000    3.1000    4.7000    1.5000    2.0000
    6.3000    2.3000    4.4000    1.3000    2.0000
    5.6000    3.0000    4.1000    1.3000    2.0000
    5.5000    2.5000    4.0000    1.3000    2.0000
    5.5000    2.6000    4.4000    1.2000    2.0000
    6.1000    3.0000    4.6000    1.4000    2.0000
    5.8000    2.6000    4.0000    1.2000    2.0000
    5.0000    2.3000    3.3000    1.0000    2.0000
    5.6000    2.7000    4.2000    1.3000    2.0000
    5.7000    3.0000    4.2000    1.2000    2.0000
    5.7000    2.9000    4.2000    1.3000    2.0000
    6.2000    2.9000    4.3000    1.3000    2.0000
    5.1000    2.5000    3.0000    1.1000    2.0000
    5.7000    2.8000    4.1000    1.3000    2.0000
    6.3000    3.3000    6.0000    2.5000    3.0000
    5.8000    2.7000    5.1000    1.9000    3.0000
    7.1000    3.0000    5.9000    2.1000    3.0000
    6.3000    2.9000    5.6000    1.8000    3.0000
    6.5000    3.0000    5.8000    2.2000    3.0000
    7.6000    3.0000    6.6000    2.1000    3.0000
    4.9000    2.5000    4.5000    1.7000    3.0000
    7.3000    2.9000    6.3000    1.8000    3.0000
    6.7000    2.5000    5.8000    1.8000    3.0000
    7.2000    3.6000    6.1000    2.5000    3.0000
    6.5000    3.2000    5.1000    2.0000    3.0000
    6.4000    2.7000    5.3000    1.9000    3.0000
    6.8000    3.0000    5.5000    2.1000    3.0000
    5.7000    2.5000    5.0000    2.0000    3.0000
    5.8000    2.8000    5.1000    2.4000    3.0000
    6.4000    3.2000    5.3000    2.3000    3.0000
    6.5000    3.0000    5.5000    1.8000    3.0000
    7.7000    3.8000    6.7000    2.2000    3.0000
    7.7000    2.6000    6.9000    2.3000    3.0000
    6.0000    2.2000    5.0000    1.5000    3.0000
    6.9000    3.2000    5.7000    2.3000    3.0000
    5.6000    2.8000    4.9000    2.0000    3.0000
    7.7000    2.8000    6.7000    2.0000    3.0000
    6.3000    2.7000    4.9000    1.8000    3.0000
    6.7000    3.3000    5.7000    2.1000    3.0000
    7.2000    3.2000    6.0000    1.8000    3.0000
    6.2000    2.8000    4.8000    1.8000    3.0000
    6.1000    3.0000    4.9000    1.8000    3.0000
    6.4000    2.8000    5.6000    2.1000    3.0000
    7.2000    3.0000    5.8000    1.6000    3.0000
    7.4000    2.8000    6.1000    1.9000    3.0000
    7.9000    3.8000    6.4000    2.0000    3.0000
    6.4000    2.8000    5.6000    2.2000    3.0000
    6.3000    2.8000    5.1000    1.5000    3.0000
    6.1000    2.6000    5.6000    1.4000    3.0000
    7.7000    3.0000    6.1000    2.3000    3.0000
    6.3000    3.4000    5.6000    2.4000    3.0000
    6.4000    3.1000    5.5000    1.8000    3.0000
    6.0000    3.0000    4.8000    1.8000    3.0000
    6.9000    3.1000    5.4000    2.1000    3.0000
    6.7000    3.1000    5.6000    2.4000    3.0000
    6.9000    3.1000    5.1000    2.3000    3.0000
    5.8000    2.7000    5.1000    1.9000    3.0000
    6.8000    3.2000    5.9000    2.3000    3.0000
    6.7000    3.3000    5.7000    2.5000    3.0000
    6.7000    3.0000    5.2000    2.3000    3.0000
    6.3000    2.5000    5.0000    1.9000    3.0000
    6.5000    3.0000    5.2000    2.0000    3.0000
    6.2000    3.4000    5.4000    2.3000    3.0000
    5.9000    3.0000    5.1000    1.8000    3.0000

现在我知道我可以通过计算然后除以总数得到先验:

load('iris.data');
iris
classes = iris(:, 5);

%priors by counting

class1 = (classes == 1);
prior_1 = sum(class1)./length(class1);
class2 = (classes == 2);
prior_2  = sum(class2)./length(class2);
class3 = (classes == 3);
prior_3 = sum(class3)./length(class3);

%% Now find a way to get the likelihood of the data given the class p(x|c)
% to apply bayes p(c|x_i) = p(x_i|c)p(c)/p(x_i){p(x_i|c_1)p(c_1) +
% p(x_i|c_2)p(c_2) + p(x_i|c_3)p(c_3)}

但是我如何才能获得这种可能性,我觉得无法计算,至少我认为。那我该怎么办?请帮助,我完全迷失了(:谢谢。

1 个答案:

答案 0 :(得分:1)

正态分布:

%% Load Fisher's Iris data set
load iris.dat;
iris;
number_of_features = 4;
classes = iris(:, number_of_features + 1);
number_of_classes = length(unique(classes));

%% Priors by counting
for class_number = 1:number_of_classes
    class{class_number} = (classes == class_number);
    prior{class_number} = sum(class{class_number})./length(class{class_number});
end

%% Compute likelihood 
% Assumption: distributions are Gaussian.
% The probability density function for the normal distribution is defined 
% by two parameters (mean and standard deviation)
% (We could shorten the code by using the 'By' parameter of fitdist())
for class_number = 1:number_of_classes
    likelihood{class_number} = struct;
    for feature_number = 1:number_of_features
        likelihood{class_number}.pd{feature_number} = fitdist(iris(find(iris(:, 5) == class_number), feature_number),'Normal');
    end
end

%% Compute posteriors for all flowers (= making predictions)
% Note that we don't take into account the predictor prior probability
% because it won't impact the class we choose.
posterior = zeros(length(iris), number_of_classes);
for flower_number = 1:length(iris)
    flower = iris(flower_number, 1:number_of_features);
    for class_number = 1:number_of_classes
        flower_likelihood = 1;
        for feature_number = 1:number_of_features
            pd = likelihood{class_number}.pd{feature_number};
            flower_likelihood = flower_likelihood * pdf(pd,flower(feature_number));
        end  % Naive Bayes -> strong (naive) independence assumptions.  
        posterior(flower_number, class_number) = flower_likelihood * prior{class_number};
    end
end

% PS: A nice tutorial: http://www.saedsayad.com/naive_bayesian.htm