本文整理汇总了C++中ModelType::setTree方法的典型用法代码示例。如果您正苦于以下问题:C++ ModelType::setTree方法的具体用法?C++ ModelType::setTree怎么用?C++ ModelType::setTree使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ModelType
的用法示例。
在下文中一共展示了ModelType::setTree方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: train
//Classification
void CARTTrainer::train(ModelType& model, ClassificationDataset const& dataset){
//Store the number of input dimensions
m_inputDimension = inputDimension(dataset);
//Pass input dimension (i.e., number of attributes) to tree model
model.setInputDimension(m_inputDimension);
//Find the largest label, so we know how big the histogram should be
m_maxLabel = static_cast<unsigned int>(numberOfClasses(dataset))-1;
// create cross-validation folds
ClassificationDataset set=dataset;
CVFolds<ClassificationDataset> folds = createCVSameSizeBalanced(set, m_numberOfFolds);
//find the best tree for the cv folds
double bestErrorRate = std::numeric_limits<double>::max();
CARTClassifier<RealVector>::TreeType bestTree;
//Run through all the cross validation sets
for (unsigned fold = 0; fold < m_numberOfFolds; ++fold) {
ClassificationDataset dataTrain = folds.training(fold);
ClassificationDataset dataTest = folds.validation(fold);
//Create attribute tables
//O.K. stores how often label(i) can be found in the dataset
//O.K. TODO: std::vector<unsigned int> is sufficient
boost::unordered_map<std::size_t, std::size_t> cAbove = createCountMatrix(dataTrain);
AttributeTables tables = createAttributeTables(dataTrain.inputs());
//create initial tree for the fold
CARTClassifier<RealVector>::TreeType tree = buildTree(tables, dataTrain, cAbove, 0);
model.setTree(tree);
while(true){
ZeroOneLoss<unsigned int, RealVector> loss;
double errorRate = loss.eval(dataTest.labels(), model(dataTest.inputs()));
if(errorRate < bestErrorRate){
//We have found a subtree that has a smaller error rate when tested!
bestErrorRate = errorRate;
bestTree = tree;
}
if(tree.size()!=1) break;
pruneTree(tree);
model.setTree(tree);
}
}
SHARK_CHECK(bestTree.size() > 0, "We should never set a tree that is empty.");
model.setTree(bestTree);
}
示例2: train
//Train model with a regression dataset
void CARTTrainer::train(ModelType& model, RegressionDataset const& dataset)
{
//Store the number of input dimensions
m_inputDimension = inputDimension(dataset);
//Pass input dimension (i.e., number of attributes) to tree model
model.setInputDimension(m_inputDimension);
//Store the size of the labels
m_labelDimension = labelDimension(dataset);
// create cross-validation folds
RegressionDataset set=dataset;
CVFolds<RegressionDataset > folds = createCVSameSize(set, m_numberOfFolds);
double bestErrorRate = std::numeric_limits<double>::max();
CARTClassifier<RealVector>::TreeType bestTree;
for (unsigned fold = 0; fold < m_numberOfFolds; ++fold){
//Run through all the cross validation sets
RegressionDataset dataTrain = folds.training(fold);
RegressionDataset dataTest = folds.validation(fold);
std::size_t numTrainElements = dataTrain.numberOfElements();
AttributeTables tables = createAttributeTables(dataTrain.inputs());
std::vector < RealVector > labels(numTrainElements);
boost::copy(dataTrain.labels().elements(),labels.begin());
//Build tree form this fold
CARTClassifier<RealVector>::TreeType tree = buildTree(tables, dataTrain, labels, 0, dataTrain.numberOfElements());
//Add the tree to the model and prune
model.setTree(tree);
while(true){
//evaluate the error of current tree
SquaredLoss<> loss;
double error = loss.eval(dataTest.labels(), model(dataTest.inputs()));
if(error < bestErrorRate){
//We have found a subtree that has a smaller error rate when tested!
bestErrorRate = error;
bestTree = tree;
}
if(tree.size() == 1) break;
pruneTree(tree);
model.setTree(tree);
}
}
SHARK_CHECK(bestTree.size() > 0, "We should never set a tree that is empty.");
model.setTree(bestTree);
}