打印矩阵很好地使用pandas python外观的异常

时间:2017-01-23 10:11:47

标签: python-2.7 pandas matrix

我正在尝试使用pandas很好地打印矩阵。 但我有一个问题 我有这个矩阵:

[[  0  -2  -4  -6  -8 -10]
 [ -2   1  -1  -3  -5  -7]
 [ -4  -1   2   0  -2  -4]
 [ -6  -3   0   1   1  -1]
 [ -8  -5  -2  -1   0   2]]

我填写了这段代码:

def NW(s1,s2,match = 1,mismatch = -1, gap = -2):
    penalty = {'MATCH': match, 'MISMATCH': mismatch, 'GAP': gap} #A dictionary for all the penalty valuse.
    n = len(s1) + 1 #The dimension of the matrix columns.
    m = len(s2) + 1 #The dimension of the matrix rows.
    al_mat = np.zeros((m,n),dtype = int) #Initializes the alighment matrix with zeros.

    #Scans all the first rows element in the matrix and fill it with "gap penalty"
    for i in range(m):
        al_mat[i][0] = penalty['GAP'] * i
        p_mat[i][0] = 'V'
    #Scans all the first columns element in the matrix and fill it with "gap penalty"
    for j in range (n):
        al_mat[0][j] = penalty['GAP'] * j
    #Fill the matrix with the correct values.
    for i in range(1,m):
        for j in range(1,n):
            di = al_mat[i-1][j-1] + Diagonal(s1[j-1],s2[i-1],penalty) #The value for match/mismatch -  diagonal.
            ho = al_mat[i][j-1] + penalty['GAP'] #The value for gap - horizontal.(from the left cell)
            ve = al_mat[i-1][j] + penalty['GAP'] #The value for gap - vertical.(from the upper cell)
            al_mat[i][j] = max(di,ho,ve) #Fill the matrix with the maximal value.(based on the python default maximum)

    return al_mat

我希望矩阵看起来像这样:

     T  C  G  C  A
  0 -2 -4 -6 -8 -10
T -2  1 -1 -3 -5 -7
C -4 -1  2  0 -2 -4
C -6 -3  0  1  1 -1
A -8 -5 -2 -1  0  2

或者像这样:

      T  C  G  C  A
  [0 -2 -4 -6 -8 -10]
T [-2  1 -1 -3 -5 -7]
C [-4 -1  2  0 -2 -4]
C [-6 -3  0  1  1 -1]
A [-8 -5 -2 -1  0  2]

我试图写这段代码:

import pandas as pd
col1 = [' ', 'T', 'C', 'G', 'C', 'A']
col2 = [' ', 'T', 'C', 'C', 'A']
df = pd.DataFrame(mat,index = col2, columns = col1)
print df

但我和#34; v得到了这个错误:

    df = pd.DataFrame(mat,index = col2, columns = col1)
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 163, in __init__
    copy=copy)
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 224, in _init_ndarray
    return BlockManager([block], [columns, index])
  File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 237, in __init__
    self._verify_integrity()
  File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 313, in _verify_integrity
    union_items = _union_block_items(self.blocks)
  File "C:\Python27\lib\site-packages\pandas\core\internals.py", line 906, in _union_block_items
    raise Exception('item names overlap')
Exception: item names overlap

所以只有当我换了1个字母时它才起作用,所以我得到了:

       T   B   G   C   A  
   0   -2  -4  -6  -8  -10
T  -2  1   -1  -3  -5  -7 
C  -4  -1  2   0   -2  -4 
C  -6  -3  0   1   1   -1 
A  -8  -5  -2  -1  0   2  

但矩阵的布局仍然不太好。  我该如何解决这些问题呢?

1 个答案:

答案 0 :(得分:0)

根据您提供的矩阵判断,我猜测您的问题是您尝试使用的矩阵不包含逗号,这就是您的原因所在'得到错误。尝试将mat设置为等于:

[[0,-2,-4,-6,-8,-10],
 [-2,1,-1,-3,-5,-7],
 [-4,-1,2,0,-2,-4],
 [-6,-3,0,1,1,-1],
 [-8,-5,-2,-1,0,2]]