77from sklearn .utils import check_random_state
88from sklearn .neighbors import NearestNeighbors
99
10+
1011__all__ = ['Constraints' ]
1112
1213
@@ -31,21 +32,21 @@ def __init__(self, partial_labels):
3132 partial_labels = np .asanyarray (partial_labels , dtype = int )
3233 self .partial_labels = partial_labels
3334
34- def positive_negative_pairs (self , num_constraints , same_length = False ,
35- random_state = None ):
35+ def positive_negative_pairs (self , n_constraints , same_length = False ,
36+ random_state = None , num_constraints = 'deprecated' ):
3637 """
3738 Generates positive pairs and negative pairs from labeled data.
3839
39- Positive pairs are formed by randomly drawing ``num_constraints `` pairs of
40+ Positive pairs are formed by randomly drawing ``n_constraints `` pairs of
4041 points with the same label. Negative pairs are formed by randomly drawing
41- ``num_constraints `` pairs of points with different label.
42+ ``n_constraints `` pairs of points with different label.
4243
4344 In the case where it is not possible to generate enough positive or
4445 negative pairs, a smaller number of pairs will be returned with a warning.
4546
4647 Parameters
4748 ----------
48- num_constraints : int
49+ n_constraints : int
4950 Number of positive and negative constraints to generate.
5051
5152 same_length : bool, optional (default=False)
@@ -55,6 +56,8 @@ def positive_negative_pairs(self, num_constraints, same_length=False,
5556 random_state : int or numpy.RandomState or None, optional (default=None)
5657 A pseudo random number generator object or a seed for it if int.
5758
59+ num_constraints : Renamed to n_constraints. Will be deprecated in 0.7.0
60+
5861 Returns
5962 -------
6063 a : array-like, shape=(n_constraints,)
@@ -69,10 +72,18 @@ def positive_negative_pairs(self, num_constraints, same_length=False,
6972 d : array-like, shape=(n_constraints,)
7073 1D array of indicators for the right elements of negative pairs.
7174 """
75+ if num_constraints != 'deprecated' :
76+ warnings .warn ('"num_constraints" parameter has been renamed to'
77+ ' "n_constraints". It has been deprecated in'
78+ ' version 0.6.3 and will be removed in 0.7.0'
79+ '' , FutureWarning )
80+ self .n_constraints = num_constraints
81+ else :
82+ self .n_constraints = n_constraints
7283 random_state = check_random_state (random_state )
73- a , b = self ._pairs (num_constraints , same_label = True ,
84+ a , b = self ._pairs (n_constraints , same_label = True ,
7485 random_state = random_state )
75- c , d = self ._pairs (num_constraints , same_label = False ,
86+ c , d = self ._pairs (n_constraints , same_label = False ,
7687 random_state = random_state )
7788 if same_length and len (a ) != len (c ):
7889 n = min (len (a ), len (c ))
@@ -190,15 +201,15 @@ def generate_knntriplets(self, X, k_genuine, k_impostor):
190201
191202 return triplets
192203
193- def _pairs (self , num_constraints , same_label = True , max_iter = 10 ,
204+ def _pairs (self , n_constraints , same_label = True , max_iter = 10 ,
194205 random_state = np .random ):
195206 known_label_idx , = np .where (self .partial_labels >= 0 )
196207 known_labels = self .partial_labels [known_label_idx ]
197208 num_labels = len (known_labels )
198209 ab = set ()
199210 it = 0
200- while it < max_iter and len (ab ) < num_constraints :
201- nc = num_constraints - len (ab )
211+ while it < max_iter and len (ab ) < n_constraints :
212+ nc = n_constraints - len (ab )
202213 for aidx in random_state .randint (num_labels , size = nc ):
203214 if same_label :
204215 mask = known_labels [aidx ] == known_labels
@@ -209,25 +220,26 @@ def _pairs(self, num_constraints, same_label=True, max_iter=10,
209220 if len (b_choices ) > 0 :
210221 ab .add ((aidx , random_state .choice (b_choices )))
211222 it += 1
212- if len (ab ) < num_constraints :
223+ if len (ab ) < n_constraints :
213224 warnings .warn ("Only generated %d %s constraints (requested %d)" % (
214- len (ab ), 'positive' if same_label else 'negative' , num_constraints ))
215- ab = np .array (list (ab )[:num_constraints ], dtype = int )
225+ len (ab ), 'positive' if same_label else 'negative' , n_constraints ))
226+ ab = np .array (list (ab )[:n_constraints ], dtype = int )
216227 return known_label_idx [ab .T ]
217228
218- def chunks (self , num_chunks = 100 , chunk_size = 2 , random_state = None ):
229+ def chunks (self , n_chunks = 100 , chunk_size = 2 , random_state = None ,
230+ num_chunks = 'deprecated' ):
219231 """
220232 Generates chunks from labeled data.
221233
222- Each of ``num_chunks `` chunks is composed of ``chunk_size`` points from
234+ Each of ``n_chunks `` chunks is composed of ``chunk_size`` points from
223235 the same class drawn at random. Each point can belong to at most 1 chunk.
224236
225- In the case where there is not enough points to generate ``num_chunks ``
237+ In the case where there is not enough points to generate ``n_chunks ``
226238 chunks of size ``chunk_size``, a ValueError will be raised.
227239
228240 Parameters
229241 ----------
230- num_chunks : int, optional (default=100)
242+ n_chunks : int, optional (default=100)
231243 Number of chunks to generate.
232244
233245 chunk_size : int, optional (default=2)
@@ -236,26 +248,34 @@ def chunks(self, num_chunks=100, chunk_size=2, random_state=None):
236248 random_state : int or numpy.RandomState or None, optional (default=None)
237249 A pseudo random number generator object or a seed for it if int.
238250
251+ num_chunks : Renamed to n_chunks. Will be deprecated in 0.7.0
252+
239253 Returns
240254 -------
241255 chunks : array-like, shape=(n_samples,)
242256 1D array of chunk indicators, where -1 indicates that the point does not
243257 belong to any chunk.
244258 """
259+ if num_chunks != 'deprecated' :
260+ warnings .warn ('"num_chunks" parameter has been renamed to'
261+ ' "n_chunks". It has been deprecated in'
262+ ' version 0.6.3 and will be removed in 0.7.0'
263+ '' , FutureWarning )
264+ n_chunks = num_chunks
245265 random_state = check_random_state (random_state )
246266 chunks = - np .ones_like (self .partial_labels , dtype = int )
247267 uniq , lookup = np .unique (self .partial_labels , return_inverse = True )
248268 unknown_uniq = np .where (uniq < 0 )[0 ]
249269 all_inds = [set (np .where (lookup == c )[0 ]) for c in range (len (uniq ))
250270 if c not in unknown_uniq ]
251271 max_chunks = int (np .sum ([len (s ) // chunk_size for s in all_inds ]))
252- if max_chunks < num_chunks :
272+ if max_chunks < n_chunks :
253273 raise ValueError (('Not enough possible chunks of %d elements in each'
254274 ' class to form expected %d chunks - maximum number'
255275 ' of chunks is %d'
256- ) % (chunk_size , num_chunks , max_chunks ))
276+ ) % (chunk_size , n_chunks , max_chunks ))
257277 idx = 0
258- while idx < num_chunks and all_inds :
278+ while idx < n_chunks and all_inds :
259279 if len (all_inds ) == 1 :
260280 c = 0
261281 else :
0 commit comments