コード例 #1
0
ファイル: predict.cpp プロジェクト: TPNguyen/mlkid
void validate(const ME_Model & model, 
              const string & input_file_name,
              const string & output_file_name)
{
    ifstream ifile(input_file_name.c_str());
    ofstream ofile(output_file_name.c_str());

    if (!ifile)
    {
        cerr << "error: cannot open " << input_file_name << endl;
        exit(1);
    }
    if (!ofile)
    {
        cerr << "error: cannot open " << output_file_name << endl;
        exit(1);
    }

    int n_correct = 0;
    int n_total   = 0;

    string line;
    while (getline(ifile, line))
    {
        vector<string> vs = read_line(line);
        ME_Sample mes = sample(vs);
        model.predict(mes);

        ofile << mes.label << endl;

        if (mes.label == vs[0])  n_correct++;
        n_total++;
    }

    double accuracy = (double)n_correct / n_total;
    cout << "accuracy = " << n_correct << " / " << n_total
         << " = " << accuracy << endl;
}