2017-01-23 9 views
1

私はパンダを使って素敵な行列を印刷しようとしています。 しかし、私はこの行列を持つ問題 を持っている:pandasを使用してプリントマトリックスをきれいにする例外の外観

 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 

か:

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 

私は行列は次のようになりたい:私はこのコードでいっぱいということ

[[ 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]] 

このように:

 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 

をしかし、私は "vはこのエラーを得た:私は1つの文字を変更したときに

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 

だから、それが唯一働いていますので、私が得た:

 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 

しかし、まだ行列のレイアウトはあまりよくありません。 これらの問題を解決するにはどうすればよいですか?

+0

「マット」はどのようなものか、どこから来ていますか?私は 'mat = np.random.randn(5,6)'(ダミーデータセットを得るためにnumpyをインポートしました)に設定してみました。うまくいきました。 – Clusks

+0

[ヘッダヘッダで素早くnumpy行列を印刷する方法 - python](http://stackoverflow.com/questions/41738174/how-to-print-numpy-matrix-nicely-with-text-headers-python) – Clusks

+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] – Elizabeth

答えて

0

あなたが提供したマトリックスで判断すると、使用しようとしているマトリックスにカンマが含まれていないことが原因であると推測します。 を次のように設定してみてください:

[[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]] 
+0

私はnumpyを使用しました。 マトリックスのマニュアルを設定しませんでした。 – Elizabeth

+0

numpyで正確に何をしているのかをコードに含めることができますか?それはもっと明確になります! – Clusks

+0

それは実際には針鳴き癖アルゴリズムなので、複雑です。 しかし、私はそれが理解されることを願っています。 – Elizabeth

関連する問題