skmultilearn.base package

The skmultilearn.base module implements base classifier classes for scikit-multilearn’s multi-label classification.

Two base classifier classes are in use currently in scikit-multilearn:

class skmultilearn.base.MLClassifierBase[source]

Bases: sklearn.base.BaseEstimator, sklearn.base.ClassifierMixin

Base class providing API and common functions for all multi-label classifiers.

Parameters:

classifier : scikit classifier type

The base classifier that will be used in a class, will be automagically put under self.classifier for future access.

require_dense : boolean

Whether the base classifier requires input as dense arrays, False by default

ensure_input_format(X, sparse_format='csr', enforce_sparse=False)[source]

Ensure the desired input format

This function ensures that input format follows the density/sparsity requirements of base classifier.

Parameters:

X : array-like or sparse matrix, shape = [n_samples, n_features]

An input feature matrix

sparse_format: string

Requested format of returned scipy.sparse matrix, if sparse is returned

enforce_sparse : bool

Ignore require_dense and enforce sparsity, useful internally

Returns:

transformed X : array-like or sparse matrix, shape = [n_samples, n_features]

If require_dense was set to true for input features in the constructor, the returned value is an array-like of array-likes. If require_dense is set to false, a sparse matrix of format sparse_format is returned, if possible - without cloning.

ensure_output_format(matrix, sparse_format='csr', enforce_sparse=False)[source]

Ensure the desired output format

This function ensures that output format follows the density/sparsity requirements of base classifier.

Parameters:

matrix : array-like with shape = [n_samples] or [n_samples, n_outputs]; or sparse matrix, shape = [n_samples, n_outputs]

An input feature matrix

sparse_format: string

Requested format of returned scipy.sparse matrix, if sparse is returned

enforce_sparse : bool

Ignore require_dense and enforce sparsity, useful internally

Returns:

transformed matrix: array-like with shape = [n_samples] or [n_samples, n_outputs]; or sparse matrix, shape = [n_samples, n_outputs]

If require_dense was set to True for input features in the constructor, the returned value is an array-like of array-likes. If require_dense is set to False, a sparse matrix of format sparse_format is returned, if possible - without cloning.

fit(X, y)[source]

Abstract method to fit classifier with training data

Parameters:
  • X (dense or sparse matrix (n_samples, n_features)) – input features
  • y (dense or sparse matrix of {0, 1} (n_samples, n_labels)) – binary indicator matrix with label assignments

Should return self - a trained instance of the classifier.

Raises:NotImplementedError – this is just an abstract method
generate_data_subset(y, subset, axis)[source]

Subset rows or columns from matrix

This function subsets the array of binary label vectors to include only certain labels.

Parameters:

y : array-like of array-likes

An array-like of binary label vectors.

subset: array-like of integers

array of integers, indices that will be subsetted from array-likes in y

axis: integer 0 for ‘rows’, 1 for ‘labels’,

control variable for whether to return rows or labels as indexed by subset

Returns:

multi-label binary label vector : array-like of array-likes of {0,1}

array of binary label vectors including label data only for labels from parameter labels

get_params(deep=True)[source]

Get parameters to subobjects

Introspection of classifier for search models like cross validation and grid search.

Parameters:

deep : boolean

If true all params will be introspected also and appended to the output dict.

Returns:

out : dictionary

Dictionary of all parameters and their values. If deep=True the dictionary also holds the parameters of the parameters.

predict(X)[source]

Abstract method to predict labels

Parameters:X (dense or sparse matrix (n_samples, n_features)) – input features

Should return sparse matrix of {0, 1} (n_samples, n_labels).

Raises:NotImplementedError – this is just an abstract method
set_params(**parameters)[source]

Propagate parameters to subobjects

Set parameters as returned by get_params. @see https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/base.py#L243

class skmultilearn.base.ProblemTransformationBase(classifier=None, require_dense=None)[source]

Bases: skmultilearn.base.base.MLClassifierBase

Base class providing common functions for multi-label classifiers that follow the problem transformation approach.

Problem transformation is the approach in which the original multi-label classification problem is transformed into one or more single-label problems, which are then solved by single-class or multi-class classifiers.

Scikit-multilearn provides a number of such methods:

  • BinaryRelevance - which performs a single-label single-class classification for each label and sums the results BinaryRelevance
  • ClassifierChains - which performs a single-label single-class classification for each label and sums the results ClassifierChain
  • LabelPowerset - which performs a single-label single-class classification for each label and sums the results LabelPowerset
Parameters:

classifier : scikit classifier type

The base classifier that will be used in a class, will be automagically put under self.classifier for future access.

require_dense : boolean

Whether the base classifier requires input as dense arrays, False by default

ensure_multi_label_from_single_class(matrix, matrix_format='csr')[source]

Transform single class outputs to a 2D sparse matrix

Submodules

skmultilearn.base.base module

class skmultilearn.base.base.MLClassifierBase[source]

Bases: sklearn.base.BaseEstimator, sklearn.base.ClassifierMixin

Base class providing API and common functions for all multi-label classifiers.

Parameters:

classifier : scikit classifier type

The base classifier that will be used in a class, will be automagically put under self.classifier for future access.

require_dense : boolean

Whether the base classifier requires input as dense arrays, False by default

ensure_input_format(X, sparse_format='csr', enforce_sparse=False)[source]

Ensure the desired input format

This function ensures that input format follows the density/sparsity requirements of base classifier.

Parameters:

X : array-like or sparse matrix, shape = [n_samples, n_features]

An input feature matrix

sparse_format: string

Requested format of returned scipy.sparse matrix, if sparse is returned

enforce_sparse : bool

Ignore require_dense and enforce sparsity, useful internally

Returns:

transformed X : array-like or sparse matrix, shape = [n_samples, n_features]

If require_dense was set to true for input features in the constructor, the returned value is an array-like of array-likes. If require_dense is set to false, a sparse matrix of format sparse_format is returned, if possible - without cloning.

ensure_output_format(matrix, sparse_format='csr', enforce_sparse=False)[source]

Ensure the desired output format

This function ensures that output format follows the density/sparsity requirements of base classifier.

Parameters:

matrix : array-like with shape = [n_samples] or [n_samples, n_outputs]; or sparse matrix, shape = [n_samples, n_outputs]

An input feature matrix

sparse_format: string

Requested format of returned scipy.sparse matrix, if sparse is returned

enforce_sparse : bool

Ignore require_dense and enforce sparsity, useful internally

Returns:

transformed matrix: array-like with shape = [n_samples] or [n_samples, n_outputs]; or sparse matrix, shape = [n_samples, n_outputs]

If require_dense was set to True for input features in the constructor, the returned value is an array-like of array-likes. If require_dense is set to False, a sparse matrix of format sparse_format is returned, if possible - without cloning.

fit(X, y)[source]

Abstract method to fit classifier with training data

Parameters:
  • X (dense or sparse matrix (n_samples, n_features)) – input features
  • y (dense or sparse matrix of {0, 1} (n_samples, n_labels)) – binary indicator matrix with label assignments

Should return self - a trained instance of the classifier.

Raises:NotImplementedError – this is just an abstract method
generate_data_subset(y, subset, axis)[source]

Subset rows or columns from matrix

This function subsets the array of binary label vectors to include only certain labels.

Parameters:

y : array-like of array-likes

An array-like of binary label vectors.

subset: array-like of integers

array of integers, indices that will be subsetted from array-likes in y

axis: integer 0 for ‘rows’, 1 for ‘labels’,

control variable for whether to return rows or labels as indexed by subset

Returns:

multi-label binary label vector : array-like of array-likes of {0,1}

array of binary label vectors including label data only for labels from parameter labels

get_params(deep=True)[source]

Get parameters to subobjects

Introspection of classifier for search models like cross validation and grid search.

Parameters:

deep : boolean

If true all params will be introspected also and appended to the output dict.

Returns:

out : dictionary

Dictionary of all parameters and their values. If deep=True the dictionary also holds the parameters of the parameters.

predict(X)[source]

Abstract method to predict labels

Parameters:X (dense or sparse matrix (n_samples, n_features)) – input features

Should return sparse matrix of {0, 1} (n_samples, n_labels).

Raises:NotImplementedError – this is just an abstract method
set_params(**parameters)[source]

Propagate parameters to subobjects

Set parameters as returned by get_params. @see https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/base.py#L243

skmultilearn.base.mock module

class skmultilearn.base.mock.MockClassifier[source]

Bases: skmultilearn.base.base.MLClassifierBase

A stub classifier

fit(X, y)[source]
predict(X)[source]

skmultilearn.base.problem_transformation module

class skmultilearn.base.problem_transformation.ProblemTransformationBase(classifier=None, require_dense=None)[source]

Bases: skmultilearn.base.base.MLClassifierBase

Base class providing common functions for multi-label classifiers that follow the problem transformation approach.

Problem transformation is the approach in which the original multi-label classification problem is transformed into one or more single-label problems, which are then solved by single-class or multi-class classifiers.

Scikit-multilearn provides a number of such methods:

  • BinaryRelevance - which performs a single-label single-class classification for each label and sums the results BinaryRelevance
  • ClassifierChains - which performs a single-label single-class classification for each label and sums the results ClassifierChain
  • LabelPowerset - which performs a single-label single-class classification for each label and sums the results LabelPowerset
Parameters:

classifier : scikit classifier type

The base classifier that will be used in a class, will be automagically put under self.classifier for future access.

require_dense : boolean

Whether the base classifier requires input as dense arrays, False by default

ensure_multi_label_from_single_class(matrix, matrix_format='csr')[source]

Transform single class outputs to a 2D sparse matrix