@@ -59,16 +59,25 @@ def inplace_column_scale(X, scale):
5959 "Unsupported type; expected a CSR or CSC sparse matrix." )
6060
6161
62- def swap_row_csc (X , m , n ):
62+ def inplace_swap_row_csc (X , m , n ):
6363 """
6464 Swaps two rows of a CSC matrix in-place.
6565
6666 Parameters
6767 ----------
68- X : scipy.sparse.csc_matrix, shape=(n_samples, n_features)
69- m : int, index of first sample
70- n : int, index of second sample
68+ X: scipy.sparse.csc_matrix, shape=(n_samples, n_features)
69+ Matrix whose two rows are to be swapped.
70+
71+ m: int
72+ Index of the row of X to be swapped.
73+
74+ n: int
75+ Index of the row of X to be swapped.
7176 """
77+ for t in [m , n ]:
78+ if isinstance (t , np .ndarray ):
79+ raise TypeError ("m and n should be valid integers" )
80+
7281 if m < 0 :
7382 m += X .shape [0 ]
7483 if n < 0 :
@@ -79,20 +88,32 @@ def swap_row_csc(X, m, n):
7988 X .indices [m_mask ] = n
8089
8190
82- def swap_row_csr (X , m , n ):
91+ def inplace_swap_row_csr (X , m , n ):
8392 """
8493 Swaps two rows of a CSR matrix in-place.
8594
8695 Parameters
8796 ----------
88- X : scipy.sparse.csc_matrix, shape=(n_samples, n_features)
89- m : int, index of first sample
90- n : int, index of second sample
97+ X: scipy.sparse.csr_matrix, shape=(n_samples, n_features)
98+ Matrix whose two rows are to be swapped.
99+
100+ m: int
101+ Index of the row of X to be swapped.
102+
103+ n: int
104+ Index of the row of X to be swapped.
91105 """
106+ for t in [m , n ]:
107+ if isinstance (t , np .ndarray ):
108+ raise TypeError ("m and n should be valid integers" )
109+
92110 if m < 0 :
93111 m += X .shape [0 ]
94112 if n < 0 :
95113 n += X .shape [0 ]
114+
115+ # The following swapping makes life easier since m is assumed to be the
116+ # smaller integer below.
96117 if m > n :
97118 m , n = n , m
98119
@@ -123,43 +144,53 @@ def swap_row_csr(X, m, n):
123144 X .data [n_stop :]])
124145
125146
126- def swap_row (X , m , n ):
147+ def inplace_swap_row (X , m , n ):
127148 """
128149 Swaps two rows of a CSC/CSR matrix in-place.
129150
130151 Parameters
131152 ----------
132- X : scipy.sparse.csc_matrix, shape=(n_samples, n_features)
133- m : int, index of first sample
134- n : int, index of second sample
153+ X : CSR or CSC sparse matrix, shape=(n_samples, n_features)
154+ Matrix whose two rows are to be swapped.
155+
156+ m: int
157+ Index of the row of X to be swapped.
158+
159+ n: int
160+ Index of the row of X to be swapped.
135161 """
136162 if isinstance (X , sp .csc_matrix ):
137- return swap_row_csc (X , m , n )
163+ return inplace_swap_row_csc (X , m , n )
138164 elif isinstance (X , sp .csr_matrix ):
139- return swap_row_csr (X , m , n )
165+ return inplace_swap_row_csr (X , m , n )
140166 else :
141167 raise TypeError (
142168 "Unsupported type; expected a CSR or CSC sparse matrix." )
143169
144170
145- def swap_column (X , m , n ):
171+ def inplace_swap_column (X , m , n ):
146172 """
147173 Swaps two columns of a CSC/CSR matrix in-place.
148174
149175 Parameters
150176 ----------
151- X : scipy.sparse.csc_matrix, shape=(n_samples, n_features)
152- m : int, index of first sample
153- n : int, index of second sample
177+ X : CSR or CSC sparse matrix, shape=(n_samples, n_features)
178+ Matrix whose two columns are to be swapped.
179+
180+ m: int
181+ Index of the column of X to be swapped.
182+
183+ n : int
184+ Index of the column of X to be swapped.
154185 """
155186 if m < 0 :
156187 m += X .shape [1 ]
157188 if n < 0 :
158189 n += X .shape [1 ]
159190 if isinstance (X , sp .csc_matrix ):
160- return swap_row_csr (X , m , n )
191+ return inplace_swap_row_csr (X , m , n )
161192 elif isinstance (X , sp .csr_matrix ):
162- return swap_row_csc (X , m , n )
193+ return inplace_swap_row_csc (X , m , n )
163194 else :
164195 raise TypeError (
165196 "Unsupported type; expected a CSR or CSC sparse matrix." )
0 commit comments