ryan
ryan

Reputation: 33

fill off diagonal of numpy array fails

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'

Upvotes: 3

Views: 3083

Answers (2)

hpaulj
hpaulj

Reputation: 231385

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))

Upvotes: 0

user2357112
user2357112

Reputation: 281177

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)

Upvotes: 7

Related Questions