instance.h
00001
00002
00003
00004
00005
00006 #include <string>
00007 #include <sstream>
00008 #include <iostream>
00009 #include <map>
00010 #include <cassert>
00011 using namespace std;
00012
00013 namespace librf {
00014
00015 template <class out_type, class in_value>
00016 out_type cast_stream(const in_value & t) {
00017 stringstream ss;
00018 ss << t;
00019 out_type result;
00020 ss >> result;
00021 return result;
00022 }
00023
00024
00025
00026 struct Instance {
00027 Instance(const string& line, int num_features) : features_(num_features,0.0){
00028 stringstream ss(line);
00029 string pair_str;
00030 float label;
00031 ss >> label;
00032 if (label == -1.0) {
00033 true_label =0;
00034 } else if (label ==0.0) {
00035 true_label =0;
00036 } else if (label ==1.0) {
00037 true_label =1;
00038 } else {
00039 cerr << "Incorrect label (only +1,0,-1 supported)" << endl;
00040 assert(false);
00041 }
00042 while (ss >> pair_str) {
00043 string::size_type pos = pair_str.find_first_of(':');
00044 string first = pair_str.substr(0,pos);
00045 string second = pair_str.substr(pos+1);
00046 pair<int,float> p = make_pair(cast_stream<int>(first), cast_stream<float>(second));
00047 if (p.first < num_features) {
00048 features_[p.first] = p.second;
00049 } else {
00050 cout << line << endl;
00051 assert(false);
00052 }
00053 }
00054 }
00055
00056
00057 vector<float> features_;
00058 bool true_label;
00059 };
00060
00061 }
Generated on Mon Jan 8 23:19:06 2007 for librf by
1.3.7