c++ 数据预处理(数据去噪,归一化)
正态分布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++ 数据预处理(数据去噪,归一化)的更多相关文章
- Python数据预处理(sklearn.preprocessing)—归一化(MinMaxScaler),标准化(StandardScaler),正则化(Normalizer, normalize)
关于数据预处理的几个概念 归一化 (Normalization): 属性缩放到一个指定的最大和最小值(通常是1-0)之间,这可以通过preprocessing.MinMaxScaler类实现. 常 ...
- sklearn中的数据预处理----good!! 标准化 归一化 在何时使用
RESCALING attribute data to values to scale the range in [0, 1] or [−1, 1] is useful for the optimiz ...
- spark 数据预处理 特征标准化 归一化模块
#We will also standardise our data as we have done so far when performing distance-based clustering. ...
- Python数据预处理:机器学习、人工智能通用技术(1)
Python数据预处理:机器学习.人工智能通用技术 白宁超 2018年12月24日17:28:26 摘要:大数据技术与我们日常生活越来越紧密,要做大数据,首要解决数据问题.原始数据存在大量不完整.不 ...
- 机器学习PAL数据预处理
机器学习PAL数据预处理 本文介绍如何对原始数据进行数据预处理,得到模型训练集和模型预测集. 前提条件 完成数据准备,详情请参见准备数据. 操作步骤 登录PAI控制台. 在左侧导航栏,选择模型开发和训 ...
- Python数据挖掘——数据预处理
Python数据挖掘——数据预处理 数据预处理 数据质量 准确性.完整性.一致性.时效性.可信性.可解释性 数据预处理的主要任务 数据清理 数据集成 数据归约 维归约 数值归约 数据变换 规范化 数据 ...
- 机器学习实战基础(八):sklearn中的数据预处理和特征工程(一)简介
1 简介 数据挖掘的五大流程: 1. 获取数据 2. 数据预处理 数据预处理是从数据中检测,纠正或删除损坏,不准确或不适用于模型的记录的过程 可能面对的问题有:数据类型不同,比如有的是文字,有的是数字 ...
- Python数据预处理—归一化,标准化,正则化
关于数据预处理的几个概念 归一化 (Normalization): 属性缩放到一个指定的最大和最小值(通常是1-0)之间,这可以通过preprocessing.MinMaxScaler类实现. 常用的 ...
- 关于使用sklearn进行数据预处理 —— 归一化/标准化/正则化
一.标准化(Z-Score),或者去除均值和方差缩放 公式为:(X-mean)/std 计算时对每个属性/每列分别进行. 将数据按期属性(按列进行)减去其均值,并处以其方差.得到的结果是,对于每个属 ...
随机推荐
- JS——switch case
语法: switch(n) { case 1: 执行代码块 1 break; case 2: 执行代码块 2 break; default: n 与 case 1 和 case 2 不同时执行的代码 ...
- 【转】npm 是干什么的
https://blog.csdn.net/zouzhigang96/article/details/79071854 网上的 npm 教程主要都在讲怎么安装.配置和使用 npm,却不告诉新人「为什么 ...
- Qt 窗体间传值(代码备份)
刚开始看的时候看的云里雾里的,现在稍微明白一点了.现在假设有一个form,一个MainWindow,如图所示: 实现点击PushButton,将文本框中的内容传输到MainWindow中,显示为Lab ...
- atom 插件安装【转载】
http://www.cnblogs.com/20145221GQ/p/5334762.html 问题: 输入apm install后,可能一直没有响应,要强行退. 不知道安装的时候可不可以打开ato ...
- GridView中字符串太长处理方式
<asp:TemplateField HeaderText="子机构编号"> <ItemTemplate> <asp:Label ID="L ...
- GatewayWorker + LayIM实现即时聊天
一.程序目录结构 二.代码展示 附LayIM开发文档:https://www.layui.com/doc/modules/layim.html 1.前端代码 <!DOCTYPE html> ...
- mysql高可用架构mha之master_ip_failover脚本
脚本如下: #!/usr/bin/env perl use strict; use warnings FATAL => 'all'; use Getopt::Long; my ...
- cuda_device_functions.h:32:31: fatal error: cuda/include/cuda.h: 没有那个文件或目录
问题在复现工程https://github.com/google/hdrnet时遇到. 现象: 解决办法: 修改hdrnet文件夹下的Makefile文件:在在nvcc里面添加路径:-I /usr/l ...
- HDU 1465(错排公式)
不容易系列之一 题意: 一个人要寄n个信封,结果装错了.信纸的编号为1到n,信封的编号为1到n,信纸的编号不能和信封的编号一样,全都不能一样. 思路:错排公式. D(n)表示n件信封装错的所有的情况. ...
- C#学习笔记_07_数组
07_数组 数组的声明与实例化 名词解释 数组:数组是一个容器,用来存储一系列相兼容的数据类型的变量: 实例化:声明一个数组,并且赋初始值: 数组长度:就是数组的容量,表示这个数组可以存储多少个数据: ...