<SYSTEM_TASK:>
Given the following text problem statement, write Python code to implement the functionality described below in problem statement
<END_TASK>
<USER_TASK:>
Problem:
How can I extract the main diagonal(1-d array) of a sparse matrix? The matrix is created in scipy.sparse. I want equivalent of np.diagonal(), but for sparse matrix.
<ASSISTANT_TASK:>
Python Code:
import numpy as np
from scipy.sparse import csr_matrix
arr = np.random.rand(4, 4)
M = csr_matrix(arr)
result = M.A.diagonal(0)
<END_TASK>