-
moore數(shù)據(jù)集有10個文件扶平,可以把前面若干行刪除后只留下逗號分隔的每行一條流的文件txt文件。
10個文件 - 然后可以使用:
cat entry01.txt >> total.txt
到cat entry02.txt >> total.txt
最后到cat entry10.txt >> total.txt
將10個txt文件的內(nèi)容全部追加合并到total.txt中。>>
是輸出重定向命令飞苇。 -
使用awk命令可以求出對應(yīng)的一列的最大值或者最小值
注意指定分隔符
awk求最值 - 使用sed命令
#sed -i "s/?/0/g" total.txt
將文本中一些特殊字符串替換成其他的,比如一行數(shù)據(jù)的?號全替換成0,同理可將"Y,"和"N,"等字符替換成"0,"和"1,"數(shù)字房蝉,加上逗號是為了防止將分類中的YN字符也被替換
- 使用c++程序,打開total文件找到各列的最大值和最小值微渠,并保存到另一個文件find_max_min.txt搭幻。
#include<vector>
#include<fstream>
#include<string>
using namespace std;
//字符串分割函數(shù)
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數(shù)據(jù)集逞盆,最后一列是分類字符串檀蹋,不是數(shù)字,因此要放棄
count = 0;
}
if (count > 0)
{
string temp = str.substr(pos1, count);
v_double.push_back(stod(temp)); //將每一個string值轉(zhuǎn)換成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); //保存各列的最小值
//分割讀入的每一行字符串,并存入分割出的字符串?dāng)?shù)組到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文件俯逾。
得到各個類別的流量集合 利用前面得到的各列的最值,分別對每一個流量類別的元素進(jìn)行歸一化到0-1進(jìn)行處理舅逸。(當(dāng)前值-最小值)/(最大值-最小值)桌肴。
#include<vector>
#include<fstream>
#include<string>
#include<iostream>
using namespace std;
//字符串分割函數(shù)
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值轉(zhuǎn)換成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();
//讀取一個類別的文件,并將每一行的各個數(shù)值歸一化到0-1區(qū)間
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 << ",";
}
//每行輸出后面補(bǔ)0琉历,直到每行有256個數(shù)
for (int i = length; i < 255; i++)
{
out << 0 << ",";
}
out << 0 << endl; //輸出一行
}
in_class.close();
out.close();
return 0;
}