8000 DOC add docstring for inplace_csr_row_normalize_l1 and inplace_csr_ro… · scikit-learn/scikit-learn@1c93eb3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1c93eb3

Browse files
DOC add docstring for inplace_csr_row_normalize_l1 and inplace_csr_row_normalize_l2 (#28286)
Co-authored-by: Guillaume Lemaitre <guillaume@probabl.ai>
1 parent 18b0402 commit 1c93eb3

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

sklearn/utils/sparsefuncs_fast.pyx

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,29 @@ def _incr_mean_variance_axis0(
492492

493493

494494
def inplace_csr_row_normalize_l1(X):
495-
"""Inplace row normalize using the l1 norm"""
495+
"""Normalize inplace the rows of a CSR matrix or array by their L1 norm.
496+
497+
Parameters
498+
----------
499+
X : scipy.sparse.csr_matrix and scipy.sparse.csr_array, \
500+
shape=(n_samples, n_features)
501+
The input matrix or array to be modified inplace.
502+
503+
Examples
504+
--------
505+
>>> from scipy.sparse import csr_matrix
506+
>>> from sklearn.utils.sparsefuncs_fast import inplace_csr_row_normalize_l1
507+
>>> X = csr_matrix(([1.0, 2.0, 3.0], [0, 2, 3], [0, 3, 4]), shape=(3, 4))
508+
>>> X.toarray()
509+
array([[1., 2., 0., 0.],
510+
[0., 0., 3., 0.],
511+
[0., 0., 0., 4.]])
512+
>>> inplace_csr_row_normalize_l1(X)
513+
>>> X.toarray()
514+
array([[0.33... , 0.66... , 0. , 0. ],
515+
[0. , 0. , 1. , 0. ],
516+
[0. , 0. , 0. , 1. ]])
517+
"""
496518
_inplace_csr_row_normalize_l1(X.data, X.shape, X.indices, X.indptr)
497519

498520

@@ -529,7 +551,28 @@ def _inplace_csr_row_normalize_l1(
529551

530552

531553
def inplace_csr_row_normalize_l2(X):
532-
"""Inplace row normalize using the l2 norm"""
554+
"""Normalize inplace the rows of a CSR matrix or array by their L2 norm.
555+
556+
Parameters
557+
----------
558+
X : scipy.sparse.csr_matrix, shape=(n_samples, n_features)
559+
The input matrix or array to be modified inplace.
560+
561+
Examples
562+
--------
563+
>>> from scipy.sparse import csr_matrix
564+
>>> from sklearn.utils.sparsefuncs_fast import inplace_csr_row_normalize_l2
565+
>>> X = csr_matrix(([1.0, 2.0, 3.0], [0, 2, 3], [0, 3, 4]), shape=(3, 4))
566+
>>> X.toarray()
567+
array([[1., 2., 0., 0.],
568+
[0., 0., 3., 0.],
569+
[0., 0., 0., 4.]])
570+
>>> inplace_csr_row_normalize_l2(X)
571+
>>> X.toarray()
572+
array([[0.44... , 0.89... , 0. , 0. ],
573+
[0. , 0. , 1. , 0. ],
574+
[0. , 0. , 0. , 1. ]])
575+
"""
533576
_inplace_csr_row_normalize_l2(X.data, X.shape, X.indices, X.indptr)
534577

535578

0 commit comments

Comments
 (0)
0