请各位高手帮帮忙看看Apriori C++代码 含义,帮忙注释一下

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <tchar.h>
using namespace std;

typedef vector<pair<vector<string>,int> > Items;

Items initdata();
bool Item_Exists(Items& item,vector<string>& arr);
void Item_Show(Items& item);
int Calculate_Count(Items& item,const vector<string>& arr);
Items Item_Generate(Items& item,int& k);

static double MINSUP = 0.3;
int _tmain(int argc, _TCHAR* argv[])
{
Items data = initdata();
Item_Show(data);
Items item;
Items::iterator iter;
for(iter = data.begin(); iter != data.end(); iter++)
{
for(int i = 0; i < iter->first.size(); i++)
{
vector<string> item_tmp;
item_tmp.push_back(iter->first[i]);
if(!Item_Exists(item,item_tmp))
{
int c = Calculate_Count(data,item_tmp);
if((double)c/data.size() >=MINSUP)
item.push_back(pair<vector<string>, int>(item_tmp, c));
}
}
}
Item_Show(item);
for (int k = 2;item.size() > 0; k++)
{
Items item2 = Item_Generate(item,k);
for (iter = item2.begin(); iter != item2.end(); iter++)
{
int c = Calculate_Count(data,iter->first);
iter->second = c;
}
Item_Show(item2);
item = item2;
}
cout<<endl;
return 0;
}

第1个回答  2009-11-21
还有下面的函数也是,是接着问题的函数的~~
Items initdata() //数据集函数
{
//static string data[][5] = {
// {"牛肉","鸡肉","牛奶"},
// {"牛肉","奶酪"},
// {"奶酪","靴子"},
// {"牛肉","鸡肉","奶酪"},
// {"牛肉","鸡肉","衣服","奶酪","牛奶"},
// {"鸡肉","衣服","牛奶"},
// {"鸡肉","牛奶","衣服"}
//};
Items data;
vector<string> item;

item.push_back("牛肉");
item.push_back("鸡肉");
item.push_back("牛奶");
//sort(item.begin(),item.end());
data.push_back(pair<vector<string>,int>(item,0));
item.erase(item.begin(),item.end());

item.push_back("牛肉");
item.push_back("奶酪");
//sort(item.begin(),item.end());
data.push_back(pair<vector<string>,int>(item,0));
item.erase(item.begin(),item.end());

item.push_back("奶酪");
item.push_back("靴子");
//sort(item.begin(),item.end());
data.push_back(pair<vector<string>,int>(item,0));
item.erase(item.begin(),item.end());

item.push_back("牛肉");
item.push_back("鸡肉");
item.push_back("奶酪");
//sort(item.begin(),item.end());
data.push_back(pair<vector<string>,int>(item,0));
item.erase(item.begin(),item.end());

item.push_back("牛肉");
item.push_back("鸡肉");
item.push_back("衣服");
item.push_back("奶酪");
item.push_back("牛奶");
//sort(item.begin(),item.end());
data.push_back(pair<vector<string>,int>(item,0));
item.erase(item.begin(),item.end());

item.push_back("鸡肉");
item.push_back("衣服");
item.push_back("牛奶");
//sort(item.begin(),item.end());
data.push_back(pair<vector<string>,int>(item,0));
item.erase(item.begin(),item.end());

item.push_back("鸡肉");
item.push_back("牛奶");
item.push_back("衣服");
//sort(item.begin(),item.end());
data.push_back(pair<vector<string>,int>(item,0));
item.erase(item.begin(),item.end());

return data;
}

Items Item_Generate(Items& item,int& k)
{
Items data;
Items::iterator iter;

vector<string> arr;
for (iter = item.begin(); iter != item.end(); iter++)
{
Items::iterator iter2 = iter;
iter2++;
for (; iter2 != item.end(); iter2++)
{
int i = 0;
for (; i < k-2; i++)
{
if(iter->first[i] == iter2->first[i])
arr.push_back(iter->first[i]);
}
if(arr.size() == k-2 && iter->first[i] != iter2->first[i])
{
arr.push_back(iter->first[i]);
arr.push_back(iter2->first[i]);
data.push_back(pair<vector<string>,int>(arr,0));
}
arr.erase(arr.begin(),arr.end());
}
}
return data;
}

int Calculate_Count(Items& item,const vector<string>& arr)
{
int ret = 0;
Items::iterator iter;
vector<string>::iterator iter_find;
for (iter = item.begin(); iter != item.end(); iter++)
{
if(iter->first.size() >= arr.size())
{
int c = 0;
for(int i = 0; i < arr.size(); i++)
{
iter_find = find(iter->first.begin(),iter->first.end(),arr[i]);
if(iter_find != iter->first.end())
c++;
}
if(c == arr.size())
ret++;
}
}

return ret;
}

bool Item_Exists(Items& item,vector<string>& arr)
{
bool ret = false;

Items::iterator iter;
for (iter = item.begin(); iter != item.end(); iter++)
{
if(iter->first.size() == arr.size())
{
bool b = true;
for (int i = 0; i < iter->first.size(); i++)
{
if(iter->first[i] != arr[i])
{
b = false;
break;
}
}
if(b)
{
ret = true;
break;
}
}
}

return ret;
}

void Item_Show(Items& item) //显示数据
{
Items::iterator iter;
for (iter = item.begin(); iter != item.end(); iter++)
{
cout<<"{";
for(int i = 0; i < iter->first.size(); i++)
{
cout<<iter->first[i];
if(i < iter->first.size() - 1)
cout<<",";
}
cout<<"}"<<" "<<iter->second<<endl;
}
}

请各位高手帮帮忙看看Apriori C++代码 含义,帮忙注释一下
还有下面的函数也是,是接着问题的函数的~~Items initdata() \/\/数据集函数 { \/\/static string data[][5] = { \/\/ {"牛肉","鸡肉","牛奶"},\/\/ {"牛肉","奶酪"},\/\/ {"奶酪","靴子"},\/\/ {"牛肉","鸡肉","奶酪"},\/\/ {"牛肉","鸡肉","衣服","奶酪","牛...

相似回答
大家正在搜