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

  • 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;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 基础命令 主要的命令和快捷键 Linux系统命令由三部分组成:cmd + [options]+[operation...
    485b1aca799e阅读 1,124评论 0 0
  • 第 2 章 SHELL 基础知识2.1 shell脚本我们在上面简单介绍了一下什么是shell脚本,现在我们来进一...
    LiWei_9e4b阅读 1,592评论 0 0
  • sed与awk实例 文本间隔 在每一行后面增加一空行 将原来的所有空行删除并在每一行后面增加一空行。这样在输出的文...
    stuha阅读 1,920评论 0 21
  • 一、前言 我们都知道,在Linux中一切皆文件,比如配置文件,日志文件,启动文件等等。如果我们相对这些文件进行一些...
    以七v为书阅读 1,544评论 0 5
  • 本文承接之前写的三十分钟学会AWK一文,在学习完AWK之后,趁热打铁又学习了一下SED,不得不说这两个工具真的堪称...
    mylxsw阅读 4,417评论 3 74