Example usage for weka.core Instances instance

List of usage examples for weka.core Instances instance

Introduction

In this page you can find the example usage for weka.core Instances instance.

Prototype



publicInstance instance(int index) 

Source Link

Document

Returns the instance at the given position.

Usage

From source file:ANN_single2.MultilayerPerceptron.java

@Override
public void buildClassifier(Instances i) {

    //mengubah class menjadi numeric (diambil indexnya)
    listDoubleinstance = new double[i.numInstances()];
    for (int numIns = 0; numIns < i.numInstances(); numIns++) {
        listDoubleinstance[numIns] = i.instance(numIns).toDoubleArray()[i.classIndex()];
    }//from  w w w.  j a  v  a2  s. co  m
    int cnt = 0;
    for (int itt = 0; itt < 10000; itt++) {
        for (int idxInstance = 0; idxInstance < i.numInstances(); idxInstance++) {
            //buat list input
            ArrayList<Double> listInput = new ArrayList<>();
            listInput.add(1.0); //ini untuk bias
            for (int ins = 0; ins < i.get(idxInstance).numAttributes() - 1; ins++) {
                listInput.add(i.get(idxInstance).value(ins));
            }

            ArrayList<Double> listHide = new ArrayList<>();
            listHide.add(1.0);
            //Hitung output hidden layer
            for (int idxHidden = 1; idxHidden < listHidden.size(); idxHidden++) {
                output(listHidden, listInput, idxHidden);
                listHide.add(listHidden.get(idxHidden).getValue());
            }

            //Hitung ouput output lyer
            for (int idxOutput = 0; idxOutput < listOutput.size(); idxOutput++) {
                output(listOutput, listHide, idxOutput);
            }

            //Hitung error
            calculateError(idxInstance);
            //update bobot
            updateBobot(listInput);
        }
        //Hitung seluruh error untuk menentukan kapan harus berhenti
        //            double error = 0;
        //            for (int idx =0; idx < i.numInstances(); idx++) {
        //                for (int idxOut=0; idxOut < listOutput.size(); idxOut++) {
        //                    error += Math.pow(listOutput.get(idxOut).getError(), 2)/2;
        //                }
        //            }
        //            cnt++;
        //            if (cnt==1000) {
        //                System.out.println("error " + error);
        //                cnt=0;
        //            }
        //            if (error <= threshold) break;
    }
    double error = 0;
    fold++;
    for (int idx = 0; idx < i.numInstances(); idx++) {
        for (int idxOut = 0; idxOut < listOutput.size(); idxOut++) {
            error += Math.pow(listOutput.get(idxOut).getError(), 2) / 2;
        }
    }
    System.out.println("Fold " + fold);
    System.out.println("error " + error);

}

From source file:ANN_single2.SinglelayerPerceptron.java

@Override
public void buildClassifier(Instances i) {
    listOutput = new ArrayList<>();
    for (int idx = 0; idx < i.numClasses(); idx++) {
        listOutput.add(new Node(i.numAttributes()));
    }// w ww  . j av  a2s.  co  m

    //mengubah class menjadi numeric (diambil indexnya)
    listDoubleinstance = new double[i.numInstances()];
    for (int numIns = 0; numIns < i.numInstances(); numIns++) {
        listDoubleinstance[numIns] = i.instance(numIns).toDoubleArray()[i.classIndex()];
    }

    double error = 0;
    for (int iter = 0; iter < itteration; iter++) {
        double errorThres = 0;
        for (int idxInstance = 0; idxInstance < i.numInstances(); idxInstance++) {

            //buat list input
            ArrayList<Double> listInput = new ArrayList<>();
            listInput.add(1.0); //ini bias
            for (int idx = 0; idx < i.numAttributes() - 1; idx++) {
                listInput.add(i.get(idxInstance).value(idx));
            }

            //Hitung output rumus = sigmoid dari sigma
            for (int idxOut = 0; idxOut < listOutput.size(); idxOut++) {
                output(listInput, idxOut);
            }

            //Hitung error
            calculateError(idxInstance);
            //update bobot
            updateBobot(listInput);

        }
        for (int idxOut = 0; idxOut < listOutput.size(); idxOut++) {
            errorThres += Math.pow(listOutput.get(idxOut).getError(), 2) / 2;
        }
        if (errorThres <= threshold)
            break;
        //            System.out.println(errorThres);
    }
    //        fold++;
    //        for (int idx =0; idx < i.numInstances(); idx++) {
    //            for (int idxOut=0; idxOut < listOutput.size(); idxOut++) {
    //                error += Math.pow(listOutput.get(idxOut).getError(), 2)/2;
    //            }
    //        }
    //        System.out.println("Fold " + fold);
    //        System.out.println("error " + error);
}

From source file:asap.PostProcess.java

private void formatPredictions(Instances instances, double[] predictions, String[] columnNames,
        int predictionsColumnIndex, String predictionsColumnName, String columnSeparator, String outputFilename,
        boolean writeColumnsHeaderLine) {
    PerformanceCounters.startTimer("formatPredictions");

    System.out.println("Formatting predictions to file " + outputFilename + "...");
    File outputFile = new File(outputFilename);
    PrintWriter writer;//from  ww w.  ja  v a 2 s  .  c  o  m

    try {
        outputFile.getParentFile().mkdirs();
        outputFile.createNewFile();
        writer = new PrintWriter(outputFile, "UTF-8");
    } catch (IOException ex) {
        Logger.getLogger(PostProcess.class.getName()).log(Level.SEVERE, null, ex);
        return;
    }

    StringBuilder sb = new StringBuilder();
    DecimalFormat df = new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.US));
    df.setMaximumFractionDigits(3);

    int i = -1;
    if (!writeColumnsHeaderLine) {
        i = 0;
    }
    for (; i < instances.numInstances(); i++) {
        sb.delete(0, sb.length());

        for (int j = 0; j < columnNames.length; j++) {
            if (j > 0) {
                sb.append(columnSeparator);
            }

            if (j == predictionsColumnIndex) {
                if (i < 0) {
                    sb.append(predictionsColumnName);
                } else {
                    sb.append(df.format(predictions[i]));
                }
                sb.append(columnSeparator);
            }
            if (i < 0) {
                sb.append(columnNames[j]);
            } else {
                if (columnNames[j].toLowerCase().contains("id")) {
                    Attribute attribute = instances.attribute(columnNames[j]);
                    if (attribute != null) {
                        sb.append((int) instances.instance(i).value(attribute.index()));
                    } else {
                        sb.append(0);
                    }
                } else {
                    Attribute attribute = instances.attribute(columnNames[j]);
                    if (attribute != null) {
                        sb.append(instances.instance(i).value(attribute.index()));
                    } else {
                        sb.append(df.format(0d));
                    }
                }
            }
        }

        if (columnNames.length == predictionsColumnIndex) {
            sb.append(columnSeparator);
            if (i < 0) {
                sb.append(predictionsColumnName);
            } else {
                sb.append(df.format(predictions[i]));
            }
        }

        writer.println(sb);
    }
    writer.flush();
    writer.close();
    System.out.println("\tdone.");
    PerformanceCounters.stopTimer("formatPredictions");
}

From source file:at.aictopic1.sentimentanalysis.machinelearning.impl.TwitterClassifer.java

public Integer classify(Tweet[] tweets) {
    // TEST/*from   w w w  .j a v  a2 s. c o m*/

    // Generate two tweet examples
    Tweet exOne = new Tweet("This is good and fantastic");
    exOne.setPreprocessedText("This is good and fantastic");
    Tweet exTwo = new Tweet("Horribly, terribly bad and more");
    exTwo.setPreprocessedText("Horribly, terribly bad and more");
    Tweet exThree = new Tweet(
            "I want to update lj and read my friends list, but I\\'m groggy and sick and blargh.");
    exThree.setPreprocessedText(
            "I want to update lj and read my friends list, but I\\'m groggy and sick and blargh.");
    Tweet exFour = new Tweet("bad hate worst sick");
    exFour.setPreprocessedText("bad hate worst sick");
    tweets = new Tweet[] { exOne, exTwo, exThree, exFour };
    // TEST

    // Load model
    //        loadModel();
    // Convert Tweet to Instance type 
    // Get String Data
    // Create attributes for the Instances set
    Attribute twitter_id = new Attribute("twitter_id");
    //        Attribute body = new Attribute("body");

    FastVector classVal = new FastVector(2);
    classVal.addElement("pos");
    classVal.addElement("neg");

    Attribute class_attr = new Attribute("class_attr", classVal);

    // Add them to a list
    FastVector attrVector = new FastVector(3);
    //        attrVector.addElement(twitter_id);
    //        attrVector.addElement(new Attribute("body", (FastVector) null));
    //        attrVector.addElement(class_attr);

    // Get the number of tweets and then create predictSet
    int numTweets = tweets.length;
    Enumeration structAttrs = dataStructure.enumerateAttributes();

    //        ArrayList<Attribute> attrList = new ArrayList<Attribute>(dataStructure.numAttributes());
    while (structAttrs.hasMoreElements()) {
        attrVector.addElement((Attribute) structAttrs.nextElement());
    }
    Instances predictSet = new Instances("predictInstances", attrVector, numTweets);
    //        Instances predictSet = new Instances(dataStructure);
    predictSet.setClassIndex(2);

    // init prediction
    double prediction = -1;

    System.out.println("PredictSet matches source structure: " + predictSet.equalHeaders(dataStructure));

    System.out.println("PredSet struct: " + predictSet.attribute(0));
    System.out.println("PredSet struct: " + predictSet.attribute(1));
    System.out.println("PredSet struct: " + predictSet.attribute(2));
    // Array to return predictions 
    //double[] tweetsClassified = new double[2][numTweets];
    //List<Integer, Double> tweetsClass = new ArrayList<Integer, Double>(numTweets);
    for (int i = 0; i < numTweets; i++) {
        String content = (String) tweets[i].getPreprocessedText();

        System.out.println("Tweet content: " + content);

        //            attrList
        Instance tweetInstance = new Instance(predictSet.numAttributes());

        tweetInstance.setDataset(predictSet);

        tweetInstance.setValue(predictSet.attribute(0), i);
        tweetInstance.setValue(predictSet.attribute(1), content);
        tweetInstance.setClassMissing();

        predictSet.add(tweetInstance);

        try {
            // Apply string filter
            StringToWordVector filter = new StringToWordVector();

            filter.setInputFormat(predictSet);
            Instances filteredPredictSet = Filter.useFilter(predictSet, filter);

            // Apply model
            prediction = trainedModel.classifyInstance(filteredPredictSet.instance(i));
            filteredPredictSet.instance(i).setClassValue(prediction);
            System.out.println("Classification: " + filteredPredictSet.instance(i).toString());
            System.out.println("Prediction: " + prediction);

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

    return 0;
}

From source file:aw_cluster.myKMeans.java

@Override
public void buildClusterer(Instances data) throws Exception {
    getCapabilities().testWithFail(data);

    Instances instances = new Instances(data);
    instances.setClassIndex(-1);// w  w  w  .j a  va2 s .co m

    if (instances.numInstances() == 0) {
        throw new RuntimeException("Dataset should not be empty");
    }

    assignments = new int[instances.numInstances()];
    centroid = new Instances(instances, numCluster);
    distanceFunction.setInstances(instances);
    squaredError = new double[numCluster];

    // Initialize Centroid Random From seed
    Random random = new Random(getSeedRandom());
    Instances tempInstances = new Instances(instances);

    int tI = tempInstances.numInstances() - 1;
    while (tI >= 0 && centroid.numInstances() < numCluster) {
        int indexCentroid = random.nextInt(tI + 1);
        centroid.add(tempInstances.instance(indexCentroid));
        tempInstances.swap(tI, indexCentroid);
        tI--;
    }

    tempInstances = null;

    boolean converged = false;
    while (!converged) {
        converged = true;
        numIteration++;
        for (int i = 0; i < instances.numInstances(); ++i) {
            Instance toCluster = instances.instance(i);
            int clusterResult = clusterInstanceProcess(toCluster, true);
            if (clusterResult != assignments[i])
                converged = false;
            assignments[i] = clusterResult;
        }

        // update centroid
        Instances[] TempI = new Instances[numCluster];
        centroid = new Instances(instances, numCluster);
        for (int i = 0; i < TempI.length; ++i) {
            TempI[i] = new Instances(instances, 0);
        }
        for (int i = 0; i < instances.numInstances(); ++i) {
            TempI[assignments[i]].add(instances.instance(i));
        }
        for (int i = 0; i < TempI.length; ++i) {
            moveCentroid(TempI[i]);
        }
        if (converged)
            squaredError = new double[numCluster];
        if (numIteration == maxIteration)
            converged = true;
        sizeEachCluster = new int[numCluster];
        for (int i = 0; i < numCluster; ++i) {
            sizeEachCluster[i] = TempI[i].numInstances();
        }

    }
}

From source file:binarytreesom.clustering.BinaryTreeSOMClustering.java

/**
 * Initialize the tree configuration. This implementation considers a complete binary tree of depth h. 
 *//*  w w w.j  ava2s .  c o  m*/
private void initialize() throws IOException {
    //the number of nodes N, is penednt on h. actualy N
    //h=ln N -> N=2^h
    Instances instances = readArff(getFilenameARFF());
    instances.setClassIndex(-1);//clustering Stuff

    numberOfInstances = instances.numInstances();
    dimensionality = instances.numAttributes();
    data = new double[getNumberOfInstances()][getDimensionality()];
    weight = new double[getNumberOfNeurons()][getDimensionality()];
    //randomly select instances and assign to weight.

    for (int k = 0; k < getNumberOfNeurons(); k++) {
        weight[k] = instances.instance(r.nextInt(getNumberOfInstances())).toDoubleArray(); //hard copy of the double array
    }

    for (int k = 0; k < getNumberOfInstances(); k++) {
        data[k] = instances.instance(k).toDoubleArray(); //hard copy of the double array
    }
}

From source file:bme.mace.logicdomain.Evaluation.java

License:Open Source License

/**
 * Evaluates the classifier on a given set of instances. Note that the data
 * must have exactly the same format (e.g. order of attributes) as the data
 * used to train the classifier! Otherwise the results will generally be
 * meaningless.//from  w w w . j a  v  a 2s  .  co  m
 * 
 * @param classifier machine learning classifier
 * @param data set of test instances for evaluation
 * @param forPredictionsString varargs parameter that, if supplied, is
 *          expected to hold a StringBuffer to print predictions to, a Range
 *          of attributes to output and a Boolean (true if the distribution is
 *          to be printed)
 * @return the predictions
 * @throws Exception if model could not be evaluated successfully
 */
public double[] evaluateModel(List<LibSVM> classifier, List<Double> classifierWeight, Instances data,
        Object... forPredictionsPrinting) throws Exception {
    // for predictions printing
    StringBuffer buff = null;
    Range attsToOutput = null;
    boolean printDist = false;

    double predictions[] = new double[data.numInstances()];

    if (forPredictionsPrinting.length > 0) {
        buff = (StringBuffer) forPredictionsPrinting[0];
        attsToOutput = (Range) forPredictionsPrinting[1];
        printDist = ((Boolean) forPredictionsPrinting[2]).booleanValue();
    }

    // Need to be able to collect predictions if appropriate (for AUC)

    for (int i = 0; i < data.numInstances(); i++) {
        predictions[i] = evaluateModelOnceAndRecordPrediction(classifier, classifierWeight, data.instance(i));

    }
    for (int i = 0; i < predictions.length; i++) {
        System.out.print(predictions[i]);
    }
    return predictions;
}

From source file:bme.mace.logicdomain.Evaluation.java

License:Open Source License

/**
 * Sets the class prior probabilities/*from ww  w.j a  v  a 2  s  . c  o  m*/
 * 
 * @param train the training instances used to determine the prior
 *          probabilities
 * @throws Exception if the class attribute of the instances is not set
 */
public void setPriors(Instances train) throws Exception {
    m_NoPriors = false;

    if (!m_ClassIsNominal) {

        m_NumTrainClassVals = 0;
        m_TrainClassVals = null;
        m_TrainClassWeights = null;
        m_PriorErrorEstimator = null;
        m_ErrorEstimator = null;

        for (int i = 0; i < train.numInstances(); i++) {
            Instance currentInst = train.instance(i);
            if (!currentInst.classIsMissing()) {
                addNumericTrainClass(currentInst.classValue(), currentInst.weight());
            }
        }

    } else {
        for (int i = 0; i < m_NumClasses; i++) {
            m_ClassPriors[i] = 1;
        }
        m_ClassPriorsSum = m_NumClasses;
        for (int i = 0; i < train.numInstances(); i++) {
            if (!train.instance(i).classIsMissing()) {
                m_ClassPriors[(int) train.instance(i).classValue()] += train.instance(i).weight();
                m_ClassPriorsSum += train.instance(i).weight();
            }
        }
    }
}

From source file:boostingPL.boosting.AdaBoost.java

License:Open Source License

public AdaBoost(Instances insts, int numInterations) {
    this.insts = insts;
    this.numIterations = numInterations;
    this.classifiers = new Classifier[numInterations];
    this.cweights = new double[numInterations];

    // initialize instance's weight
    int numInstances = insts.numInstances();
    for (int i = 0; i < numInstances; i++) {
        double tweight = 1.0 / numInstances;
        insts.instance(i).setWeight(tweight);
    }/*www. j a  v  a  2s  .  com*/
    //System.out.println("instances weights total: " + insts.sumOfWeights());

}

From source file:boostingPL.boosting.SAMME.java

License:Open Source License

public SAMME(Instances insts, int numInterations) {
    this.insts = insts;
    this.numIterations = numInterations;
    this.classifiers = new Classifier[numInterations];
    this.cweights = new double[numInterations];

    // initialize instance's weight
    final int numInstances = insts.numInstances();
    for (int i = 0; i < numInstances; i++) {
        double tweight = 1.0 / numInstances;
        insts.instance(i).setWeight(tweight);
    }/*from ww w.j a  v a2  s.  c o  m*/
    //System.out.println("instances weights total: " + insts.sumOfWeights());

}