⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 数据挖掘中Aprior算法的C++实现
💻 CPP
字号:
#include <iostream>
using namespace std;
#include"apriori.h"
#include"association.h"
#include"HashTree.h"
#include"ItemSet.h"
#include"List.h"
#include"tzObject.h"
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<string>
char *m_data;
char *m_rule;
double	m_support;
double	m_confidence;
#include "file.h"
List *loadItemSets(const char *datafile, int& max_pageno, bool keeporder)
{
    List *m_data;
    itemSet *pitemset;
    char line[20*4096];
    int pageno;
    char *t, *s;
    FILE *fp;

    // try to open the data file
    if((fp = fopen(datafile, "rt")) == NULL)
		return (List *)NULL;

    // this is the data structure that stores the input data
    m_data = (List *)new List();
    max_pageno = -1;

    // Read in each line, extract all the items within this line
    while(fgets(line, 20*4096, fp) != NULL)
    {
        if (strchr(line, ',') != NULL)
        {
            s = (char *) new char[strlen(line) + 1];
            strcpy(s, line);
            if ((t = strtok(s, ",")) == NULL)
            {
                delete s;
                break;
            }

            pitemset = (itemSet *)new itemSet();
			pitemset->keeporder(keeporder);
            
            pageno = atoi(t);
            if(max_pageno < pageno)
                max_pageno = pageno;
            pitemset->add(pageno);
            
            t = strtok((char *)NULL, ",");

            for ( ; t != NULL; t = strtok((char *)NULL, ","))
            {
                pageno = atoi(t);
                if(max_pageno < pageno)
                    max_pageno = pageno;
				
                pitemset->add(pageno);
            }

            // if the length of the session is greater than 1, then insert it into the session list
            if(pitemset->size() > 1)
            {
                pitemset->support(1);
                m_data->add(pitemset);
            }
			else
	            delete pitemset;

            delete s;
        }
    }

    fclose(fp);

    return(m_data);
}

main(){
	CApriori *m_apriori;
	CAssociationRule *m_rules;
    List *traindata;
    int pagenum;
    double support = 0.1, confidence = 0.5;
    cout<< "set support,confidence==";
	cin>>support>>confidence;
	scanf("%f,%f",&support,&confidence);
	FileItemtoNum("data.txt");
    // Read in the structure of the web site from a map file
    if((traindata = loadItemSets("temp.txt", pagenum, false)) == NULL)
	{
		printf("Cannot load data!");
		return;
	}
	
    m_apriori = new CApriori();
    m_apriori->setsupport(support);
	m_apriori->pagenum = pagenum + 1;

    // Finding large Itemsets from training data
    m_apriori->FindLargeItemSets(traindata);
    
    // Association Rules from extracted large item sets
    m_rules = new CAssociationRule();
    m_rules->setconfidence(confidence);
    m_rules->m_LargeItemSets = m_apriori->m_Ls;
    m_rules->genrules();
    m_rules->save("data.rules");

    // Clear off
    delete m_apriori;
    delete m_rules;

    delete traindata;

	printf("Association Rule Mining Successfully Done!");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -