python - 条件概率计算

标签 python python-2.7

我正在尝试制作马尔可夫模型,与此相关,我需要计算某些字母的条件概率/质量概率。 我创建了字母频率的二元组。

我将如何设法计算我的字母的条件概率/质量概率?

最佳答案

计算条件概率的最简单方法是遍历模型中的案例,计算 1) 条件出现的案例和 2) 条件和目标字母出现的案例。条件概率是这两者的比率。

def cp(target, given):
    'Given is a one or two tuple and target is the letter following'
    g = 0.0
    g_and_t = 0.0
    n = len(given)
    for case, count in model.iteritems():
        if case[:n] == given:
            g += count
            if case[n] == target:
                g_and_t += count
    return g_and_t / g if g else 0.0

print cp(target='r', given=('f', 'o'))

关于python - 条件概率计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27993316/

相关文章:

python - 具有单个元素的元组的literal_eval

python-2.7 - GStreamer Python 错误 : 'gst.ElementNotFoundError: videoconvert'

python-2.7 - bitwise_and 它是如何工作的?

python-2.7 - Python XlsxWriter - 写入多张纸

python - wxPython 和高速公路 websockets

python - 我想在 PyQt4 的代码中使用键盘快捷键

python - 如何对 numpy 数组进行采样并有效地对每个样本执行计算?

Python,Quickly 和 Glade,在 TextView 中显示标准输出

python - 什么可能导致 NetworkX 和 PyGraphViz 单独工作但不能一起工作?

python - psycopg2 : cursor already closed