NTU-Coursera機器學習:HomeWork 2 Q16-20
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(minErrInPositivecurMinErrIn){
minErrIn = curMinErrIn;
minErrInDem = dem+1;
}
}
cout<<"The demension with min error is : "< 文件打開失敗"<輸出結果:
關於Machine Learning更多討論與交流,敬請關注本博客和新浪微博songzi_tea.