我有这个小脚本:
from numpy import *
import numpy as np
import scipy.spatial as spt
X= np.loadtxt('edm')
myfile = open('edm.txt','w')
V= spt.distance.pdist(X.T,'sqeuclidean')
P = spt.distance.squareform(V)
print P
myfile.write(P)
这个矩阵:
0 199.778354301
201.857546133 0
如果我运行我的程序;我在终端得到这个(根据“打印”):
[[ 0. 80657.85977805]
[ 80657.85977805 0. ]]
但在我的输出文件中;我得到如下无效字符:
��������z°¶¡±Û@z°¶¡±Û@��������
你知道为什么吗?
感谢
答案 0 :(得分:1)
You can use NumPy savetxt
method to save arrays and not worry about codification.
As in the Docs,
>>> np.savetxt('edm.txt', x) # x is an array
答案 1 :(得分:1)
The file contains the binary representation of the numbers in the matrix.
$ od -t x1z edm.txt 0000000 00 00 00 00 00 00 00 00 79 a1 a6 c1 1d b1 f3 40 ........y......@ 0000020 79 a1 a6 c1 1d b1 f3 40 00 00 00 00 00 00 00 00 y......@........ 0000040