I'm having trouble understanding how to split data in a csv file into train and test in c++ and have no clue how to go about it. I thought about only reading in he first 800 lines as train and the rest as test but am unsure how to read in only the first 800 lines of a file. Or if there's an easier way to split the data.
You can try:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
#define TRAIN_MAX_LEN 500
int main() {
ifstream inFile("E:\\xxx.csv");
string lineStr = "";
vector<vector<string>> trainArr, testArr;
int curline = 0;
while (getline(inFile, lineStr)) {
stringstream ss(lineStr);
string str = "";
vector<string> lineArr;
while (getline(ss, str, ',')){
lineArr.push_back(str);
}
if (curline < TRAIN_MAX_LINE)
trainArr.push_back(lineArray);
else
testArr.push_back(lineArray);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.