Python:元素明智的除法运算符错误

时间:2016-02-24 09:36:39

标签: python for-loop numpy division elementwise-operations

我想知道有没有更好的方法在python中执行元素智能除法运算符。下面的代码假设执行带有B1行的A1和带有B2行的A2,因此我的预期输出只有两行。但是,除法部分是B1,B1是A1,A1是B2,A2是B1,A2是B2。任何人都可以帮助我吗?

二进制文件用于使用1000,0100,0010,0001的A,C,G,T表示。     除法文件有四列,每列各有A,C,G,T,因此得到的值     早先必须相应划分。

代码

import numpy as np
from numpy import genfromtxt
import csv
csvfile = open('output.csv', 'wb')
writer = csv.writer(csvfile)

#open csv file into arrays
with open('binary.csv') as actg:
    actg=actg.readlines()
    with open('single.csv') as single:
        single=single.readlines()
        with open('division.csv') as division:
            division=division.readlines()

            # Converting binary line and single line into 3 rows and 4 columns 
            # binary values using reshape
            for line in actg:
                myarray = np.fromstring(line, dtype=float, sep=',')                
                myarray = myarray.reshape((-1, 3, 4))
                for line2 in single:                    
                    single1 = np.fromstring(line2, dtype=float, sep=',')
                    single1 = single1.reshape((-1, 4))
                    # This division is in 2 rows and 4 column: first column 
                    # represents 1000, 2nd-0100, 3rd-0010, 4th-0001 in the
                    # binary.csv. Therefore the division part where 1000's
                    # value should be divided by 1st column, 0010 should be
                    # divided by 3rd column value
                    for line1 in division:
                        division1 = np.fromstring(line1, dtype=float, sep=',')
                        m=np.asmatrix(division1)
                        m=np.array(m)
                        res2 = (single1[np.newaxis,:,:] / m[:,np.newaxis,:] * myarray).sum(axis=-1)                        
                        print(res2)
                        writer.writerow(res2)


csvfile.close()

binary.csv

0,1,0,0,1,0,0,0,0,0,0,1
0,0,1,0,1,0,0,0,1,0,0,0

single.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

division.csv

0.4,0.5,0.7,0.1
0.2,0.8,0.9,0.3

预期输出

 0.44,0.3,6.5
 0.26,0.6,2.2

实际输出

0.44,0.3,6.5
0.275,0.6,2.16666667
0.32857143,0.3,1.1       
0.25555556,0.6,2.2       

错误说明

让分区文件如下:

A,B,C,D
E,F,G,H

让单个和二进制计算结果如下:

1,3,4
2,2,1

将数字1,2,3,4分配给位置A,B,C,D和下一行E,F,G,H

1/A,3/C,4/D
2/F,2/F,1/E

其中1除以A,3除以C,依此类推。基本上这就是代码可以做的事情。不幸的是,分裂部分碰巧就像之前描述的那样。 221与BBC一起运行,134运行EGH,因此输出有4行,这不是我想要的。

2 个答案:

答案 0 :(得分:1)

我不知道这是否是您正在寻找的,但这是获得您想要的(我认为)的简短方法:

queue:work

输出:

import numpy as np

binary = np.genfromtxt('binary.csv', delimiter = ',').reshape((2, 3, 4))
single = np.genfromtxt('single.csv', delimiter = ',').reshape((1, 3, 4))
divisi = np.genfromtxt('division.csv', delimiter = ',').reshape((2, 1, 4))

print(np.sum(single / divisi * binary, axis = -1))

答案 1 :(得分:1)

程序的输出看起来像这样:

myarray
[ 0.  1.  0.  0.  1.  0.  0.  0.  0.  0.  0.  1.]

[[[ 0.  1.  0.  0.]
  [ 1.  0.  0.  0.]
  [ 0.  0.  0.  1.]]]

single1
[ 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.28  0.22  0.23  0.27]
 [ 0.12  0.29  0.34  0.21]
 [ 0.44  0.56  0.51  0.65]]

    division
    [ 0.4  0.5  0.7  0.1]    
    m
    [[ 0.4  0.5  0.7  0.1]]    
    res2
    [[ 0.44  0.3   6.5 ]]

    division
    [ 0.2  0.8  0.9  0.3]    
    m
    [[ 0.2  0.8  0.9  0.3]]        
    res2
    [[ 0.275       0.6         2.16666667]]

myarray
[ 0.  0.  1.  0.  1.  0.  0.  0.  1.  0.  0.  0.]

[[[ 0.  0.  1.  0.]
  [ 1.  0.  0.  0.]
  [ 1.  0.  0.  0.]]]


single1
[ 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.28  0.22  0.23  0.27]
 [ 0.12  0.29  0.34  0.21]
 [ 0.44  0.56  0.51  0.65]]

    division
    [ 0.4  0.5  0.7  0.1]
    m
    [[ 0.4  0.5  0.7  0.1]]
    res2
    [[ 0.32857143  0.3         1.1       ]]

    division
    [ 0.2  0.8  0.9  0.3]
    m
    [[ 0.2  0.8  0.9  0.3]]
    res2
    [[ 0.25555556  0.6         2.2       ]]

因此,考虑到这一点,看起来你输出的最后两行,你没想到的那一行是由 binary.csv 中的第二行引起的。因此,如果您不想在结果中使用4行,请不要在计算中使用该行。