将矩阵每行的最大值相加

2024-09-30 00:28:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码

 #!/usr/bin/python
  import sys
  import re
  import string
  indexfile="ABC.txt"
  for line in open(indexfile,'rU').xreadlines():
    t = string.split(line,'\t')
    id = t[0];
    gene=t[1];
    pwmfile=id+'.txt'
    matrix_file = open(pwmfile, "rU")
    matrix = matrix_file.readlines()
    vals = [line[1:] for line in matrix[1:]]
    newpwmfile=id+'_formated.txt'
    ea=open(newpwmfile,'w')
    ea.seek(0)
    ea.write(">"+"ASTTCCTCTT "+gene)
    ea.writelines([line.lstrip('\t') for line in vals])
    ea.close()

下面是我得到的矩阵:

^{pr2}$

我想从每一行中找出最大值,然后除以0.25,然后对每一行求和划船。还有我想得到一个字符串,它为字符串中的每个位置指定字母,例如,如果最大值在第三列中,那么在第二行的第一列中是G,然后G等等,然后把它们串联起来,这样我就可以在找到每行的最大分数的同时得到一个类似GAUUCCCG的字符串。在


Tags: 字符串inimporttxtidforstringru
1条回答
网友
1楼 · 发布于 2024-09-30 00:28:48
import numpy as np

#make some fake data
m = np.random.random((8,4))

#get the sum you described
print 0.25*np.max(m, axis=1).sum()

#next, get the index the max value, for each row
xs = np.argmax(m, axis=1)
#use these as indexes into a string, e.g.
s = "GAUC"
print "".join(s[x] for x in xs)

相关问题 更多 >

    热门问题