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

📄 main.cpp

📁 数据挖掘的多维关联规则apriori算法的实现
💻 CPP
字号:
/*=============================================================================|| Description: This program is an implementation of the Association Rule|              learning algorithm(Apriori).|              Implemented by:|              Tingshao Zhu(tszhu@cs.ualberta.ca)|              http://www.cs.ualberta.ca/~tszhu|| Usage:       apriori -d datafile [-s support][-c confidence]||              -d datafile|                 Specify the data file which will be used to extract association rules.|              -s support|                 Use the specified support instead of the default(0.001).|              -c confidence|                 Use the specified confidence instead of the default(0.05).|=============================================================================*/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fstream.h>#include <string.h>#include "Apriori.h"#include "AssociationRule.h"#include "List.h"#include "itemSet.h"void usage(){    cout << "\nusage: apriori -d datafile [-s support][-c confidence]\n\n";    cout << "Options:\n";    cout << "\t-d datafile\n";    cout << "\t\tSpecify the data file which will be used to extract association rules.\n\n";	    cout << "\t-s support\n";    cout << "\t\tUse the specified support instead of the\n";    cout << "\t\tdefault(0.001).\n\n";    cout << "\t-c confidence\n";    cout << "\t\tUse the specified confidence instead of the\n";    cout << "\t\tdefault(0.5).\n\n";    exit(0);			}List *loadItemSets(char *datafile, int& max_pageno, bool keeporder){    List *m_data;    itemSet *pitemset;    char line[20*4096];    int pageno, hostid;    char *t, *s;    double weight;    FILE *fp;    // try to open the data file    if((fp = fopen(datafile, "rt")) == NULL)    {        printf("Cannot open the data file %s !\n", datafile);		return (List *)NULL;    }    printf("\n\nLoading data from file %s  ...  ", datafile);	fflush(stdout);    // 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);    printf("Finish Loading!\n\n");	fflush(stdout);    return(m_data);}main( int argc, char *argv[] ){	CApriori *m_apriori;	CAssociationRule *m_rules;    List *traindata;    int pagenum;    char *datafile = NULL, *rules;    double support = 0.1, confidence = 0.5;        int			c;    extern char		*optarg;        /*      Get command line arguments    */    if ( argc < 3 )    {        usage();        exit(-9);    }		    //    // Parse command line arguments    //    while ((c = getopt(argc, argv, "d:c:s:")) != -1)    {		switch (c)		{		    case 'd':				datafile = optarg;				break;		    case 'c':				confidence = atof(optarg);				break;		    case 's':				support = atof(optarg);				break;		    case '?':				usage();		}    }	rules = (char *)new char[strlen(datafile) + 6 + 1];	strcpy(rules, datafile);    strcat(rules, ".rules");    // Read in the structure of the web site from a map file    if((traindata = loadItemSets(datafile, pagenum, false)) == NULL)	{		printf("Cannot get data from %s\n", datafile);		exit(9);	}    printf("Maximum Page Number : %d\n", pagenum);	    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(rules);    // Clear off    delete m_apriori;    delete m_rules;    delete traindata;    	delete rules;}

⌨️ 快捷键说明

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