This class implements the Logistic Regression algorithm. Logistic Regression is a simple but effective regression algorithm that can map an N-dimensional signal to a 1-dimensional signal. If you want to use LogisticRegression for classification tasks, then you should use the GRT SoftMax algorithm.
GRT MIT License Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using namespace std;
int main (int argc, const char * argv[])
{
if( argc != 3 ){
cout << "Error: failed to parse data filename from command line. You should run this example with two arguments for the training and test datasets filenames\n";
return EXIT_FAILURE;
}
const string trainingDataFilename = argv[1];
const string testDataFilename = argv[2];
if( !trainingData.
load( trainingDataFilename ) ){
cout << "ERROR: Failed to load training data: " << trainingDataFilename << endl;
return EXIT_FAILURE;
}
if( !testData.
load( testDataFilename ) ){
cout << "ERROR: Failed to load test data: " << testDataFilename << endl;
return EXIT_FAILURE;
}
cout <<
"ERROR: The number of input dimensions in the training data (" << trainingData.
getNumInputDimensions() <<
")";
cout <<
" does not match the number of input dimensions in the test data (" << testData.
getNumInputDimensions() <<
")\n";
return EXIT_FAILURE;
}
cout <<
"ERROR: The number of target dimensions in the training data (" << testData.
getNumTargetDimensions() <<
")";
cout <<
" does not match the number of target dimensions in the test data (" << testData.
getNumTargetDimensions() <<
")\n";
return EXIT_FAILURE;
}
cout << "Training and Test datasets loaded\n";
cout << "Training data stats:\n";
trainingData.
printStats();
cout << "Test data stats:\n";
testData.printStats();
{
const bool scaleData = true;
const Float learningRate = 0.2;
const Float minChange = 1.0e-8;
const UINT batchSize = 20;
const UINT maxNumEpochs = 1000;
pipeline << regression;
}
cout << "Training LogisticRegression model...\n";
if( !pipeline.
train( trainingData ) ){
cout << "ERROR: Failed to train LogisticRegression model!\n";
return EXIT_FAILURE;
}
cout << "Model trained.\n";
cout << "Testing LogisticRegression model...\n";
if( !pipeline.
test( testData ) ){
cout << "ERROR: Failed to test LogisticRegression model!\n";
return EXIT_FAILURE;
}
cout <<
"Test complete. Test RMS error: " << pipeline.
getTestRMSError() << endl;
fstream file;
file.open("LogisticRegressionResultsData.csv", fstream::out);
inputVector = testData[i].getInputVector();
targetVector = testData[i].getTargetVector();
if( !pipeline.
predict( inputVector ) ){
cout << "ERROR: Failed to map test sample " << i << endl;
return EXIT_FAILURE;
}
for(UINT j=0; j<outputVector.size(); j++){
file << outputVector[j] << "\t";
}
for(UINT j=0; j<targetVector.size(); j++){
file << targetVector[j] << "\t";
}
file << endl;
}
file.close();
return EXIT_SUCCESS;
}