Package cc.mallet.types

Examples of cc.mallet.types.LabelAlphabet


    pred.addThruPipe (new ArrayIterator (predStrings));

    InstanceList targets = new InstanceList (pipe);
    targets.addThruPipe (new ArrayIterator (trueStrings));

    LabelAlphabet dict = (LabelAlphabet) pipe.getTargetAlphabet ();
    Extraction extraction = new Extraction (null, dict);

    for (int i = 0; i < pred.size(); i++) {
      Instance aPred = pred.get (i);
      Instance aTarget = targets.get (i);
View Full Code Here


  }

  public String toString () {
    StringBuffer sb = new StringBuffer ();
    int maxLabelNameLength = 0;
    LabelAlphabet labelAlphabet = trial.getClassifier().getLabelAlphabet();
    for (int i = 0; i < numClasses; i++) {
      int len = labelAlphabet.lookupLabel(i).toString().length();
      if (maxLabelNameLength < len)
        maxLabelNameLength = len;
    }

    sb.append ("Confusion Matrix, row=true, column=predicted  accuracy="+trial.getAccuracy()+"\n");
    for (int i = 0; i < maxLabelNameLength-5+4; i++) sb.append (' ');
    sb.append ("label");
    for (int c2 = 0; c2 < Math.min(10,numClasses); c2++sb.append ("   "+c2);
    for (int c2 = 10; c2 < numClasses; c2++sb.append ("  "+c2);
    sb.append ("  |total\n");
    for (int c = 0; c < numClasses; c++) {
      appendJustifiedInt (sb, c, false);
      String labelName = labelAlphabet.lookupLabel(c).toString();
      for (int i = 0; i < maxLabelNameLength-labelName.length(); i++) sb.append (' ');
      sb.append (" "+labelName+" ");
      for (int c2 = 0; c2 < numClasses; c2++) {
        appendJustifiedInt (sb, values[c][c2], true);
        sb.append (' ');
View Full Code Here

  public MaxEntOptimizableByLabelLikelihood (InstanceList trainingSet, MaxEnt initialClassifier)
  {
    this.trainingList = trainingSet;
    Alphabet fd = trainingSet.getDataAlphabet();
    LabelAlphabet ld = (LabelAlphabet) trainingSet.getTargetAlphabet();
    // Don't fd.stopGrowth, because someone might want to do feature induction
    ld.stopGrowth();
    // Add one feature for the "default feature".
    this.numLabels = ld.size();
    this.numFeatures = fd.size() + 1;
    this.defaultFeatureIndex = numFeatures-1;
    this.parameters = new double [numLabels * numFeatures];
    this.constraints = new double [numLabels * numFeatures];
    this.cachedGradient = new double [numLabels * numFeatures];
 
View Full Code Here

  }
 
  public void print ()
  {   
    final Alphabet dict = getAlphabet();
    final LabelAlphabet labelDict = (LabelAlphabet)getLabelAlphabet();
   
    int numFeatures = dict.size() + 1;
    int numLabels = labelDict.size();
   
    // Include the feature weights according to each label
    //for (int li = 0; li < numLabels; li++) {
    System.out.println ("FEATURES FOR CLASS "+labelDict.lookupObject (0));
    System.out.println (" <default> "+parameters [defaultFeatureIndex]);
    for (int i = 0; i < defaultFeatureIndex; i++) {
      Object name = dict.lookupObject (i);
      double weight = parameters [i];
      System.out.println (" "+name+" "+weight);
View Full Code Here

    }

  public void print ()
  {   
    final Alphabet dict = getAlphabet();
    final LabelAlphabet labelDict = getLabelAlphabet();
       
    int numFeatures = dict.size() + 1;
    int numLabels = labelDict.size();
   
     // Include the feature weights according to each label
     for (int li = 0; li < numLabels; li++) {
       System.out.println ("FEATURES FOR CLASS "+labelDict.lookupObject (li));
       System.out.println (" <default> "+parameters [li*numFeatures + defaultFeatureIndex]);
       for (int i = 0; i < defaultFeatureIndex; i++) {
         Object name = dict.lookupObject (i);
              double weight = parameters [li*numFeatures + i];
         System.out.println (" "+name+" "+weight);
View Full Code Here

  public MaxEntOptimizableByLabelDistribution (InstanceList trainingSet, MaxEnt initialClassifier)
  {
    this.trainingList = trainingSet;
    Alphabet fd = trainingSet.getDataAlphabet();
    LabelAlphabet ld = (LabelAlphabet) trainingSet.getTargetAlphabet();
    // Don't fd.stopGrowth, because someone might want to do feature induction
    ld.stopGrowth();
    // Add one feature for the "default feature".
    this.numLabels = ld.size();
    this.numFeatures = fd.size() + 1;
    this.defaultFeatureIndex = numFeatures-1;
    this.parameters = new double [numLabels * numFeatures];
    this.constraints = new double [numLabels * numFeatures];
    this.cachedGradient = new double [numLabels * numFeatures];
 
View Full Code Here

    public MaximizableTrainer (InstanceList ilist, RankMaxEnt initialClassifier)
    {
      this.trainingList = ilist;
      Alphabet fd = ilist.getDataAlphabet();
      LabelAlphabet ld = (LabelAlphabet) ilist.getTargetAlphabet();
      // Don't fd.stopGrowth, because someone might want to do feature induction
      //ld.stopGrowth();
      // Add one feature for the "default feature".
      // assume underlying Instances are binary
      //this.numLabels = underlyingLabelAlphabet.size();
View Full Code Here

    public MaximizableTrainer (InstanceList ilist, MCMaxEnt initialClassifier)
    {
      this.trainingList = ilist;
      Alphabet fd = ilist.getDataAlphabet();
      LabelAlphabet ld = (LabelAlphabet) ilist.getTargetAlphabet();
      // Don't fd.stopGrowth, because someone might want to do feature induction
      ld.stopGrowth();
      // Add one feature for the "default feature".
      this.numLabels = ld.size();
      this.numFeatures = fd.size() + 1;
      this.defaultFeatureIndex = numFeatures-1;
      this.parameters = new double [numLabels * numFeatures];
      this.constraints = new double [numLabels * numFeatures];
      this.cachedGradient = new double [numLabels * numFeatures];
 
View Full Code Here

    if (minClusterSize.value > 1) {
      for (int i = 0; i < clusterings.size(); i++) {
        Clustering clustering = clusterings.get(i);
        InstanceList oldInstances = clustering.getInstances();
        Alphabet alph = oldInstances.getDataAlphabet();
        LabelAlphabet lalph = (LabelAlphabet) oldInstances.getTargetAlphabet();
        if (alph == null) alph = new Alphabet();
        if (lalph == null) lalph = new LabelAlphabet();
        Pipe noop = new Noop(alph, lalph);
        InstanceList newInstances = new InstanceList(noop);
        for (int j = 0; j < oldInstances.size(); j++) {
          int label = clustering.getLabel(j);
          Instance instance = oldInstances.get(j);
          if (clustering.size(label) >= minClusterSize.value)
            newInstances.add(noop.pipe(new Instance(instance.getData(), lalph.lookupLabel(new Integer(label)), instance.getName(), instance.getSource())));
        }
        clusterings.set(i, createSmallerClustering(newInstances));
      }
      if (outputPrefixFile.value != null) {
        try {
          ObjectOutputStream oos =
            new ObjectOutputStream(new FileOutputStream(outputPrefixFile.value));
          oos.writeObject(clusterings);
          oos.close();
        } catch (Exception e) {
          logger.warning("Exception writing clustering to file " + outputPrefixFile.value                        + " " + e);
          e.printStackTrace();
        }
      }
    }
   
   
    // Split into training/testing
    if (trainingProportion.value > 0) {
      if (clusterings.size() > 1)
        throw new IllegalArgumentException("Expect one clustering to do train/test split, not " + clusterings.size());
      Clustering clustering = clusterings.get(0);
      int targetTrainSize = (int)(trainingProportion.value * clustering.getNumInstances());
      TIntHashSet clustersSampled = new TIntHashSet();
      Randoms random = new Randoms(123);
      LabelAlphabet lalph = new LabelAlphabet();
      InstanceList trainingInstances = new InstanceList(new Noop(null, lalph));
      while (trainingInstances.size() < targetTrainSize) {
        int cluster = random.nextInt(clustering.getNumClusters());
        if (!clustersSampled.contains(cluster)) {
          clustersSampled.add(cluster);
          InstanceList instances = clustering.getCluster(cluster);
          for (int i = 0; i < instances.size(); i++) {
            Instance inst = instances.get(i);
            trainingInstances.add(new Instance(inst.getData(), lalph.lookupLabel(new Integer(cluster)), inst.getName(), inst.getSource()));
          }
        }
      }
      trainingInstances.shuffle(random);
      Clustering trainingClustering = createSmallerClustering(trainingInstances);
     
      InstanceList testingInstances = new InstanceList(null, lalph);
      for (int i = 0; i < clustering.getNumClusters(); i++) {
        if (!clustersSampled.contains(i)) {
          InstanceList instances = clustering.getCluster(i);
          for (int j = 0; j < instances.size(); j++) {
            Instance inst = instances.get(j);
            testingInstances.add(new Instance(inst.getData(), lalph.lookupLabel(new Integer(i)), inst.getName(), inst.getSource()));
          }         
        }
      }
      testingInstances.shuffle(random);
      Clustering testingClustering = createSmallerClustering(testingInstances);
View Full Code Here

    super(dataAlphabet, labelAlphabet);
  }
 
  public Target2Label ()
  {
    this(null, new LabelAlphabet());
  }
View Full Code Here

TOP

Related Classes of cc.mallet.types.LabelAlphabet

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.