int main ( int argc, char *argv[] ) { //Variables for parsing the data file std::string filename = "SPECT.train"; std::string line; std::stringstream parse; int ssize = 100; //establish a buffer size to store attribute values, //which for binary classification string are no bigger than 1 char c[ssize]; char delimiter = ','; //Variables to store the values in the data file std::vector<int> tmpcase; std::vector< std::vector<int> > training_set; cv::Mat sample(0, 1, CV_32FC1); cv::Mat labels(0, 1 , CV_16SC1); cv::Mat train_set; std::ifstream dataset_file(filename.c_str(), std::ios::in); if(!dataset_file) { std::cerr << "Cannot load training set file" << std::endl; } else { while( (getline(dataset_file, line))!= NULL ) { parse << line; while( parse.getline(c,ssize,delimiter) ) { tmpcase.push_back( (*c-'0') ); sample.push_back( (float)(*c-'0') ); } parse.str(""); //safety measure to erase previous contents parse.clear(); //clear flags to be able to read from it again training_set.push_back(tmpcase); tmpcase.clear(); train_set.push_back(sample.reshape(0,1)); labels.push_back((int)(sample.at<float>(0))); sample = cv::Mat(); } } std::cout << train_set << std::endl; cv::FileStorage fstore_traindata("spect_train.yml",cv::FileStorage::WRITE); cv::Mat train_samples(train_set.colRange(1,train_set.cols)); fstore_traindata << "train_samples" << train_samples; fstore_traindata << "train_labels" << labels; fstore_traindata.release(); std::cout << train_samples << std::endl; std::cout << labels << std::endl; std::vector<int> tmp; for(std::vector< std::vector<int> >::iterator it = training_set.begin(); it != training_set.end(); ++it) { tmp = *it; for(std::vector<int>::iterator it2 = tmp.begin(); it2 != tmp.end(); ++it2) { std::cout << *it2 << " "; } std::cout << std::endl; tmp.clear(); } }
int main(int argc, char* argv[]) { // Let us make libsvm quiet gaml::libsvm::quiet(); // random seed initialization std::srand(std::time(0)); try { // Let us collect samples. DataSet basis; basis.resize(100); for(auto& data : basis) { double theta = gaml::random::uniform(0,2*M_PI); data = Data(theta,oracle(theta)); } // Let us set configure a svm struct svm_parameter params; gaml::libsvm::init(params); params.kernel_type = RBF; // RBF kernel params.gamma = 1; // k(u,v) = exp(-gamma*(u-v)^2) params.svm_type = EPSILON_SVR; params.p = .01; // epsilon params.C = 10; params.eps = 1e-10; // numerical tolerence // This sets up a svm learning algorithm for predicting a scalar. auto scalar_learner = gaml::libsvm::supervized::learner<double,double>(params, nb_nodes_of, fill_nodes); // Let us use it for learning x, y and z. auto learner = gaml::multidim::learner<Point,3>(scalar_learner, array_of_output, output_of_array); // Let us train it and get some predictor f. f is a function, as the oracle. std::cout << "Learning..." << std::endl; auto f = learner(basis.begin(), basis.end(), input_of, output_of); // Let us retrieve the three SVMs in order to save each one. auto f_predictors = f.predictors(); std::array<std::string,3> filenames = {{std::string("x.pred"),"y.pred","z.pred"}}; auto name_iter = filenames.begin(); for(auto& pred : f_predictors) pred.save_model(*(name_iter++)); // We can compute the empirical risk with gaml tools. auto evaluator = gaml::risk::empirical(Loss()); double risk = evaluator(f, basis.begin(), basis.end(), input_of, output_of); std::cout << "Empirical risk : " << risk << std::endl << " sqrt(risk) : " << sqrt(risk) << std::endl; // Let us load our predictor. We need first to gather each loaded // scalar predictor in a collection... std::list<gaml::libsvm::Predictor<double,double>> predictors; name_iter= filenames.begin(); for(unsigned int dim = 0; dim < 3; ++dim) { gaml::libsvm::Predictor<double,double> predictor(nb_nodes_of, fill_nodes); predictor.load_model(*(name_iter++)); predictors.push_back(predictor); } // ... and create the 3D predictor (variable g) from the // collection of scalar predictors. gaml::multidim::Predictor<Point, gaml::libsvm::Predictor<double,double>, 3> g(output_of_array, predictors.begin(), predictors.end()); // let us gnuplot what we have. std::ofstream dataset_file("dataset.data"); for(auto& data : basis) dataset_file << data.first << ' ' << data.second.x << ' ' << data.second.y << ' ' << data.second.z << std::endl; dataset_file.close(); std::ofstream prediction_file("prediction.data"); for(double theta = 0; theta <= 2*M_PI; theta += .01) { Point p = g(theta); prediction_file << theta << ' ' << p.x << ' ' << p.y << ' ' << p.z << std::endl; } prediction_file.close(); std::ofstream gnuplot_file_3d("3D.plot"); gnuplot_file_3d << "set ticslevel 0" << std::endl << "set xlabel 'x(theta)'" << std::endl << "set ylabel 'y(theta)'" << std::endl << "set zlabel 'z(theta)' " << std::endl << "splot 'dataset.data' using 2:3:4 with points notitle, " << "'prediction.data' using 2:3:4 with lines notitle" << std::endl; gnuplot_file_3d.close(); std::cout << std::endl << "Try 'gnuplot -p 3D.plot'" << std::endl; std::ofstream gnuplot_file_1d("1D-x.plot"); gnuplot_file_1d << "set xlabel 'theta'" << std::endl << "set ylabel 'x(theta)'" << std::endl << "plot 'dataset.data' using 1:2 with points notitle, " << "'prediction.data' using 1:2 with lines notitle" << std::endl; gnuplot_file_1d.close(); std::cout << "Try 'gnuplot -p 1D-x.plot'" << std::endl << std::endl; } catch(gaml::exception::Any& e) { std::cout << e.what() << std::endl; } return 0; }