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 MAXROWS #define MAXCOLS #define MAXCLASS Define classes #define DEMOCRAT #define REPUBLICAN Define votes #define YEA #define NAY #define ABSTAIN Data structure for the dataset typedef struct int class; int attributesMAXCOLS ; Record; Record dataMAXROWS; int totalrecords ; Function to read the dataset void readdataconst char filename int treatmissingasabstain FILE file fopenfilenamer; if file perrorError opening file"; exitEXITFAILURE; char line; totalrecords ; while fgetsline sizeofline file char token strtokline; datatotalrecordsclass strcmptoken "democrat" DEMOCRAT : REPUBLICAN; for int i ; i MAXCOLS ; i token strtokNULL; if strcmptokeny datatotalrecordsattributesi YEA; else if strcmptokenn datatotalrecordsattributesi NAY; else datatotalrecordsattributesi treatmissingasabstain ABSTAIN : ; totalrecords; fclosefile; Function to shuffle data void shuffledataRecord data int n srandtimeNULL; for int i n ; i ; i int j randi ; Record temp datai; datai dataj; dataj temp; Function to stratify split the data void stratifiedsplitRecord data int total, Record train int trainsize, Record test int testsize, double splitratio int democratcount republicancount ; for int i ; i total; i if dataiclass DEMOCRAT democratcount; else republicancount; int traindemocrats democratcount splitratio; int trainrepublicans republicancount splitratio; int democratadded republicanadded ; trainsize testsize ; for int i ; i total; i if dataiclass DEMOCRAT && democratadded traindemocrats traintrainsize datai; democratadded; else if dataiclass REPUBLICAN && republicanadded trainrepublicans traintrainsize datai; republicanadded; else testtestsize datai; Function to calculate probabilities with Laplace smoothing void calculateprobabilitiesRecord train int trainsize, double probabilitiesMAXCLASSMAXCOLS double classpriorsMAXCLASS double lambda int classcountsMAXCLASS; for int c ; c MAXCLASS; c for int i ; i MAXCOLS ; i for int v ; v ; v probabilitiesciv lambda; for int i ; i trainsize; i classcountstrainiclass; for int j ; j MAXCOLS ; j if trainiattributesj probabilitiestrainiclassjtrainiattributesj; for int c ; c MAXCLASS; c classpriorscdoubleclasscountsc trainsize; for int i ; i MAXCOLS ; i double total classcountsc lambda; for int v ; v ; v probabilitiesciv total; Function to predict using Naive Bayes int naivebayespredictRecord record double probabilitiesMAXCLASSMAXCOLS double classpriorsMAXCLASS double logprobsMAXCLASSlogclasspriorsDEMOCRAT logclasspriorsREPUBLICAN; for int c ; c MAXCLASS; c for int i ; i MAXCOLS ; i if recordattributesi logprobsc logprobabilitiescirecordattributesi; return logprobsDEMOCRAT logprobsREPUBLICAN DEMOCRAT : REPUBLICAN; Function to evaluate model accuracy void evaluatemodelRecord data int size, double probabilitiesMAXCLASSMAXCOLS double classpriorsMAXCLASS double accuracy int correct ; for int i ; i size; i if naivebayespredict&datai probabilities, classpriors dataiclass correct; accuracy doublecorrect size; Function for fold crossvalidation void
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
