📄 adaboost_common.cpp
字号:
if (*i == (c_train-1)){
*i = (c_train-10);
}
}
if (index > 0 && index < 100){
(*i) = floor(index/2)+1;
if (*i == (c_train-1)){
*i = (c_train-10);
}
}
//Matrix* i_all = new Matrix(cycles,value_one);
i_all->setValue(j,*i);
if (j > 2)
{
get_i_all->matrixCompareAndSet(i,c_train,*i_all);
}
// Get the avarage of thr data
double Sum_ava_data=0;
Ava_data_matrix->matrixSumCol(&Sum_ava_data, *train);
(*t) = Sum_ava_data / (step * (c_train-2));
// if (step >= 300){
// (*t) = (*t) + 15;
// }
/*
//t=16*(mod(tt,16)+1);
//(*t) = sqrt_c_train * ((index % 16) + 1);
(*t) = 9 * ((index % 13) + 1);
t_all->setValue(j,*t);
if (j > 1)
{
get_t_all->matrixCompareAndSetfor_t(j,t,c_train,*t_all);
} */
/*
//(*i) = index;
// t=16*(mod(tt,4)+1);
int i_rand_tmp;
//int i_rand;
srand( (unsigned)time( NULL ) );
i_rand_tmp=rand();
if (i_rand_tmp > 100 && i_rand_tmp < 1000){
//i_rand=floor(i_rand_tmp/100)+1;
(*t)=floor(i_rand_tmp/10)+50;
if (*t < 75) {
(*t)=floor(i_rand_tmp/10)+70;
}
}
if (i_rand_tmp > 1000 && i_rand_tmp < 10000){
//i_rand=floor(i_rand_tmp/100)+1;
(*t)=floor(i_rand_tmp/100)+60;
if (*t > 105) {
(*t)=floor(i_rand_tmp/1000)+70;
}
if (*t < 75) {
(*t)=floor(i_rand_tmp/100)+70;
}
}
if (i_rand_tmp > 10000 && i_rand_tmp < 100000){
//i_rand=floor(i_rand_tmp/1000)+100;
(*t)=floor(i_rand_tmp/1000)+80;
if (*t > 105) {
(*t)=floor(i_rand_tmp/1000)+70;
}
}
if (*t < 75 ){
(*t) = (*t) + 10;
}
if (*t > 110) {
(*t)=(*t) - 10;
}
t_all->setValue(j,*t);
if (j > 1)
{
get_t_all->matrixCompareAndSetfor_t(j,t,c_train,*t_all);
}
(*t)=120;
*/
//(*t) = 3 * sqrt_c_train * ((index % sqrt_c_train) + 1);
//double error_train_array[443];
//Matrix* getArrayTrain_error = new Matrix(443,value_one);
//getArrayTrain_error->matrixToArrayold(error_train_array,*errorAbsMinusScalar);
/*
int parse_dataset(char *line,long *train_label,long*numwords,long train_max_words_doc)
{
register long wpos,pos;
//long wnum;
//double weight;
pos=0;
int len;
len = strlen(line);
len=len-2;
while(line[len])
{ // cut off comments
if(line[pos] == '#')
{
line[pos]=0;
}
else
{
pos++;
}
}
wpos=0;
if((sscanf(line,"%ld",train_label)) == EOF) return(0);
pos=0;
}
int read_documents(char *docfile,
DOC *docs,
long *label,
long max_words_doc,
long ll,
long *totwords,
long *totdoc)
{
char *line;
DOC doc;
long dnum=0,wpos,i,dpos=0,dneg=0,dunlab=0;
long doc_label;
FILE *docfl;
line = (char *)my_malloc(sizeof(char)*ll);
if ((docfl = fopen (docfile, "r")) == NULL)
{
printe (docfile);
return -1;
}
doc.words = (SVM_WORD *)my_malloc(sizeof(SVM_WORD)*(max_words_doc+10));
if (com_pro.show_readfile)
{
sprintf(temstr,"Reading examples into memory...");
printm(temstr);
}
dnum=0;
(*totwords)=0;
while((!feof(docfl)) && fgets(line,(int)ll,docfl)) {
if(line[0] == '#') continue; // line contains comments
if(!parse_document(line,&doc,&doc_label,&wpos,max_words_doc))
{
sprintf(temstr,"Parsing error in line %ld!",dnum);
printm(temstr);
}
label[dnum]=doc_label;
if(doc_label > 0) dpos++;
if (doc_label < 0) dneg++;
if (doc_label == 0) dunlab++;
if((wpos>1) && ((doc.words[wpos-2]).wnum>(*totwords)))
(*totwords)=(doc.words[wpos-2]).wnum;
docs[dnum].words = (SVM_WORD *)my_malloc(sizeof(SVM_WORD)*wpos);
docs[dnum].docnum=dnum;
for(i=0;i<wpos;i++)
docs[dnum].words[i]=doc.words[i];
docs[dnum].twonorm_sq=doc.twonorm_sq;
dnum++;
if((dnum % 100) == 0&&com_pro.show_readfile)
{
sprintf(temstr,"read %ld..",dnum);
printm(temstr);
}
}
fclose(docfl);
free(line);
free(doc.words);
if (com_pro.show_action)
{
sprintf(temstr, "OK. (%ld examples read)", dnum);
printm(temstr);
sprintf(temstr,"%ld positive, %ld negative, and %ld unlabeled examples.",dpos,dneg,dunlab);
printm(temstr);
}
(*totdoc)=dnum;
}
// Parse one line of data file
int parse_document(char *line,DOC *doc,long *label,long*numwords,long max_words_doc)
{
register long wpos,pos;
long wnum;
double weight;
pos=0;
while(line[pos])
{ // cut off comments
if(line[pos] == '#')
{
line[pos]=0;
}
else
{
pos++;
}
}
wpos=0;
if((sscanf(line,"%ld",label)) == EOF) return(0);
pos=0;
while(line[pos]==' ') pos++;
while(line[pos]>' ') pos++;
char sring_weight[5];
char NaN[5]="NaN";
int result;
while((sscanf(line+pos,"%ld:%lf",&wnum,&weight)!= EOF) && (wpos<max_words_doc))
{
if(sscanf(line+pos,"%ld:%s",&wnum,&sring_weight)!= EOF)
{
result=strcmp(NaN,sring_weight);
if(result == 0)
{
weight = 0;
}
}
while(line[pos++]==' ');
while(line[++pos]>' ');
if(wnum<=0)
{
printe ("Feature numbers must be larger or equal to 1!!!");
sprintf(temstr,"LINE: %s",line);
printm(temstr);
return (0);
}
if((wpos>0) && ((doc->words[wpos-1]).wnum >= wnum))
{
printe ("Features must be in increasing order!!!");
sprintf(temstr,"LINE: %s",line);
printm(temstr);
return (0);
}
(doc->words[wpos]).wnum=wnum;
(doc->words[wpos]).weight=weight;
wpos++;
}
(doc->words[wpos]).wnum=0;
(*numwords)=wpos+1;
doc->docnum=-1;
//doc->twonorm_sq=sprod_ss(doc->words,doc->words);
return(1);
}
double Array_train[10];
double Array_test[10];
Matrix* getArrayTrain = new Matrix(10,one);
Matrix* getArrayTest = new Matrix(10,one);
getArrayTrain->matrixToArrayold(Array_train,*errorTrain_acc);
getArrayTest->matrixToArrayold(Array_test,*errorTest_acc);
ntest = test_max_docs;
randomForest_weakLearner(train_matrix, train_label_matrix,test_matrix,test_label_matrix,
train_max_words_doc,train_max_docs,test_max_words_doc,test_max_docs,step,cycles_num,
ntest,train_error_array,test_error_array,train_array,label_train_array,test_array, label_test_array);
/**************************************************************************/
/* The predicted values of the input test set based on out-of-bag samples */
/**************************************************************************
test_hypothesis->copyVectorToMatrix(j,*test_error_array);
/*************************************************************/
/* Check if boost equal to "0" (zero) and fixed to 1e-20 */
/*************************************************************
double val_boost=0;
matrix_boost->getValueSpecific(j-1,0,&val_boost,*boost);
if (val_boost == 0.00){
val_boost = 2.7184; //1.01; //1e-20;//0.0000000001;
boost->setValue(j,val_boost);
}
delete matrix_boost;
// adaBoost(train,train_matrix,train_label,train_label_matrix,train_hypothesis,train_max_words_doc,train_max_docs,step,cycles,boost,train_array,label_train_array,ave_delta);
if (ntest > 1)
{
zer_array->xtranslate(x_test, &mdim, &nrnodes, &nsample, bestvar, bestsplit, bestsplitnext, xbestsplit, nodestatus, cat, &ndbigtree);
for (n = 1; n <= 1; ++n) {
nodestatus[n-1]=-1;
bestvar[n - 1];
treemap[(n << 1) - 2];
treemap[(n << 1) - 1];
nodeclass[n - 1];
xbestsplit[n - 1];
}
ndbigtree=1;
zer_array->testreebag(x_test, &ntest, &mdim, treemap, nodestatus, xbestsplit, cbestsplit,
bestvar, nodeclass, &nrnodes, &ndbigtree, cat, &nclass, jts, nodexts, &value_one);
zer_array->comptserr(countts, jts, clts, jet, &ntest, &nclass, &errts, pid, &value_one,hypothesis_test,label_test_array);
Matrix* get_test_array = new Matrix(1,1);
get_test_array->copyArrayToMatrix(step,value_one,jet,*test_error_array);
delete get_test_array;
}
ntest =1;
if (ntest == 1)
{
for (n = 1; n <= nsample; ++n)
{
if (jin[n - 1] == 0)
{
++counttr[jtr[n - 1] + n * 6 - 7];
++out[n - 1];
}
}
if (jb % 1 == 0 || jb == cycles_num)
{
/********************************************************************************/
/* This subroutine return two parameters "errtr" - Error rate for training set. */
/* and "errts" - Error rate for test set. */
/********************************************************************************
zer_array->oob(&nsample, &nclass, jin, cl, jtr, jerr, counttr, out, &errtr, &errc,
rmargin, q, jest, wtt,hypothesis_train);
}
/* Put the hypothesis_train array data into train_error_array Matrix*//*
for (int kkk=0; kkk <= nsample; ++kkk)
{
if (jtr[kkk] == 2 ){
jtr[kkk]=0;
}
}
get_train_array->copyArrayToMatrix(step,value_one,hypothesis_train,*train_error_array);
delete get_train_array;
}
//getError(boost,iii,ttt,train_matrix,train_label_matrix,test_matrix,test_label_matrix,errorTrain_1,errorTest_1,train_max_words_doc,train_max_docs,step_1,cycles);
//adaBoost(train,train_matrix,train_label,train_label_matrix,train_hypothesis,train_max_words_doc,train_max_docs,step_1,cycles,boost,train_array,label_train_array,ave_delta);
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -