Graph Cut¶

class
apricot.functions.graphCut.
GraphCutSelection
(n_samples=10, metric='euclidean', alpha=1, initial_subset=None, optimizer='naive', optimizer_kwds={}, n_neighbors=None, reservoir=None, max_reservoir_size=1000, n_jobs=1, random_state=None, verbose=False)[source]¶ A selector based on using a graphcut function.
Graph cuts are canonical class of functions that involves selecting examples that split the similarity matrix into subgraphs well.
Note
All ~pairwise~ values in your data must be nonnegative for this selection to work.
The general form of a graph cut function is
\[f(X, V) = \lambda\sum_{v \in V} \sum_{x \in X} \phi(x, v)  \sum_{x, y \in X} \phi(x, y)\]where \(f\) indicates the function, \(X\) is a subset, \(V\) is the ground set, and \(\phi\) is the similarity measure between two examples. Like most graphbased functons, the graphcut function requires access to the full similarity matrix.
This implementation allows users to pass in either their own symmetric square matrix of similarity values, or a data matrix as normal and a function that calculates these pairwise values.
Parameters:  n_samples (int) – The number of samples to return.
 metric (str, optional) – The method for converting a data matrix into a square symmetric matrix of pairwise similarities. If a string, can be any of the metrics implemented in sklearn (see https://scikitlearn.org/stable/modules/ generated/sklearn.metrics.pairwise_distances.html), including “precomputed” if one has already generated a similarity matrix. Note that sklearn calculates distance matrices whereas apricot operates on similarity matrices, and so a distances.max()  distances transformation is performed on the resulting distances. For backcompatibility, ‘corr’ will be read as ‘correlation’. Default is ‘euclidean’.
 alpha (float) – The weight of the first term in the graphcut objective, which measures the how representative the selected examples are of the ground set. The larger this is, the more likely examples are chosen that are near points in the ground set, even if there are other alreadyselected examples that are similar. Default is 1.
 initial_subset (list, numpy.ndarray or None, optional) – If provided, this should be a list of indices into the data matrix to use as the initial subset, or a group of examples that may not be in the provided data should beused as the initial subset. If indices, the provided array should be onedimensional. If a group of examples, the data should be 2 dimensional. Default is None.
 optimizer (string or optimizers.BaseOptimizer, optional) –
The optimization approach to use for the selection. Default is ‘twostage’, which makes selections using the naive greedy algorithm initially and then switches to the lazy greedy algorithm. Must be one of
’random’ : randomly select elements (dummy optimizer) ‘modular’ : approximate the function using its modular upper bound ‘naive’ : the naive greedy algorithm ‘lazy’ : the lazy (or accelerated) greedy algorithm ‘approximatelazy’ : the approximate lazy greedy algorithm ‘twostage’ : starts with naive and switches to lazy ‘stochastic’ : the stochastic greedy algorithm ‘sample’ : randomly take a subset and perform selection on that ‘greedi’ : the GreeDi distributed algorithm ‘bidirectional’ : the bidirectional greedy algorithmDefault is ‘twostage’.
 optimizer_kwds (dict or None) – A dictionary of arguments to pass into the optimizer object. The keys of this dictionary should be the names of the parameters in the optimizer and the values in the dictionary should be the values that these parameters take. Default is None.
 n_neighbors (int or None) – When constructing a similarity matrix, the number of nearest neighbors whose similarity values will be kept. The result is a sparse similarity matrix which can significantly speed up computation at the cost of accuracy. Default is None.
 reservoir (numpy.ndarray or None) – The reservoir to use when calculating gains in the sieve greedy streaming optimization algorithm in the partial_fit method. Currently only used for graphbased functions. If a numpy array is passed in, it will be used as the reservoir. If None is passed in, will use reservoir sampling to collect a reservoir. Default is None.
 max_reservoir_size (int) – The maximum size that the reservoir can take. If a reservoir is passed in, this value is set to the size of that array. Default is 1000.
 n_jobs (int) – The number of threads to use when performing computation in parallel. Currently, this parameter is exposed but does not actually do anything. This will be fixed soon.
 random_state (int or RandomState or None, optional) – The random seed to use for the random selection process. Only used for stochastic greedy.
 verbose (bool) – Whether to print output during the selection process.

n_samples
¶ The number of samples to select.
Type: int

pairwise_func
¶ A function that takes in a data matrix and converts it to a square symmetric matrix.
Type: callable

ranking
¶ The selected samples in the order of their gain.
Type: numpy.array int

gains
¶ The gain of each sample in the returned set when it was added to the growing subset. The first number corresponds to the gain of the first added sample, the second corresponds to the gain of the second added sample, and so forth.
Type: numpy.array float

fit
(X, y=None, sample_weight=None, sample_cost=None)[source]¶ Run submodular optimization to select the examples.
This method is a wrapper for the full submodular optimization process. It takes in some data set (and optionally labels that are ignored during this process) and selects n_samples from it in the greedy manner specified by the optimizer.
This method will return the selector object itself, not the transformed data set. The transform method will then transform a data set to the selected points, or alternatively one can use the ranking stored in the self.ranking attribute. The fit_transform method will perform both optimization and selection and return the selected items.
Parameters:  X (list or numpy.ndarray, shape=(n, d)) – The data set to transform. Must be numeric.
 y (list or numpy.ndarray or None, shape=(n,), optional) – The labels to transform. If passed in this function will return both the data and th corresponding labels for the rows that have been selected.
 sample_weight (list or numpy.ndarray or None, shape=(n,), optional) – The weight of each example. Currently ignored in apricot but included to maintain compatibility with sklearn pipelines.
 sample_cost (list or numpy.ndarray or None, shape=(n,), optional) – The cost of each item. If set, indicates that optimization should be performed with respect to a knapsack constraint.
Returns: self – The fit step returns this selector object.
Return type:

fit_transform
(X, y=None, sample_weight=None, sample_cost=None)¶ Run optimization and select a subset of examples.
This method will first perform the fit step and then perform the transform step, returning a transformed data set.
Parameters:  X (list or numpy.ndarray, shape=(n, d)) – The data set to transform. Must be numeric.
 y (list or numpy.ndarray or None, shape=(n,), optional) – The labels to transform. If passed in this function will return both the data and the corresponding labels for the rows that have been selected. Default is None.
 sample_weight (list or numpy.ndarray or None, shape=(n,), optional) – The sample weights to transform. If passed in this function will return the selected labels (y) and the selected samples, even if no labels were passed in. Default is None.
 sample_cost (list or numpy.ndarray or None, shape=(n,), optional) – The cost of each item. If set, indicates that optimization should be performed with respect to a knapsack constraint.
Returns:  X_subset (numpy.ndarray, shape=(n_samples, d)) – A subset of the data such that n_samples < n and n_samples is the integer provided at initialization.
 y_subset (numpy.ndarray, shape=(n_samples,), optional) – The labels that match with the indices of the samples if y is passed in. Only returned if passed in.
 sample_weight_subset (numpy.ndarray, shape=(n_samples,), optional) – The weight of each example.

transform
(X, y=None, sample_weight=None)¶ Transform a data set to include only the selected examples.
This method will return a selection of X and optionally selections of y and sample_weight. The default setting is to select items based on the ranking determined in the fit step with examples in the same order as that ranking. Optionally, the whole data set can be returned, with the weights corresponding to samples that were not selected set to 0. This setting can be controlled by setting pipeline=True.
Parameters:  X (list or numpy.ndarray, shape=(n, d)) – The data set to transform. Must be numeric.
 y (list or numpy.ndarray or None, shape=(n,), optional) – The labels to transform. If passed in this function will return both the data and the corresponding labels for the rows that have been selected. Default is None.
 sample_weight (list or numpy.ndarray or None, shape=(n,), optional) – The sample weights to transform. If passed in this function will return the selected labels (y) and the selected samples, even if no labels were passed in. Default is None.
Returns:  X_subset (numpy.ndarray, shape=(n_samples, d)) – A subset of the data such that n_samples < n and n_samples is the integer provided at initialization.
 y_subset (numpy.ndarray, shape=(n_samples,), optional) – The labels that match with the indices of the samples if y is passed in. Only returned if passed in.
 sample_weight_subset (numpy.ndarray, shape=(n_samples,), optional) – The weight of each example.