繁体   English   中英

如何从数据计算条件概率

[英]How do I calculate conditional probabilities from data

我在Matlab中做了一个天真的贝叶斯,直到他们说我需要条件概率才一直很好。 现在我知道条件p(A | B)= P(A和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,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1,2,2,2,1,3,1,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的第一列? 我将它们更改为列的平均值,类的优先级可以通过计算为第一列的类#/总数来完成。 这很简单,但有条件的?

---class---
      /   /   \    \
     x1  x2...x_i  xn

贝叶斯p(c | x)= p(x | c)p(c)/ p(x)。 谢谢。

编辑:我认为我需要的是,有人可以解释从数据中获取条件的过程,如果可能的话,如果我需要做CPT并给我指导如何做,那我就是大多数程序员。

这是一个强力代码。 只是为了确定我们正在谈论同样的问题。

% 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, :, :))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM