pystruct.models.MultiLabelClf

class pystruct.models.MultiLabelClf(n_labels=None, n_features=None, edges=None, inference_method=None)[source]

Multi-label model for predicting several binary classes.

Multi-label classification is a generalization of multi-class classification, in that multiple classes can be present in each example. This can also be thought of as predicting binary indicator per class.

This class supports different models via the “edges” parameter. Giving no edges yields independent classifiers for each class. Giving “full” yields a fully connected graph over the labels, while “tree” yields the best tree-shaped graph (using the Chow-Liu algorithm). It is also possible to specify a custom connectivity structure.

Parameters:

n_labels : int (default=None)

Number of labels. Inferred from data if not provided.

n_features : int (default=None)

Number of input features. Inferred from data if not provided.

edges : array-like, string or None

Either None, which yields independent models, ‘tree’, which yields the Chow-Liu tree over the labels, ‘full’, which yields a fully connected graph, or an array-like of edges for a custom dependency structure.

inference_method : :

The inference method to be used.

Methods

batch_inference(X, w[, relaxed])
batch_joint_feature(X, Y[, Y_true])
batch_loss(Y, Y_hat)
batch_loss_augmented_inference(X, Y, w[, ...])
continuous_loss(y, y_hat)
inference(x, w[, relaxed, return_energy]) Inference for x using parameters w.
initialize(X, Y)
joint_feature(x, y)
loss(y, y_hat)
loss_augmented_inference(x, y, w[, relaxed, ...]) Loss-augmented Inference for x relative to y using parameters w.
max_loss(y)
__init__(n_labels=None, n_features=None, edges=None, inference_method=None)[source]
inference(x, w, relaxed=False, return_energy=False)

Inference for x using parameters w.

Finds (approximately) armin_y np.dot(w, joint_feature(x, y)) using self.inference_method.

Parameters:

x : tuple

Instance of a graph with unary evidence. x=(unaries, edges) unaries are an nd-array of shape (n_nodes, n_states), edges are an nd-array of shape (n_edges, 2)

w : ndarray, shape=(size_joint_feature,)

Parameters for the CRF energy function.

relaxed : bool, default=False

Whether relaxed inference should be performed. Only meaningful if inference method is ‘lp’ or ‘ad3’. By default fractional solutions are rounded. If relaxed=True, fractional solutions are returned directly.

return_energy : bool, default=False

Whether to return the energy of the solution (x, y) that was found.

Returns:

y_pred : ndarray or tuple

By default an inter ndarray of shape=(width, height) of variable assignments for x is returned. If relaxed=True and inference_method is lp or ad3, a tuple (unary_marginals, pairwise_marginals) containing the relaxed inference result is returned. unary marginals is an array of shape (width, height, n_states), pairwise_marginals is an array of shape (n_states, n_states) of accumulated pairwise marginals.

loss_augmented_inference(x, y, w, relaxed=False, return_energy=False)

Loss-augmented Inference for x relative to y using parameters w.

Finds (approximately) armin_y_hat np.dot(w, joint_feature(x, y_hat)) + loss(y, y_hat) using self.inference_method.

Parameters:

x : tuple

Instance of a graph with unary evidence. x=(unaries, edges) unaries are an nd-array of shape (n_nodes, n_features), edges are an nd-array of shape (n_edges, 2)

y : ndarray, shape (n_nodes,)

Ground truth labeling relative to which the loss will be measured.

w : ndarray, shape=(size_joint_feature,)

Parameters for the CRF energy function.

relaxed : bool, default=False

Whether relaxed inference should be performed. Only meaningful if inference method is ‘lp’ or ‘ad3’. By default fractional solutions are rounded. If relaxed=True, fractional solutions are returned directly.

return_energy : bool, default=False

Whether to return the energy of the solution (x, y) that was found.

Returns:

y_pred : ndarray or tuple

By default an inter ndarray of shape=(n_nodes) of variable assignments for x is returned. If relaxed=True and inference_method is lp or ad3, a tuple (unary_marginals, pairwise_marginals) containing the relaxed inference result is returned. unary marginals is an array of shape (n_nodes, n_states), pairwise_marginals is an array of shape (n_states, n_states) of accumulated pairwise marginals.