Scipy: Sparse Matrix giving incorrect values
问题 Below is my code for generating my sparse matrix: import numpy as np import scipy def sparsemaker(X, Y, Z): 'X, Y, and Z are 2D arrays of the same size' x_, row = np.unique(X, return_inverse=True) y_, col = np.unique(Y, return_inverse=True) return scipy.sparse.csr_matrix( (Z.flat,(row,col)), shape=(x_.size, y_.size) ) >>> print sparsemaker(A, B, C) #A, B, and C are (220, 256) sized arrays. (0, 0) 167064.269831 (0, 2) 56.6146564629 (0, 9) 53.8660340698 (0, 23) 80.6529717039 (0, 28) 0.0 (0, 33)