这几天小白让我做一个max-miner 最大项集的挖掘,一般的算法有apriori和FP-TREE 考虑到用FP-TREE 可能有点复杂就用apriori算法先测试下,在小样本测试的时候速度非常快,当对一个5W行的文本测试效率变得不可承受了,因此对算法进行了分析,假设每行有100个单词当用apriori算法对3-候选集进行count的时候每一个候选集需要:
5W * 100*3 = 1500W 次的比较 当3-候选集有1000个那么需要150亿次比较效率变得不可接受即使用优化的算法在1天也很难得到结果。因此在样本很大的情况下Apriori算法并不适用。
原始Apriori算法借鉴了别人的一个程序进行了自己的改进,找不到原作者了在这里对作者表示感谢。
5W * 100*3 = 1500W 次的比较 当3-候选集有1000个那么需要150亿次比较效率变得不可接受即使用优化的算法在1天也很难得到结果。因此在样本很大的情况下Apriori算法并不适用。
原始Apriori算法借鉴了别人的一个程序进行了自己的改进,找不到原作者了在这里对作者表示感谢。
下面是代码:
#ifndef APRIOR_H
#define APRIOR_H
#include <vector>
#include <map>
using namespace std;
class aprior{
public:
aprior(int supp, string name):support(supp),file(name){}
~aprior();
void openData();
void generate_1Itemset();
void generate_Alternative2();
void generate_Alternative();
void generate_ItemSet();
void get_Big_set();
void get2B();
void begin();
void countWord(char s);
private:
void countSupport();
void getNextSet();
void output1();
void output2();
bool compareS(const vector<string> s1, const vector<string> s2);
string getRes();
struct item{
string key;
int value;
};
struct mutiItem{
vector<string> key;
int value;
};
int support;
vector<mutiItem> vec_n_current;
vector<mutiItem> vec_muti_pre;
vector<mutiItem> vec_muti_current;
vector<item> vec_item;
vector<string> vec_str;
map<string, int> sequence;
string file;
};
#endif //end APRIOR_H
//aprior.cpp
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#include <fstream>
#include "aprior.h"
#include <string>
#include <cassert>
#include <cstdio>
using namespace std;
//open file and each line of file as input da