本文整理汇总了C++中TimeSeriesClassificationData::getNumClasses方法的典型用法代码示例。如果您正苦于以下问题:C++ TimeSeriesClassificationData::getNumClasses方法的具体用法?C++ TimeSeriesClassificationData::getNumClasses怎么用?C++ TimeSeriesClassificationData::getNumClasses使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TimeSeriesClassificationData
的用法示例。
在下文中一共展示了TimeSeriesClassificationData::getNumClasses方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: train_
bool ParticleClassifier::train_(TimeSeriesClassificationData &trainingData){
clear();
numClasses = trainingData.getNumClasses();
numInputDimensions = trainingData.getNumDimensions();
ranges = trainingData.getRanges();
//Scale the training data if needed
if( useScaling ){
trainingData.scale(0, 1);
}
//Train the particle filter
particleFilter.train( numParticles, trainingData, sensorNoise, transitionSigma, phaseSigma, velocitySigma );
classLabels.resize(numClasses);
classLikelihoods.resize(numClasses,0);
classDistances.resize(numClasses,0);
for(unsigned int i=0; i<numClasses; i++){
classLabels[i] = trainingData.getClassTracker()[i].classLabel;
}
trained = true;
return trained;
}
示例2: main
int main (int argc, const char * argv[])
{
TimeSeriesClassificationData trainingData; //This will store our training data
GestureRecognitionPipeline pipeline; //This is a wrapper for our classifier and any pre/post processing modules
string dirPath = "/home/vlad/AndroidStudioProjects/DataCapture/dataSetGenerator/build";
if (!trainingData.loadDatasetFromFile(dirPath + "/acc-training-set-segmented.data")) {
printf("Cannot open training segmented set\n");
return 0;
}
printf("Successfully opened training data set ...\n");
DTW dtw;
// LowPassFilter lpf(0.1, 1, 1);
// pipeline.setPreProcessingModule(lpf);
// DoubleMovingAverageFilter filter( 1000, 3 );
// pipeline.setPreProcessingModule(filter);
//dtw.enableNullRejection( true );
//Set the null rejection coefficient to 3, this controls the thresholds for the automatic null rejection
//You can increase this value if you find that your real-time gestures are not being recognized
//If you are getting too many false positives then you should decrease this value
//dtw.setNullRejectionCoeff( 5 );
dtw.enableTrimTrainingData(true, 0.1, 90);
// dtw.setOffsetTimeseriesUsingFirstSample(true);
pipeline.setClassifier( dtw );
UINT KFolds = 5;
/* Separate input dataset using KFold */
KfoldTimeSeriesData* kFoldTS = new KfoldTimeSeriesData(trainingData);
if( !kFoldTS->spiltDataIntoKFolds(KFolds) ) {
printf("BaseTGTestModel: Failed to spiltDataIntoKFolds!");
return 0;
}
UINT maxTrainigSetSize = trainingData.getNumSamples() * (KFolds - 1) / (KFolds * trainingData.getNumClasses());
// KFolds
ofstream myfile;
myfile.open ("example.txt");
Float acc = 0;
for (GRT::UINT k = 1 ; k < KFolds; k++) {
printf("Running tests for: %d fold", k);
// maxTrainigSetSize
// for (UINT trainingSetSize = 1; trainingSetSize <= maxTrainigSetSize; trainingSetSize ++) {
/* Set up training datasets for current fold */
TimeSeriesClassificationData trainingDataset = kFoldTS->getTrainingFoldData(k, maxTrainigSetSize);
/* Set up validation datasets for current fold */
TimeSeriesClassificationDataStream testDataset = kFoldTS->getTestFoldData(k);
/* Log test dataset size */
//printf("Data set size: training %d; testing %d",
// trainingDataset.getNumSamples(), testDataset.getNumSamples());
/* Run test for current fold */
pipeline.train(trainingDataset);
pipeline.test(testDataset);
myfile << pipeline.getTestAccuracy() << "\n";
// }
}
myfile.close();
printf("Accuracy = %f ; %d\n", acc, maxTrainigSetSize);
}
示例3: train_continuous
bool HMM::train_continuous(TimeSeriesClassificationData &trainingData){
clear();
if( trainingData.getNumSamples() == 0 ){
errorLog << "train_continuous(TimeSeriesClassificationData &trainingData) - There are no training samples to train the CHMM classifer!" << endl;
return false;
}
//Reset the CHMM
numInputDimensions = trainingData.getNumDimensions();
numClasses = trainingData.getNumClasses();
classLabels.resize( numClasses );
for(UINT k=0; k<numClasses; k++){
classLabels[k] = trainingData.getClassTracker()[k].classLabel;
}
//Scale the training data if needed
ranges = trainingData.getRanges();
if( useScaling ){
trainingData.scale(0, 1);
}
//Setup the models, there will be 1 model for each training sample
const UINT numTrainingSamples = trainingData.getNumSamples();
continuousModels.resize( numTrainingSamples );
//Train each of the models
for(UINT k=0; k<numTrainingSamples; k++){
//Init the model
continuousModels[k].setDownsampleFactor( downsampleFactor );
continuousModels[k].setModelType( modelType );
continuousModels[k].setDelta( delta );
continuousModels[k].setSigma( sigma );
continuousModels[k].setAutoEstimateSigma( autoEstimateSigma );
continuousModels[k].enableScaling( false ); //Scaling should always off for the models as we do any scaling in the CHMM
//Train the model
if( !continuousModels[k].train_( trainingData[k] ) ){
errorLog << "train_continuous(TimeSeriesClassificationData &trainingData) - Failed to train CHMM for sample " << k << endl;
return false;
}
}
if( committeeSize > trainingData.getNumSamples() ){
committeeSize = trainingData.getNumSamples();
warningLog << "train_continuous(TimeSeriesClassificationData &trainingData) - The committeeSize is larger than the number of training sample. Setting committeeSize to number of training samples: " << trainingData.getNumSamples() << endl;
}
//Flag that the model has been trained
trained = true;
//Compute any null rejection thresholds if needed
if( useNullRejection ){
//Compute the rejection thresholds
nullRejectionThresholds.resize(numClasses);
}
return true;
}
示例4: train_discrete
bool HMM::train_discrete(TimeSeriesClassificationData &trainingData){
clear();
if( trainingData.getNumSamples() == 0 ){
errorLog << "train_discrete(TimeSeriesClassificationData &trainingData) - There are no training samples to train the HMM classifer!" << endl;
return false;
}
if( trainingData.getNumDimensions() != 1 ){
errorLog << "train_discrete(TimeSeriesClassificationData &trainingData) - The number of dimensions in the training data must be 1. If your training data is not 1 dimensional then you must quantize the training data using one of the GRT quantization algorithms" << endl;
return false;
}
//Reset the HMM
numInputDimensions = trainingData.getNumDimensions();
numClasses = trainingData.getNumClasses();
discreteModels.resize( numClasses );
classLabels.resize( numClasses );
//Init the models
for(UINT k=0; k<numClasses; k++){
discreteModels[k].resetModel(numStates,numSymbols,modelType,delta);
discreteModels[k].setMaxNumEpochs( maxNumEpochs );
discreteModels[k].setMinChange( minChange );
}
//Train each of the models
for(UINT k=0; k<numClasses; k++){
//Get the class ID of this gesture
UINT classID = trainingData.getClassTracker()[k].classLabel;
classLabels[k] = classID;
//Convert this classes training data into a list of observation sequences
TimeSeriesClassificationData classData = trainingData.getClassData( classID );
vector< vector< UINT > > observationSequences;
if( !convertDataToObservationSequence( classData, observationSequences ) ){
return false;
}
//Train the model
if( !discreteModels[k].train( observationSequences ) ){
errorLog << "train_discrete(TimeSeriesClassificationData &trainingData) - Failed to train HMM for class " << classID << endl;
return false;
}
}
//Compute the rejection thresholds
nullRejectionThresholds.resize(numClasses);
for(UINT k=0; k<numClasses; k++){
//Get the class ID of this gesture
UINT classID = trainingData.getClassTracker()[k].classLabel;
classLabels[k] = classID;
//Convert this classes training data into a list of observation sequences
TimeSeriesClassificationData classData = trainingData.getClassData( classID );
vector< vector< UINT > > observationSequences;
if( !convertDataToObservationSequence( classData, observationSequences ) ){
return false;
}
//Test the model
double loglikelihood = 0;
double avgLoglikelihood = 0;
for(UINT i=0; i<observationSequences.size(); i++){
loglikelihood = discreteModels[k].predict( observationSequences[i] );
avgLoglikelihood += fabs( loglikelihood );
}
nullRejectionThresholds[k] = -( avgLoglikelihood / double( observationSequences.size() ) );
}
//Flag that the model has been trained
trained = true;
return true;
}
示例5: main
int main() {
vector<string> gestures(0,"");
GetFilesInDirectory(gestures, "rawdata");
CreateDirectory("processed", NULL);
sort(gestures.begin(), gestures.end());
data = vector<vector<vector<double > > >(gestures.size(), vector<vector<double > >(0,vector<double>(0,0)));
for(size_t i = 0; i < gestures.size(); i++) {
ifstream fin(gestures[i]);
int n; fin >> n;
// cerr << gestures[i] << endl;
// cerr << n << endl;
data[i] = vector<vector<double> >(n, vector<double>(NUMPARAM, 0));
for(int j = 0; j < n; j++) {
for(int k = 0; k < NUMPARAM; k++) {
fin >> data[i][j][k];
}
}
fin.close();
}
//Create a new instance of the TimeSeriesClassificationDataStream
TimeSeriesClassificationData trainingData;
// ax, ay, az
trainingData.setNumDimensions(3);
trainingData.setDatasetName("processed\\GestureTrainingData.txt");
ofstream labelfile("processed\\GestureTrainingDataLabels.txt");
UINT currLabel = 1;
Random random;
map<string, int> gesturenames;
for(size_t overall = 0; overall < gestures.size(); overall++) {
string nam = gestures[overall].substr(8,gestures[overall].find_first_of('_')-8);
if(gesturenames.count(nam)) currLabel = gesturenames[nam];
else {
currLabel = gesturenames.size()+1;
gesturenames[nam] = currLabel;
labelfile << currLabel << " " << nam << endl;
}
MatrixDouble trainingSample;
VectorDouble currVec( trainingData.getNumDimensions() );
for(size_t k = 1; k < data[overall].size(); k++) {
for(UINT j=0; j<currVec.size(); j++){
currVec[j] = data[overall][k][j];
}
trainingSample.push_back(currVec);
}
trainingData.addSample(currLabel, trainingSample);
}
for(size_t i = 0; i < gestures.size(); i++) {
MatrixDouble trainingSample;
VectorDouble currVec(trainingData.getNumDimensions());
for(UINT j = 0; j < currVec.size(); j++) {
currVec[j] = random.getRandomNumberUniform(-1.0, 1.0);
}
for(size_t k = 0; k < 100; k++) {
trainingSample.push_back(currVec);
}
trainingData.addSample(0, trainingSample);
}
//After recording your training data you can then save it to a file
if( !trainingData.save( "processed\\TrainingData.grt" ) ){
cout << "ERROR: Failed to save dataset to file!\n";
return EXIT_FAILURE;
}
//This can then be loaded later
if( !trainingData.load( "processed\\TrainingData.grt" ) ){
cout << "ERROR: Failed to load dataset from file!\n";
return EXIT_FAILURE;
}
//This is how you can get some stats from the training data
string datasetName = trainingData.getDatasetName();
string infoText = trainingData.getInfoText();
UINT numSamples = trainingData.getNumSamples();
UINT numDimensions = trainingData.getNumDimensions();
UINT numClasses = trainingData.getNumClasses();
cout << "Dataset Name: " << datasetName << endl;
cout << "InfoText: " << infoText << endl;
cout << "NumberOfSamples: " << numSamples << endl;
cout << "NumberOfDimensions: " << numDimensions << endl;
cout << "NumberOfClasses: " << numClasses << endl;
//You can also get the minimum and maximum ranges of the data
vector< MinMax > ranges = trainingData.getRanges();
cout << "The ranges of the dataset are: \n";
for(UINT j=0; j<ranges.size(); j++){
cout << "Dimension: " << j << " Min: " << ranges[j].minValue << " Max: " << ranges[j].maxValue << endl;
}
DTW dtw;
if( !dtw.train( trainingData ) ){
cerr << "Failed to train classifier!\n";
//.........这里部分代码省略.........
示例6: main
int main (int argc, const char * argv[])
{
//Create a new instance of the TimeSeriesClassificationData
TimeSeriesClassificationData trainingData;
//Set the dimensionality of the data (you need to do this before you can add any samples)
trainingData.setNumDimensions( 3 );
//You can also give the dataset a name (the name should have no spaces)
trainingData.setDatasetName("DummyData");
//You can also add some info text about the data
trainingData.setInfoText("This data contains some dummy timeseries data");
//Here you would record a time series, when you have finished recording the time series then add the training sample to the training data
UINT gestureLabel = 1;
MatrixDouble trainingSample;
//For now we will just add 10 x 20 random walk data timeseries
Random random;
for(UINT k=0; k<10; k++){//For the number of classes
gestureLabel = k+1;
//Get the init random walk position for this gesture
VectorDouble startPos( trainingData.getNumDimensions() );
for(UINT j=0; j<startPos.size(); j++){
startPos[j] = random.getRandomNumberUniform(-1.0,1.0);
}
//Generate the 20 time series
for(UINT x=0; x<20; x++){
//Clear any previous timeseries
trainingSample.clear();
//Generate the random walk
UINT randomWalkLength = random.getRandomNumberInt(90, 110);
VectorDouble sample = startPos;
for(UINT i=0; i<randomWalkLength; i++){
for(UINT j=0; j<startPos.size(); j++){
sample[j] += random.getRandomNumberUniform(-0.1,0.1);
}
//Add the sample to the training sample
trainingSample.push_back( sample );
}
//Add the training sample to the dataset
trainingData.addSample( gestureLabel, trainingSample );
}
}
//After recording your training data you can then save it to a file
if( !trainingData.saveDatasetToFile( "TrainingData.txt" ) ){
cout << "Failed to save dataset to file!\n";
return EXIT_FAILURE;
}
//This can then be loaded later
if( !trainingData.loadDatasetFromFile( "TrainingData.txt" ) ){
cout << "Failed to load dataset from file!\n";
return EXIT_FAILURE;
}
//This is how you can get some stats from the training data
string datasetName = trainingData.getDatasetName();
string infoText = trainingData.getInfoText();
UINT numSamples = trainingData.getNumSamples();
UINT numDimensions = trainingData.getNumDimensions();
UINT numClasses = trainingData.getNumClasses();
cout << "Dataset Name: " << datasetName << endl;
cout << "InfoText: " << infoText << endl;
cout << "NumberOfSamples: " << numSamples << endl;
cout << "NumberOfDimensions: " << numDimensions << endl;
cout << "NumberOfClasses: " << numClasses << endl;
//You can also get the minimum and maximum ranges of the data
vector< MinMax > ranges = trainingData.getRanges();
cout << "The ranges of the dataset are: \n";
for(UINT j=0; j<ranges.size(); j++){
cout << "Dimension: " << j << " Min: " << ranges[j].minValue << " Max: " << ranges[j].maxValue << endl;
}
//If you want to partition the dataset into a training dataset and a test dataset then you can use the partition function
//A value of 80 means that 80% of the original data will remain in the training dataset and 20% will be returned as the test dataset
TimeSeriesClassificationData testData = trainingData.partition( 80 );
//If you have multiple datasets that you want to merge together then use the merge function
if( !trainingData.merge( testData ) ){
cout << "Failed to merge datasets!\n";
return EXIT_FAILURE;
}
//If you want to run K-Fold cross validation using the dataset then you should first spilt the dataset into K-Folds
//A value of 10 splits the dataset into 10 folds and the true parameter signals that stratified sampling should be used
if( !trainingData.spiltDataIntoKFolds( 10, true ) ){
cout << "Failed to spiltDataIntoKFolds!\n";
//.........这里部分代码省略.........