假设我用 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.
提前感谢!!
显然,我在风影评论之后找到了解决方案。对于大矩阵来说,它感觉效率很低,但它可以完成工作:
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
谢谢!
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