average_sd.c
来自「vc的源代码程序的基于最小特征向量的元素的集合方法的程序源代码」· C语言 代码 · 共 105 行
C
105 行
/*************************************************************************//* *//* Average results for training and test sets *//* and gives the standard deviation *//* ------------------------------------------ *//* *//* This is a generic program that averages any numbers found on *//* a set of lines of the same pattern. *//* *//*************************************************************************/#include <stdio.h>#include <stdlib.h>#define MAXLINE 200 /* max line length */#define MAXVALS 10 /* max values to be averaged */float value[2][MAXLINE/2][MAXVALS];float sqr(float v){ return v*v;}double std(int N, int col, int TrainTest, float mean){ float var = 0; int i; for(i = 0; i<N; i++) var += sqr(value[TrainTest][i][col]); /* calculate the most efficient estimator */ if(N>1) return sqrt((var-(float)N*sqr(mean))/(N-1)); return 0;}main(){ char Line[MAXLINE], *p1, *p2; int Numbers=0, Lines=0, i, TrainTest; float Val, Sum[2][MAXVALS]; float var, /* variance */ mean; /* mean value */ double strtod(); for ( i = 0 ; i < MAXVALS ; i++ ) Sum[0][i] = Sum[1][i] = 0; while ( fgets(Line, MAXLINE, stdin) ) { int vLines = Lines / 2; i = 0; TrainTest = Lines % 2; /* Count the numbers appearing on the line */ for ( p1 = Line ; *p1 != '\n' ; p1++ ) { if ( *p1 < '0' || *p1 > '9' ) continue; Val = strtod(p1, &p2); value[TrainTest][vLines][i] = Val; /* printf("%g ", value[TrainTest][vLines][i]);*/ Sum[TrainTest][i++] += Val; p1 = p2-1; } /* The number of numbers must match any previous lines */ printf("%s", Line); if ( Lines ) { if ( i != Numbers ) exit(1); } else Numbers = i; Lines++; } putchar('\n'); for ( TrainTest = 0 ; TrainTest <= 1 ; TrainTest++ ) { i = 0;/* printf("%s:\t", TrainTest ? "test" : "train");*/ for ( p1 = Line ; *p1 != '\n' ; p1++ ) { if ( *p1 < '0' || *p1 > '9' ) { putchar(*p1); } else { mean = Sum[TrainTest][i] * 2 / Lines; var = std(Lines/2, i++, TrainTest, mean); printf("%.1f+-%.1f", mean, var); strtod(p1, &p2); p1 = p2-1; } } putchar('\n'); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?