zoukankan      html  css  js  c++  java
  • AMS算法

      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 #include<time.h>
      4 
      5 #define N 1000//采样点的个数
      6 #define M 5//分组
      7 
      8 #define SIZE 10000
      9 
     10 typedef struct
     11 {
     12     int element;
     13     int count;
     14 }ATTRIBUTE;
     15 
     16 
     17 int main()
     18 {
     19     ATTRIBUTE attr[100000];
     20     int secmom=0;//真实二阶矩
     21     int data;//接收读取之后的值
     22     int i=0,j,k;//循环控制变量
     23     int count_tmp=0;//临时计数变量
     24     //int n=5;//采样点的个数
     25     double secmom_estima=0;
     26     ATTRIBUTE sample[10000];
     27     int tmp;
     28     int a[10000];
     29     int loc[N];//采样位置保存在数组里
     30     int sample_group[M][N];
     31     //FILE *fp =fopen("./stream_for_ams.txt","r");
     32 
     33     FILE *fp =fopen("./stream_sample_ams.txt","r");
     34 
     35     srand(time(0));
     36     for(j=0;j<N;j++)
     37     {
     38         loc[j]=rand()%SIZE;//获取随机采样位置
     39     }
     40 
     41     for(j=0;j<N;j++)//随机位置排序
     42     {
     43         for(k=j+1;k<N;k++)
     44         {
     45             if(loc[j] > loc[k])
     46             {
     47                 tmp = loc[k];
     48                 loc[k]=loc[j];
     49                 loc[j]=tmp;
     50             }
     51         }
     52     }
     53 
     54     if(fp == NULL)//文件读取失败
     55     {
     56         printf("file can't open!\n");
     57         exit(0);
     58     }
     59     while(!feof(fp))
     60     {
     61         fscanf(fp,"%d",&data);
     62         a[i]=data;//存数据
     63         /***************采样*****************/
     64         for(j=0;j<N;j++)
     65         {
     66             if(loc[j] == i)
     67             {
     68                 sample[j].element=data;
     69                 sample[j].count=0;
     70             }
     71             if(sample[j].element == data)
     72             {
     73                 sample[j].count+=1;
     74             }
     75         }
     76 
     77         /*****************真实***********************/
     78         for(j=0;j<count_tmp;j++)
     79         {
     80             if(attr[j].element == data)//如果该数据被存过则count+1
     81             {
     82                 attr[j].count +=1;
     83                 break;//扫描比对成功退出循环
     84             }
     85         }
     86         if( j == count_tmp)//如果没有该数据第一次出现
     87         {
     88             attr[count_tmp].element=data;//记录
     89             attr[count_tmp].count=1;
     90             count_tmp++;//记录第一次出现的数
     91         }
     92         printf("%.2lf%%\r", i * 100.0/ SIZE);
     93         i++;
     94     }
     95 
     96     /******************组合估计二阶矩***********************/
     97     for(i=0;i<M;i++)//分组M
     98     {
     99         for(j=0;j<N;j++)//每组N个随机位置
    100         {
    101             loc[j]=rand()%SIZE;//获取随机采样位置
    102         }
    103         for(j=0;j<N;j++)//随机位置排序
    104         {
    105             for(k=j+1;k<N;k++)
    106             {
    107                 if(loc[j] > loc[k])
    108                 {
    109                     tmp = loc[k];
    110                     loc[k]=loc[j];
    111                     loc[j]=tmp;
    112                 }
    113             }
    114         }
    115         for(j=0;j<N;j++)
    116         {
    117             if(loc[j] == i)
    118             {
    119                 sample_group[i][j]=
    120                 sample[j].count=0;
    121             }
    122             if(sample[j].element == data)
    123             {
    124                 sample[j].count+=1;
    125             }
    126         }
    127 
    128     }
    129 
    130     /************估计无组合估计二阶矩**********************/
    131     for(j=0;j<N;j++)
    132     {
    133         secmom_estima+=SIZE*(2.0*sample[j].count-1);
    134     }
    135     printf("无组合估计二阶矩:%.2lf\n",secmom_estima/N);
    136 
    137     /***********真实二阶矩*************/
    138     for(j=0;j<count_tmp;j++)
    139     {
    140         //printf("%d\t%d\n",attr[j].element,attr[j].count);
    141         secmom+=attr[j].count*attr[j].count;
    142     }
    143     printf("独立数%d\n",count_tmp);
    144     printf("真实二阶矩为:%d\n",secmom);
    145 
    146     system("pause");
    147     return 0;
    148 }
  • 相关阅读:
    Java+7入门经典 -1 简介
    优化算法动画演示Alec Radford's animations for optimization algorithms
    如何写科技论文How to write a technical paper
    开始学习深度学习和循环神经网络Some starting points for deep learning and RNNs
    用500行Julia代码开始深度学习之旅 Beginning deep learning with 500 lines of Julia
    用10张图来看机器学习Machine learning in 10 pictures
    ICLR 2013 International Conference on Learning Representations深度学习论文papers
    ICLR 2014 International Conference on Learning Representations深度学习论文papers
    卷积神经网络CNN(Convolutional Neural Networks)没有原理只有实现
    卷积神经网络Convolutional Neural Networks
  • 原文地址:https://www.cnblogs.com/SimonKly/p/6838825.html
Copyright © 2011-2022 走看看