Previous topic

MLProblems

Next topic

Classification MLProblems

This Page

Generic MLProblems

The mlproblems.generic module contains MLProblems that are not designed for a specific type of problem. They typically allow for manipulations that can be useful for many tasks.

This module contains the following classes:

  • MLProblem: Root class for machine learning problems.
  • SubsetProblem: Extracts a subset of examples from a dataset.
  • KFirstProblem: Extracts a the first few (K) examples from a dataset.
  • SubsetFieldsProblem: Extracts a subset of the fields in a dataset.
  • MergedProblem: Merges several datasets together.
  • PreprocessedProblem: Applies an arbitrary preprocessing on a dataset.
  • MinibatchProblem: Puts examples of datasets into mini-batches.
  • SemisupervisedProblem: Removes the labels of a subset of the examples in a dataset.
class mlproblems.generic.MLProblem(data=None, metadata={}, call_setup=True)[source]

Root class for machine learning problems.

An MLProblem consists simply in an iterator over elements in data. It also has some metadata, or “data about the data”. All that is assume about data is that it is possible to iterate over its content.

The metadata can be given explicitly by the user in the constructor. If data is itself an MLProblem, then its metadata will also be used (with priority given to the explicitly passed metadata).

Required metadata:

  • 'length': Number of examples (optional, will set the output of __len__(self)).
setup()[source]

Adapts the MLProblem to the given data’s content. For this root class, it does nothing.

However, an MLProblem that would normalize examples by subtracting the data’s average would compute this average in this method.

apply_on(new_data, new_metadata={})[source]

Returns a new MLProblem that will apply on some new data the same processing that this MLProblem applies on its data. For this root class, there isn’t any processing to share, hence this method doesn’t do much, besides calling data.apply_on(new_data,new_metadata) if data is itself an MLProblem.

However, for an MLProblem that would normalize examples by subtracting the data’s average, it would construct a new MLProblem such that it’ll subtract the same average.

peak()[source]

Returns the first example of the MLProblem.

raw_source()[source]

Returns the data and metadata of the first MLProblem in the series that led to this MLProblem.

class mlproblems.generic.SubsetProblem(data=None, metadata={}, call_setup=True, subset=set([]))[source]

Extracts a subset of the examples in a dataset.

The examples that are extracted have their ID (i.e. the example number from 0 to len(data)-1, as defined by the order in which the iterator yields the examples) in a given subset.

class mlproblems.generic.KFirstProblem(data=None, metadata={}, call_setup=True, K=100)[source]

Extracts the first few examples (K) in a dataset.

This is useful to run quick experiments to debug.

class mlproblems.generic.SubsetFieldsProblem(data=None, metadata={}, call_setup=True, fields=[0])[source]

Extracts a subset of the fields in a dataset.

The fields that are selected are given by option fields, a list of indices corresponding to the fields to keep. Each example of the new dataset will now be a list of those fields, unless fields contains only one index, in which case each example will correspond to that field.

class mlproblems.generic.MergedProblem(data=None, metadata={}, call_setup=True, serial=True)[source]

Merges several datasets together.

Each element of data should itself be an iterator over examples. All examples of the first dataset are first iterated over, then all examples of the second, and so on.

If option serial is False, then instead of iterating over the examples of one dataset at a time, it cycles over datasets and each time returns only one example. The iterator stops when all examples in all datasets have been iterated over at least once. Notice that if the datasets don’t all have the same size, then some examples will be iterated over at least twice.

class mlproblems.generic.PreprocessedProblem(data=None, metadata={}, call_setup=True, preprocess=None)[source]

MLProblem that applies a preprocessing function on examples from a dataset.

The examples of this MLProblem is the result of applying option preprocess on the examples in the original data. Hence, preprocess should be a callable function taking two arguments (an example from the original data as well as the metadata) and returning a preprocessed example.

IMPORANT: if preprocess changes the size of the inputs, the metadata (i.e. 'input_size') should be changed accordingly within preprocess.

class mlproblems.generic.MinibatchProblem(data=None, metadata={}, call_setup=True, minibatch_size=None, has_single_field=True)[source]

MLProblem that puts examples into mini-batches.

Option minibatch_size determines the size of the mini-batches. By default, this class assumes that the underlying dataset corresponds to a single field (e.g. the input). If this is not the case (e.g. contains pairs of inputs and targets), option has_single_field should be set to False.

If the examples don’t fit evenly into mini-batches of the desired size, the last mini-batch will be filled with copies of the remaining examples.

Defined metadata:

  • 'minibatch_size': number of examples in each mini-batch
class mlproblems.generic.SemisupervisedProblem(data=None, metadata={}, call_setup=True, unlabeled_ids=set([]), label_field=1)[source]

Removes the labels of a subset of the examples in a dataset.

The examples that have their ID (i.e. the example number from 0 to len(data)-1, as defined by the order in which the iterator yields the examples) in unlabeled_ids will have their labels be replaced by None.

The index of the label field can be given by option label_field.