我想对其字母表进行除法。举个例子如下:
给出的二进制文件是 csv 格式:
A=1000, C=0100, G=0010, T=0001
binary.csv:CAT、GAA
0,1,0,0,1,0,0,0,0,0,0,1
0,0,1,0,1,0,0,0,1,0,0,0
binary.csv 需要与 csv 文件中的单行值相乘。
单.csv:
0.28,0.22,0.23,0.27,0.12,0.29,0.34,0.21,0.44,0.56,0.51,0.65
下面的代码对文件和输出中的值进行乘法运算:
0.22,0.12,0.65
0.23,0.12,0.44
代码
import csv
with open('single.csv', 'rb') as csvfile:
for row in csv.reader(csvfile, delimiter=','):
reals = row
with open('binary.csv', 'rb') as csvfile:
pwreader = csv.reader(csvfile, delimiter=',')
with open('output.csv','wb') as testfile:
csv_writer=csv.writer(testfile)
for row in pwreader:
result = []
for i,b in enumerate(row):
if b == '1' :
result.append(reals[i])
csv_writer.writerow(result)
我有额外的 csv 文件,我想对先前的输出和相对于其字母表的值进行除法:
A C G T
0.4,0.5,0.7,0.1
0.2,0.8,0.9,0.3
CAT 的值除以 0.5,0.4,0.1,GAA 分别除以 0.9,0.2,0.2,这样我就可以得到一个全新的输出,如下所示:
0.44,0.3,6.5
0.26,0.6,2.2
在数组上使用 numpy 可能会解决这个问题,但是当用于数千个数据时,它可能不合适。当我尝试 60,000++ 数据时,发生内存不足。
谁能帮我?