Skip to content

Commit

Permalink
Merge 3e338fb into 3a68289
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Schalk committed Jan 21, 2018
2 parents 3a68289 + 3e338fb commit ad88234
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Readme.md
Expand Up @@ -31,8 +31,8 @@ devtools::install_github("schalkdaniel/compboost")
- [ ] Prediction:
- [x] General predict function on trian data
- [ ] Predict function for iteration `k < iter.max`
- [ ] Prediction on newdata
- [ ] Prediction on newdata for iteration `k < iter.max`
- [x] Prediction on newdata
- [x] Prediction on newdata for iteration `k < iter.max`

- [ ] Tests:
- [ ] Iterate over tests (they are notd coded very well)
Expand Down
56 changes: 56 additions & 0 deletions src/compboost.cpp
Expand Up @@ -183,6 +183,62 @@ std::pair<std::vector<std::string>, arma::mat> Compboost::GetParameterMatrix ()
return blearner_track.GetParameterMatrix();
}

arma::vec Compboost::Predict (std::map<std::string, arma::mat> data_map)
{
// std::cout << "Get into Compboost::Predict" << std::endl;

std::map<std::string, arma::mat> parameter_map = blearner_track.GetParameterMap();

arma::vec pred(data_map.begin()->second.n_rows);
pred.fill(initialization);

// std::cout << "initialize pred vec" << std::endl;

for (auto& it : parameter_map) {

std::string sel_factory = it.first;

// std::cout << "Fatory id of parameter map: " << sel_factory << std::endl;

blearnerfactory::BaselearnerFactory* sel_factory_obj = used_baselearner_list.GetMap().find(sel_factory)->second;

// std::cout << "Data of selected factory: " << sel_factory_obj->GetDataIdentifier() << std::endl;

arma::mat data_trafo = sel_factory_obj->InstantiateData((data_map.find(sel_factory_obj->GetDataIdentifier())->second));
pred += data_trafo * it.second;

}
return pred;
}

arma::vec Compboost::PredictionOfIteration (std::map<std::string, arma::mat> data_map, unsigned int k)
{
// std::cout << "Get into Compboost::Predict" << std::endl;

std::map<std::string, arma::mat> parameter_map = blearner_track.GetEstimatedParameterForIteration(k);

arma::vec pred(data_map.begin()->second.n_rows);
pred.fill(initialization);

// std::cout << "initialize pred vec" << std::endl;

for (auto& it : parameter_map) {

std::string sel_factory = it.first;

// std::cout << "Fatory id of parameter map: " << sel_factory << std::endl;

blearnerfactory::BaselearnerFactory* sel_factory_obj = used_baselearner_list.GetMap().find(sel_factory)->second;

// std::cout << "Data of selected factory: " << sel_factory_obj->GetDataIdentifier() << std::endl;

arma::mat data_trafo = sel_factory_obj->InstantiateData((data_map.find(sel_factory_obj->GetDataIdentifier())->second));
pred += data_trafo * it.second;

}
return pred;
}

// Destructor:
Compboost::~Compboost ()
{
Expand Down
3 changes: 2 additions & 1 deletion src/compboost.h
Expand Up @@ -97,7 +97,8 @@ class Compboost

std::pair<std::vector<std::string>, arma::mat> GetParameterMatrix ();

// arma::mat Predict (std::map<std::string, arma::mat>);
arma::vec Predict (std::map<std::string, arma::mat>);
arma::vec PredictionOfIteration (std::map<std::string, arma::mat>, unsigned int);

// Destructor:
~Compboost ();
Expand Down
32 changes: 32 additions & 0 deletions src/compboost_modules.cpp
Expand Up @@ -790,6 +790,36 @@ class CompboostWrapper
);
}

arma::vec predict (Rcpp::List input_data)
{
std::map<std::string, arma::mat> data_map;

// Create data map:
for (unsigned int i = 0; i < input_data.size(); i++) {

std::vector<std::string> names = input_data.names();
arma::mat temp = Rcpp::as<arma::mat>(input_data[i]);
data_map[ names[i] ] = temp;

}
return obj->Predict(data_map);
}

arma::vec predictionOfIteration (Rcpp::List input_data, unsigned int k)
{
std::map<std::string, arma::mat> data_map;

// Create data map:
for (unsigned int i = 0; i < input_data.size(); i++) {

std::vector<std::string> names = input_data.names();
arma::mat temp = Rcpp::as<arma::mat>(input_data[i]);
data_map[ names[i] ] = temp;

}
return obj->PredictionOfIteration(data_map, k);
}

// Destructor:
~CompboostWrapper ()
{
Expand Down Expand Up @@ -827,6 +857,8 @@ RCPP_MODULE (compboost_module)
.method("getEstimatedParameter", &CompboostWrapper::getEstimatedParameter, "Get the estimated paraemter")
.method("getEstimatedParameterOfIteration", &CompboostWrapper::getEstimatedParameterOfIteration, "Get the estimated parameter for iteration k < iter.max")
.method("getParameterMatrix", &CompboostWrapper::getParameterMatrix, "Get matrix of all estimated parameter in each iteration")
.method("predict", &CompboostWrapper::predict, "Predict newdata")
.method("predictionOfIteration", &CompboostWrapper::predictionOfIteration, "Predict newdata for iteration k < iter.max")
;
}

20 changes: 20 additions & 0 deletions tests/testthat/test_compboost.R
Expand Up @@ -16,6 +16,20 @@ test_that("compboost does the same as mboost", {

y = df[["mpg"]]

eval.hp = runif(10)
eval.wt = runif(10)

eval.data = list(
"hp" = as.matrix(eval.hp),
"wt" = as.matrix(eval.wt)
)

eval.df = data.frame(
hp = eval.hp,
wt = eval.wt,
hp2 = eval.hp^2
)

# Hyperparameter for the algorithm:
learning.rate = 0.05
iter.max = 500
Expand Down Expand Up @@ -163,5 +177,11 @@ test_that("compboost does the same as mboost", {

expect_equal(cboost$getParameterMatrix()$parameter.matrix[idx, ], matrix.compare)

# Test if prediction works:
# --------------------------

expect_equal(cboost$predict(eval.data), predict(mod, eval.df))
expect_equal(cboost$predictionOfIteration(eval.data, 200), predict(mod.reduced, eval.df))

})

9 changes: 9 additions & 0 deletions tutorials/compboost_class.R
Expand Up @@ -23,6 +23,11 @@ X.wt = as.matrix(df[["wt"]], ncol = 1)

y = df[["mpg"]]

eval.data = list(
"hp" = as.matrix(mtcars$hp),
"wt" = as.matrix(mtcars$wt)
)

# Hyperparameter for the algorithm:
learning.rate = 0.05
iter.max = 500
Expand Down Expand Up @@ -101,3 +106,7 @@ cboost$getLoggerData()

# Get parameter matrix:
param.matrix = cboost$getParameterMatrix()

# Predict for "new data":
cboost$predict(eval.data)
cboost$predictionOfIteration(eval.data, 200)
30 changes: 25 additions & 5 deletions tutorials/compboost_vs_mboost.R
Expand Up @@ -13,12 +13,26 @@ df = mtcars
# Create new variable to check the polynomial baselearner with degree 2:
df$hp2 = df[["hp"]]^2

# Data for compboost:
# Data for compboost, wt with intercept:
X.hp = as.matrix(df[["hp"]], ncol = 1)
X.wt = as.matrix(df[["wt"]], ncol = 1)
X.wt = cbind(1, df[["wt"]])

y = df[["mpg"]]

eval.hp = runif(10)
eval.wt = runif(10)

eval.data = list(
"hp" = as.matrix(eval.hp),
"wt" = cbind(1, eval.wt)
)

eval.df = data.frame(
hp = eval.hp,
wt = eval.wt,
hp2 = eval.hp^2
)

# Hyperparameter for the algorithm:
learning.rate = 0.05
iter.max = 500
Expand Down Expand Up @@ -91,7 +105,7 @@ library(mboost)

mod = mboost(
formula = mpg ~ bols(hp, intercept = FALSE) +
bols(wt, intercept = FALSE) +
bols(wt) +
bols(hp2, intercept = FALSE),
data = df,
control = boost_control(mstop = iter.max, nu = learning.rate)
Expand Down Expand Up @@ -135,7 +149,7 @@ microbenchmark::microbenchmark(
data = df,
control = boost_control(mstop = iter.max, nu = learning.rate)
),
times = 10L
times = 100L
)

# Profiling to compare used memory:
Expand All @@ -157,11 +171,17 @@ print(p)

mod.reduced = mboost(
formula = mpg ~ bols(hp, intercept = FALSE) +
bols(wt, intercept = FALSE) +
bols(wt) +
bols(hp2, intercept = FALSE),
data = df,
control = boost_control(mstop = 200, nu = learning.rate)
)

mod.reduced$coef()
cboost$getEstimatedParameterOfIteration(200)



predict(mod.reduced, eval.df)
cboost$predictionOfIteration(eval.data, 200)

0 comments on commit ad88234

Please sign in to comment.