safe_sparse_dot#
- sklearn.utils.extmath.safe_sparse_dot(a, b, *, dense_output=False)[source]#
正确处理稀疏矩阵情况的点积。
- 参数:
- a{ndarray, 稀疏矩阵}
- b{ndarray, 稀疏矩阵}
- dense_outputbool, 默认为 False
当为 False 时,
a
和b
都为稀疏矩阵将产生稀疏输出。当为 True 时,输出将始终是密集数组。
- 返回:
- dot_product{ndarray, 稀疏矩阵}
如果
a
和b
为稀疏矩阵且dense_output=False
,则为稀疏矩阵。
示例
>>> from scipy.sparse import csr_matrix >>> from sklearn.utils.extmath import safe_sparse_dot >>> X = csr_matrix([[1, 2], [3, 4], [5, 6]]) >>> dot_product = safe_sparse_dot(X, X.T) >>> dot_product.toarray() array([[ 5, 11, 17], [11, 25, 39], [17, 39, 61]])