Unexpected Errors in Naive Bayes Classifier












0














I was working on my project on a Naive Bayes implementation when I started running into some unexpected issues. Any help?



`



    #include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdio>
#include <cctype>
#include <cstdlib>
using namespace std;

struct Person
{
int age;
string workclass;
int fnlwgt;
string sex;
int capitalGain;
int capitalLoss;
int hoursPerWeek;
string salary;

void printPerson()
{
cout << age << endl << workclass << endl << fnlwgt;
cout << endl << sex << endl;
cout << capitalGain << endl << capitalLoss;
cout << endl << hoursPerWeek << endl << salary << endl;
}
void getPerson()
{
cout << "nEnter age: ";
cin >> age;
cout << "nEnter work class: ";
getline(cin, workclass);
cout << "nEnter final weight: ";
cin >> fnlwgt;
cout << "nEnter sex: ";
getline(cin, sex);
cout << "nEnter capital gain: ";
cin >> capitalGain;
cout << "nEnter capital loss: ";
cin >> capitalLoss;
cout << "nEnter salary: ";
getline(cin, salary);
}

};

void writeToData()
{
ofstream writer;
writer.open("Dataset.dat", ios::binary | ios::app);
if (!writer)
{
cout << "nError in file open!!";
return;
}
Person p;
while(1)
{
char yn;
p.getPerson();
writer.write((char*)&p, sizeof(p));
cout << "nAre there more records? ";
cin >> yn;
if (yn == 'n' || yn == 'N')
{
break;
}
}
}



string convertInt(int x){
string result;
ostringstream convert;

convert << x;
result = convert.str();
return result;
}

int randNumGenerator(int max){
int num = (rand() % max);

return num;
}
template <typename Predicate>
vector<Person> filter( vector<Person>& testData, Predicate pred) {
vector<Person> out;
copy_if(testData.begin(), testData.end(), back_inserter(out), pred);
return out;
}
vector<Person> positive = filter(testData, [=](const Person& p)
return p.salary == ">50K"

void setData(Person person, string line){

line.erase(remove(line.begin(), line.end(), ','), line.end());

stringstream s(line);
string str;


string workclass;
string sex;


string salary;


int age;
string ageStr;

int fnlwgt;
string fnlwgtStr;

int capitalGain;
string capitalGainStr;

int capitalLoss;
string capitalLossStr;

int hoursPerWeek;
string hoursPerWeekStr;


if(s >> age >> workclass >> fnlwgt >> sex >> capitalGain >> capitalLoss >> hoursPerWeek >> salary)
{


ageStr = convertInt(age);
fnlwgtStr = convertInt(fnlwgt);
capitalGainStr = convertInt(capitalGain);
capitalLossStr = convertInt(capitalLoss);
hoursPerWeekStr = convertInt(hoursPerWeek);


if(ageStr == "?" || workclass == "?" || fnlwgtStr == "?" || sex == "?" || capitalGainStr == "?" || capitalLossStr == "?" || hoursPerWeekStr == "?")
{

}
else{
person.age = age;
person.workclass = workclass;
person.fnlwgt = fnlwgt;
person.sex = sex;
person.capitalGain = capitalGain;
person.capitalLoss = capitalLoss;
person.hoursPerWeek = hoursPerWeek;
person.salary = salary;
testData.push_back(person);
}
}


}


vector<Person> sample(vector<Person> wholeDataSet, int percentage, vector<Person> &testingSet){
int wholeDataSize = wholeDataSet.size();

vector<Person> stratifiedSet;

int limit = (wholeDataSize * percentage) / 100;
int randNum= 0;

vector<bool> numsUsedAlready(wholeDataSize);

for(int i = 0; i < limit; i++){
randNum = randNumGenerator(wholeDataSize);
while(numsUsedAlready[randNum]){
randNum = randNumGenerator(wholeDataSize);
}

numsUsedAlready[randNum] = true;
stratifiedSet.push_back(wholeDataSet[randNum]);
}
for(int i = 0; i < numsUsedAlready.size(); i++){
if(!numsUsedAlready[i]){
testingSet.push_back(wholeDataSet[i]);
}
}


return stratifiedSet;
}

vector<Person> concatVectors(vector<Person> a, vector<Person> b){
vector<Person> ab;
ab.reserve(a.size() + b.size());
ab.insert(ab.end(), a.begin(), a.end());
ab.insert(ab.end(), b.begin(), b.end());
return ab;
}

void compareAttributeInt(int sample, int trained, string salary, int &count, int posOrNeg){
if(sample == trained){
if(salary == ">50K" && posOrNeg == 1){
count++;
}
if(salary == "<=50K" && posOrNeg == 0){
count++;
}
}
}

void compareAttributeStr(string sample, string trained, string salary, int &count, int posOrNeg){
if(sample == trained){
if(salary == ">50K" && posOrNeg == 1){
count++;
}
if(salary == "<=50K" && posOrNeg == 0){
count++;
}

}
}

class Count
{
public:
int pos = 0;
int neg = 0;
void increment(bool flag) {
if (flag) {
pos++;
}
else {
neg++;
}
}
};

float naiveBayesian(vector<Person> trainingSet, vector<Person> testingSet){
float accuracy = 0;
int accuracyCount = 0;
int randNum = 0;

vector<Person> sampleSet;
vector<bool> numsUsedAlready(testingSet.size());

for(int i = 0; i < 20; i++){
randNum = randNumGenerator(testingSet.size());
while(numsUsedAlready[randNum]){
randNum = randNumGenerator(testingSet.size());
}
numsUsedAlready[randNum] = true;
sampleSet.push_back(testingSet[randNum]);
}



int numOver50k = 0;
int numUnder50k = 0;

for(int i = 0; i < trainingSet.size(); i++){
if(trainingSet[i].salary == ">50K"){
numOver50k++;
}
else{
numUnder50k++;
}
}

float probOver50k = (float)numOver50k / trainingSet.size();
float probUnder50k = (float)numUnder50k / trainingSet.size();

float probYes = 0;
float probNo = 0;

float yes= 0;
float no = 0;


template <typename T>
Count compute_count(const Person& sample, const vector<Person>& trainingSet, T Person::*member)
{
Count res;
for (const Person& training : trainingSet) {
if ((sample.*member) == (training.*member)) {
res.increment(training.salary == ">50K");
}
}
return res;
}
for (const Person& sample : sampleSet)
{
vector<Count> counts;
counts.push_back(compute_count(sample, trainingSet, &Person::age));
counts.push_back(compute_count(sample, trainingSet, &Person::workclass));
counts.push_back(compute_count(sample, trainingSet, &Person::fnlwgt));
counts.push_back(compute_count(sample, trainingSet, &Person::capitalGain));
counts.push_back(compute_count(sample, trainingSet, &Person::capitalLoss));
counts.push_back(compute_count(sample, trainingSet, &Person::hoursPerWeek));
}

bool salaryGreaterThan50k = (probYes * probOver50k) > (probNo * probUnder50k);

double probYes = accumulate(counts.begin(), counts.end(), 1.0, [=](double init, const Count& count){
return init * count.pos / numOver50k;
});
double probNo = accumulate(counts.begin(), counts.end(), 1.0, [=](double init, const Count& count){
return init * count.neg / numUnder50k;
});

if ((salaryGreaterThan50k && (sample.salary == ">50K")) || (!salaryGreaterThan50k && !(sample.salary == ">50K")))
++accuracyCount;

accuracy = (float)accuracyCount / 20;

return accuracy;
}

void stratifiedSample(){
srand(time(NULL));

vector<Person> positiveSamples;
vector<Person> negativeSamples;
positiveSamples = setPositive(positiveSamples);


negativeSamples = setNegative(negativeSamples);


vector<Person> posTestingSet10 = positiveSamples;
vector<Person> posTestingSet30 = positiveSamples;
vector<Person> posTestingSet50 = positiveSamples;
vector<Person> posTestingSet70 = positiveSamples;
vector<Person> posTestingSet90 = positiveSamples;

vector<Person> negTestingSet10 = negativeSamples;
vector<Person> negTestingSet30 = negativeSamples;
vector<Person> negTestingSet50 = negativeSamples;
vector<Person> negTestingSet70 = negativeSamples;
vector<Person> negTestingSet90 = negativeSamples;

vector<Person> posStratifiedSet_10;
vector<Person> posTesting_10;
vector<Person> negStratifiedSet_10;
vector<Person> negTesting_10;

vector<Person> posStratifiedSet_30;
vector<Person> posTesting_30;
vector<Person> negStratifiedSet_30;
vector<Person> negTesting_30;

vector<Person> posStratifiedSet_50;
vector<Person> posTesting_50;
vector<Person> negStratifiedSet_50;
vector<Person> negTesting_50;

vector<Person> posStratifiedSet_70;
vector<Person> posTesting_70;
vector<Person> negStratifiedSet_70;
vector<Person> negTesting_70;

vector<Person> posStratifiedSet_90;
vector<Person> posTesting_90;
vector<Person> negStratifiedSet_90;
vector<Person> negTesting_90;

vector<Person> stratifiedSet_10;
vector<Person> stratifiedSet_30;
vector<Person> stratifiedSet_50;
vector<Person> stratifiedSet_70;
vector<Person> stratifiedSet_90;

vector<Person> testingSet_10;
vector<Person> testingSet_30;
vector<Person> testingSet_50;
vector<Person> testingSet_70;
vector<Person> testingSet_90;

posStratifiedSet_10 = sample(posTestingSet10, 10, posTesting_10);


negStratifiedSet_10 = sample(negTestingSet10, 10, negTesting_10);


posStratifiedSet_30 = sample(posTestingSet30, 30, posTesting_30);

negStratifiedSet_30 = sample(negTestingSet30, 30, negTesting_30);


posStratifiedSet_50 = sample(posTestingSet50, 50, posTesting_50);

negStratifiedSet_50 = sample(negTestingSet50, 50, negTesting_50);


posStratifiedSet_70 = sample(posTestingSet70, 70, posTesting_70);

negStratifiedSet_70 = sample(negTestingSet70, 70, negTesting_70);


posStratifiedSet_90 = sample(posTestingSet90, 90, posTesting_90);

negStratifiedSet_90 = sample(negTestingSet90, 90, negTesting_90);


stratifiedSet_10 = concatVectors(posStratifiedSet_10, negStratifiedSet_10);
stratifiedSet_30 = concatVectors(posStratifiedSet_30, negStratifiedSet_30);
stratifiedSet_50 = concatVectors(posStratifiedSet_50, negStratifiedSet_50);
stratifiedSet_70 = concatVectors(posStratifiedSet_70, negStratifiedSet_70);
stratifiedSet_90 = concatVectors(posStratifiedSet_90, negStratifiedSet_90);

testingSet_10 = concatVectors(posTesting_10, negTesting_10);
testingSet_30 = concatVectors(posTesting_30, negTesting_30);
testingSet_50 = concatVectors(posTesting_50, negTesting_50);
testingSet_70 = concatVectors(posTesting_70, negTesting_70);
testingSet_90 = concatVectors(posTesting_90, negTesting_90);


float accuracy10 = 0;
float accuracy30 = 0;
float accuracy50 = 0;
float accuracy70 = 0;
float accuracy90 = 0;

accuracy10 = naiveBayesian(stratifiedSet_10, testingSet_10);
cout << "accuracy for 10%: " << accuracy10 << endl;

accuracy30 = naiveBayesian(stratifiedSet_30, testingSet_30);
cout << "accuracy for 30%: " << accuracy30 << endl;

accuracy50 = naiveBayesian(stratifiedSet_50, testingSet_50);
cout << "accuracy for 50%: " << accuracy50 << endl;

accuracy70 = naiveBayesian(stratifiedSet_70, testingSet_70);
cout << "accuracy for 70%: " << accuracy70 << endl;

accuracy90 = naiveBayesian(stratifiedSet_90, testingSet_90);
cout << "accuracy for 90%: " << accuracy90 << endl;

}


void readInputFile()
{
ifstream reader;
reader.open("Dataset.dat" , ios::binary);
if(!reader)
{
cout << "nError in open";
return;
}
Person p;
reader.read((char*) &p, sizeof(p));
}

int main(int argc, char const *argv)
{
system("CLS");

readInputFile();

stratifiedSample();
return 0;
}


`



I work on the inbuilt gcc compiler for mac and the errors it throws during compilation are:



Error IMage



What have I been doing wrong?









share







New contributor




S-A117 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0














    I was working on my project on a Naive Bayes implementation when I started running into some unexpected issues. Any help?



    `



        #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <algorithm>
    #include <ctime>
    #include <cstdio>
    #include <cctype>
    #include <cstdlib>
    using namespace std;

    struct Person
    {
    int age;
    string workclass;
    int fnlwgt;
    string sex;
    int capitalGain;
    int capitalLoss;
    int hoursPerWeek;
    string salary;

    void printPerson()
    {
    cout << age << endl << workclass << endl << fnlwgt;
    cout << endl << sex << endl;
    cout << capitalGain << endl << capitalLoss;
    cout << endl << hoursPerWeek << endl << salary << endl;
    }
    void getPerson()
    {
    cout << "nEnter age: ";
    cin >> age;
    cout << "nEnter work class: ";
    getline(cin, workclass);
    cout << "nEnter final weight: ";
    cin >> fnlwgt;
    cout << "nEnter sex: ";
    getline(cin, sex);
    cout << "nEnter capital gain: ";
    cin >> capitalGain;
    cout << "nEnter capital loss: ";
    cin >> capitalLoss;
    cout << "nEnter salary: ";
    getline(cin, salary);
    }

    };

    void writeToData()
    {
    ofstream writer;
    writer.open("Dataset.dat", ios::binary | ios::app);
    if (!writer)
    {
    cout << "nError in file open!!";
    return;
    }
    Person p;
    while(1)
    {
    char yn;
    p.getPerson();
    writer.write((char*)&p, sizeof(p));
    cout << "nAre there more records? ";
    cin >> yn;
    if (yn == 'n' || yn == 'N')
    {
    break;
    }
    }
    }



    string convertInt(int x){
    string result;
    ostringstream convert;

    convert << x;
    result = convert.str();
    return result;
    }

    int randNumGenerator(int max){
    int num = (rand() % max);

    return num;
    }
    template <typename Predicate>
    vector<Person> filter( vector<Person>& testData, Predicate pred) {
    vector<Person> out;
    copy_if(testData.begin(), testData.end(), back_inserter(out), pred);
    return out;
    }
    vector<Person> positive = filter(testData, [=](const Person& p)
    return p.salary == ">50K"

    void setData(Person person, string line){

    line.erase(remove(line.begin(), line.end(), ','), line.end());

    stringstream s(line);
    string str;


    string workclass;
    string sex;


    string salary;


    int age;
    string ageStr;

    int fnlwgt;
    string fnlwgtStr;

    int capitalGain;
    string capitalGainStr;

    int capitalLoss;
    string capitalLossStr;

    int hoursPerWeek;
    string hoursPerWeekStr;


    if(s >> age >> workclass >> fnlwgt >> sex >> capitalGain >> capitalLoss >> hoursPerWeek >> salary)
    {


    ageStr = convertInt(age);
    fnlwgtStr = convertInt(fnlwgt);
    capitalGainStr = convertInt(capitalGain);
    capitalLossStr = convertInt(capitalLoss);
    hoursPerWeekStr = convertInt(hoursPerWeek);


    if(ageStr == "?" || workclass == "?" || fnlwgtStr == "?" || sex == "?" || capitalGainStr == "?" || capitalLossStr == "?" || hoursPerWeekStr == "?")
    {

    }
    else{
    person.age = age;
    person.workclass = workclass;
    person.fnlwgt = fnlwgt;
    person.sex = sex;
    person.capitalGain = capitalGain;
    person.capitalLoss = capitalLoss;
    person.hoursPerWeek = hoursPerWeek;
    person.salary = salary;
    testData.push_back(person);
    }
    }


    }


    vector<Person> sample(vector<Person> wholeDataSet, int percentage, vector<Person> &testingSet){
    int wholeDataSize = wholeDataSet.size();

    vector<Person> stratifiedSet;

    int limit = (wholeDataSize * percentage) / 100;
    int randNum= 0;

    vector<bool> numsUsedAlready(wholeDataSize);

    for(int i = 0; i < limit; i++){
    randNum = randNumGenerator(wholeDataSize);
    while(numsUsedAlready[randNum]){
    randNum = randNumGenerator(wholeDataSize);
    }

    numsUsedAlready[randNum] = true;
    stratifiedSet.push_back(wholeDataSet[randNum]);
    }
    for(int i = 0; i < numsUsedAlready.size(); i++){
    if(!numsUsedAlready[i]){
    testingSet.push_back(wholeDataSet[i]);
    }
    }


    return stratifiedSet;
    }

    vector<Person> concatVectors(vector<Person> a, vector<Person> b){
    vector<Person> ab;
    ab.reserve(a.size() + b.size());
    ab.insert(ab.end(), a.begin(), a.end());
    ab.insert(ab.end(), b.begin(), b.end());
    return ab;
    }

    void compareAttributeInt(int sample, int trained, string salary, int &count, int posOrNeg){
    if(sample == trained){
    if(salary == ">50K" && posOrNeg == 1){
    count++;
    }
    if(salary == "<=50K" && posOrNeg == 0){
    count++;
    }
    }
    }

    void compareAttributeStr(string sample, string trained, string salary, int &count, int posOrNeg){
    if(sample == trained){
    if(salary == ">50K" && posOrNeg == 1){
    count++;
    }
    if(salary == "<=50K" && posOrNeg == 0){
    count++;
    }

    }
    }

    class Count
    {
    public:
    int pos = 0;
    int neg = 0;
    void increment(bool flag) {
    if (flag) {
    pos++;
    }
    else {
    neg++;
    }
    }
    };

    float naiveBayesian(vector<Person> trainingSet, vector<Person> testingSet){
    float accuracy = 0;
    int accuracyCount = 0;
    int randNum = 0;

    vector<Person> sampleSet;
    vector<bool> numsUsedAlready(testingSet.size());

    for(int i = 0; i < 20; i++){
    randNum = randNumGenerator(testingSet.size());
    while(numsUsedAlready[randNum]){
    randNum = randNumGenerator(testingSet.size());
    }
    numsUsedAlready[randNum] = true;
    sampleSet.push_back(testingSet[randNum]);
    }



    int numOver50k = 0;
    int numUnder50k = 0;

    for(int i = 0; i < trainingSet.size(); i++){
    if(trainingSet[i].salary == ">50K"){
    numOver50k++;
    }
    else{
    numUnder50k++;
    }
    }

    float probOver50k = (float)numOver50k / trainingSet.size();
    float probUnder50k = (float)numUnder50k / trainingSet.size();

    float probYes = 0;
    float probNo = 0;

    float yes= 0;
    float no = 0;


    template <typename T>
    Count compute_count(const Person& sample, const vector<Person>& trainingSet, T Person::*member)
    {
    Count res;
    for (const Person& training : trainingSet) {
    if ((sample.*member) == (training.*member)) {
    res.increment(training.salary == ">50K");
    }
    }
    return res;
    }
    for (const Person& sample : sampleSet)
    {
    vector<Count> counts;
    counts.push_back(compute_count(sample, trainingSet, &Person::age));
    counts.push_back(compute_count(sample, trainingSet, &Person::workclass));
    counts.push_back(compute_count(sample, trainingSet, &Person::fnlwgt));
    counts.push_back(compute_count(sample, trainingSet, &Person::capitalGain));
    counts.push_back(compute_count(sample, trainingSet, &Person::capitalLoss));
    counts.push_back(compute_count(sample, trainingSet, &Person::hoursPerWeek));
    }

    bool salaryGreaterThan50k = (probYes * probOver50k) > (probNo * probUnder50k);

    double probYes = accumulate(counts.begin(), counts.end(), 1.0, [=](double init, const Count& count){
    return init * count.pos / numOver50k;
    });
    double probNo = accumulate(counts.begin(), counts.end(), 1.0, [=](double init, const Count& count){
    return init * count.neg / numUnder50k;
    });

    if ((salaryGreaterThan50k && (sample.salary == ">50K")) || (!salaryGreaterThan50k && !(sample.salary == ">50K")))
    ++accuracyCount;

    accuracy = (float)accuracyCount / 20;

    return accuracy;
    }

    void stratifiedSample(){
    srand(time(NULL));

    vector<Person> positiveSamples;
    vector<Person> negativeSamples;
    positiveSamples = setPositive(positiveSamples);


    negativeSamples = setNegative(negativeSamples);


    vector<Person> posTestingSet10 = positiveSamples;
    vector<Person> posTestingSet30 = positiveSamples;
    vector<Person> posTestingSet50 = positiveSamples;
    vector<Person> posTestingSet70 = positiveSamples;
    vector<Person> posTestingSet90 = positiveSamples;

    vector<Person> negTestingSet10 = negativeSamples;
    vector<Person> negTestingSet30 = negativeSamples;
    vector<Person> negTestingSet50 = negativeSamples;
    vector<Person> negTestingSet70 = negativeSamples;
    vector<Person> negTestingSet90 = negativeSamples;

    vector<Person> posStratifiedSet_10;
    vector<Person> posTesting_10;
    vector<Person> negStratifiedSet_10;
    vector<Person> negTesting_10;

    vector<Person> posStratifiedSet_30;
    vector<Person> posTesting_30;
    vector<Person> negStratifiedSet_30;
    vector<Person> negTesting_30;

    vector<Person> posStratifiedSet_50;
    vector<Person> posTesting_50;
    vector<Person> negStratifiedSet_50;
    vector<Person> negTesting_50;

    vector<Person> posStratifiedSet_70;
    vector<Person> posTesting_70;
    vector<Person> negStratifiedSet_70;
    vector<Person> negTesting_70;

    vector<Person> posStratifiedSet_90;
    vector<Person> posTesting_90;
    vector<Person> negStratifiedSet_90;
    vector<Person> negTesting_90;

    vector<Person> stratifiedSet_10;
    vector<Person> stratifiedSet_30;
    vector<Person> stratifiedSet_50;
    vector<Person> stratifiedSet_70;
    vector<Person> stratifiedSet_90;

    vector<Person> testingSet_10;
    vector<Person> testingSet_30;
    vector<Person> testingSet_50;
    vector<Person> testingSet_70;
    vector<Person> testingSet_90;

    posStratifiedSet_10 = sample(posTestingSet10, 10, posTesting_10);


    negStratifiedSet_10 = sample(negTestingSet10, 10, negTesting_10);


    posStratifiedSet_30 = sample(posTestingSet30, 30, posTesting_30);

    negStratifiedSet_30 = sample(negTestingSet30, 30, negTesting_30);


    posStratifiedSet_50 = sample(posTestingSet50, 50, posTesting_50);

    negStratifiedSet_50 = sample(negTestingSet50, 50, negTesting_50);


    posStratifiedSet_70 = sample(posTestingSet70, 70, posTesting_70);

    negStratifiedSet_70 = sample(negTestingSet70, 70, negTesting_70);


    posStratifiedSet_90 = sample(posTestingSet90, 90, posTesting_90);

    negStratifiedSet_90 = sample(negTestingSet90, 90, negTesting_90);


    stratifiedSet_10 = concatVectors(posStratifiedSet_10, negStratifiedSet_10);
    stratifiedSet_30 = concatVectors(posStratifiedSet_30, negStratifiedSet_30);
    stratifiedSet_50 = concatVectors(posStratifiedSet_50, negStratifiedSet_50);
    stratifiedSet_70 = concatVectors(posStratifiedSet_70, negStratifiedSet_70);
    stratifiedSet_90 = concatVectors(posStratifiedSet_90, negStratifiedSet_90);

    testingSet_10 = concatVectors(posTesting_10, negTesting_10);
    testingSet_30 = concatVectors(posTesting_30, negTesting_30);
    testingSet_50 = concatVectors(posTesting_50, negTesting_50);
    testingSet_70 = concatVectors(posTesting_70, negTesting_70);
    testingSet_90 = concatVectors(posTesting_90, negTesting_90);


    float accuracy10 = 0;
    float accuracy30 = 0;
    float accuracy50 = 0;
    float accuracy70 = 0;
    float accuracy90 = 0;

    accuracy10 = naiveBayesian(stratifiedSet_10, testingSet_10);
    cout << "accuracy for 10%: " << accuracy10 << endl;

    accuracy30 = naiveBayesian(stratifiedSet_30, testingSet_30);
    cout << "accuracy for 30%: " << accuracy30 << endl;

    accuracy50 = naiveBayesian(stratifiedSet_50, testingSet_50);
    cout << "accuracy for 50%: " << accuracy50 << endl;

    accuracy70 = naiveBayesian(stratifiedSet_70, testingSet_70);
    cout << "accuracy for 70%: " << accuracy70 << endl;

    accuracy90 = naiveBayesian(stratifiedSet_90, testingSet_90);
    cout << "accuracy for 90%: " << accuracy90 << endl;

    }


    void readInputFile()
    {
    ifstream reader;
    reader.open("Dataset.dat" , ios::binary);
    if(!reader)
    {
    cout << "nError in open";
    return;
    }
    Person p;
    reader.read((char*) &p, sizeof(p));
    }

    int main(int argc, char const *argv)
    {
    system("CLS");

    readInputFile();

    stratifiedSample();
    return 0;
    }


    `



    I work on the inbuilt gcc compiler for mac and the errors it throws during compilation are:



    Error IMage



    What have I been doing wrong?









    share







    New contributor




    S-A117 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0







      I was working on my project on a Naive Bayes implementation when I started running into some unexpected issues. Any help?



      `



          #include <iostream>
      #include <fstream>
      #include <string>
      #include <sstream>
      #include <vector>
      #include <algorithm>
      #include <ctime>
      #include <cstdio>
      #include <cctype>
      #include <cstdlib>
      using namespace std;

      struct Person
      {
      int age;
      string workclass;
      int fnlwgt;
      string sex;
      int capitalGain;
      int capitalLoss;
      int hoursPerWeek;
      string salary;

      void printPerson()
      {
      cout << age << endl << workclass << endl << fnlwgt;
      cout << endl << sex << endl;
      cout << capitalGain << endl << capitalLoss;
      cout << endl << hoursPerWeek << endl << salary << endl;
      }
      void getPerson()
      {
      cout << "nEnter age: ";
      cin >> age;
      cout << "nEnter work class: ";
      getline(cin, workclass);
      cout << "nEnter final weight: ";
      cin >> fnlwgt;
      cout << "nEnter sex: ";
      getline(cin, sex);
      cout << "nEnter capital gain: ";
      cin >> capitalGain;
      cout << "nEnter capital loss: ";
      cin >> capitalLoss;
      cout << "nEnter salary: ";
      getline(cin, salary);
      }

      };

      void writeToData()
      {
      ofstream writer;
      writer.open("Dataset.dat", ios::binary | ios::app);
      if (!writer)
      {
      cout << "nError in file open!!";
      return;
      }
      Person p;
      while(1)
      {
      char yn;
      p.getPerson();
      writer.write((char*)&p, sizeof(p));
      cout << "nAre there more records? ";
      cin >> yn;
      if (yn == 'n' || yn == 'N')
      {
      break;
      }
      }
      }



      string convertInt(int x){
      string result;
      ostringstream convert;

      convert << x;
      result = convert.str();
      return result;
      }

      int randNumGenerator(int max){
      int num = (rand() % max);

      return num;
      }
      template <typename Predicate>
      vector<Person> filter( vector<Person>& testData, Predicate pred) {
      vector<Person> out;
      copy_if(testData.begin(), testData.end(), back_inserter(out), pred);
      return out;
      }
      vector<Person> positive = filter(testData, [=](const Person& p)
      return p.salary == ">50K"

      void setData(Person person, string line){

      line.erase(remove(line.begin(), line.end(), ','), line.end());

      stringstream s(line);
      string str;


      string workclass;
      string sex;


      string salary;


      int age;
      string ageStr;

      int fnlwgt;
      string fnlwgtStr;

      int capitalGain;
      string capitalGainStr;

      int capitalLoss;
      string capitalLossStr;

      int hoursPerWeek;
      string hoursPerWeekStr;


      if(s >> age >> workclass >> fnlwgt >> sex >> capitalGain >> capitalLoss >> hoursPerWeek >> salary)
      {


      ageStr = convertInt(age);
      fnlwgtStr = convertInt(fnlwgt);
      capitalGainStr = convertInt(capitalGain);
      capitalLossStr = convertInt(capitalLoss);
      hoursPerWeekStr = convertInt(hoursPerWeek);


      if(ageStr == "?" || workclass == "?" || fnlwgtStr == "?" || sex == "?" || capitalGainStr == "?" || capitalLossStr == "?" || hoursPerWeekStr == "?")
      {

      }
      else{
      person.age = age;
      person.workclass = workclass;
      person.fnlwgt = fnlwgt;
      person.sex = sex;
      person.capitalGain = capitalGain;
      person.capitalLoss = capitalLoss;
      person.hoursPerWeek = hoursPerWeek;
      person.salary = salary;
      testData.push_back(person);
      }
      }


      }


      vector<Person> sample(vector<Person> wholeDataSet, int percentage, vector<Person> &testingSet){
      int wholeDataSize = wholeDataSet.size();

      vector<Person> stratifiedSet;

      int limit = (wholeDataSize * percentage) / 100;
      int randNum= 0;

      vector<bool> numsUsedAlready(wholeDataSize);

      for(int i = 0; i < limit; i++){
      randNum = randNumGenerator(wholeDataSize);
      while(numsUsedAlready[randNum]){
      randNum = randNumGenerator(wholeDataSize);
      }

      numsUsedAlready[randNum] = true;
      stratifiedSet.push_back(wholeDataSet[randNum]);
      }
      for(int i = 0; i < numsUsedAlready.size(); i++){
      if(!numsUsedAlready[i]){
      testingSet.push_back(wholeDataSet[i]);
      }
      }


      return stratifiedSet;
      }

      vector<Person> concatVectors(vector<Person> a, vector<Person> b){
      vector<Person> ab;
      ab.reserve(a.size() + b.size());
      ab.insert(ab.end(), a.begin(), a.end());
      ab.insert(ab.end(), b.begin(), b.end());
      return ab;
      }

      void compareAttributeInt(int sample, int trained, string salary, int &count, int posOrNeg){
      if(sample == trained){
      if(salary == ">50K" && posOrNeg == 1){
      count++;
      }
      if(salary == "<=50K" && posOrNeg == 0){
      count++;
      }
      }
      }

      void compareAttributeStr(string sample, string trained, string salary, int &count, int posOrNeg){
      if(sample == trained){
      if(salary == ">50K" && posOrNeg == 1){
      count++;
      }
      if(salary == "<=50K" && posOrNeg == 0){
      count++;
      }

      }
      }

      class Count
      {
      public:
      int pos = 0;
      int neg = 0;
      void increment(bool flag) {
      if (flag) {
      pos++;
      }
      else {
      neg++;
      }
      }
      };

      float naiveBayesian(vector<Person> trainingSet, vector<Person> testingSet){
      float accuracy = 0;
      int accuracyCount = 0;
      int randNum = 0;

      vector<Person> sampleSet;
      vector<bool> numsUsedAlready(testingSet.size());

      for(int i = 0; i < 20; i++){
      randNum = randNumGenerator(testingSet.size());
      while(numsUsedAlready[randNum]){
      randNum = randNumGenerator(testingSet.size());
      }
      numsUsedAlready[randNum] = true;
      sampleSet.push_back(testingSet[randNum]);
      }



      int numOver50k = 0;
      int numUnder50k = 0;

      for(int i = 0; i < trainingSet.size(); i++){
      if(trainingSet[i].salary == ">50K"){
      numOver50k++;
      }
      else{
      numUnder50k++;
      }
      }

      float probOver50k = (float)numOver50k / trainingSet.size();
      float probUnder50k = (float)numUnder50k / trainingSet.size();

      float probYes = 0;
      float probNo = 0;

      float yes= 0;
      float no = 0;


      template <typename T>
      Count compute_count(const Person& sample, const vector<Person>& trainingSet, T Person::*member)
      {
      Count res;
      for (const Person& training : trainingSet) {
      if ((sample.*member) == (training.*member)) {
      res.increment(training.salary == ">50K");
      }
      }
      return res;
      }
      for (const Person& sample : sampleSet)
      {
      vector<Count> counts;
      counts.push_back(compute_count(sample, trainingSet, &Person::age));
      counts.push_back(compute_count(sample, trainingSet, &Person::workclass));
      counts.push_back(compute_count(sample, trainingSet, &Person::fnlwgt));
      counts.push_back(compute_count(sample, trainingSet, &Person::capitalGain));
      counts.push_back(compute_count(sample, trainingSet, &Person::capitalLoss));
      counts.push_back(compute_count(sample, trainingSet, &Person::hoursPerWeek));
      }

      bool salaryGreaterThan50k = (probYes * probOver50k) > (probNo * probUnder50k);

      double probYes = accumulate(counts.begin(), counts.end(), 1.0, [=](double init, const Count& count){
      return init * count.pos / numOver50k;
      });
      double probNo = accumulate(counts.begin(), counts.end(), 1.0, [=](double init, const Count& count){
      return init * count.neg / numUnder50k;
      });

      if ((salaryGreaterThan50k && (sample.salary == ">50K")) || (!salaryGreaterThan50k && !(sample.salary == ">50K")))
      ++accuracyCount;

      accuracy = (float)accuracyCount / 20;

      return accuracy;
      }

      void stratifiedSample(){
      srand(time(NULL));

      vector<Person> positiveSamples;
      vector<Person> negativeSamples;
      positiveSamples = setPositive(positiveSamples);


      negativeSamples = setNegative(negativeSamples);


      vector<Person> posTestingSet10 = positiveSamples;
      vector<Person> posTestingSet30 = positiveSamples;
      vector<Person> posTestingSet50 = positiveSamples;
      vector<Person> posTestingSet70 = positiveSamples;
      vector<Person> posTestingSet90 = positiveSamples;

      vector<Person> negTestingSet10 = negativeSamples;
      vector<Person> negTestingSet30 = negativeSamples;
      vector<Person> negTestingSet50 = negativeSamples;
      vector<Person> negTestingSet70 = negativeSamples;
      vector<Person> negTestingSet90 = negativeSamples;

      vector<Person> posStratifiedSet_10;
      vector<Person> posTesting_10;
      vector<Person> negStratifiedSet_10;
      vector<Person> negTesting_10;

      vector<Person> posStratifiedSet_30;
      vector<Person> posTesting_30;
      vector<Person> negStratifiedSet_30;
      vector<Person> negTesting_30;

      vector<Person> posStratifiedSet_50;
      vector<Person> posTesting_50;
      vector<Person> negStratifiedSet_50;
      vector<Person> negTesting_50;

      vector<Person> posStratifiedSet_70;
      vector<Person> posTesting_70;
      vector<Person> negStratifiedSet_70;
      vector<Person> negTesting_70;

      vector<Person> posStratifiedSet_90;
      vector<Person> posTesting_90;
      vector<Person> negStratifiedSet_90;
      vector<Person> negTesting_90;

      vector<Person> stratifiedSet_10;
      vector<Person> stratifiedSet_30;
      vector<Person> stratifiedSet_50;
      vector<Person> stratifiedSet_70;
      vector<Person> stratifiedSet_90;

      vector<Person> testingSet_10;
      vector<Person> testingSet_30;
      vector<Person> testingSet_50;
      vector<Person> testingSet_70;
      vector<Person> testingSet_90;

      posStratifiedSet_10 = sample(posTestingSet10, 10, posTesting_10);


      negStratifiedSet_10 = sample(negTestingSet10, 10, negTesting_10);


      posStratifiedSet_30 = sample(posTestingSet30, 30, posTesting_30);

      negStratifiedSet_30 = sample(negTestingSet30, 30, negTesting_30);


      posStratifiedSet_50 = sample(posTestingSet50, 50, posTesting_50);

      negStratifiedSet_50 = sample(negTestingSet50, 50, negTesting_50);


      posStratifiedSet_70 = sample(posTestingSet70, 70, posTesting_70);

      negStratifiedSet_70 = sample(negTestingSet70, 70, negTesting_70);


      posStratifiedSet_90 = sample(posTestingSet90, 90, posTesting_90);

      negStratifiedSet_90 = sample(negTestingSet90, 90, negTesting_90);


      stratifiedSet_10 = concatVectors(posStratifiedSet_10, negStratifiedSet_10);
      stratifiedSet_30 = concatVectors(posStratifiedSet_30, negStratifiedSet_30);
      stratifiedSet_50 = concatVectors(posStratifiedSet_50, negStratifiedSet_50);
      stratifiedSet_70 = concatVectors(posStratifiedSet_70, negStratifiedSet_70);
      stratifiedSet_90 = concatVectors(posStratifiedSet_90, negStratifiedSet_90);

      testingSet_10 = concatVectors(posTesting_10, negTesting_10);
      testingSet_30 = concatVectors(posTesting_30, negTesting_30);
      testingSet_50 = concatVectors(posTesting_50, negTesting_50);
      testingSet_70 = concatVectors(posTesting_70, negTesting_70);
      testingSet_90 = concatVectors(posTesting_90, negTesting_90);


      float accuracy10 = 0;
      float accuracy30 = 0;
      float accuracy50 = 0;
      float accuracy70 = 0;
      float accuracy90 = 0;

      accuracy10 = naiveBayesian(stratifiedSet_10, testingSet_10);
      cout << "accuracy for 10%: " << accuracy10 << endl;

      accuracy30 = naiveBayesian(stratifiedSet_30, testingSet_30);
      cout << "accuracy for 30%: " << accuracy30 << endl;

      accuracy50 = naiveBayesian(stratifiedSet_50, testingSet_50);
      cout << "accuracy for 50%: " << accuracy50 << endl;

      accuracy70 = naiveBayesian(stratifiedSet_70, testingSet_70);
      cout << "accuracy for 70%: " << accuracy70 << endl;

      accuracy90 = naiveBayesian(stratifiedSet_90, testingSet_90);
      cout << "accuracy for 90%: " << accuracy90 << endl;

      }


      void readInputFile()
      {
      ifstream reader;
      reader.open("Dataset.dat" , ios::binary);
      if(!reader)
      {
      cout << "nError in open";
      return;
      }
      Person p;
      reader.read((char*) &p, sizeof(p));
      }

      int main(int argc, char const *argv)
      {
      system("CLS");

      readInputFile();

      stratifiedSample();
      return 0;
      }


      `



      I work on the inbuilt gcc compiler for mac and the errors it throws during compilation are:



      Error IMage



      What have I been doing wrong?









      share







      New contributor




      S-A117 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I was working on my project on a Naive Bayes implementation when I started running into some unexpected issues. Any help?



      `



          #include <iostream>
      #include <fstream>
      #include <string>
      #include <sstream>
      #include <vector>
      #include <algorithm>
      #include <ctime>
      #include <cstdio>
      #include <cctype>
      #include <cstdlib>
      using namespace std;

      struct Person
      {
      int age;
      string workclass;
      int fnlwgt;
      string sex;
      int capitalGain;
      int capitalLoss;
      int hoursPerWeek;
      string salary;

      void printPerson()
      {
      cout << age << endl << workclass << endl << fnlwgt;
      cout << endl << sex << endl;
      cout << capitalGain << endl << capitalLoss;
      cout << endl << hoursPerWeek << endl << salary << endl;
      }
      void getPerson()
      {
      cout << "nEnter age: ";
      cin >> age;
      cout << "nEnter work class: ";
      getline(cin, workclass);
      cout << "nEnter final weight: ";
      cin >> fnlwgt;
      cout << "nEnter sex: ";
      getline(cin, sex);
      cout << "nEnter capital gain: ";
      cin >> capitalGain;
      cout << "nEnter capital loss: ";
      cin >> capitalLoss;
      cout << "nEnter salary: ";
      getline(cin, salary);
      }

      };

      void writeToData()
      {
      ofstream writer;
      writer.open("Dataset.dat", ios::binary | ios::app);
      if (!writer)
      {
      cout << "nError in file open!!";
      return;
      }
      Person p;
      while(1)
      {
      char yn;
      p.getPerson();
      writer.write((char*)&p, sizeof(p));
      cout << "nAre there more records? ";
      cin >> yn;
      if (yn == 'n' || yn == 'N')
      {
      break;
      }
      }
      }



      string convertInt(int x){
      string result;
      ostringstream convert;

      convert << x;
      result = convert.str();
      return result;
      }

      int randNumGenerator(int max){
      int num = (rand() % max);

      return num;
      }
      template <typename Predicate>
      vector<Person> filter( vector<Person>& testData, Predicate pred) {
      vector<Person> out;
      copy_if(testData.begin(), testData.end(), back_inserter(out), pred);
      return out;
      }
      vector<Person> positive = filter(testData, [=](const Person& p)
      return p.salary == ">50K"

      void setData(Person person, string line){

      line.erase(remove(line.begin(), line.end(), ','), line.end());

      stringstream s(line);
      string str;


      string workclass;
      string sex;


      string salary;


      int age;
      string ageStr;

      int fnlwgt;
      string fnlwgtStr;

      int capitalGain;
      string capitalGainStr;

      int capitalLoss;
      string capitalLossStr;

      int hoursPerWeek;
      string hoursPerWeekStr;


      if(s >> age >> workclass >> fnlwgt >> sex >> capitalGain >> capitalLoss >> hoursPerWeek >> salary)
      {


      ageStr = convertInt(age);
      fnlwgtStr = convertInt(fnlwgt);
      capitalGainStr = convertInt(capitalGain);
      capitalLossStr = convertInt(capitalLoss);
      hoursPerWeekStr = convertInt(hoursPerWeek);


      if(ageStr == "?" || workclass == "?" || fnlwgtStr == "?" || sex == "?" || capitalGainStr == "?" || capitalLossStr == "?" || hoursPerWeekStr == "?")
      {

      }
      else{
      person.age = age;
      person.workclass = workclass;
      person.fnlwgt = fnlwgt;
      person.sex = sex;
      person.capitalGain = capitalGain;
      person.capitalLoss = capitalLoss;
      person.hoursPerWeek = hoursPerWeek;
      person.salary = salary;
      testData.push_back(person);
      }
      }


      }


      vector<Person> sample(vector<Person> wholeDataSet, int percentage, vector<Person> &testingSet){
      int wholeDataSize = wholeDataSet.size();

      vector<Person> stratifiedSet;

      int limit = (wholeDataSize * percentage) / 100;
      int randNum= 0;

      vector<bool> numsUsedAlready(wholeDataSize);

      for(int i = 0; i < limit; i++){
      randNum = randNumGenerator(wholeDataSize);
      while(numsUsedAlready[randNum]){
      randNum = randNumGenerator(wholeDataSize);
      }

      numsUsedAlready[randNum] = true;
      stratifiedSet.push_back(wholeDataSet[randNum]);
      }
      for(int i = 0; i < numsUsedAlready.size(); i++){
      if(!numsUsedAlready[i]){
      testingSet.push_back(wholeDataSet[i]);
      }
      }


      return stratifiedSet;
      }

      vector<Person> concatVectors(vector<Person> a, vector<Person> b){
      vector<Person> ab;
      ab.reserve(a.size() + b.size());
      ab.insert(ab.end(), a.begin(), a.end());
      ab.insert(ab.end(), b.begin(), b.end());
      return ab;
      }

      void compareAttributeInt(int sample, int trained, string salary, int &count, int posOrNeg){
      if(sample == trained){
      if(salary == ">50K" && posOrNeg == 1){
      count++;
      }
      if(salary == "<=50K" && posOrNeg == 0){
      count++;
      }
      }
      }

      void compareAttributeStr(string sample, string trained, string salary, int &count, int posOrNeg){
      if(sample == trained){
      if(salary == ">50K" && posOrNeg == 1){
      count++;
      }
      if(salary == "<=50K" && posOrNeg == 0){
      count++;
      }

      }
      }

      class Count
      {
      public:
      int pos = 0;
      int neg = 0;
      void increment(bool flag) {
      if (flag) {
      pos++;
      }
      else {
      neg++;
      }
      }
      };

      float naiveBayesian(vector<Person> trainingSet, vector<Person> testingSet){
      float accuracy = 0;
      int accuracyCount = 0;
      int randNum = 0;

      vector<Person> sampleSet;
      vector<bool> numsUsedAlready(testingSet.size());

      for(int i = 0; i < 20; i++){
      randNum = randNumGenerator(testingSet.size());
      while(numsUsedAlready[randNum]){
      randNum = randNumGenerator(testingSet.size());
      }
      numsUsedAlready[randNum] = true;
      sampleSet.push_back(testingSet[randNum]);
      }



      int numOver50k = 0;
      int numUnder50k = 0;

      for(int i = 0; i < trainingSet.size(); i++){
      if(trainingSet[i].salary == ">50K"){
      numOver50k++;
      }
      else{
      numUnder50k++;
      }
      }

      float probOver50k = (float)numOver50k / trainingSet.size();
      float probUnder50k = (float)numUnder50k / trainingSet.size();

      float probYes = 0;
      float probNo = 0;

      float yes= 0;
      float no = 0;


      template <typename T>
      Count compute_count(const Person& sample, const vector<Person>& trainingSet, T Person::*member)
      {
      Count res;
      for (const Person& training : trainingSet) {
      if ((sample.*member) == (training.*member)) {
      res.increment(training.salary == ">50K");
      }
      }
      return res;
      }
      for (const Person& sample : sampleSet)
      {
      vector<Count> counts;
      counts.push_back(compute_count(sample, trainingSet, &Person::age));
      counts.push_back(compute_count(sample, trainingSet, &Person::workclass));
      counts.push_back(compute_count(sample, trainingSet, &Person::fnlwgt));
      counts.push_back(compute_count(sample, trainingSet, &Person::capitalGain));
      counts.push_back(compute_count(sample, trainingSet, &Person::capitalLoss));
      counts.push_back(compute_count(sample, trainingSet, &Person::hoursPerWeek));
      }

      bool salaryGreaterThan50k = (probYes * probOver50k) > (probNo * probUnder50k);

      double probYes = accumulate(counts.begin(), counts.end(), 1.0, [=](double init, const Count& count){
      return init * count.pos / numOver50k;
      });
      double probNo = accumulate(counts.begin(), counts.end(), 1.0, [=](double init, const Count& count){
      return init * count.neg / numUnder50k;
      });

      if ((salaryGreaterThan50k && (sample.salary == ">50K")) || (!salaryGreaterThan50k && !(sample.salary == ">50K")))
      ++accuracyCount;

      accuracy = (float)accuracyCount / 20;

      return accuracy;
      }

      void stratifiedSample(){
      srand(time(NULL));

      vector<Person> positiveSamples;
      vector<Person> negativeSamples;
      positiveSamples = setPositive(positiveSamples);


      negativeSamples = setNegative(negativeSamples);


      vector<Person> posTestingSet10 = positiveSamples;
      vector<Person> posTestingSet30 = positiveSamples;
      vector<Person> posTestingSet50 = positiveSamples;
      vector<Person> posTestingSet70 = positiveSamples;
      vector<Person> posTestingSet90 = positiveSamples;

      vector<Person> negTestingSet10 = negativeSamples;
      vector<Person> negTestingSet30 = negativeSamples;
      vector<Person> negTestingSet50 = negativeSamples;
      vector<Person> negTestingSet70 = negativeSamples;
      vector<Person> negTestingSet90 = negativeSamples;

      vector<Person> posStratifiedSet_10;
      vector<Person> posTesting_10;
      vector<Person> negStratifiedSet_10;
      vector<Person> negTesting_10;

      vector<Person> posStratifiedSet_30;
      vector<Person> posTesting_30;
      vector<Person> negStratifiedSet_30;
      vector<Person> negTesting_30;

      vector<Person> posStratifiedSet_50;
      vector<Person> posTesting_50;
      vector<Person> negStratifiedSet_50;
      vector<Person> negTesting_50;

      vector<Person> posStratifiedSet_70;
      vector<Person> posTesting_70;
      vector<Person> negStratifiedSet_70;
      vector<Person> negTesting_70;

      vector<Person> posStratifiedSet_90;
      vector<Person> posTesting_90;
      vector<Person> negStratifiedSet_90;
      vector<Person> negTesting_90;

      vector<Person> stratifiedSet_10;
      vector<Person> stratifiedSet_30;
      vector<Person> stratifiedSet_50;
      vector<Person> stratifiedSet_70;
      vector<Person> stratifiedSet_90;

      vector<Person> testingSet_10;
      vector<Person> testingSet_30;
      vector<Person> testingSet_50;
      vector<Person> testingSet_70;
      vector<Person> testingSet_90;

      posStratifiedSet_10 = sample(posTestingSet10, 10, posTesting_10);


      negStratifiedSet_10 = sample(negTestingSet10, 10, negTesting_10);


      posStratifiedSet_30 = sample(posTestingSet30, 30, posTesting_30);

      negStratifiedSet_30 = sample(negTestingSet30, 30, negTesting_30);


      posStratifiedSet_50 = sample(posTestingSet50, 50, posTesting_50);

      negStratifiedSet_50 = sample(negTestingSet50, 50, negTesting_50);


      posStratifiedSet_70 = sample(posTestingSet70, 70, posTesting_70);

      negStratifiedSet_70 = sample(negTestingSet70, 70, negTesting_70);


      posStratifiedSet_90 = sample(posTestingSet90, 90, posTesting_90);

      negStratifiedSet_90 = sample(negTestingSet90, 90, negTesting_90);


      stratifiedSet_10 = concatVectors(posStratifiedSet_10, negStratifiedSet_10);
      stratifiedSet_30 = concatVectors(posStratifiedSet_30, negStratifiedSet_30);
      stratifiedSet_50 = concatVectors(posStratifiedSet_50, negStratifiedSet_50);
      stratifiedSet_70 = concatVectors(posStratifiedSet_70, negStratifiedSet_70);
      stratifiedSet_90 = concatVectors(posStratifiedSet_90, negStratifiedSet_90);

      testingSet_10 = concatVectors(posTesting_10, negTesting_10);
      testingSet_30 = concatVectors(posTesting_30, negTesting_30);
      testingSet_50 = concatVectors(posTesting_50, negTesting_50);
      testingSet_70 = concatVectors(posTesting_70, negTesting_70);
      testingSet_90 = concatVectors(posTesting_90, negTesting_90);


      float accuracy10 = 0;
      float accuracy30 = 0;
      float accuracy50 = 0;
      float accuracy70 = 0;
      float accuracy90 = 0;

      accuracy10 = naiveBayesian(stratifiedSet_10, testingSet_10);
      cout << "accuracy for 10%: " << accuracy10 << endl;

      accuracy30 = naiveBayesian(stratifiedSet_30, testingSet_30);
      cout << "accuracy for 30%: " << accuracy30 << endl;

      accuracy50 = naiveBayesian(stratifiedSet_50, testingSet_50);
      cout << "accuracy for 50%: " << accuracy50 << endl;

      accuracy70 = naiveBayesian(stratifiedSet_70, testingSet_70);
      cout << "accuracy for 70%: " << accuracy70 << endl;

      accuracy90 = naiveBayesian(stratifiedSet_90, testingSet_90);
      cout << "accuracy for 90%: " << accuracy90 << endl;

      }


      void readInputFile()
      {
      ifstream reader;
      reader.open("Dataset.dat" , ios::binary);
      if(!reader)
      {
      cout << "nError in open";
      return;
      }
      Person p;
      reader.read((char*) &p, sizeof(p));
      }

      int main(int argc, char const *argv)
      {
      system("CLS");

      readInputFile();

      stratifiedSample();
      return 0;
      }


      `



      I work on the inbuilt gcc compiler for mac and the errors it throws during compilation are:



      Error IMage



      What have I been doing wrong?







      c++ error-handling





      share







      New contributor




      S-A117 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      S-A117 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      S-A117 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 mins ago









      S-A117

      1




      1




      New contributor




      S-A117 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      S-A117 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      S-A117 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          S-A117 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210185%2funexpected-errors-in-naive-bayes-classifier%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          S-A117 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          S-A117 is a new contributor. Be nice, and check out our Code of Conduct.













          S-A117 is a new contributor. Be nice, and check out our Code of Conduct.












          S-A117 is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210185%2funexpected-errors-in-naive-bayes-classifier%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python