NTU-Coursera Machine Learning: HomeWork 2 Q16-20
QUESTION 16 ~ 18
VcHLxMe49me6r8r9oaM8L3A + PHA + PC9wPjxwcmUgY2xhc3M9 "brush: java;"> # include # Include # Include # Include using namespace std; # define SAMPLE_SIZE 20 // sample size struct Hypothesis {int coef; double threshold ;}; // calculate the digit's symbol int sign (double x) {if (x <0) return-1; else if (x> 0) return 1; else return-1;} // returns the int flipSign (int num) of the reversed number) {return num * (-1);} // calculates the sample error rate. double calErrInSample (vector & InputVec, vector & OutputVec, Hypothesis & hypo) {int errCount = 0; for (int I = 0; I & InputVec) {for (int I = 0; I & InputVec, vector & OutputVec) {int output; double randNum; for (int I = 0; I & InputVec, vector & OutputVec, Hypothesis & hypo, double & bestThres) {double minErrIn = 1.0; double curErrIn; for (int I = 0; I & InputVec, vector & OutputVec, Hypothesis & hypo) {double minErrInPositive = 1.0; double minErrInNegtive = 1.0; double minErrIn; double bestThresPositive; double bestThresNegtive; hypo. coef = 1; minErrInPositive = getMinErrIn (inputVec, outputVec, hypo, bestThresPositive); hypo. coef =-1; minErrInNegtive = getMinErrIn (inputVec, outputVec, hypo, bestThresNegtive); if (minErrInPositive InputVec; vector OutputVec; Hypothesis hypo; getTrainingData (inputVec); calOutput (inputVec, outputVec); errpivotal + = encrypt (inputVec, outputVec, hypo); errOutTotal + = calErrOutSample (hypo ); cout <"----------------- No." < Output result
QUESTION 19 ~ 20
This topic expands the demo-stump in question 16 to multiple dimensions. It is required to find the dimension with the smallest E-in and calculate the E-out of the corresponding dimension on the test data:
# Include
# Include
# Include
# Include
# Include using namespace std; # define DEMENSION 9 // data dimension char * file = "training.txt"; char * file_test = "testing.txt"; struct record {double input [DEMENSION]; int output ;}; struct singleDemensionRecord {double input; int output ;}; struct Hypothesis {int coef; double threshold ;}; // evaluate the int sign of a number (double x) {if (x <0) return-1; else if (x> 0) return 1; else return-1;} // read data from the file void getData (ifstream & dataFile, vector
& Data) {while (! DataFile. eof () {record curRecord; for (int I = 0; I
> CurRecord. input [I];} dataFile> curRecord. output; data. push_back (curRecord);} dataFile. close () ;}// calculate the sample error rate of the specified dimension double calErr (vector
& SingleDemensionVec, vector
& Hypo, int demension) {int errCount = 0; int length = singleDemensionVec. size (); for (int I = 0; I
& DataSet, vector
& SingleDemensionVec, int demension) {int recordSize = dataSet. size (); singleDemensionRecord curRec; for (int I = 0; I
& SingleDemensionVec, vector
& Hypo, int demension, double & bestThres) {double minErrIn = 1.0; double curErrIn; int recordSize = singleDemensionVec. size (); for (int I = 0; I
& TrainingSet, vector
& TestSet, vector
& Amp; hypo) {int recordSize = trainingSet. size (); int minErrInDem; double minErrIn = 1.1; for (int dem = 0; dem
SingleDemensionVec; double curMinErrIn; double round; double minErrInPositive; double minErrInNegtive; getInputByDemension (trainingSet, singleDemensionVec, dem + 1); hypo [dem]. coef = 1; minErrInPositive = getMinErrIn (singleDemensionVec, hypo, dem + 1, bestThresPositive); hypo [dem]. coef =-1; minErrInNegtive = getMinErrIn (singleDemensionVec, hypo, dem + 1, bestThresNegtive); if (minErrInPositive
CurMinErrIn) {minErrIn = curMinErrIn; minErrInDem = dem + 1 ;}} cout <"The demension with min error is:" <
File opening failed "<
Output result:
For more discussions and exchanges on Machine Learning, please stay tuned to this blog and Sina Weibo songzi_tea.