jsat.classifiers.boosting
Class Bagging
- java.lang.Object
-
- jsat.classifiers.boosting.Bagging
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable, Classifier, Parameterized, Regressor
public class Bagging extends java.lang.Object implements Classifier, Regressor, Parameterized
An implementation of Bootstrap Aggregating, as described by LEO BREIMAN in "Bagging Predictors".
Bagging is an ensemble learner, it takes a weak learner and trains several to create a better over result. Bagging is particularly useful when the base classifier has some amount of predictive power, but is hindered by variance in the output (small change in input causes large change in output), or variances in input (handles noisy data badly or is has a brittle learning algorithm). It is common to perform bagging onDecision Trees, because they meet these strengths and weaknesses.
Bagging produces little to no improvement when using learners that have low variance and robust learning methods.NearestNeighbouris an example of a particularly bad method to bag.
Bagging has many similarities to boosting.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field and Description static intDEFAULT_EXTRA_SAMPLESThe number of extra samples to take when bagging in each round used by default in the constructor: 0static intDEFAULT_ROUNDSThe number of rounds of bagging that will be used by default in the constructor: 20static booleanDEFAULT_SIMULTANIOUS_TRAININGThe default behavior for parallel training, as specified bysetSimultaniousTraining(boolean)is true
-
Constructor Summary
Constructors Constructor and Description Bagging(Classifier baseClassifier)Creates a new Bagger for classification.Bagging(Classifier baseClassifier, int extraSamples, boolean simultaniousTraining)Creates a new Bagger for classification.Bagging(Classifier baseClassifier, int extraSamples, boolean simultaniousTraining, int rounds, java.util.Random random)Creates a new Bagger for classification.Bagging(Regressor baseRegressor)Creates a new Bagger for regression.Bagging(Regressor baseRegressor, int extraSamples, boolean simultaniousTraining)Creates a new Bagger for regression.Bagging(Regressor baseRegressor, int extraSamples, boolean simultaniousTraining, int rounds, java.util.Random random)Creates a new Bagger for regression.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description CategoricalResultsclassify(DataPoint data)Performs classification on the given data point.Baggingclone()intgetExtraSamples()intgetRounds()Returns the number of rounds of boosting that will be done, which is also the number of base learners that will be trainedstatic ClassificationDataSetgetSampledDataSet(ClassificationDataSet dataSet, int[] sampledCounts)Creates a new data set from the given sample counts.static RegressionDataSetgetSampledDataSet(RegressionDataSet dataSet, int[] sampledCounts)Creates a new data set from the given sample counts.static ClassificationDataSetgetWeightSampledDataSet(ClassificationDataSet dataSet, int[] sampledCounts)Creates a new data set from the given sample counts.static RegressionDataSetgetWeightSampledDataSet(RegressionDataSet dataSet, int[] sampledCounts)Creates a new data set from the given sample counts.doubleregress(DataPoint data)static voidsampleWithReplacement(int[] sampleCounts, int samples, java.util.Random rand)Performs the sampling based on the number of data points, storing the counts in an array to be constructed from XXXXvoidsetExtraSamples(int i)Bagging samples from the training set with replacement, and draws a sampleWithReplacement at least as large as the training set.voidsetRounds(int rounds)Sets the number of rounds that bagging is done, meaning how many base learners are trainedvoidsetSimultaniousTraining(boolean simultaniousTraining)Bagging produces multiple base learners.booleansupportsWeightedData()Indicates whether the model knows how to train using weighted data points.voidtrain(ClassificationDataSet dataSet, boolean parallel)Trains the classifier and constructs a model for classification using the given data set.voidtrain(RegressionDataSet dataSet, boolean parallel)-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jsat.classifiers.Classifier
train
-
Methods inherited from interface jsat.parameters.Parameterized
getParameter, getParameters
-
-
-
-
Field Detail
-
DEFAULT_ROUNDS
public static final int DEFAULT_ROUNDS
The number of rounds of bagging that will be used by default in the constructor: 20- See Also:
- Constant Field Values
-
DEFAULT_EXTRA_SAMPLES
public static final int DEFAULT_EXTRA_SAMPLES
The number of extra samples to take when bagging in each round used by default in the constructor: 0- See Also:
- Constant Field Values
-
DEFAULT_SIMULTANIOUS_TRAINING
public static final boolean DEFAULT_SIMULTANIOUS_TRAINING
The default behavior for parallel training, as specified bysetSimultaniousTraining(boolean)is true- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Bagging
public Bagging(Classifier baseClassifier)
Creates a new Bagger for classification. This can not be changed after construction.- Parameters:
baseClassifier- the base learner to use.
-
Bagging
public Bagging(Classifier baseClassifier, int extraSamples, boolean simultaniousTraining)
Creates a new Bagger for classification. This can not be changed after construction.- Parameters:
baseClassifier- the base learner to use.extraSamples- how many extra samples past the training size to takesimultaniousTraining- controls whether base learners are trained sequentially or simultaneously
-
Bagging
public Bagging(Classifier baseClassifier, int extraSamples, boolean simultaniousTraining, int rounds, java.util.Random random)
Creates a new Bagger for classification. This can not be changed after construction.- Parameters:
baseClassifier- the base learner to use.extraSamples- how many extra samples past the training size to takesimultaniousTraining- controls whether base learners are trained sequentially or simultaneouslyrounds- how many rounds of bagging to perform.random- the source of randomness for sampling
-
Bagging
public Bagging(Regressor baseRegressor)
Creates a new Bagger for regression. This can not be changed after construction.- Parameters:
baseRegressor- the base learner to use.
-
Bagging
public Bagging(Regressor baseRegressor, int extraSamples, boolean simultaniousTraining)
Creates a new Bagger for regression. This can not be changed after construction.- Parameters:
baseRegressor- the base learner to use.extraSamples- how many extra samples past the training size to takesimultaniousTraining- controls whether base learners are trained sequentially or simultaneously
-
Bagging
public Bagging(Regressor baseRegressor, int extraSamples, boolean simultaniousTraining, int rounds, java.util.Random random)
Creates a new Bagger for regression. This can not be changed after construction.- Parameters:
baseRegressor- the base learner to use.extraSamples- how many extra samples past the training size to takesimultaniousTraining- controls whether base learners are trained sequentially or simultaneouslyrounds- how many rounds of bagging to perform.random- the source of randomness for sampling
-
-
Method Detail
-
setExtraSamples
public void setExtraSamples(int i)
Bagging samples from the training set with replacement, and draws a sampleWithReplacement at least as large as the training set. This controls how many extra samples are taken. If negative, fewer samples will be taken. Using negative values is not recommended.- Parameters:
i- how many extra samples to take
-
getExtraSamples
public int getExtraSamples()
-
setRounds
public void setRounds(int rounds)
Sets the number of rounds that bagging is done, meaning how many base learners are trained- Parameters:
rounds- the number of base learners to train- Throws:
java.lang.ArithmeticException- if the number specified is not a positive value
-
getRounds
public int getRounds()
Returns the number of rounds of boosting that will be done, which is also the number of base learners that will be trained- Returns:
- the number of rounds of boosting that will be done, which is also the number of base learners that will be trained
-
setSimultaniousTraining
public void setSimultaniousTraining(boolean simultaniousTraining)
Bagging produces multiple base learners. These can all be trained at the same time, using more memory, or sequentially using the base learner's parallel training method. If set to true, the base learners will be trained simultaneously.- Parameters:
simultaniousTraining- true to train all learners at the same time, false to train them sequentially
-
classify
public CategoricalResults classify(DataPoint data)
Description copied from interface:ClassifierPerforms classification on the given data point.- Specified by:
classifyin interfaceClassifier- Parameters:
data- the data point to classify- Returns:
- the results of the classification.
-
train
public void train(ClassificationDataSet dataSet, boolean parallel)
Description copied from interface:ClassifierTrains the classifier and constructs a model for classification using the given data set. If the training method knows how, it will used the threadPool to conduct training in parallel. This method will block until the training has completed.- Specified by:
trainin interfaceClassifier- Parameters:
dataSet- the data set to train onparallel-trueif multiple threads should be used to train the model.falseif it should be done in a single threaded manner.
-
getSampledDataSet
public static ClassificationDataSet getSampledDataSet(ClassificationDataSet dataSet, int[] sampledCounts)
Creates a new data set from the given sample counts. Points sampled multiple times will have multiple entries in the data set.- Parameters:
dataSet- the data set that was sampled fromsampledCounts- the sampling values obtained fromsampleWithReplacement(int[], int, java.util.Random)- Returns:
- a new sampled classification data set
-
getWeightSampledDataSet
public static ClassificationDataSet getWeightSampledDataSet(ClassificationDataSet dataSet, int[] sampledCounts)
Creates a new data set from the given sample counts. Points sampled multiple times will be added once to the data set with their weight multiplied by the number of times it was sampled.- Parameters:
dataSet- the data set that was sampled fromsampledCounts- the sampling values obtained fromsampleWithReplacement(int[], int, java.util.Random)- Returns:
- a new sampled classification data set
-
getSampledDataSet
public static RegressionDataSet getSampledDataSet(RegressionDataSet dataSet, int[] sampledCounts)
Creates a new data set from the given sample counts. Points sampled multiple times will have multiple entries in the data set.- Parameters:
dataSet- the data set that was sampled fromsampledCounts- the sampling values obtained fromsampleWithReplacement(int[], int, java.util.Random)- Returns:
- a new sampled classification data set
-
getWeightSampledDataSet
public static RegressionDataSet getWeightSampledDataSet(RegressionDataSet dataSet, int[] sampledCounts)
Creates a new data set from the given sample counts. Points sampled multiple times will be added once to the data set with their weight multiplied by the number of times it was sampled.- Parameters:
dataSet- the data set that was sampled fromsampledCounts- the sampling values obtained fromsampleWithReplacement(int[], int, java.util.Random)- Returns:
- a new sampled classification data set
-
sampleWithReplacement
public static void sampleWithReplacement(int[] sampleCounts, int samples, java.util.Random rand)Performs the sampling based on the number of data points, storing the counts in an array to be constructed from XXXX- Parameters:
sampleCounts- an array to keep count of how many times each data point was sampled. The array will be filled with zeros before sampling startssamples- the number of samples to take from the data setrand- the source of randomness
-
supportsWeightedData
public boolean supportsWeightedData()
Description copied from interface:ClassifierIndicates whether the model knows how to train using weighted data points. If it does, the model will train assuming the weights. The values returned by this method may change depending on the parameters set for the model.- Specified by:
supportsWeightedDatain interfaceClassifier- Specified by:
supportsWeightedDatain interfaceRegressor- Returns:
- true if the model supports weighted data, false otherwise
-
train
public void train(RegressionDataSet dataSet, boolean parallel)
-
clone
public Bagging clone()
- Specified by:
clonein interfaceClassifier- Specified by:
clonein interfaceRegressor- Overrides:
clonein classjava.lang.Object
-
-
DataMelt 3.0 © DataMelt by jWork.ORG