https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.dia_matrix.html dia_matrix((data, offsets), shape=(M, N)) where the data[k,:] stores the diagonal entries for diagonal offsets[k] (See example below)
这个真是太难懂了。。
只好搞个例子。。。
data = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12]])
print(data)
offsets = np.array([0, -1, 1])
sp.dia_matrix((data, offsets), shape=(4, 4)).toarray()
# output
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
array([[ 1, 10, 0, 0],
[ 5, 2, 11, 0],
[ 0, 6, 3, 12],
[ 0, 0, 7, 4]])
data = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12]])
print(data)
offsets = np.array([0, -5, 1])
sp.dia_matrix((data, offsets), shape=(10, 5)).toarray()
# output
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
array([[ 1, 10, 0, 0, 0],
[ 0, 2, 11, 0, 0],
[ 0, 0, 3, 12, 0],
[ 0, 0, 0, 4, 0],
[ 0, 0, 0, 0, 0],
[ 5, 0, 0, 0, 0],
[ 0, 6, 0, 0, 0],
[ 0, 0, 7, 0, 0],
[ 0, 0, 0, 8, 0],
[ 0, 0, 0, 0, 0]])