metric_learn
.Constraints
- class metric_learn.Constraints(partial_labels)[source]
Class to build constraints from labeled data.
See more in the User Guide.
- Parameters:
- partial_labelsnumpy.ndarray of ints, shape=(n_samples,)
Array of labels, with -1 indicating unknown label.
- Attributes:
- partial_labelsnumpy.ndarray of ints, shape=(n_samples,)
Array of labels, with -1 indicating unknown label.
Methods
chunks
([n_chunks, chunk_size, random_state, ...])Generates chunks from labeled data.
generate_knntriplets
(X, k_genuine, k_impostor)Generates triplets from labeled data.
positive_negative_pairs
(n_constraints[, ...])Generates positive pairs and negative pairs from labeled data.
- chunks(n_chunks=100, chunk_size=2, random_state=None, num_chunks='deprecated')[source]
Generates chunks from labeled data.
Each of
n_chunks
chunks is composed ofchunk_size
points from the same class drawn at random. Each point can belong to at most 1 chunk.In the case where there is not enough points to generate
n_chunks
chunks of sizechunk_size
, a ValueError will be raised.- Parameters:
- n_chunksint, optional (default=100)
Number of chunks to generate.
- chunk_sizeint, optional (default=2)
Number of points in each chunk.
- random_stateint or numpy.RandomState or None, optional (default=None)
A pseudo random number generator object or a seed for it if int.
- num_chunksRenamed to n_chunks. Will be deprecated in 0.7.0
- Returns:
- chunksarray-like, shape=(n_samples,)
1D array of chunk indicators, where -1 indicates that the point does not belong to any chunk.
- generate_knntriplets(X, k_genuine, k_impostor)[source]
Generates triplets from labeled data.
For every point (X_a) the triplets (X_a, X_b, X_c) are constructed from all the combinations of taking one of its k_genuine-nearest neighbors of the same class (X_b) and taking one of its k_impostor-nearest neighbors of other classes (X_c).
In the case a class doesn’t have enough points in the same class (other classes) to yield k_genuine (k_impostor) neighbors a warning will be raised and the maximum value of genuine (impostor) neighbors will be used for that class.
- Parameters:
- X(n x d) matrix
Input data, where each row corresponds to a single instance.
- k_genuineint
Number of neighbors of the same class to be taken into account.
- k_impostorint
Number of neighbors of different classes to be taken into account.
- Returns:
- tripletsarray-like, shape=(n_constraints, 3)
2D array of triplets of indicators.
- positive_negative_pairs(n_constraints, same_length=False, random_state=None, num_constraints='deprecated')[source]
Generates positive pairs and negative pairs from labeled data.
Positive pairs are formed by randomly drawing
n_constraints
pairs of points with the same label. Negative pairs are formed by randomly drawingn_constraints
pairs of points with different label.In the case where it is not possible to generate enough positive or negative pairs, a smaller number of pairs will be returned with a warning.
- Parameters:
- n_constraintsint
Number of positive and negative constraints to generate.
- same_lengthbool, optional (default=False)
If True, forces the number of positive and negative pairs to be equal by ignoring some pairs from the larger set.
- random_stateint or numpy.RandomState or None, optional (default=None)
A pseudo random number generator object or a seed for it if int.
- num_constraintsRenamed to n_constraints. Will be deprecated in 0.7.0
- Returns:
- aarray-like, shape=(n_constraints,)
1D array of indicators for the left elements of positive pairs.
- barray-like, shape=(n_constraints,)
1D array of indicators for the right elements of positive pairs.
- carray-like, shape=(n_constraints,)
1D array of indicators for the left elements of negative pairs.
- darray-like, shape=(n_constraints,)
1D array of indicators for the right elements of negative pairs.