/** * Dataset class. * * Vladimir Vacic * Computer Science and Engineering Department * University of California, Riverside * * May-6-2005 * */ #include "Dataset.h" #include using namespace std; Dataset::Dataset() {} Dataset::Dataset(const vector > &tdata, const vector &tlabels) { if (tdata.size() == tlabels.size()) { data = tdata; labels = tlabels; } else { cerr << "Number of examples does not match the number of labels." << endl; exit(1); } } Dataset::Dataset(const Dataset &tset) { data = tset.getData(); labels = tset.getLabels(); } Dataset:: ~Dataset() {} int Dataset::width() const { return data.size(); } int Dataset::length() const { if (!data.empty()) return data[0].size(); else return 0; } double Dataset::operator()(int r, int c) const { if (r < length() && c < width()) return data[r][c]; else return 0; } vector Dataset::column(int c) const { vector temp; if (c < width()) { for(int i = 0; i < length(); i++) temp.push_back(data[i][c]); } return temp; } vector Dataset::row(int r) const { if (r < length()) return data[r]; else return vector(); } vector > Dataset::getData() const { return data; } vector Dataset::getLabels() const { return labels; } // Written by Christian R. Shelton. static int readvalue(istream &s) { char ch; int ret = 0; do { s >> ch; } while((ch<'0' || ch>'9') && !s.eof()); while(ch>='0' && ch<='9' && !s.eof()) { ret = ret*10+(ch-'0'); s >> ch; } s.putback(ch); return ret; } // Written by Christian R. Shelton. void Dataset::load(char *filename) { ifstream ifs(filename); if (!ifs.good()) { std::cerr << "Problem loading file " << filename << endl; exit(1); } ifs.unsetf(ios::skipws); while(!ifs.eof()) { vector dataline; char nextch; do { double d = (double) readvalue(ifs); dataline.push_back(d); ifs >> nextch; if (nextch >= '0' && nextch <= '9') ifs.putback(nextch); } while(!ifs.eof() && nextch!='\n' && nextch!='\r'); if (!dataline.empty()) { labels.push_back((double) (dataline[dataline.size()-1])); dataline.erase(dataline.end()-1); data.push_back(dataline); } } }