美文网首页
关于整理moore数据集的一些操作

关于整理moore数据集的一些操作

作者: XDgbh | 来源:发表于2019-06-21 21:38 被阅读0次
  • moore数据集有10个文件,可以把前面若干行删除后只留下逗号分隔的每行一条流的文件txt文件。


    10个文件
  • 然后可以使用:cat entry01.txt >> total.txtcat entry02.txt >> total.txt最后到cat entry10.txt >> total.txt将10个txt文件的内容全部追加合并到total.txt中。>>是输出重定向命令。
  • 使用awk命令可以求出对应的一列的最大值或者最小值


    注意指定分隔符
    awk求最值
  • 使用sed命令#sed -i "s/?/0/g" total.txt将文本中一些特殊字符串替换成其他的,比如一行数据的?号全替换成0,同理可将"Y,"和"N,"等字符替换成"0,"和"1,"数字,加上逗号是为了防止将分类中的YN字符也被替换
  • 使用c++程序,打开total文件找到各列的最大值和最小值,并保存到另一个文件find_max_min.txt。
#include<vector>
#include<fstream>
#include<string>
using namespace std;
//字符串分割函数
void string_split(const string &str, vector<double> &v_double, const string &delim)
{
    int pos1 = 0, pos2 = 0;
    int len = str.length();
    while (pos1 < len && pos2 != string::npos)
    {
        int count = 0;
        pos2 = str.find_first_of(delim, pos1);
        if (pos2 != string::npos)
        {
            if (pos1 < pos2)
            {
                count = pos2 - pos1;
            }
        }
        else if (pos1<len)
        {
            //pos2到了最后字符串末尾,对于本次处理moore数据集,最后一列是分类字符串,不是数字,因此要放弃
            count = 0;
        }
        if (count > 0)
        {
            string temp = str.substr(pos1, count);
            v_double.push_back(stod(temp)); //将每一个string值转换成double保存起来
        }
        
        pos1 = pos2 + 1;
    }
}

const int columns = 256;
int main()
{
    ifstream in("total——将其他字符都替换成了0或1.txt");
    if (!in.is_open())
    {
        return 1;
    }
    vector<double> v_double_max(columns, 0.0);  //保存各列的最大值
    vector<double> v_double_min(columns, 100000000.0);  //保存各列的最小值
    //分割读入的每一行字符串,并存入分割出的字符串数组到vv_str
    while (!in.eof())
    {
        string line;
        getline(in, line);
        vector<double> line_double;
        string_split(line, line_double, ",");
        int length = line_double.size();
        for (int i = 0; i < length; i++)
        {
            //更新最大值
            if (line_double[i]>v_double_max[i])
            {
                v_double_max[i] = line_double[i];
            }
            //更新最小值
            else if (line_double[i]<v_double_min[i])
            {
                v_double_min[i] = line_double[i];
            }
        }
    
    }
    in.close();

    //从vv_str输出字符串到output.txt
    ofstream out("find_max_min.txt");
    if (!out.is_open())
    {
        return 1;
    }
    
    //第一行输出各列最大值
    for (double d : v_double_max)
    {
            out << d << ",";
    }
    out << '\n';    //完成一行输出
    //第二行输出各列最小值
    for (double d : v_double_min)
    {
        out << d << ",";
    }
    out << '\n';    //完成一行输出
    out.close();
    return 0;
}
第一行为各列最大值,第二行为各列最小值
  • 将total.txt文件按照最后一列的流量分类分拆为12个txt文件。


    得到各个类别的流量集合
  • 利用前面得到的各列的最值,分别对每一个流量类别的元素进行归一化到0-1进行处理。(当前值-最小值)/(最大值-最小值)。

#include<vector>
#include<fstream>
#include<string>
#include<iostream>
using namespace std;

//字符串分割函数
void string_split(const string &str, vector<double> &v_double, const string &delim)
{
    int pos1 = 0, pos2 = 0;
    int len = str.length();
    while (pos1 < len && pos2 != string::npos)
    {
        int count = 0;
        pos2 = str.find_first_of(delim, pos1);
        if (pos2 != string::npos)
        {
            if (pos1 < pos2)
            {
                count = pos2 - pos1;
            }
        }
        else if (pos1<len)
        {
            //pos2到了最后字符串末尾
            count = 0;
        }
        if (count > 0)
        {
            string temp = str.substr(pos1, count);
            v_double.push_back(stod(temp)); //将每一个string值转换成double保存起来
        }
        pos1 = pos2 + 1;
    }
}

const int columns = 256;
int main()
{
    string file1 = "find_max_min_diff.txt";
    string file2 = "WWW.txt";
    string out_file = "WWW_normalize0-1.txt";
    ifstream in(file1);
    if (!in.is_open())
    {
        cout << "error to read file: " << file1 << endl;
        return 1;
    }
    vector<double> v_double_max;    //保存各列的最大值
    string line1;
    getline(in, line1);
    string_split(line1, v_double_max, ",");

    vector<double> v_double_min;    //保存各列的最小值
    string line2;
    getline(in, line2);
    string_split(line2, v_double_min, ",");
    
    vector<double> v_double_diff;   //保存各列最大最小差值
    string line3;
    getline(in, line3);
    string_split(line3, v_double_diff, ",");
    in.close();
    
    //读取一个类别的文件,并将每一行的各个数值归一化到0-1区间
    ifstream in_class(file2);
    if (!in_class.is_open())
    {
        cout << "error to read file: " << file2 << endl;
        return 1;
    }

    //写入一个该类别归一化之后的文件
    ofstream out(out_file);
    if (!out.is_open())
    {
        cout << "error to read file: " << out_file << endl;
        return 1;
    }
    //处理每一行输入和输出
    while (!in_class.eof())
    {
        string line;
        getline(in_class, line);
        vector<double> line_double;
        string_split(line, line_double, ",");
        int length = line_double.size();
        for (int i = 0; i < length; i++)
        {
            //归一化每个值
            double result = 0;
            if (0 != v_double_diff[i])
            {
                result = (line_double[i] - v_double_min[i]) / v_double_diff[i];
            }
            //输出每个值到归一化文件
            out << result << ",";
        }
        //每行输出后面补0,直到每行有256个数
        for (int i = length; i < 255; i++)
        {
            out << 0 << ",";
        }
        out << 0 << endl;   //输出一行
    }
    in_class.close();
    out.close();
    return 0;
}

相关文章

  • 关于整理moore数据集的一些操作

    moore数据集有10个文件,可以把前面若干行删除后只留下逗号分隔的每行一条流的文件txt文件。10个文件 然后可...

  • 基于Keras实现Kaggle2013--Dogs vs. Ca

    【下载数据集】 下载链接--百度网盘关于猫的部分数据集示例 【整理数据集】 将训练数据集分割成训练集、验证集、测试...

  • CV学习笔记(二十):文本识别(DenseNet)

    在上一篇文章中完成了数据集的拼接仿真,最近又做了一些关于数据集的工作,先是标注了一堆数据集,然后又把数据集再增强了...

  • 23种pandas核心操作

    基本数据集操作 (1)读取CSV格式的数据集 或者 (2)读取Excel数据集 (3)将 DataFrame 直接...

  • JDK8-Stream

    Stream流有一些特性 Stream流不是一种数据结构,不保存数据,它只是在原数据集上定义了一组操作。 这些操作...

  • MySQL数据库操作

    数据库 MySQL 基本操作 数据库操作 表的操作 数据操作 字符集编码 数据类型(列类型) 列属性(列约束) 建...

  • 2018-01-20

    SAS成长之路——数据集、变量、观测值的操作 一、数据集的操作 1、SET语句  用于从一个或多个已存在的数据集中...

  • 如何从机器学习数据中获取更多收益

    摘要:本文讲解一些关于机器学习数据集的小技巧,分享个人经验,可供读者参考。 对于深度学习而言,合适的数据集以及合适...

  • R第九天

    一、基本数据操作 dataframe na.omit(数据集)——删除缺失值 names()——查看数据集列的名字...

  • mysql基本操作命令汇总--笔记

    一、基本操作 对数据库以及表的一些基本操作 1-1.关于数据库 1-2.关于数据表 1-3表的约束 1-4索引 作...

网友评论

      本文标题:关于整理moore数据集的一些操作

      本文链接:https://www.haomeiwen.com/subject/zqddwftx.html