QUESTION 16~18


vcHLxMe49me6r8r9oaM8L3A+PHA+PC9wPjxwcmUgY2xhc3M9"brush:java;">#include
#include
#include
#include
using namespace std; #define SAMPLE_SIZE 20 //样本量 struct Hypothesis{ int coef; double threshold; }; //求数字的符号 int sign(double x) { if(x<0) return -1; else if(x>0) return 1; else return -1; } //反转数字的符号 int flipSign(int num) { return num * (-1); } //计算样本错误率 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); errInTotal += decisionStump(inputVec,outputVec,hypo); errOutTotal += calErrOutSample(hypo); cout<<"-----------------第"<
输出结果

QUESTION 19~20

这一题把16题中的 decision stump 拓展到多维,要求找出E-in最小的那一维并在测试数据上计算对应维度的E-out:

#include
#include
#include
#include
#include
using namespace std; #define DEMENSION 9 //数据维度 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; }; //求数字的符号 int sign(double x) { if(x<0) return -1; else if(x>0) return 1; else return -1; } //从文件读取数据 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(); } //计算指定维度的样本错误率 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
& hypo) { int recordSize = trainingSet.size(); int minErrInDem; double minErrIn = 1.1; for(int dem=0;dem
singleDemensionVec; double curMinErrIn; double bestThresPositive; double bestThresNegtive; 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 : "<
文件打开失败"<
输出结果:
关于Machine Learning更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.