Package cc.mallet.types

Examples of cc.mallet.types.FeatureVector


          Integer.valueOf(label.getBestLabel().getEntry().toString()).intValue();
        if (positiveIndex == -1) { // invalid instance
          logger.warning("True label is -1. Skipping...");
           continue;
        }
        FeatureVector fv = (FeatureVector)fvs.get(positiveIndex);
        Alphabet fdict = fv.getAlphabet();
        assert (fv.getAlphabet() == fd);

        // xxx ensure dimensionality of constraints correct
        MatrixOps.rowPlusEquals (constraints, numFeatures, 0, fv, instanceWeight);

        // For the default feature, whose weight is 1.0
        assert(!Double.isNaN(instanceWeight)) : "instanceWeight is NaN";
        //assert(!Double.isNaN(li)) : "bestIndex is NaN";
        boolean hasNaN = false;
        for(int i = 0; i < fv.numLocations(); i++) {
          if(Double.isNaN(fv.valueAtLocation(i))) {
            logger.info("NaN for feature " + fdict.lookupObject(fv.indexAtLocation(i)).toString());
            hasNaN = true;
          }
        }
        if(hasNaN)
          logger.info("NaN in instance: " + instance.getName());
View Full Code Here


         
          for (int si=0; si < fvs.size(); si++) {
            if (scores[si]==0)
              continue;
            assert (!Double.isInfinite(scores[si]));
            FeatureVector cfv = (FeatureVector)fvs.get(si);
            MatrixOps.rowPlusEquals (cachedGradient, numFeatures,
                                     0, cfv, -instanceWeight * scores[si]);
            cachedGradient[numFeatures*0 + defaultFeatureIndex] += (-instanceWeight * scores[si]);           
          }
        }
 
View Full Code Here

    Iterator<Integer> keyIter = labeledFeatures.keySet().iterator();
   
    double[][] featureCounts = new double[labeledFeatures.size()][numLabels];
    for (int ii = 0; ii < trainingData.size(); ii++) {
      Instance instance = trainingData.get(ii);
      FeatureVector fv = (FeatureVector)instance.getData();
      Labeling labeling = trainingData.get(ii).getLabeling();
      double[] labelDist = new double[numLabels];
     
      if (labeling == null) {
        labelByVoting(labeledFeatures,instance,labelDist);
      } else {
        int li = labeling.getBestIndex();
        labelDist[li] = 1.;
      }
 
      keyIter = labeledFeatures.keySet().iterator();
      int i = 0;
      while (keyIter.hasNext()) {
        int fi = keyIter.next();
        if (fv.location(fi) >= 0) {
          for (int li = 0; li < numLabels; li++) {
            featureCounts[i][li] += labelDist[li] * fv.valueAtLocation(fv.location(fi));
          }
        }
        i++;
      }
    }
View Full Code Here

   
    double[][] featureLabelCounts = new double[numFeatures][numLabels];
   
    for (int ii = 0; ii < list.size(); ii++) {
      Instance instance = list.get(ii);
      FeatureVector featureVector = (FeatureVector)instance.getData();
     
      // this handles distributions over labels
      for (int li = 0; li < numLabels; li++) {
        double py = instance.getLabeling().value(li);
        for (int loc = 0; loc < featureVector.numLocations(); loc++) {
          int fi = featureVector.indexAtLocation(loc);
          double val;
          if (useValues) {
            val = featureVector.valueAtLocation(loc);
          }
          else {
            val = 1.0;
          }
          featureLabelCounts[fi][li] += py * val;
 
View Full Code Here

    assert(Maths.almostEquals(MatrixOps.sum(dist),1));
    return dist;
  }
 
  private static void labelByVoting(HashMap<Integer,ArrayList<Integer>> labeledFeatures, Instance instance, double[] scores) {
    FeatureVector fv = (FeatureVector)instance.getData();
    int numFeatures = instance.getDataAlphabet().size() + 1;
   
    int[] numLabels = new int[instance.getTargetAlphabet().size()];
    Iterator<Integer> keyIterator = labeledFeatures.keySet().iterator();
    while (keyIterator.hasNext()) {
      ArrayList<Integer> majorityClassList = labeledFeatures.get(keyIterator.next());
      for (int i = 0; i < majorityClassList.size(); i++) {
        int li = majorityClassList.get(i);
        numLabels[li]++;
      }
    }
   
    keyIterator = labeledFeatures.keySet().iterator();
    while (keyIterator.hasNext()) {
      int next = keyIterator.next();
      assert(next < numFeatures);
      int loc = fv.location(next);
      if (loc < 0) {
        continue;
      }
     
      ArrayList<Integer> majorityClassList = labeledFeatures.get(next);
View Full Code Here

    }
    this.numLabels = pipe.getTargetAlphabet().size();
  }
 
  public void getClassificationScores(Instance instance, double[] scores) {
    FeatureVector input = (FeatureVector)instance.getData();
    for (MaxEntPRConstraint feature : constraints) {
      feature.preProcess(input);
    }
    for (int li = 0; li < numLabels; li++) {
      int ci = 0;
View Full Code Here

      return getLeaf(node.getRightChild(), fv);
  }
 
  public Classification classify (Instance instance)
  {
    FeatureVector fv = (FeatureVector) instance.getData ();
    assert (instancePipe == null || fv.getAlphabet () == this.instancePipe.getDataAlphabet ());
   
    Node leaf = getLeaf(m_root, fv);
    return new Classification (instance, this, leaf.getGainRatio().getBaseLabelDistribution());
  }
View Full Code Here

        throw new IllegalStateException ("Frozen.  Cannot split.");
      int numLeftChildren = 0;
      boolean[] toLeftChild = new boolean[m_instIndices.length];
      for (int i = 0; i < m_instIndices.length; i++) {
        Instance instance = m_ilist.get(m_instIndices[i]);
        FeatureVector fv = (FeatureVector) instance.getData();
        if (fv.value (m_gainRatio.getMaxValuedIndex()) <= m_gainRatio.getMaxValuedThreshold()) {
          toLeftChild[i] = true;
          numLeftChildren++;
        }
        else
          toLeftChild[i] = false;
View Full Code Here

      double instanceWeight = trainingList.getInstanceWeight(inst);
      Labeling labeling = inst.getLabeling ();
      if (labeling == null)
        continue;
      //logger.fine ("Instance "+ii+" labeling="+labeling);
      FeatureVector fv = (FeatureVector) inst.getData ();
      Alphabet fdict = fv.getAlphabet();
      assert (fv.getAlphabet() == fd);

      // Here is the difference between this code and the single label
      //  version: rather than only picking out the "best" index,
      //  loop over all label indices.
     
      assert(labeling.numLocations() == trainingSet.getTargetAlphabet().size());
      for (int pos = 0; pos < labeling.numLocations(); pos++){
        MatrixOps.rowPlusEquals (constraints, numFeatures,
                     labeling.indexAtLocation(pos),
                     fv,
                     instanceWeight*labeling.valueAtLocation(pos));
      }

      assert(!Double.isNaN(instanceWeight)) : "instanceWeight is NaN";

      boolean hasNaN = false;
      for (int i = 0; i < fv.numLocations(); i++) {
        if (Double.isNaN(fv.valueAtLocation(i))) {
          logger.info("NaN for feature " + fdict.lookupObject(fv.indexAtLocation(i)).toString());
          hasNaN = true;
        }
      }
      if (hasNaN)
        logger.info("NaN in instance: " + inst.getName());
View Full Code Here

        if (labeling == null)
          continue;
        //System.out.println("L Now "+inputAlphabet.size()+" regular features.");

        this.theClassifier.getClassificationScores (instance, scores);
        FeatureVector fv = (FeatureVector) instance.getData ();

        value = 0.0;
        for(int pos = 0; pos < labeling.numLocations(); pos++) { //loop, added by Limin Yao
          int ll = labeling.indexAtLocation(pos);
          if (scores[ll] == && labeling.valueAtLocation(pos) > 0) {
View Full Code Here

TOP

Related Classes of cc.mallet.types.FeatureVector

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.