Example usage for weka.associations Apriori getAllTheRules

List of usage examples for weka.associations Apriori getAllTheRules

Introduction

In this page you can find the example usage for weka.associations Apriori getAllTheRules.

Prototype

public ArrayList<Object>[] getAllTheRules() 

Source Link

Document

returns all the rules

Usage

From source file:ap.mavenproject1.HelloWeka.java

public static void main(String args[]) {
    Instances data = null;/*from  w  w  w.  j a  v  a  2s  .c om*/
    ArffLoader loader = new ArffLoader();
    try {

        loader.setFile(new File("C:\\Users\\USER\\Desktop\\data.arff"));

        data = loader.getDataSet();
        data.setClassIndex(data.numAttributes() - 1);

    } catch (IOException ex) {
        Logger.getLogger(HelloWeka.class.getName()).log(Level.SEVERE, null, ex);
    }

    Apriori apriori = new Apriori();
    try {
        NumericToNominal numericToNominal = new NumericToNominal();
        numericToNominal.setInputFormat(data);

        Instances nominalData = Filter.useFilter(data, numericToNominal);
        apriori.buildAssociations(nominalData);
        FastVector[] allTheRules;
        allTheRules = apriori.getAllTheRules();
        for (int i = 0; i < allTheRules.length; i++) {
            System.out.println(allTheRules[i]);
        }
        //             BufferedWriter writer = new BufferedWriter(new FileWriter("./output.arff"));
        //                writer.write(nominalData.toString());
        //                writer.flush();
        //                writer.close();

    } catch (Exception ex) {
        Logger.getLogger(HelloWeka.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:demo.bean.WekaDABeanImpl.java

License:Open Source License

@Override
public Map<Integer, ResultItem> generateResults() {
    Map<Integer, ResultItem> myResults = new HashMap<Integer, ResultItem>();

    try {/*from   w w w . j ava 2s  . c o m*/
        InputStream input = getClass().getResourceAsStream("/arff/weather.nominal.arff");
        DataSource source = new DataSource(input);
        Instances data = source.getDataSet();

        Apriori apriori = new Apriori();
        apriori.buildAssociations(data);

        FastVector res[] = apriori.getAllTheRules();
        int x = 0;
        for (FastVector rule : res) {
            if (rule == null)
                continue;

            for (int i = 0; i < rule.size(); ++i) {
                Object o = rule.elementAt(i);

                if (o instanceof AprioriItemSet) {
                    AprioriItemSet itemSet = (AprioriItemSet) o;

                    System.out.println(itemSet.toString(data) + " " + itemSet.items().length);

                    String ruleStr = itemSet.toString(data) + " ";
                    myResults.put((x + 1), new ResultItem((x + 1), ruleStr));
                    if (x >= 10)
                        break;
                    x++;
                }
            }

        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return myResults;
}

From source file:EsperTest.CEPListener.java

public void update(EventBean[] newData, EventBean[] oldData) {

    System.out.println("Event received: " + newData[0].getUnderlying());
    if (newData.length > 2) {
        //create the column name and type, these are strings
        //http://weka.wikispaces.com/Creating+an+ARFF+file
        Instances data;/*from  w ww.  ja va2  s.  c om*/
        FastVector atts = new FastVector();

        for (int j = 0; j < columnNumbers.length; j++) {
            FastVector values = new FastVector();
            for (int i = 0; i < labels.NominalCount(j); i++) {
                values.addElement(labels.GetLabel(columnNumbers[j], i));
            }
            atts.addElement(new Attribute(labels.GetHeader(columnNumbers[j]), values));
        }

        data = new Instances("Title", atts, 0);

        for (int i = 0; i < newData.length; i++) {
            Instance inst = new Instance(columnNumbers.length);
            for (int j = 0; j < columnNumbers.length; j++) {
                inst.setValue(j, newData[i].get("eventType").toString());
            }
            data.add(inst);
        }

        Apriori aprioriObj = new weka.associations.Apriori();

        try {
            aprioriObj.buildAssociations(data);
        } catch (Exception e) {
            System.out.println(e);
        }

        FastVector rules[] = aprioriObj.getAllTheRules();

    }

}