Resampling
MLJBase.CV — Typecv = CV(; nfolds=6, shuffle=nothing, rng=nothing)Cross-validation resampling strategy, for use in evaluate!, evaluate and tuning.
train_test_pairs(cv, rows)Returns an nfolds-length iterator of (train, test) pairs of vectors (row indices), where each train and test is a sub-vector of rows. The test vectors are mutually exclusive and exhaust rows. Each train vector is the complement of the corresponding test vector. With no row pre-shuffling, the order of rows is preserved, in the sense that rows coincides precisely with the concatenation of the test vectors, in the order they are generated. The first r test vectors have length n + 1, where n, r = divrem(length(rows), nfolds), and the remaining test vectors have length n.
Pre-shuffling of rows is controlled by rng and shuffle. If rng is an integer, then the CV keyword constructor resets it to MersenneTwister(rng). Otherwise some AbstractRNG object is expected.
If rng is left unspecified, rng is reset to Random.GLOBAL_RNG, in which case rows are only pre-shuffled if shuffle=true is explicitly specified.
MLJBase.Holdout — Typeholdout = Holdout(; fraction_train=0.7,
shuffle=nothing,
rng=nothing)Holdout resampling strategy, for use in evaluate!, evaluate and in tuning.
train_test_pairs(holdout, rows)Returns the pair [(train, test)], where train and test are vectors such that rows=vcat(train, test) and length(train)/length(rows) is approximatey equal to fraction_train`.
Pre-shuffling of rows is controlled by rng and shuffle. If rng is an integer, then the Holdout keyword constructor resets it to MersenneTwister(rng). Otherwise some AbstractRNG object is expected.
If rng is left unspecified, rng is reset to Random.GLOBAL_RNG, in which case rows are only pre-shuffled if shuffle=true is specified.
MLJBase.Resampler — Typeresampler = Resampler(model=ConstantRegressor(),
resampling=CV(),
measure=nothing,
weights=nothing,
class_weights=nothing
operation=predict,
repeats = 1,
acceleration=default_resource(),
check_measure=true)Resampling model wrapper, used internally by the fit method of TunedModel instances. See `evaluate! for options. Not intended for general use.
Given a machine mach = machine(resampler, args...) one obtains a performance evaluation of the specified model, performed according to the prescribed resampling strategy and other parameters, using data args..., by calling fit!(mach) followed by evaluate(mach).
The resampler internally binds model to the supplied data args in a machine (called mach_train below) on which evaluate! is called with all the options passed to the Resampler constructor. The advantage over using evaluate(model, args...) directly is that, for Holdout resampling, calling fit!(mach) only triggers a cold restart of mach_train if necessary.
The sample weights are passed to the specified performance measures that support weights for evaluation. These weights are not to be confused with any weights bound to a Resampler instance in a machine, used for training the wrapped model when supported.
The sample class_weights are passed to the specified performance measures that support per-class weights for evaluation. These weights are not to be confused with any weights bound to a Resampler instance in a machine, used for training the wrapped model when supported.
MLJBase.StratifiedCV — Typestratified_cv = StratifiedCV(; nfolds=6,
shuffle=false,
rng=Random.GLOBAL_RNG)Stratified cross-validation resampling strategy, for use in evaluate!, evaluate and in tuning. Applies only to classification problems (OrderedFactor or Multiclass targets).
train_test_pairs(stratified_cv, rows, y)Returns an nfolds-length iterator of (train, test) pairs of vectors (row indices) where each train and test is a sub-vector of rows. The test vectors are mutually exclusive and exhaust rows. Each train vector is the complement of the corresponding test vector.
Unlike regular cross-validation, the distribution of the levels of the target y corresponding to each train and test is constrained, as far as possible, to replicate that of y[rows] as a whole.
The stratified train_test_pairs algorithm is invariant to label renaming. For example, if you run replace!(y, 'a' => 'b', 'b' => 'a') and then re-run train_test_pairs, the returned (train, test) pairs will be the same.
Pre-shuffling of rows is controlled by rng and shuffle. If rng is an integer, then the StratifedCV keyword constructor resets it to MersenneTwister(rng). Otherwise some AbstractRNG object is expected.
If rng is left unspecified, rng is reset to Random.GLOBAL_RNG, in which case rows are only pre-shuffled if shuffle=true is explicitly specified.
MLJBase.evaluate! — Methodevaluate!(mach,
resampling=CV(),
measure=nothing,
rows=nothing,
weights=nothing,
class_weights=nothing,
operation=predict,
repeats=1,
acceleration=default_resource(),
force=false,
verbosity=1,
check_measure=true)Estimate the performance of a machine mach wrapping a supervised model in data, using the specified resampling strategy (defaulting to 6-fold cross-validation) and measure, which can be a single measure or vector.
Do subtypes(MLJ.ResamplingStrategy) to obtain a list of available resampling strategies. If resampling is not an object of type MLJ.ResamplingStrategy, then a vector of pairs (of the form (train_rows, test_rows) is expected. For example, setting
resampling = [(1:100), (101:200)),
(101:200), (1:100)]gives two-fold cross-validation using the first 200 rows of data.
The resampling strategy is applied repeatedly (Monte Carlo resampling) if repeats > 1. For example, if repeats = 10, then resampling = CV(nfolds=5, shuffle=true), generates a total of 50 (train, test) pairs for evaluation and subsequent aggregation.
If resampling isa MLJ.ResamplingStrategy then one may optionally restrict the data used in evaluation by specifying rows.
An optional weights vector may be passed for measures that support sample weights (MLJ.supports_weights(measure) == true), which is ignored by those that don't. These weights are not to be confused with any weights w bound to mach (as in mach = machine(model, X, y, w)). To pass these to the performance evaluation measures you must explictly specify weights=w in the evaluate! call.
Additionally, optional class_weights dictionary may be passed for measures that support class weights (MLJ.supports_class_weights(measure) == true), which is ignored by those that don't. These weights are not to be confused with any weights class_w bound to mach (as in mach = machine(model, X, y, class_w)). To pass these to the performance evaluation measures you must explictly specify class_weights=w in the evaluate! call.
User-defined measures are supported; see the manual for details.
If no measure is specified, then default_measure(mach.model) is used, unless this default is nothing and an error is thrown.
The acceleration keyword argument is used to specify the compute resource (a subtype of ComputationalResources.AbstractResource) that will be used to accelerate/parallelize the resampling operation.
Although evaluate! is mutating, mach.model and mach.args are untouched.
Summary of key-word arguments
resampling- resampling strategy (default isCV(nfolds=6))measure/measures- measure or vector of measures (losses, scores, etc)rows- vector of observation indices from which both train and test folds are constructed (default is all observations)weights- per-sample weights for measures that support them (not to be confused with weights used in training)class_weights- dictionary of per-class weights for use with measures that support these, in classification problems (not to be confused with per-sampleweightsor with class weights used in training)operation-predict,predict_mean,predict_modeorpredict_median;predictis the default but cannot be used with a deterministic measure ifmodel isa Probabilisticrepeats- default is 1; set to a higher value for repeated (Monte Carlo) resamplingacceleration- parallelization option; currently supported options are instances ofCPU1(single-threaded computation)CPUThreads(multi-threaded computation) andCPUProcesses(multi-process computation); default isdefault_resource().force- default isfalse; set totruefor force cold-restart of each training eventverbositylevel, an integer defaulting to 1.check_measure- default istrue
Return value
A property-accessible object of type PerformanceEvaluation with these properties:
measure: the vector of specified measuresmeasurements: the corresponding measurements, aggregated across the test folds using the aggregation method defined for each measure (doaggregation(measure)to inspect)per_fold: a vector of vectors of individual test fold evaluations (one vector per measure)per_observation: a vector of vectors of individual observation evaluations of those measures for whichreports_each_observation(measure)is true, which is otherwise reportedmissing
-fitted_params_per_fold: a vector containing fitted pamarms(mach) for each machine mach trained during resampling.
report_per_fold: a vector containingreport(mach)for each machinemachtraining in resampling
MLJModelInterface.evaluate — Methodevaluate(model, data...; cache=true, kw_options...)Equivalent to evaluate!(machine(model, data..., cache=cache); wk_options...). See the machine version evaluate! for the complete list of options.