2013-11-02 88 views
2

那麼這裏是我的情況,我知道一些概率論,我知道貝葉斯定理等。但是把它放到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)} 

但是我怎樣才能得到這種可能性,我覺得至少我認爲它不能算數。那麼我該怎麼做呢?請幫助,我完全失去了(:謝謝

+0

這實在不是一個實際的規劃問題,因此不是一個問題,那麼這裏有一個提示:的可能性給定數據的模型在數值上與給定模型的數據的概率相同,因此,你需要的是一個生成模型,考慮你想要應用於你的數據的模型 –

+0

嗯,我想我會申請一個正態分佈,但條件定義爲p(A | B)= p(A&B)/ p(B),這讓我不知道它在哪裏,因爲我似乎沒有理解它 –

+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 
+0

謝謝,我會檢查它。 :) –