python - 条件概率 - Python

我正在研究这个 python 问题:

Given a sequence of the DNA bases {A, C, G, T}, stored as a string, returns a conditional probability table in a data structure such that one base (b1) can be looked up, and then a second (b2), to get the probability p(b2 | b1) of the second base occurring immediately after the first. (Assumes the length of seq is >= 3, and that the probability of any b1 and b2 which have never been seen together is 0. Ignores the probability that b1 will be followed by the end of the string.)

You may use the collections module, but no other libraries.

但是我遇到了障碍:

word = 'ATCGATTGAGCTCTAGCG'

def dna_prob2(seq):
    tbl = dict()
    levels = set(word)
    freq = dict.fromkeys(levels, 0)
    for i in seq:
        freq[i] += 1
    for i in levels:
        tbl[i] = {x:0 for x in levels}
    lastlevel = ''
    for i in tbl:
        if lastlevel != '':
             tbl[lastlevel][i] += 1
        lastlevel = i
    for i in tbl:
        print(i,tbl[i][i] / freq[i])
    return tbl

tbl['T']['T'] / freq[i] 

基本上,最终结果应该是您在上面看到的最后一行 tbl。但是,当我尝试在 print(i,tbl[i][i]/freq[i) 中执行此操作并运行 dna_prob2(word) 时,我得到 0.0 s 代表一切。

想知道这里是否有人可以提供帮助。

谢谢!

最佳答案

我不确定您的代码在做什么,但这行得通:

def makeprobs(word):
  singles = {}
  probs = {}
  thedict={}
  ll = len(word)
  for i in range(ll-1):
    x1 = word[i]
    x2 = word[i+1]
    singles[x1] = singles.get(x1, 0)+1.0
    thedict[(x1, x2)] = thedict.get((x1, x2), 0)+1.0
  for i in thedict:
    probs[i] = thedict[i]/singles[i[0]]
  return probs

https://stackoverflow.com/questions/62354498/

相关文章:

html - Nodejs如何从ejs文件生成pdf文件

node.js - Node http.大文件传输失败并显示 'ERR_STREAM_PREMATU

javascript - 匿名访问如何跟踪?智威汤逊/ cookies / session ?

python - 如何在 Excel 的 Power Query 中运行 Python 脚本

flutter - 如何在 Flutter 的 initState 中获取当前的 App Local

python-3.x - pynput pip3 安装错误 : Could not find a v

javascript - react-router-dom 不工作只是渲染 "/"

javascript - 如何在对象中保留一些深层 Prop ?

python - `join` 或 `format` 字符串哪个更好?

ruby-on-rails - 如何在 ruby​​ 中找到调用者类?