统计数据分布小工具
2007-06-19 19:13
246 查看
这些天要分析很大的数据,需要统计其P[X>x]的概率分布情况,采用matlab和origin,发现慢的要死,自己写了一个分析工具
缺点:没有数据输入的检查,也就是说,假定从文件中输入的数据是数字串。不过满足一般的使用够了。
实例:resp.txt中有
3480
308
20474
729
375
94
324
1375
1322
94
858
326
335
837
366
811
319
143082
.......
结果:
C:/webme/end>distri_release resp_size.txt 100
The file resp_size.txt was opened
Entering CalcDistribution......
CalcDistribution......
100 0.715434 0.284566
200 0.710231 0.289769
300 0.620970 0.379030
400 0.485702 0.514298
500 0.440679 0.559321
600 0.416528 0.583472
700 0.398978 0.601022
800 0.384246 0.615754
900 0.356537 0.643463
1000 0.336602 0.663398
1100 0.330908 0.669092
1200 0.326110 0.673890
1300 0.306142 0.693858
1400 0.286609 0.713391
1500 0.218431 0.781569
1600 0.215034 0.784966
1700 0.211592 0.788408
1800 0.211457 0.788543
1900 0.211326 0.788674
2000 0.210898 0.789102
2100 0.210560 0.789440
...................
/**//*
统计数据分布情况
*/
/**/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 头文件
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
/**/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 全局变量
/**/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 参数表
int CalcDistribution(FILE *input_stream, const int interval)
...{
// 局部变量
int cur_input_num,nInput = 0,iPos = 0;
double distri = 0.0;
#define MAX_HOLD 2000000
#define INCREMENT 100000
int *hold;
void *alloc_result;
int maxpos = 0,holdpos,nIncre = 0;
int count =0; // 统计前面部分的数据个数总合
// 函数参数分析
assert(interval>0);
// 函数动作
printf( "Entering CalcDistribution...... ");
hold = (int *)malloc(sizeof(int)*MAX_HOLD);
if( hold == NULL )
...{
printf( "Memory alloc error! ");
return -1;
}
memset(hold,0,MAX_HOLD);
// 读取文件中的数字
while(fscanf(input_stream,"%u ",&cur_input_num) != EOF)
...{
nInput++; // 输入数字个数加一
holdpos = cur_input_num/interval;
hold[holdpos]++;
if(holdpos>maxpos) maxpos = holdpos;
// 重新分配内存
if(maxpos>(MAX_HOLD+nIncre*INCREMENT))
...{
nIncre++;
alloc_result = realloc(hold,sizeof(int)*(MAX_HOLD+nIncre*INCREMENT));
if( alloc_result == NULL )
...{
printf( "Memory alloc error! ");
free(hold);
return -1;
}
printf( "Memory realloc success! ");
}
}
printf( "CalcDistribution...... ");
for (iPos = 0;iPos < maxpos;iPos++)
...{
count += hold[iPos]; // 统计前面部分的数据个数总合
distri = (double)count/(double)nInput; // 前面部分的数据个数总合/数据总数 == 概率P[X<x]
printf("%12d %f %f ",(iPos+1)*interval,1-distri,distri);
}
free(hold);
printf( "Leaving CalcDistribution...... ");
// 返回值
return 1;
}
/**/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// 入口点
int main(int argc, char* argv[])
...{
// 局部变量
int numclosed;
int interval = 1000;
char *filename=NULL;
static FILE *input_stream;
// 函数参数分析
if(argc<2)
...{
printf("Usage: %s filename [interval] ",argv[0]);
return -1;
}
filename = (char *)strdup(argv[1]);
if(argc == 3) interval = atoi(argv[2]);
// 函数动作
// 打开文件
if( (input_stream = fopen(filename, "rb")) == NULL )
...{
printf( "The file %s was not opened ",filename);
return 0;
}
else
printf( "The file %s was opened ",filename);
// 计算数据分布
CalcDistribution(input_stream,interval);
free(filename);
/**//* All files are closed: */
numclosed = _fcloseall( );
printf( "Number of files closed by _fcloseall: %u ", numclosed );
// 返回值
return 0;
}
/**///////////////////////////////////////////////////////////////////////////////
缺点:没有数据输入的检查,也就是说,假定从文件中输入的数据是数字串。不过满足一般的使用够了。
实例:resp.txt中有
3480
308
20474
729
375
94
324
1375
1322
94
858
326
335
837
366
811
319
143082
.......
结果:
C:/webme/end>distri_release resp_size.txt 100
The file resp_size.txt was opened
Entering CalcDistribution......
CalcDistribution......
100 0.715434 0.284566
200 0.710231 0.289769
300 0.620970 0.379030
400 0.485702 0.514298
500 0.440679 0.559321
600 0.416528 0.583472
700 0.398978 0.601022
800 0.384246 0.615754
900 0.356537 0.643463
1000 0.336602 0.663398
1100 0.330908 0.669092
1200 0.326110 0.673890
1300 0.306142 0.693858
1400 0.286609 0.713391
1500 0.218431 0.781569
1600 0.215034 0.784966
1700 0.211592 0.788408
1800 0.211457 0.788543
1900 0.211326 0.788674
2000 0.210898 0.789102
2100 0.210560 0.789440
...................
/**//*
统计数据分布情况
*/
/**/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 头文件
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
/**/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 全局变量
/**/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 参数表
int CalcDistribution(FILE *input_stream, const int interval)
...{
// 局部变量
int cur_input_num,nInput = 0,iPos = 0;
double distri = 0.0;
#define MAX_HOLD 2000000
#define INCREMENT 100000
int *hold;
void *alloc_result;
int maxpos = 0,holdpos,nIncre = 0;
int count =0; // 统计前面部分的数据个数总合
// 函数参数分析
assert(interval>0);
// 函数动作
printf( "Entering CalcDistribution...... ");
hold = (int *)malloc(sizeof(int)*MAX_HOLD);
if( hold == NULL )
...{
printf( "Memory alloc error! ");
return -1;
}
memset(hold,0,MAX_HOLD);
// 读取文件中的数字
while(fscanf(input_stream,"%u ",&cur_input_num) != EOF)
...{
nInput++; // 输入数字个数加一
holdpos = cur_input_num/interval;
hold[holdpos]++;
if(holdpos>maxpos) maxpos = holdpos;
// 重新分配内存
if(maxpos>(MAX_HOLD+nIncre*INCREMENT))
...{
nIncre++;
alloc_result = realloc(hold,sizeof(int)*(MAX_HOLD+nIncre*INCREMENT));
if( alloc_result == NULL )
...{
printf( "Memory alloc error! ");
free(hold);
return -1;
}
printf( "Memory realloc success! ");
}
}
printf( "CalcDistribution...... ");
for (iPos = 0;iPos < maxpos;iPos++)
...{
count += hold[iPos]; // 统计前面部分的数据个数总合
distri = (double)count/(double)nInput; // 前面部分的数据个数总合/数据总数 == 概率P[X<x]
printf("%12d %f %f ",(iPos+1)*interval,1-distri,distri);
}
free(hold);
printf( "Leaving CalcDistribution...... ");
// 返回值
return 1;
}
/**/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// 入口点
int main(int argc, char* argv[])
...{
// 局部变量
int numclosed;
int interval = 1000;
char *filename=NULL;
static FILE *input_stream;
// 函数参数分析
if(argc<2)
...{
printf("Usage: %s filename [interval] ",argv[0]);
return -1;
}
filename = (char *)strdup(argv[1]);
if(argc == 3) interval = atoi(argv[2]);
// 函数动作
// 打开文件
if( (input_stream = fopen(filename, "rb")) == NULL )
...{
printf( "The file %s was not opened ",filename);
return 0;
}
else
printf( "The file %s was opened ",filename);
// 计算数据分布
CalcDistribution(input_stream,interval);
free(filename);
/**//* All files are closed: */
numclosed = _fcloseall( );
printf( "Number of files closed by _fcloseall: %u ", numclosed );
// 返回值
return 0;
}
/**///////////////////////////////////////////////////////////////////////////////
相关文章推荐
- 通过统计工具做数据分析、优化和挖掘
- Local Response Normalization作用——感觉LRN也是解决的梯度消失和爆炸问题,统计机器学习中的一个经典假设是“源空间(source domain)和目标空间(target domain)的数据分布(distribution)是一致的
- cpu-hogs 的 systemtap 工具,可以实时统计某段时间内实际消耗掉的 CPU 时间在所有用户进程及内核线程之间的比例分布。
- 在R中统计数据的频数或者密度分布并图形化
- 网曝CNZZ统计工具疑似出卖用户数据
- 数据挖掘学习03 - 统计工具R学习和使用小结
- 海量数据分布在100台电脑中,想个办法高效统计出这批数据的TOP10。
- KRS:一个收集 Kubernetes 资源统计数据的新工具
- 海量数据分布在100台电脑中,想个办法高效统计出这批数据的TOP10
- 6.2.5 数据分布和统计
- 数据统计工具与常用的核心数据指标
- 常用的数据统计工具
- Redis监控数据分布工具Redis-audit 使用总结
- 2009年全球即时通讯工具数据统计报告
- 如何通过统计分析工具做好APP的数据分析和运营
- 提取某个字段数据并统计其分布规律
- 业余实现一个统计A股数据工具
- 一个FLASH开发的数据统计工具 PHP可以使用