TimeSeriesClassificationData TimeSeriesClassificationData::getClassData(const UINT classLabel) const { TimeSeriesClassificationData classData(numDimensions); for(UINT x=0; x<totalNumSamples; x++){ if( data[x].getClassLabel() == classLabel ){ classData.addSample( classLabel, data[x].getData() ); } } return classData; }
TimeSeriesClassificationData TimeSeriesClassificationDataStream::getAllTrainingExamplesWithClassLabel(const UINT classLabel) const { TimeSeriesClassificationData classData(numDimensions); for(UINT x=0; x<timeSeriesPositionTracker.size(); x++){ if( timeSeriesPositionTracker[x].getClassLabel() == classLabel && timeSeriesPositionTracker[x].getEndIndex() > 0){ Matrix<double> timeSeries; for(UINT i=timeSeriesPositionTracker[x].getStartIndex(); i<timeSeriesPositionTracker[x].getEndIndex(); i++){ timeSeries.push_back( data[ i ].getSample() ); } classData.addSample(classLabel,timeSeries); } } return classData; }
// ----------------------------------------------------------------------------- // CPIMAgnToDoAdapter::ReadIntFieldsL // (other items were commented in a header) // ----------------------------------------------------------------------------- // void CPIMAgnToDoAdapter::ReadIntFieldsL(MPIMItemData& aData, CCalEntry& aEntry) { JELOG2(EPim); // Convert synchornization field to PIM API. The default value is private // and unrecognized priority values are mapped also to private due to // security reasons TPIMToDoClassValue value = EPIMToDoClassPrivate; const CCalEntry::TReplicationStatus status = aEntry.ReplicationStatusL(); if (status == CCalEntry::EOpen) { // Open is mapped as public synhronization value = EPIMToDoClassPublic; } else if (status == CCalEntry::ERestricted) { // Open is mapped as user confidential synhronization value = EPIMToDoClassConfidential; } // Note that boolean and integer fields must be identified in the constructor TPIMFieldData classData(EPIMToDoClass, EPIMFieldInt, KPIMAttrNone, value); aData.AddValueL(classData); // Default value is zero acording to vCalendar specification. PIM API default // priority is medium which indicates normal native priority // See common/pimtodo.h for platform-specific native priorities. TUint nativePriority = aEntry.PriorityL(); TPIMToDoPriority priority = EPIMToDoPriorityMedium; if (nativePriority == EPIMToDoNativePriorityHigh) { // Native priority value High is mapped to value 1 priority = EPIMToDoPriorityHigh; } else if (nativePriority == EPIMToDoNativePriorityLow) { // Native priority value Low is mapped to value 7 priority = EPIMToDoPriorityLow; } // Note that boolean and integer fields must be identified in the constructor TPIMFieldData prData(EPIMToDoPriority, EPIMFieldInt, KPIMAttrNone, priority); aData.AddValueL(prData); }
bool TimeSeriesClassificationData::spiltDataIntoKFolds(const UINT K,const bool useStratifiedSampling){ crossValidationSetup = false; crossValidationIndexs.clear(); //K can not be zero if( K == 0 ){ errorLog << "spiltDataIntoKFolds(UINT K) - K can not be zero!" << std::endl; return false; } //K can not be larger than the number of examples if( K > totalNumSamples ){ errorLog << "spiltDataIntoKFolds(UINT K,bool useStratifiedSampling) - K can not be larger than the total number of samples in the dataset!" << std::endl; return false; } //K can not be larger than the number of examples in a specific class if the stratified sampling option is true if( useStratifiedSampling ){ for(UINT c=0; c<classTracker.size(); c++){ if( K > classTracker[c].counter ){ errorLog << "spiltDataIntoKFolds(UINT K,bool useStratifiedSampling) - K can not be larger than the number of samples in any given class!" << std::endl; return false; } } } //Setup the dataset for k-fold cross validation kFoldValue = K; Vector< UINT > indexs( totalNumSamples ); //Work out how many samples are in each fold, the last fold might have more samples than the others UINT numSamplesPerFold = (UINT) floor( totalNumSamples/Float(K) ); //Resize the cross validation indexs buffer crossValidationIndexs.resize( K ); //Create the random partion indexs Random random; UINT randomIndex = 0; if( useStratifiedSampling ){ //Break the data into seperate classes Vector< Vector< UINT > > classData( getNumClasses() ); //Add the indexs to their respective classes for(UINT i=0; i<totalNumSamples; i++){ classData[ getClassLabelIndexValue( data[i].getClassLabel() ) ].push_back( i ); } //Randomize the order of the indexs in each of the class index buffers for(UINT c=0; c<getNumClasses(); c++){ UINT numSamples = (UINT)classData[c].size(); for(UINT x=0; x<numSamples; x++){ //Pick a random index randomIndex = random.getRandomNumberInt(0,numSamples); //Swap the indexs SWAP( classData[c][ x ] , classData[c][ randomIndex ] ); } } //Loop over each of the classes and add the data equally to each of the k folds until there is no data left Vector< UINT >::iterator iter; for(UINT c=0; c<getNumClasses(); c++){ iter = classData[ c ].begin(); UINT k = 0; while( iter != classData[c].end() ){ crossValidationIndexs[ k ].push_back( *iter ); iter++; k++; k = k % K; } } }else{ //Randomize the order of the data for(UINT i=0; i<totalNumSamples; i++) indexs[i] = i; for(UINT x=0; x<totalNumSamples; x++){ //Pick a random index randomIndex = random.getRandomNumberInt(0,totalNumSamples); //Swap the indexs SWAP( indexs[ x ] , indexs[ randomIndex ] ); } UINT counter = 0; UINT foldIndex = 0; for(UINT i=0; i<totalNumSamples; i++){ //Add the index to the current fold crossValidationIndexs[ foldIndex ].push_back( indexs[i] ); //Move to the next fold if ready if( ++counter == numSamplesPerFold && foldIndex < K-1 ){ foldIndex++; counter = 0; } } } crossValidationSetup = true; return true; }
TimeSeriesClassificationData TimeSeriesClassificationData::split(const UINT trainingSizePercentage,const bool useStratifiedSampling){ //Partitions the dataset into a training dataset (which is kept by this instance of the TimeSeriesClassificationData) and //a testing/validation dataset (which is return as a new instance of the TimeSeriesClassificationData). The trainingSizePercentage //therefore sets the size of the data which remains in this instance and the remaining percentage of data is then added to //the testing/validation dataset //The dataset has changed so flag that any previous cross validation setup will now not work crossValidationSetup = false; crossValidationIndexs.clear(); TimeSeriesClassificationData trainingSet(numDimensions); TimeSeriesClassificationData testSet(numDimensions); trainingSet.setAllowNullGestureClass(allowNullGestureClass); testSet.setAllowNullGestureClass(allowNullGestureClass); Vector< UINT > indexs( totalNumSamples ); //Create the random partion indexs Random random; UINT randomIndex = 0; if( useStratifiedSampling ){ //Break the data into seperate classes Vector< Vector< UINT > > classData( getNumClasses() ); //Add the indexs to their respective classes for(UINT i=0; i<totalNumSamples; i++){ classData[ getClassLabelIndexValue( data[i].getClassLabel() ) ].push_back( i ); } //Randomize the order of the indexs in each of the class index buffers for(UINT k=0; k<getNumClasses(); k++){ UINT numSamples = (UINT)classData[k].size(); for(UINT x=0; x<numSamples; x++){ //Pick a random index randomIndex = random.getRandomNumberInt(0,numSamples); //Swap the indexs SWAP( classData[k][ x ] ,classData[k][ randomIndex ] ); } } //Loop over each class and add the data to the trainingSet and testSet for(UINT k=0; k<getNumClasses(); k++){ UINT numTrainingExamples = (UINT) floor( Float(classData[k].size()) / 100.0 * Float(trainingSizePercentage) ); //Add the data to the training and test sets for(UINT i=0; i<numTrainingExamples; i++){ trainingSet.addSample( data[ classData[k][i] ].getClassLabel(), data[ classData[k][i] ].getData() ); } for(UINT i=numTrainingExamples; i<classData[k].size(); i++){ testSet.addSample( data[ classData[k][i] ].getClassLabel(), data[ classData[k][i] ].getData() ); } } //Overwrite the training data in this instance with the training data of the trainingSet data = trainingSet.getClassificationData(); totalNumSamples = trainingSet.getNumSamples(); }else{ const UINT numTrainingExamples = (UINT) floor( Float(totalNumSamples) / 100.0 * Float(trainingSizePercentage) ); //Create the random partion indexs Random random; for(UINT i=0; i<totalNumSamples; i++) indexs[i] = i; for(UINT x=0; x<totalNumSamples; x++){ //Pick a random index randomIndex = random.getRandomNumberInt(0,totalNumSamples); //Swap the indexs SWAP( indexs[ x ] , indexs[ randomIndex ] ); } //Add the data to the training and test sets for(UINT i=0; i<numTrainingExamples; i++){ trainingSet.addSample( data[ indexs[i] ].getClassLabel(), data[ indexs[i] ].getData() ); } for(UINT i=numTrainingExamples; i<totalNumSamples; i++){ testSet.addSample( data[ indexs[i] ].getClassLabel(), data[ indexs[i] ].getData() ); } //Overwrite the training data in this instance with the training data of the trainingSet data = trainingSet.getClassificationData(); totalNumSamples = trainingSet.getNumSamples(); } return testSet; }
ClassificationData ClassificationData::split(const UINT trainingSizePercentage,const bool useStratifiedSampling){ //Partitions the dataset into a training dataset (which is kept by this instance of the ClassificationData) and //a testing/validation dataset (which is return as a new instance of the ClassificationData). The trainingSizePercentage //therefore sets the size of the data which remains in this instance and the remaining percentage of data is then added to //the testing/validation dataset //The dataset has changed so flag that any previous cross validation setup will now not work crossValidationSetup = false; crossValidationIndexs.clear(); ClassificationData trainingSet(numDimensions); ClassificationData testSet(numDimensions); trainingSet.setAllowNullGestureClass( allowNullGestureClass ); testSet.setAllowNullGestureClass( allowNullGestureClass ); //Create the random partion indexs Random random; UINT randomIndex = 0; UINT K = getNumClasses(); if( useStratifiedSampling ){ //Break the data into seperate classes Vector< Vector< UINT > > classData( K ); //Add the indexs to their respective classes for(UINT i=0; i<totalNumSamples; i++){ classData[ getClassLabelIndexValue( data[i].getClassLabel() ) ].push_back( i ); } //Randomize the order of the indexs in each of the class index buffers for(UINT k=0; k<K; k++){ std::random_shuffle(classData[k].begin(), classData[k].end()); } //Reserve the memory UINT numTrainingSamples = 0; UINT numTestSamples = 0; for(UINT k=0; k<K; k++){ UINT numTrainingExamples = (UINT) floor( Float(classData[k].size()) / 100.0 * Float(trainingSizePercentage) ); UINT numTestExamples = ((UINT)classData[k].size())-numTrainingExamples; numTrainingSamples += numTrainingExamples; numTestSamples += numTestExamples; } trainingSet.reserve( numTrainingSamples ); testSet.reserve( numTestSamples ); //Loop over each class and add the data to the trainingSet and testSet for(UINT k=0; k<K; k++){ UINT numTrainingExamples = (UINT) floor( Float(classData[k].getSize()) / 100.0 * Float(trainingSizePercentage) ); //Add the data to the training and test sets for(UINT i=0; i<numTrainingExamples; i++){ trainingSet.addSample( data[ classData[k][i] ].getClassLabel(), data[ classData[k][i] ].getSample() ); } for(UINT i=numTrainingExamples; i<classData[k].getSize(); i++){ testSet.addSample( data[ classData[k][i] ].getClassLabel(), data[ classData[k][i] ].getSample() ); } } }else{ const UINT numTrainingExamples = (UINT) floor( Float(totalNumSamples) / 100.0 * Float(trainingSizePercentage) ); //Create the random partion indexs UINT randomIndex = 0; Vector< UINT > indexs( totalNumSamples ); for(UINT i=0; i<totalNumSamples; i++) indexs[i] = i; std::random_shuffle(indexs.begin(), indexs.end()); //Reserve the memory trainingSet.reserve( numTrainingExamples ); testSet.reserve( totalNumSamples-numTrainingExamples ); //Add the data to the training and test sets for(UINT i=0; i<numTrainingExamples; i++){ trainingSet.addSample( data[ indexs[i] ].getClassLabel(), data[ indexs[i] ].getSample() ); } for(UINT i=numTrainingExamples; i<totalNumSamples; i++){ testSet.addSample( data[ indexs[i] ].getClassLabel(), data[ indexs[i] ].getSample() ); } } //Overwrite the training data in this instance with the training data of the trainingSet *this = trainingSet; //Sort the class labels in this dataset sortClassLabels(); //Sort the class labels of the test dataset testSet.sortClassLabels(); return testSet; }