如何从数据中计算条件概率

3

我正在使用Matlab做朴素贝叶斯算法,一切都很顺利,直到他们说我需要条件概率。现在我知道计算条件概率的公式是p(A|B) = P(A and B)/p(B),但当我有数据需要计算时我就不知所措了。数据如下:

1,0,3,0,?,0,2,2,2,1,1,1,1,3,2,2,1,2,2,0,2,2,2,2,1,2,2,2,3,2,1,1,1,3,3,2,2,1,2,2,2,1,2,2,2,2,2,2,2,2,2,2,1,1,1,2,2
1,0,3,3,1,0,3,1,3,1,1,1,1,1,3,3,1,2,2,0,0,2,2,2,1,2,1,3,2,3,1,1,1,3,3,2,2,2,1,2,2,2,1,2,2,1,2,2,2,2,2,2,2,2,1,2,2
1,0,3,3,2,0,3,3,3,1,1,1,0,3,3,3,1,2,1,0,0,2,2,2,1,2,2,3,2,3,1,3,3,3,1,2,2,1,2,2,2,1,2,2,1,2,2,2,2,2,2,2,2,2,2,1,2
1,0,2,3,2,1,3,3,3,1,2,1,0,3,3,1,1,2,2,0,0,2,2,2,2,1,3,2,3,3,1,3,3,3,1,1,1,1,2,2,2,2,1,2,2,2,1,2,2,2,2,2,2,2,2,2,2
1,0,3,2,1,1,3,3,3,2,2,2,1,1,2,2,2,2,2,0,0,2,2,2,1,1,2,3,2,2,1,1,1,3,2,1,2,2,1,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,1,2,2
1,0,3,3,2,0,3,3,3,1,2,2,0,3,3,3,2,2,1,0,0,1,2,2,2,1,3,3,1,2,2,3,3,3,2,1,2,2,1,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,1,2
1,0,3,2,1,0,3,3,3,1,2,1,2,3,3,3,3,2,2,0,0,2,2,2,2,1,3,2,2,2,2,3,3,3,2,1,1,2,2,1,2,1,2,2,2,2,1,2,2,2,2,1,2,2,2,1,2
1,0,2,2,1,0,3,1,3,3,3,3,2,1,3,3,1,2,2,0,0,1,1,2,1,2,1,3,2,1,1,3,3,3,2,2,1,2,1,2,2,1,2,2,2,1,2,2,2,1,2,2,2,2,1,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2,2,1,2
3,0,2,2,2,0,2,1,2,1,1,1,0,2,2,3,1,2,2,2,2,2,2,2,3,3,3,2,2,1,2,2,2,2,3,1,2,2,2,2,1,2,1,1,2,2,1,2,2,2,2,2,2,2,1,2,1
3,0,2,2,1,0,2,2,2,1,1,2,0,2,2,2,1,2,2,2,2,2,2,2,1,2,1,3,3,3,1,3,3,2,2,3,1,2,1,3,2,2,3,2,2,2,3,3,3,2,2,3,2,2,2,2,1
3,0,3,2,2,0,2,2,2,1,1,2,0,2,2,2,1,2,2,2,2,2,2,1,1,2,2,2,2,2,2,1,1,1,2,1,1,3,1,3,3,3,2,3,2,2,2,2,2,2,3,1,2,2,2,2,2
3,0,2,1,1,0,2,2,1,1,1,1,0,1,1,1,2,1,2,0,2,1,1,1,1,1,2,2,1,2,1,3,3,3,1,1,3,3,3,2,3,1,2,2,3,3,2,2,2,3,2,2,2,2,2,2,1
3,0,2,3,2,1,2,2,3,1,1,2,1,2,2,2,1,2,2,0,2,2,2,1,1,2,2,2,2,2,1,2,2,3,2,2,2,1,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,1,2,2
3,0,2,3,1,0,2,3,3,1,1,1,1,2,2,2,1,2,2,0,2,2,2,2,1,2,1,2,2,2,1,1,1,1,1,2,2,1,2,2,2,1,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2

在第一列从1到3的类别中,我会将它们更改为该列的平均值,而类别的先验可以通过计算第一列的类别数/总数来完成。这很简单,但条件概率怎么办呢?
       ---类别---
      /   /   \    \
     x1  x2...x_i  xn
贝叶斯公式p(c|x) = p(x|c)p(c)/p(x)。谢谢。
编辑:我认为我需要的是,有人能够解释从数据中获得条件概率的过程,如果可能的话,用苹果来说明如何进行CPT,并给我指点,我主要是程序员。

2
你能澄清一下吗:数据是一个32行57列的矩阵吗?第一列包含来自1到3的类别(c?)。其他列中包含什么(x?)? - user2875617
是的,该数据描述了三种病理性肺癌。作者没有提供有关个体变量或数据最初使用位置的信息。 就像你说的那样,32x57,第一列是类别,其他列没有说明,谢谢。 - Pedro.Alonso
我认为每一行都代表一个观测值,其中变量从x1到x56是按照第一列的类别进行排列的,比如最后一行属于第3类,x1=0等。 - Pedro.Alonso
我不明白:你有多少个变量?我认为你只有两个变量:3个类别和4个结果。是这样吗? - user2875617
不,这个数据有56个变量x1,...,x56,并且这个数据中的类别分为1、2、3,第一列是类别,其余是变量。我可以使用Matlab中的naivebayes.fit来完成,但我想了解它是如何完成的。谢谢。 - Pedro.Alonso
显示剩余3条评论
1个回答

4
这是一段暴力代码。只是为了确保我们谈论的问题完全一致。
% 1º step: Write the 32-by-56 matrix (excluding column of classes),
% replacing "?" with nans. Name it "data".

% Check the values in data:
unique(data(~isnan(data)))
% These are 0, 1, 2 and 3

% 2º step: Find mean of each variable without considering the NaN values
data_mean = nanmean(data);

% 3º step: replace missing values with class sample mean
data_new = data;
for hh = 1:56
  inds = isnan(data(:, hh));
  data_new(inds, hh) = data_mean(hh);
end
% Only NaN values have been replaced:
find(isnan(data(:)))    % indices of NaN values in data
find(data_new(:) ~= data(:))    % indices of data_new different from data

% 4º step: compute probabilities of outcome conditional to each class
n = [0, 9, 22, 32]; % indices for classes
probs = zeros(56, 3, 4);
for hh = 1:56 % for each variable
  for ii = 1:3 % for each class
    inds = (n(ii)+1):n(ii+1);
    for jj = 1:4 % for each outcome
      probs(hh, ii, jj) = sum(data(inds, hh) == jj-1);
    end
  end
end

% The conditional probability of the outcome conditional to the class, for
% the first variable is
squeeze(probs(1, :, :))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接