从1D numpy数组中获取此类矩阵的最有效方法是什么?

时间:2016-01-18 19:30:03

标签: python numpy

我有一个总共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>

  1. 上三角值用numpy数组中的值填充' a'。
  2. 下三角值用numpy数组中的值填充' a'但乘以-1。
  3. 对角线仅由零组成。
  4. 示例(准确性很重要):

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

    实现这一目标的最有效方法是什么?

1 个答案:

答案 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