在第一行写入带有字符串的ndarray,后跟一个数字矩阵

时间:2017-05-17 10:15:16

标签: numpy python-3.6

假设我创建了一个带有np.vstack的大矩阵,其中一个字符串向量作为第一行,后跟一个带数字的矩阵。如何保存/写入文件?并以一种很好的协调方式?

简化:

names = np.array(['NAME_1', 'NAME_2', 'NAME_3'])
floats = np.array([ 0.1234 ,  0.5678 ,  0.9123 ])

# 1) In order to vstack them, do I need to expand dimensions?

np.expand_dims(floats, axis=0)
np.expand_dims(names, axis=0)

Output = np.vstack((names,floats)) # so I get the following matrix
    NAME_1   NAME_2  NAME_3
    0.1234  0.5678   0.9123

# 2) How can a save/print into a file being able to modify the format of the numbers? 
# And keep the columns aligned? 
# Something like this: (taking into account that I have a lot of columns)
    NAME_1    NAME_2    NAME_3
    1.23e-1  5.67e-1    9.12e-1
# I tryied with:
np.savetxt('test.txt',  Matrix, fmt=' %- 1.8s' , delimiter='\t')

# But I can't change the format of the numbers.

提前致谢!!

2 个答案:

答案 0 :(得分:2)

显然我在kazemakase评论后找到了一个解决方案。对于大型矩阵来说效率非常低,但是它可以完成工作:

names  = np.array(['NAME_1', 'NAME_2', 'NAME_3'])
floats = np.array([[ 0.1234 ,  0.5678 ,  0.9123 ],
                  [ 0.1234 ,  -0.5678 ,  0.9123 ]])

with open('test.txt', 'w+') as f:
    for i in range(names.shape[0]) :
        f.write( '{:^15}'.format(names[i]))
    f.write( '{}'.format('\n'))   

    for i in range(floats.shape[0]) :
        for j in range(floats.shape[1]) :
            f.write( '{:^ 15.4e}'.format(floats[i,j]))
        f.write( '{}'.format('\n'))  

提供所需的输出:

    NAME_1         NAME_2         NAME_3     
   1.2340e-01     5.6780e-01     9.1230e-01  
   1.2340e-01    -5.6780e-01     9.1230e-01  

谢谢!

答案 1 :(得分:0)

savetxt需要header参数。

In [3]: header = '   '.join(names)
In [4]: header
Out[4]: 'NAME_1   NAME_2   NAME_3'
In [5]: np.savetxt('test.txt', floats, fmt='%15.4e', header=header)
In [6]: cat test.txt
# NAME_1   NAME_2   NAME_3
     1.2340e-01      5.6780e-01      9.1230e-01
     1.2340e-01     -5.6780e-01      9.1230e-01

这将浮动放在右列;标题格式需要调整。

如果你走vstack路线,就会得到一串字符串。

In [7]: np.vstack((names, floats))
Out[7]: 
array([['NAME_1', 'NAME_2', 'NAME_3'],
       ['0.1234', '0.5678', '0.9123'],
       ['0.1234', '-0.5678', '0.9123']], 
      dtype='<U32')

可以使用savetxt编写,但您必须使用%15s种格式。

至于效率,savetxt就像你的答案一样,除了它一次格式化和写一整行。我的savetxt电话确实有效:

fmt = ''.join(['%15.4e']*3)+'\n'
f = open(file, 'wb')
f.write(header); f.write('\nl')
for row in floats:
   f.write( fmt % tuple(row)) 

In [9]: fmt=''.join(['%15.4e']*3)
In [10]: print(fmt%tuple(floats[0]))
     1.2340e-01     5.6780e-01     9.1230e-01