📄 d1.c
字号:
#include "discTable.h"// int left; // the starting row// int right; // the ending row// int c; // the column numberint DiscreteTable::getBoundary(int left, int right, int c){ int tmpB; // temporary boundary int nC = Col() - 1; if(!isValueHasPureClass(left, c, tmpB)) return tmpB; // not pure class Description tmpClass = value(left, nC); int next=tmpB; for(int r = next+1; r<=right; r++) if(isValueHasPureClass(r, c, next)) { if(tmpClass == value(r,nC)) { r = next; tmpB = next; } else return tmpB; // different class } else return tmpB; // not pure class return right;}int DiscreteTable::buildBoundaryTable(int *disc, int c){ int nC = Col()-1; int nR = Row(); int cnt = 0; for(int r = 0; r<nR; r++) // r will be increment by 1 { int l = r; r = getBoundary(r, nR-1, c); disc[cnt++] = r;#ifdef ELOMAA__DBG printSee(l, r, c); cout <<"disc["<<cnt-1<<"]="<<disc[cnt-1]<<endl;#endif } return cnt; // the number of boundaries found.}int DiscreteTable::isSameClass(int l, int r){ int nC = Col()-1; Description tmp_c = value(l, nC); for(int i = l+1; i<=r; i++) if(value(i,nC) != tmp_c) return 0; return 1;}Description DiscreteTable::getMaxClass(){ int nR = Row(); int nC = Col()-1; Description curr = value(0, nC); for(int r=1; r<nR; r++) if(curr < value(r,nC)) { curr = value(r,nC); } return curr;}void DiscreteTable::discretizeTable(){ for(int i=0; i<Col()-1; i++) { getCutsOnCol(i); discretizeOnCol(i); }}void DiscreteTable::discretizeToIntTable(){ clock_t start, end; struct rusage r,r1; start = clock();// getrusage(RUSAGE_SELF, &r); for(int i=0; i<Col()-1; i++) { getCutsOnCol(i); discretizeToIntOnCol(i); } end = clock();// getrusage(RUSAGE_SELF, &r1); double duration = (double)(end - start) / CLOCKS_PER_SEC;// long user = r1.ru_utime.tv_usec - r.ru_utime.tv_usec ;// long sys = r1.ru_stime.tv_usec - r.ru_stime.tv_usec ; FILE *fp = fopen("Clock.sec", "w+"); fprintf(fp, "%.6lg\n", duration); fclose(fp);// printf( "Rusage: (user)%ld + (sys)%ld = %ld microseconds\n", user, sys, user + sys );}void DiscreteTable::discretizeToIntTableUDF(){ clock_t start, end; struct rusage r,r1; start = clock();// getrusage(RUSAGE_SELF, &r); getCutsTable(); for(int i=0; i<Col()-1; i++) { sort(i); discretizeToIntOnCol(i); } end = clock();// getrusage(RUSAGE_SELF, &r1); double duration = (double)(end - start) / CLOCKS_PER_SEC;// long user = r1.ru_utime.tv_usec - r.ru_utime.tv_usec ;// long sys = r1.ru_stime.tv_usec - r.ru_stime.tv_usec ; FILE *fp = fopen("Clock.sec", "w+"); fprintf(fp, "%.6lg\n", duration); fclose(fp);// printf( "Rusage: (user)%ld + (sys)%ld = %ld microseconds\n", user, sys, user + sys );}void DiscreteTable::printDiscreteCutTable(char *name){ FILE *fp; if(name == NULL) fp = stdout; else { char fn[2048]; strcpy(fn, name); strcat(fn, ".discTable"); fp = fopen(fn, "w+"); ERROR2(fp, "File %s: cannot be opened for writing!\n", fn); } for(int i=0; i<Col()-1; i++) { fprintf(fp, "%d:", DiscTableSize[i]); for(int r=0; r<DiscTableSize[i]; r++) fprintf(fp, "%g ", DiscTable[i][r]); fprintf(fp, "\n"); } fclose(fp);}void DiscreteTable::printDiscreteDataTable(char *name){ FILE *fp; if(name == NULL) fp = stdout; else { char fn[2048]; strcpy(fn, "disc_"); strcat(fn, name); fp = fopen(fn, "w+"); ERROR2(fp, "File %s: cannot be opened for writing!\n", fn); } printDataTableToOutStream(fp); fclose(fp);}void DiscreteTable::discretizeOnCol(int c){#ifdef DISCTABLE_DBG2 int nC = Col()-1; // for Debug only cout << "Discretize Column "<<c<<endl;#endif DiscTable[c][DiscTableSize[c]-1] = LARGE_NUM; for(int row = 0,r=0; row < Row()&& r<DiscTableSize[c]; r++) { Description val = DiscTable[c][r]; while(value(row,c) <= val) {#ifdef DISCTABLE_DBG2 // see whether the discretiztion are performed properly cout << value(row, c) << " -> " << val <<" | "; cout << value(row, nC) <<endl;#endif setValue(row++, c, val); if(row>=Row()) break; }#ifdef DISCTABLE_DBG2 // show the end of one partition cout << "----"<<endl;#endif }}void DiscreteTable::discretizeToIntOnCol(int c){ // never cut, we will used the old value if(DiscTableSize[c] <= 1) {// float ratio = (float)DiscTableSize[c]/ nValuesBefore[c];// if(ratio>0.01) return; return; } #ifdef DISCTABLE_DBG2 int nC = Col()-1; // for Debug only cout << "Discretize Column "<<c<<endl;#endif for(int row = 0,r=0; row < Row()&& r<DiscTableSize[c]; r++) { Description val = DiscTable[c][r]; while(value(row,c) <= val) {#ifdef DISCTABLE_DBG2 // see whether the discretiztion are performed properly cout << value(row, c) << " -> " << r <<" | "; cout << value(row, nC) <<endl;#endif setValue(row++, c, r); if(row>=Row()) break; }#ifdef DISCTABLE_DBG2 // show the end of one partition cout << "----"<<endl;#endif }}void DiscreteTable::discretizeTable(SupervisedDataTable &tt){#ifdef DISCTABLE_DBG2 cout << "Discretization performed:\n";#endif for(int c=0; c<tt.Col()-1; c++) { tt.sort(c); int row = 0; for(int r=0; row < tt.Row()&& r<DiscTableSize[c]; r++) { Description val = DiscTable[c][r]; while(tt.value(row,c) <= val) {#ifdef DISCTABLE_DBG2 cout << tt.value(row, c) << " -> " << val <<endl;#endif tt.setValue(row++, c, val); if(row>=tt.Row()) break; } }#ifdef DISCTABLE_DBG2 // show the end of one partition cout << "----"<<endl;#endif // for those not within the discretization range // produced from the training data, we simply use // their old values }}void DiscreteTable::discretizeToIntTable(SupervisedDataTable &tt){#ifdef DISCTABLE_DBG2 cout << "Discretization performed:\n";#endif for(int c=0; c<tt.Col()-1; c++) { if(DiscTableSize[c] <= 1) {// float ratio = (float)DiscTableSize[c]/ nValuesBefore[c];// if(ratio>0.01) continue; // never cut we continue continue; } // never cut, we will used the old value tt.sort(c); int row = 0; for(int r=0; row < tt.Row()&& r<DiscTableSize[c]; r++) { Description val = DiscTable[c][r]; while(tt.value(row,c) <= val) {#ifdef DISCTABLE_DBG2 cout << tt.value(row, c) << " -> " << r <<endl;#endif tt.setValue(row++, c, r); if(row>=tt.Row()) break; } }#ifdef DISCTABLE_DBG2 // show the end of one partition cout << "----"<<endl;#endif // for those not within the discretization range // produced from the training data, we simply use // their old values }}int DiscreteTable::getNumOfAttributeValues(int c){ int nR = Row(); int cnt = 1; // by default there is at least one value Description curr = value(0, c); for(int r=1; r<nR; r++) if(curr != value(r,c)) { cnt++; curr = value(r,c); } return cnt;}void DiscreteTable::printAllAttributeInfo(){ FILE *fp; fp = fopen("Points", "w+"); ERROR2(fp, "File %s: cannot be opened for writing!\n", "Points"); int cnt=0; float ratio=0; fprintf(fp, "%d ", 2*(Col() - 1)); for(int i=0; i<Col()-1; i++) { fprintf(fp, "%d %d ", nValuesBefore[i], DiscTableSize[i]); if(DiscTableSize[i] != 1) { // 1 means never cut ratio += DiscTableSize[i] / nValuesBefore[i]; cnt ++; } } fprintf(fp, "\n"); fclose(fp); fp = fopen("Ratio", "w+"); ERROR2(fp, "File %s: cannot be opened for writing!\n", "Ratio"); if(cnt!=0) fprintf(fp, "%.2g\n", ratio/cnt); else fprintf(fp, "%.2g\n", 0); fclose(fp);}void DiscreteTable::getAttrInfoBeforeDisc(){ for(int i = 0; i<Col()-1; i++) { sort(i); nValuesBefore[i] = getNumOfAttributeValues(i); }}void DiscreteTable::cleanCntV(int *CntV){ for(int i = 0 ; i<MaxClass; i++) CntV[i] = 0;}void DiscreteTable::getClassCntV(int *cntV, int left, int right){ int nC = Col()-1; for(int r = left; r<=right; r++) cntV[ (int)value(r, nC) ] ++;}int DiscreteTable::countNumOfClass(int *cntV){ int cnt = 0; for(int r = 0; r<MaxClass; r++) if(cntV[r]) cnt ++; return cnt;}int DiscreteTable::countNumOfClass(int *lCntV, int *rCntV) { int cnt = 0; for(int r = 0; r<MaxClass; r++) if(lCntV[r]||rCntV[r]) cnt ++; return cnt;}int DiscreteTable::getSameValueBoundary(int begin, int c){ int nC = Col()-1, nR = Row(); Description tmp = value(begin, c); for(int i = begin+1; i < Row(); i++) { if(tmp != value(i, c)) return i-1; } return nR - 1;}int DiscreteTable::isValueHasPureClass(int begin, int c, int &next){ next = getSameValueBoundary(begin, c); if(next == begin) return 1; return isSameClass(begin, next);}/*void DiscreteTable::printSee(int begin, int end, int c){ for(int r = begin; r<=end; r++) // r will be increment by 1 cout << value(r,c) << "-" << value(r, Col()-1) <<endl; cout << "------\n";}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -