makeWeightedClassesWrapper: Wraps a classifier for weighted fitting where each class...

Description Usage Arguments Value See Also Examples

View source: R/WeightedClassesWrapper.R

Description

Creates a wrapper, which can be used like any other learner object.

Fitting is performed in a weighted fashion where each observation receives a weight, depending on the class it belongs to, see wcw.weight. This might help to mitigate problems caused by imbalanced class distributions.

This weighted fitting can be achieved in two ways:

a) The learner already has a parameter for class weighting, so one weight can directly be defined per class. Example: “classif.ksvm” and parameter class.weights. In this case we don't really do anything fancy. We convert wcw.weight a bit, but basically simply bind its value to the class weighting param. The wrapper in this case simply offers a convenient, consistent fashion for class weighting - and tuning! See example below.

b) The learner does not have a direct parameter to support class weighting, but supports observation weights, so hasLearnerProperties(learner, 'weights') is TRUE. This means that an individual, arbitrary weight can be set per observation during training. We set this weight depending on the class internally in the wrapper. Basically we introduce something like a new “class.weights” parameter for the learner via observation weights.

Usage

1
makeWeightedClassesWrapper(learner, wcw.param = NULL, wcw.weight = 1)

Arguments

learner

(Learner | character(1))
The classification learner. If you pass a string the learner will be created via makeLearner.

wcw.param

(character(1))
Name of already existing learner parameter, which allows class weighting. The default (wcw.param = NULL) will use the parameter defined in the learner (class.weights.param). During training, the parameter must accept a named vector of class weights, where length equals the number of classes.

wcw.weight

(numeric)
Weight for each class. Must be a vector of the same number of elements as classes are in task, and must also be in the same order as the class levels are in getTaskDesc(task)$class.levels. For convenience, one must pass a single number in case of binary classification, which is then taken as the weight of the positive class, while the negative class receives a weight of 1. Default is 1.

Value

Learner.

See Also

Other wrapper: makeBaggingWrapper, makeClassificationViaRegressionWrapper, makeConstantClassWrapper, makeCostSensClassifWrapper, makeCostSensRegrWrapper, makeDownsampleWrapper, makeDummyFeaturesWrapper, makeExtractFDAFeatsWrapper, makeFeatSelWrapper, makeFilterWrapper, makeImputeWrapper, makeMulticlassWrapper, makeMultilabelBinaryRelevanceWrapper, makeMultilabelClassifierChainsWrapper, makeMultilabelDBRWrapper, makeMultilabelNestedStackingWrapper, makeMultilabelStackingWrapper, makeOverBaggingWrapper, makePreprocWrapperCaret, makePreprocWrapper, makeRemoveConstantFeaturesWrapper, makeSMOTEWrapper, makeTuneWrapper, makeUndersampleWrapper

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# using the direct parameter of the SVM (which is already defined in the learner)
lrn = makeWeightedClassesWrapper("classif.ksvm", wcw.weight = 0.01)
res = holdout(lrn, sonar.task)
print(calculateConfusionMatrix(res$pred))

# using the observation weights of logreg
lrn = makeWeightedClassesWrapper("classif.logreg", wcw.weight = 0.01)
res = holdout(lrn, sonar.task)
print(calculateConfusionMatrix(res$pred))

# tuning the imbalancy param and the SVM param in one go
lrn = makeWeightedClassesWrapper("classif.ksvm", wcw.param = "class.weights")
ps = makeParamSet(
  makeNumericParam("wcw.weight", lower = 1, upper = 10),
  makeNumericParam("C", lower = -12, upper = 12, trafo = function(x) 2^x),
  makeNumericParam("sigma", lower = -12, upper = 12, trafo = function(x) 2^x)
)
ctrl = makeTuneControlRandom(maxit = 3L)
rdesc = makeResampleDesc("CV", iters = 2L, stratify = TRUE)
res = tuneParams(lrn, sonar.task, rdesc, par.set = ps, control = ctrl)
print(res)
print(res$opt.path)

Example output

Loading required package: ParamHelpers
[Resample] holdout iter 1: mmce.test.mean=0.557
[Resample] Aggr. Result: mmce.test.mean=0.557
        predicted
true     M  R -err.-
  M      0 39     39
  R      0 31      0
  -err.- 0 39     39
[Resample] holdout iter 1: mmce.test.mean=0.257
[Resample] Aggr. Result: mmce.test.mean=0.257
Warning messages:
1: glm.fit: algorithm did not converge 
2: glm.fit: fitted probabilities numerically 0 or 1 occurred 
        predicted
true      M  R -err.-
  M      32  6      6
  R      12 20     12
  -err.- 12  6     18
[Tune] Started tuning learner weightedclasses.classif.ksvm for parameter set:
              Type len Def    Constr Req Tunable Trafo
wcw.weight numeric   -   -   1 to 10   -    TRUE     -
C          numeric   -   - -12 to 12   -    TRUE     Y
sigma      numeric   -   - -12 to 12   -    TRUE     Y
With control class: TuneControlRandom
Imputation value: 1
[Tune-x] 1: wcw.weight=1.14; C=3.11; sigma=1.26e+03
[Tune-y] 1: mmce.test.mean=0.466; time: 0.0 min
[Tune-x] 2: wcw.weight=1.16; C=2.81e+03; sigma=3
[Tune-y] 2: mmce.test.mean=0.466; time: 0.0 min
[Tune-x] 3: wcw.weight=2.23; C=0.0295; sigma=0.00136
[Tune-y] 3: mmce.test.mean=0.466; time: 0.0 min
[Tune] Result: wcw.weight=1.14; C=3.11; sigma=1.26e+03 : mmce.test.mean=0.466
Tune result:
Op. pars: wcw.weight=1.14; C=3.11; sigma=1.26e+03
mmce.test.mean=0.466
Optimization path
  Dimensions: x = 3/3, y = 1
  Length: 3
  Add x values transformed: FALSE
  Error messages: TRUE. Errors: 0 / 3.
  Exec times: TRUE. Range: 0.057 - 1.013. 0 NAs.

mlr documentation built on April 1, 2018, 12:03 p.m.