@@ -24,32 +24,32 @@ ctypedef cnp.float64_t DOUBLE
24
24
25
25
26
26
def csr_row_norms (X ):
27
- """ L2 norm of each row in CSR matrix X."""
27
+ """ Squared L2 norm of each row in CSR matrix X."""
28
28
if X.dtype not in [np.float32, np.float64]:
29
29
X = X.astype(np.float64)
30
- return _csr_row_norms(X.data, X.shape, X. indices, X.indptr)
30
+ return _csr_row_norms(X.data, X.indices, X.indptr)
31
31
32
32
33
- def _csr_row_norms (cnp.ndarray[floating , ndim = 1 , mode = " c" ] X_data,
34
- shape ,
35
- cnp.ndarray[integral , ndim = 1 , mode = " c" ] X_indices,
36
- cnp.ndarray[integral , ndim = 1 , mode = " c" ] X_indptr):
33
+ def _csr_row_norms (
34
+ const floating[::1] X_data ,
35
+ const integral[::1] X_indices ,
36
+ const integral[::1] X_indptr ,
37
+ ):
37
38
cdef:
38
- unsigned long long n_samples = shape[0 ]
39
- unsigned long long i
40
- integral j
39
+ integral n_samples = X_indptr.shape[0 ] - 1
40
+ integral i, j
41
41
double sum_
42
42
43
- norms = np.empty(n_samples, dtype = X_data.dtype)
44
- cdef floating[::1 ] norms_view = norms
43
+ dtype = np.float32 if floating is float else np.float64
45
44
46
- for i in range (n_samples):
47
- sum_ = 0.0
48
- for j in range (X_indptr[i], X_indptr[i + 1 ]):
49
- sum_ += X_data[j] * X_data[j]
50
- norms_view[i] = sum_
45
+ cdef floating[::1 ] norms = np.zeros(n_samples, dtype = dtype)
46
+
47
+ with nogil:
48
+ for i in range (n_samples):
49
+ for j in range (X_indptr[i], X_indptr[i + 1 ]):
50
+ norms[i] += X_data[j] * X_data[j]
51
51
52
- return norms
52
+ return np.asarray( norms)
53
53
54
54
55
55
def csr_mean_variance_axis0 (X , weights = None , return_sum_weights = False ):
0 commit comments