fill off diagonal of numpy array fails

最后都变了- 提交于 2019-12-20 02:31:11

问题


I'm trying to the fill the offset diagonals of a matrix:

loss_matrix = np.zeros((125,125))

np.diagonal(loss_matrix, 3).fill(4)

ValueError: assignment destination is read-only

Two questions:

1) Without iterating over indexes, how can I set the offset diagonals of a numpy array?

2) Why is the result of np.diagonal read only? The documentation for numpy.diagonal reads: "In NumPy 1.10, it will return a read/write view and writing to the returned array will alter your original array."

np.__version__

'1.10.1'

回答1:


Judging by the discussion on the NumPy issue tracker, it looks like the feature is stuck in limbo and they never got around to fixing the documentation to say it was delayed.

If you need writability, you can force it. This will only work on NumPy 1.9 and up, since np.diagonal makes a copy on lower versions:

diag = np.diagonal(loss_matrix, 3)

# It's not writable. MAKE it writable.
diag.setflags(write=True)

diag.fill(4)



回答2:


In an older version, diagflat constructs an array from a diagonal.

In [180]: M=np.diagflat(np.ones(125-3)*4,3)
In [181]: M.shape
Out[181]: (125, 125)
In [182]: M.diagonal(3)
Out[182]: 
array([ 4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,...  4.])

In [183]: np.__version__
Out[183]: '1.8.2'

Effectively it does this (working from its Python code)

res = np.zeros((125, 125))
i = np.arange(122)  
fi = i+3+i*125
res.flat[fi] = 4

That is, it finds the flatten array equivalent indices of the diagonal.

I can also get fi with:

In [205]: i=np.arange(0,122)
In [206]: np.ravel_multi_index((i,i+3),(125,125))


来源:https://stackoverflow.com/questions/35022178/fill-off-diagonal-of-numpy-array-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!