正态分布3σ原则,把3倍方差之外的点设想为噪声数据来排除。

归一化,将数据经过处理之后限定到一定的范围内,一般都会将数据限定到[0,1]。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <fstream>
#include <sstream>

template <class DataType>
void ReadDataFromFile(std::string &filename, std::vector<std::vector<DataType> > &lines_feat) {
  std::ifstream vm_info(filename.c_str());
  std::string lines;
  DataType var;
  std::vector<DataType> row;

  lines_feat.clear();

  while(!vm_info.eof()) {
    getline(vm_info, lines);
    if(lines.empty())
      break;
    std::stringstream stringin(lines);
    row.clear();

    while(stringin >> var) {
      row.push_back(var);
    }
    lines_feat.push_back(row);
  }
}

template <class DataType>
void Display2DVector(std::vector<std::vector<DataType> > &vv) {
  std::cout<<"the total rows of 2d vector_data: "<<vv.size()<<"\n";

  for(size_t i=0;i<vv.size();++i) {
    for(typename::std::vector<DataType>::const_iterator it=vv[i].begin();it!=vv[i].end();++it) {
      std::cout<<*it<<" ";
    }
    std::cout<<"\n";
  }
  std::cout<<"--------the end of the Display2DVector()--------\n";
}

template <class DataType>
void ProcessVector(std::vector<std::vector<DataType> > &vv) {
  std::vector<double> temp;
  double u[3]={0.0}, sum[3]={0.0}, sigma[3]={0.0};
  for(size_t j=0; j<3; ++j) {
    temp.clear();
    for(size_t i=0; i<vv.size(); ++i) {
      temp.push_back(vv[i][j]);
    }
    sum[j]=std::accumulate(temp.begin(), temp.end(), 0);
    u[j]=sum[j]/vv.size();
  }

  for(size_t j=0;j<3;++j) {
    temp.clear();
    sum[j]=0.0;
    for(size_t i=0;i<vv.size();++i) {
      temp.push_back(std::pow(vv[i][j]-u[j], 2.0));
    }
    sum[j]=std::accumulate(temp.begin(), temp.end(), 0.0);
    sigma[j]=sum[j]/vv.size();
    sigma[j]=sqrt(sigma[j]);
  }

  double MaxValue[3]={0.0}, MinValue[3]={0.0};
  for(size_t j=0;j<3;++j) {
    temp.clear();
    for(size_t i=0;i<vv.size();++i) {
      if((vv[i][j]>(u[j]-3*sigma[j])) && (vv[i][j]<(u[j]+3*sigma[j]))) {
        std::cout<<vv[i][j]<<" ";
      temp.push_back(vv[i][j]);
      }
    }
    std::cout<<"\n";
    MaxValue[j]=*std::max_element(temp.begin(), temp.end());
    MinValue[j]=*std::min_element(temp.begin(), temp.end());
  }

  for(size_t j=0;j<3;++j) {
    for(size_t i=0;i<vv.size();++i) {
      if((vv[i][j]>(u[j]-3*sigma[j])) && (vv[i][j]<(u[j]+3*sigma[j]))) {
        std::cout<<(vv[i][j]-MinValue[j])/(MaxValue[j]-MinValue[j])<<" ";
      }
    }
    std::cout<<"\n";
  }
}

int main() {
  std::vector<std::vector<int> > lines_feat;
  std::string filename="vm.data";

  /*read data from file to 2d vector*/
  ReadDataFromFile(filename, lines_feat);

  /*display the raw data*/
  Display2DVector(lines_feat);

  /*process the data*/
  ProcessVector(lines_feat);

  std::cout<<"--------The end of main()--------\n";

  return 0;
}

源数据如下(cat vm.data):

19 26 63
13 62 65
16 69 15
14 56 17
19 6 15
11 42 15
18 58 36
12 77 33
10 75 47
15 54 70
10017 1421077 4196

c++ 数据预处理(数据去噪,归一化)的更多相关文章

  1. Python数据预处理(sklearn.preprocessing)—归一化(MinMaxScaler),标准化(StandardScaler),正则化(Normalizer, normalize)

      关于数据预处理的几个概念 归一化 (Normalization): 属性缩放到一个指定的最大和最小值(通常是1-0)之间,这可以通过preprocessing.MinMaxScaler类实现. 常 ...

  2. sklearn中的数据预处理----good!! 标准化 归一化 在何时使用

    RESCALING attribute data to values to scale the range in [0, 1] or [−1, 1] is useful for the optimiz ...

  3. spark 数据预处理 特征标准化 归一化模块

    #We will also standardise our data as we have done so far when performing distance-based clustering. ...

  4. Python数据预处理:机器学习、人工智能通用技术(1)

    Python数据预处理:机器学习.人工智能通用技术 白宁超  2018年12月24日17:28:26 摘要:大数据技术与我们日常生活越来越紧密,要做大数据,首要解决数据问题.原始数据存在大量不完整.不 ...

  5. 机器学习PAL数据预处理

    机器学习PAL数据预处理 本文介绍如何对原始数据进行数据预处理,得到模型训练集和模型预测集. 前提条件 完成数据准备,详情请参见准备数据. 操作步骤 登录PAI控制台. 在左侧导航栏,选择模型开发和训 ...

  6. Python数据挖掘——数据预处理

    Python数据挖掘——数据预处理 数据预处理 数据质量 准确性.完整性.一致性.时效性.可信性.可解释性 数据预处理的主要任务 数据清理 数据集成 数据归约 维归约 数值归约 数据变换 规范化 数据 ...

  7. 机器学习实战基础(八):sklearn中的数据预处理和特征工程(一)简介

    1 简介 数据挖掘的五大流程: 1. 获取数据 2. 数据预处理 数据预处理是从数据中检测,纠正或删除损坏,不准确或不适用于模型的记录的过程 可能面对的问题有:数据类型不同,比如有的是文字,有的是数字 ...

  8. Python数据预处理—归一化,标准化,正则化

    关于数据预处理的几个概念 归一化 (Normalization): 属性缩放到一个指定的最大和最小值(通常是1-0)之间,这可以通过preprocessing.MinMaxScaler类实现. 常用的 ...

  9. 关于使用sklearn进行数据预处理 —— 归一化/标准化/正则化

    一.标准化(Z-Score),或者去除均值和方差缩放 公式为:(X-mean)/std  计算时对每个属性/每列分别进行. 将数据按期属性(按列进行)减去其均值,并处以其方差.得到的结果是,对于每个属 ...

随机推荐

  1. vs for Mac中的启用Entity Framework Core .NET命令行工具

    在vs for Mac的工具菜单中已没有了Package Manager Console. 我们可以通过以下方法使用Entity Framework Core .NET命令行工具: 1.添加Nuget ...

  2. Ubuntu 关闭guest用户

    Ubuntu 关闭guest用户 ca0gu0@ub:~$ cat /etc/lightdm/lightdm.conf [SeatDefaults]autologin-user=falseallow- ...

  3. /etc目录常用配置文件

    /etc/resolv.conf DNS客户端配置文件,逐渐被网卡配置文件所替代 /etc/hosts 本机DNS解析文件,优先级高于DNS服务器 /etc/hostname CentOS 7 主机名 ...

  4. iOS标准库中常用数据结构和算法之查找

    参数: key: [in] 要查找的元素.base:[in] 数组元素的首地址.nelp: [in/out] 数组的元素个数指针.width: [in] 数组中每个元素的尺寸.compar: [in] ...

  5. mysql跟java时间类型转换

    参照这个就行了,这个对应注入类型.===========java注入数据库==========java类型 mysql类型 成功与否date date yesdate time nodate time ...

  6. qwb和李主席

    qwb和李主席 Time Limit: 4 Sec  Memory Limit: 128 MB Description qwb和李主席打算平分一堆宝藏,他们想确保分配公平,可惜他们都太懒了,你能帮助他 ...

  7. poj 1273最大流dinic算法模板

    #include<stdio.h> #include<string.h> #define N 300 #define inf 0x7fffffff #include<qu ...

  8. 08springMVC拦截器

    u  概述 u  拦截器接口 u  拦截器适配器 u  运行流程图 u  拦截器HelloWorld u  常见应用之性能监控 1      概述 1.1    简介     Spring Web M ...

  9. Spring MVC 注解基础

    @Controller @Controller 注解用于标记在 Java 类上.被 @Controller 标记过的类就是一个 SpringMVC Controller对象.DispatcherSer ...

  10. 优化实例- not use hash to avoid temp space issue

    在展开下面的original sql 和 execution plan之前,要知道这个SQL的问题就在于占用大量的TEMP space orignal SQL SELECT roster.IC_N A ...