What is the fastest way to compute a sparse Gram matrix in Python?
问题 A Gram matrix is a matrix of the structure X @ X.T which of course is symmetrical. When dealing with dense matrices, the numpy.dot product implementation is intelligent enough to recognize the self-multiplication to exploit the symmetry and thus speed up the computations (see this). However, no such effect can be observed when using scipy.sparse matrices: random.seed(0) X = random.randn(5,50) X[X < 1.5] = 0 X = scipy.sparse.csr_matrix(X) print(f'sparsity of X: {100 * (1 - X.count_nonzero() /