我有一个总共4950
个值的文件,如:
0.012345678912345678
我使用以下方式阅读文件:
a = numpy.genfromtxt(file_name, dtype=str, delimiter=',') # a.shape = (4950L, 1L) #dtype=str as I don't want to compromise accuracy
#say a == ['0.000000000000000001', -'0.000000000000000002', ...., '0.000000000004950']
我想要获得的是获得大小为b
的矩阵(100L, 100L)
,其中:{/ p>
示例(准确性很重要):
array = ['1','2','-3','-5','6','-7'] # In reality the data is up to 18 decimal places.
final_matrix = [
['0','1','2','-3'],
['-1',0,'-5','6'],
['-2','5','0','-7'],
['3','-6','7','0']
]
实现这一目标的最有效方法是什么?
答案 0 :(得分:5)
不确定它是否是最有效的方式,但这似乎非常有效。
import numpy
# create some random data for testing
sz = 100
a = numpy.random.random(sz*sz/2 - sz/2).astype('S50')
# convert back to float for a test on minus signs,
# as it would be done if a is read as string values
amins = numpy.where(a.astype(float) <= 0, "", "-")
# get the values without minus signs
aplus = numpy.char.lstrip(a, "-")
# addup to negated string values
aminus = numpy.char.add(amins, aplus)
# create an empty matrix
m = numpy.zeros(shape=(sz,sz), dtype='S51')
# ids of the upper triangle
u_ids = numpy.triu_indices(sz,1)
# set upper values
m[u_ids] = a
# switch coordinates to set lower values
m[u_ids[1],u_ids[0]] = aminus
# fill diag with zeros
numpy.fill_diagonal(m, numpy.zeros(sz).astype('S51'))
print m