Commit 3108946b authored by Julien Jerphanion's avatar Julien Jerphanion

Remove unused support for sparse matrices

parent 362ffb35
......@@ -19,7 +19,6 @@ from libc.stdlib cimport malloc, calloc, free
from libc.string cimport memset
from libc.math cimport sqrt
from scipy.linalg.cython_blas cimport sgemm, dgemm
from scipy import sparse
np.import_array()
......@@ -244,38 +243,12 @@ cpdef void _relocate_empty_clusters_dense(
weight_in_clusters[new_cluster_id] = weight
weight_in_clusters[old_cluster_id] -= weight
def _csr_row_norms(X):
"""L2 norm of each row in CSR matrix X."""
if X.dtype not in [np.float32, np.float64]:
X = X.astype(np.float64)
return _csr_row_norms(X.data, X.shape, X.indices, X.indptr)
def _csr_row_norms(np.ndarray[floating, ndim=1, mode="c"] X_data,
shape,
np.ndarray[integral, ndim=1, mode="c"] X_indices,
np.ndarray[integral, ndim=1, mode="c"] X_indptr):
cdef:
unsigned long long n_samples = shape[0]
unsigned long long i
integral j
double sum_
norms = np.empty(n_samples, dtype=X_data.dtype)
cdef floating[::1] norms_view = norms
for i in range(n_samples):
sum_ = 0.0
for j in range(X_indptr[i], X_indptr[i + 1]):
sum_ += X_data[j] * X_data[j]
norms_view[i] = sum_
return norms
def row_norms(X, squared=False):
"""Row-wise (squared) Euclidean norm of X.
Equivalent to np.sqrt((X * X).sum(axis=1)), but also supports sparse
matrices and does not create an X.shape-sized temporary.
Equivalent to np.sqrt((X * X).sum(axis=1)), but does not create
an X.shape-sized temporary.
Performs no input validation.
......@@ -291,12 +264,7 @@ def row_norms(X, squared=False):
array-like
The row-wise (squared) Euclidean norm of X.
"""
if sparse.issparse(X):
if not isinstance(X, sparse.csr_matrix):
X = sparse.csr_matrix(X)
norms = _csr_row_norms(X)
else:
norms = np.einsum('ij,ij->i', X, X)
norms = np.einsum('ij,ij->i', X, X)
if not squared:
np.sqrt(norms, norms)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment