Question: Please write the following code properly in C + + programming language: #include #include #include #include #include #define MAX _ ROWS 4 3 5 #define

Please write the following code properly in C++ programming language:
#include #include #include #include #include #define MAX_ROWS 435 #define MAX_COLS 17 #define MAX_CLASS 2// Define classes #define DEMOCRAT 0 #define REPUBLICAN 1// Define votes #define YEA 1 #define NAY 0 #define ABSTAIN 2// Data structure for the dataset typedef struct { int class; int attributes[MAX_COLS -1]; } Record; Record data[MAX_ROWS]; int total_records =0; // Function to read the dataset void read_data(const char *filename, int treat_missing_as_abstain){ FILE *file = fopen(filename,"r"); if (!file){ perror("Error opening file"); exit(EXIT_FAILURE); } char line[256]; total_records =0; while (fgets(line, sizeof(line), file)){ char *token = strtok(line,","); data[total_records].class = strcmp(token, "democrat")==0? DEMOCRAT : REPUBLICAN; for (int i =0; i < MAX_COLS -1; i++){ token = strtok(NULL,","); if (strcmp(token,"y")==0){ data[total_records].attributes[i]= YEA; } else if (strcmp(token,"n")==0){ data[total_records].attributes[i]= NAY; } else { data[total_records].attributes[i]= treat_missing_as_abstain ? ABSTAIN : -1; }} total_records++; } fclose(file); }// Function to shuffle data void shuffle_data(Record *data, int n){ srand(time(NULL)); for (int i = n -1; i >0; i--){ int j = rand()%(i +1); Record temp = data[i]; data[i]= data[j]; data[j]= temp; }}// Function to stratify split the data void stratified_split(Record *data, int total, Record *train, int *train_size, Record *test, int *test_size, double split_ratio){ int democrat_count =0, republican_count =0; for (int i =0; i < total; i++){ if (data[i].class == DEMOCRAT) democrat_count++; else republican_count++; } int train_democrats = democrat_count * split_ratio; int train_republicans = republican_count * split_ratio; int democrat_added =0, republican_added =0; *train_size =*test_size =0; for (int i =0; i < total; i++){ if (data[i].class == DEMOCRAT && democrat_added < train_democrats){ train[(*train_size)++]= data[i]; democrat_added++; } else if (data[i].class == REPUBLICAN && republican_added < train_republicans){ train[(*train_size)++]= data[i]; republican_added++; } else { test[(*test_size)++]= data[i]; }}}// Function to calculate probabilities with Laplace smoothing void calculate_probabilities(Record *train, int train_size, double probabilities[MAX_CLASS][MAX_COLS -1][3], double class_priors[MAX_CLASS], double lambda){ int class_counts[MAX_CLASS]={0}; for (int c =0; c < MAX_CLASS; c++){ for (int i =0; i < MAX_COLS -1; i++){ for (int v =0; v <3; v++){ probabilities[c][i][v]= lambda; }}} for (int i =0; i < train_size; i++){ class_counts[train[i].class]++; for (int j =0; j < MAX_COLS -1; j++){ if (train[i].attributes[j]!=-1){ probabilities[train[i].class][j][train[i].attributes[j]]++; }}} for (int c =0; c < MAX_CLASS; c++){ class_priors[c]=(double)class_counts[c]/ train_size; for (int i =0; i < MAX_COLS -1; i++){ double total = class_counts[c]+3* lambda; for (int v =0; v <3; v++){ probabilities[c][i][v]/= total; }}}}// Function to predict using Naive Bayes int naive_bayes_predict(Record *record, double probabilities[MAX_CLASS][MAX_COLS -1][3], double class_priors[MAX_CLASS]){ double log_probs[MAX_CLASS]={log(class_priors[DEMOCRAT]), log(class_priors[REPUBLICAN])}; for (int c =0; c < MAX_CLASS; c++){ for (int i =0; i < MAX_COLS -1; i++){ if (record->attributes[i]!=-1){ log_probs[c]+= log(probabilities[c][i][record->attributes[i]]); }}} return log_probs[DEMOCRAT]> log_probs[REPUBLICAN]? DEMOCRAT : REPUBLICAN; }// Function to evaluate model accuracy void evaluate_model(Record *data, int size, double probabilities[MAX_CLASS][MAX_COLS -1][3], double class_priors[MAX_CLASS], double *accuracy){ int correct =0; for (int i =0; i < size; i++){ if (naive_bayes_predict(&data[i], probabilities, class_priors)== data[i].class){ correct++; }}*accuracy =(double)correct / size; }// Function for 10-fold cross-validation void

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!