
Nuno Moniz
nuno.moniz@fc.up.pt
The setting
Main Goal of Performance Estimation Obtain a reliable estimate of the expected prediction error of a model on the unknown data distribution
The golden rule of Performance Estimation: The data used for evaluating (or comparing) any models cannot be seen during model development.
library(DMwR)
data(Boston, package='MASS')
## random selection of the holdout
trPerc <- 0.7
sp <- sample(1:nrow(Boston), as.integer(trPerc * nrow(Boston)))
## division in two samples
tr <- Boston[sp,]
ts <- Boston[-sp,]
## obtaining the model and respective predictions on the test set
m <- rpartXse(medv ~ ., tr)
p <- predict(m, ts)
## evaluation
regr.eval(ts$medv, p, train.y = tr$medv)
## mae mse rmse mape nmse nmae
## 3.0525037 17.9729392 4.2394503 0.1619334 0.2558434 0.5084804
data(Boston, package='MASS')
nreps <- 200
scores <- vector("numeric", length = nreps)
n <- nrow(Boston)
for(i in 1:nreps) {
# random sample with replacement
sp <- sample(n, n, replace = TRUE) # data splitting
tr <- Boston[sp,]
ts <- Boston[-sp,]
# model learning and prediction
m <- lm(medv ~ ., tr)
p <- predict(m, ts)
# evaluation
scores[i] <- mean((ts$medv - p)^2)
}
# calculating means and standard errors
summary(scores)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 13.97 21.90 24.12 24.62 26.66 39.11
install.packages("performanceEstimation")
performanceEstimation()
library(performanceEstimation)
library(DMwR)
data(Boston,package='MASS')
res <- performanceEstimation(PredTask(medv ~ ., Boston),
Workflow("standardWF", learner="rpartXse"),
EstimationTask(metrics="mse", method=CV(nReps = 1, nFolds = 10)))
##
##
## ##### PERFORMANCE ESTIMATION USING CROSS VALIDATION #####
##
## ** PREDICTIVE TASK :: Boston.medv
##
## ++ MODEL/WORKFLOW :: rpartXse
## Task for estimating mse using
## 1 x 10 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
summary(res)
## Length Class Mode
## Boston.medv 1 -none- list
plot(res)
data(iris)
PredTask(Species ~ ., iris)
## Prediction Task Object:
## Task Name :: iris.Species
## Task Type :: classification
## Target Feature :: Species
## Formula :: Species ~ .
## <environment: 0x7f93149f29b8>
## Task Data Source :: iris
standardWF
for classification and regressiontimeseriesWF
for time series forecastinglibrary(e1071)
Workflow("standardWF", learner = "svm", learner.pars = list(cost = 10, gamma = 0.1))
## Workflow Object:
## Workflow ID :: svm
## Workflow Function :: standardWF
## Parameter values:
## learner -> svm
## learner.pars -> cost=10 gamma=0.1
"standardWF" can be omitted ...
Workflow(learner="svm", learner.pars = list(cost = 5))
## Workflow Object:
## Workflow ID :: svm
## Workflow Function :: standardWF
## Parameter values:
## learner -> svm
## learner.pars -> cost=5
learner
- which function is used to obtain the model for the training datalearner.par
` - list with the parameter settings to pass to the learnerpredictor
- function used to obtain the predictions (defaults to predict()
)
+`predictor.pars
- list with the parameter settings to pass to the predictorpre
- vector with function names to be applied to the training and test sets before learningpre.pars
- list with the parameter settings to pass to the functionspost
- vector with function names to be applied to the predictions post.pars
- list with the parameter settings to pass to the functionsdata(algae, package="DMwR")
res <- performanceEstimation(PredTask(a1 ~ ., algae[, 1:12], "A1"),
Workflow(learner = "lm", pre = "centralImp", post = "onlyPos"),
EstimationTask("mse", method = CV())) # defaults to 1x10-fold CV
##
##
## ##### PERFORMANCE ESTIMATION USING CROSS VALIDATION #####
##
## ** PREDICTIVE TASK :: A1
##
## ++ MODEL/WORKFLOW :: lm
## Task for estimating mse using
## 1 x 10 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
library(e1071)
data(Boston,package="MASS")
res2 <- performanceEstimation(PredTask(medv ~ .,Boston),
workflowVariants(learner="svm",learner.pars=list(cost=1:5,gamma=c(0.1,0.01))),
EstimationTask(metrics="mse",method=CV()))
##
##
## ##### PERFORMANCE ESTIMATION USING CROSS VALIDATION #####
##
## ** PREDICTIVE TASK :: Boston.medv
##
## ++ MODEL/WORKFLOW :: svm.v1
## Task for estimating mse using
## 1 x 10 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
##
##
## ++ MODEL/WORKFLOW :: svm.v2
## Task for estimating mse using
## 1 x 10 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
##
##
## ++ MODEL/WORKFLOW :: svm.v3
## Task for estimating mse using
## 1 x 10 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
##
##
## ++ MODEL/WORKFLOW :: svm.v4
## Task for estimating mse using
## 1 x 10 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
##
##
## ++ MODEL/WORKFLOW :: svm.v5
## Task for estimating mse using
## 1 x 10 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
##
##
## ++ MODEL/WORKFLOW :: svm.v6
## Task for estimating mse using
## 1 x 10 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
##
##
## ++ MODEL/WORKFLOW :: svm.v7
## Task for estimating mse using
## 1 x 10 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
##
##
## ++ MODEL/WORKFLOW :: svm.v8
## Task for estimating mse using
## 1 x 10 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
##
##
## ++ MODEL/WORKFLOW :: svm.v9
## Task for estimating mse using
## 1 x 10 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
##
##
## ++ MODEL/WORKFLOW :: svm.v10
## Task for estimating mse using
## 1 x 10 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
summary(res2)
## Length Class Mode
## Boston.medv 10 -none- list
getWorkflow("svm.v1", res2)
## Workflow Object:
## Workflow ID :: svm.v1
## Workflow Function :: standardWF
## Parameter values:
## learner.pars -> cost=1 gamma=0.1
## learner -> svm
topPerformers(res2)
## $Boston.medv
## Workflow Estimate
## mse svm.v5 10.287
plot(res2)
EstimationTask(metrics = c("F", "rec", "prec"), method = Bootstrap(nReps = 100))
## Task for estimating F,rec,prec using
## 100 repetitions of e0 Bootstrap experiment
## Run with seed = 1234
classificationMetrics
and regressionMetrics
EstimationTask
constructorlibrary(randomForest)
library(e1071)
res3 <- performanceEstimation(PredTask(medv ~ ., Boston),
workflowVariants("standardWF",learner=c("rpartXse","svm","randomForest")),
EstimationTask(metrics="mse",method=CV(nReps=2,nFolds=5)))
##
##
## ##### PERFORMANCE ESTIMATION USING CROSS VALIDATION #####
##
## ** PREDICTIVE TASK :: Boston.medv
##
## ++ MODEL/WORKFLOW :: rpartXse
## Task for estimating mse using
## 2 x 5 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
##
##
## ++ MODEL/WORKFLOW :: svm
## Task for estimating mse using
## 2 x 5 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
##
##
## ++ MODEL/WORKFLOW :: randomForest
## Task for estimating mse using
## 2 x 5 - Fold Cross Validation
## Run with seed = 1234
## Iteration :**********
rankWorkflows(res3, 3)
## $Boston.medv
## $Boston.medv$mse
## Workflow Estimate
## 1 randomForest 10.65158
## 2 svm 14.83765
## 3 rpartXse 22.27489
plot(res3)
data(Glass,package='mlbench')
res4 <- performanceEstimation(PredTask(Type ~ ., Glass),
workflowVariants(learner="svm", # You may omit "standardWF" !
learner.pars=list(cost=c(1,10),gamma=c(0.1,0.01))),
EstimationTask(metrics="err",method=Holdout(nReps=5,hldSz=0.3)))
##
##
## ##### PERFORMANCE ESTIMATION USING HOLD OUT #####
##
## ** PREDICTIVE TASK :: Glass.Type
##
## ++ MODEL/WORKFLOW :: svm.v1
## Task for estimating err using
## 5 x 70 % / 30 % Holdout
## Run with seed = 1234
## Iteration : 1 2 3 4 5
##
##
## ++ MODEL/WORKFLOW :: svm.v2
## Task for estimating err using
## 5 x 70 % / 30 % Holdout
## Run with seed = 1234
## Iteration : 1 2 3 4 5
##
##
## ++ MODEL/WORKFLOW :: svm.v3
## Task for estimating err using
## 5 x 70 % / 30 % Holdout
## Run with seed = 1234
## Iteration : 1 2 3 4 5
##
##
## ++ MODEL/WORKFLOW :: svm.v4
## Task for estimating err using
## 5 x 70 % / 30 % Holdout
## Run with seed = 1234
## Iteration : 1 2 3 4 5
plot(res4)
Load in the data set algae and answer the following questions:
Estimate the MSE of a regression tree for forecasting alga a1 using 10-fold Cross validation.
Repeat the previous exercise this time trying some variants of random forests. Check what are the characteristics of the best performing variant.
Compare the results in terms of mean absolute error of the default variants of a regression tree, a linear regression model and a random forest, in the task of predicting alga a3. Use 2 repetitions of a 5-fold Cross Validation experiment.
Carry out an experiment designed to select what are the best models for each of the seven harmful algae. Use 10-fold Cross Validation. For illustrative purposes consider only the default variants of regression trees, linear regression and random forests.