ClassificationData ClassificationData::getTestFoldData(const UINT foldIndex) const{ ClassificationData testData; testData.setNumDimensions( numDimensions ); testData.setAllowNullGestureClass( allowNullGestureClass ); if( !crossValidationSetup ) return testData; if( foldIndex >= kFoldValue ) return testData; //Add the class labels to make sure they all exist for(UINT k=0; k<getNumSamples(); k++){ testData.addClass( classTracker[k].classLabel, classTracker[k].className ); } testData.reserve( (UINT)crossValidationIndexs[ foldIndex ].size() ); //Add the data to the test fold UINT index = 0; for(UINT i=0; i<crossValidationIndexs[ foldIndex ].size(); i++){ index = crossValidationIndexs[ foldIndex ][i]; testData.addSample( data[ index ].getClassLabel(), data[ index ].getSample() ); } //Sort the class labels testData.sortClassLabels(); return testData; }
ClassificationData ClassificationData::getTrainingFoldData(const UINT foldIndex) const{ ClassificationData trainingData; trainingData.setNumDimensions( numDimensions ); trainingData.setAllowNullGestureClass( allowNullGestureClass ); if( !crossValidationSetup ){ errorLog << "getTrainingFoldData(const UINT foldIndex) - Cross Validation has not been setup! You need to call the spiltDataIntoKFolds(UINT K,bool useStratifiedSampling) function first before calling this function!" << endl; return trainingData; } if( foldIndex >= kFoldValue ) return trainingData; //Add the class labels to make sure they all exist for(UINT k=0; k<getNumSamples(); k++){ trainingData.addClass( classTracker[k].classLabel, classTracker[k].className ); } //Add the data to the training set, this will consist of all the data that is NOT in the foldIndex UINT index = 0; for(UINT k=0; k<kFoldValue; k++){ if( k != foldIndex ){ for(UINT i=0; i<crossValidationIndexs[k].size(); i++){ index = crossValidationIndexs[k][i]; trainingData.addSample( data[ index ].getClassLabel(), data[ index ].getSample() ); } } } //Sort the class labels trainingData.sortClassLabels(); return trainingData; }
ClassificationData ClassificationData::getBootstrappedDataset(UINT numSamples) const{ Random rand; ClassificationData newDataset; newDataset.setNumDimensions( getNumDimensions() ); newDataset.setAllowNullGestureClass( allowNullGestureClass ); newDataset.setExternalRanges( externalRanges, useExternalRanges ); if( numSamples == 0 ) numSamples = totalNumSamples; newDataset.reserve( numSamples ); //Add all the class labels to the new dataset to ensure the dataset has a list of all the labels for(UINT k=0; k<getNumClasses(); k++){ newDataset.addClass( classTracker[k].classLabel ); } //Randomly select the training samples to add to the new data set UINT randomIndex; for(UINT i=0; i<numSamples; i++){ randomIndex = rand.getRandomNumberInt(0, totalNumSamples); newDataset.addSample(data[randomIndex].getClassLabel(), data[randomIndex].getSample()); } //Sort the class labels so they are in order newDataset.sortClassLabels(); return newDataset; }
ClassificationData ClassificationData::getBootstrappedDataset(UINT numSamples,bool balanceDataset) const{ Random rand; ClassificationData newDataset; newDataset.setNumDimensions( getNumDimensions() ); newDataset.setAllowNullGestureClass( allowNullGestureClass ); newDataset.setExternalRanges( externalRanges, useExternalRanges ); if( numSamples == 0 ) numSamples = totalNumSamples; newDataset.reserve( numSamples ); const UINT K = getNumClasses(); //Add all the class labels to the new dataset to ensure the dataset has a list of all the labels for(UINT k=0; k<K; k++){ newDataset.addClass( classTracker[k].classLabel ); } if( balanceDataset ){ //Group the class indexs std::vector< std::vector< UINT > > classIndexs( K ); for(UINT i=0; i<totalNumSamples; i++){ classIndexs[ getClassLabelIndexValue( data[i].getClassLabel() ) ].push_back( i ); } //Get the class with the minimum number of examples UINT numSamplesPerClass = (UINT)floor( numSamples / double(K) ); //Randomly select the training samples from each class UINT classIndex = 0; UINT classCounter = 0; UINT randomIndex = 0; for(UINT i=0; i<numSamples; i++){ randomIndex = rand.getRandomNumberInt(0, (UINT)classIndexs[ classIndex ].size() ); randomIndex = classIndexs[ classIndex ][ randomIndex ]; newDataset.addSample(data[ randomIndex ].getClassLabel(), data[ randomIndex ].getSample()); if( classCounter++ >= numSamplesPerClass && classIndex+1 < K ){ classCounter = 0; classIndex++; } } }else{ //Randomly select the training samples to add to the new data set UINT randomIndex; for(UINT i=0; i<numSamples; i++){ randomIndex = rand.getRandomNumberInt(0, totalNumSamples); newDataset.addSample( data[randomIndex].getClassLabel(), data[randomIndex].getSample() ); } } //Sort the class labels so they are in order newDataset.sortClassLabels(); return newDataset; }