Package org.fnlp.ml.types

Examples of org.fnlp.ml.types.Instance


    System.out.println("Testing Knn...\n");
    int count=0;
    fs.fS_CS(tf, 0.1f);
    knn.setFs(fs);
    for(int i=0;i<testset.size();i++){
      Instance data = testset.getInstance(i);
      Integer gold = (Integer) data.getTarget();
      Predict<String> pres=(Predict<String>) knn.classify(data, Type.STRING, 3);
      String pred_label=pres.getLabel();
      String gold_label = knn.getLabel(gold);
     
      if(pred_label.equals(gold_label)){
        //System.out.println(pred_label+" : "+testsetknn.getInstance(i).getTempData());
        count++;
      }
      else{
//        System.err.println(gold_label+"->"+pred_label+" : "+testset.getInstance(i).getTempData());
//        for(int j=0;j<3;j++)
//          System.out.println(pres.getLabel(j)+":"+pres.getScore(j));
      }
    }
    int knnCount=count;
    System.out.println("..Testing Knn Complete");
    System.out.println("Knn Precision:"+((float)knnCount/testset.size())+"("+knnCount+"/"+testset.size()+")");
    knn.noFeatureSelection();
    int flag=0;
    long time_sum=0,time_times=0;
    float[] percents_cs=new float[]{1.0f,0.9f,0.8f,0.7f,0.5f,0.3f,0.2f,0.1f};
    int[] counts_cs=new int[10];
    for(int test=0;test<percents_cs.length;test++){
      long time_st=System.currentTimeMillis();
      System.out.println("Testing Bayes"+percents_cs[test]+"...");
      if(test!=0){
        fs.fS_CS(tf, percents_cs[test]);
        knn.setFs(fs);
      }
      count=0;
      for(int i=0;i<testset.size();i++){
        Instance data = testset.getInstance(i);
        Integer gold = (Integer) data.getTarget();
        Predict<String> pres=(Predict<String>)knn.classify(data, Type.STRING, 3);
        String pred_label=pres.getLabel();
        String gold_label = knn.getLabel(gold);
       
        if(pred_label.equals(gold_label)){
          count++;
        }
        else{
        }
      }
      counts_cs[test]=count;
      long time_ed=System.currentTimeMillis();
      time_sum+=time_ed-time_st;
      time_times++;
      System.out.println("Knn Precision("+percents_cs[test]+"):"
      +((float)count/testset.size())+"("+count+"/"+testset.size()+")"+"  "+(time_ed-time_st)+"ms");
    }
   
    knn.noFeatureSelection();
    float[] percents_csmax=new float[]{1.0f,0.9f,0.8f,0.7f,0.5f,0.3f,0.2f,0.1f};
    int[] counts_csmax=new int[10];
    for(int test=0;test<percents_csmax.length;test++){
      long time_st=System.currentTimeMillis();
      System.out.println("Testing Bayes"+percents_csmax[test]+"...");
      if(test!=0){
        fs.fS_CS_Max(tf, percents_cs[test]);
        knn.setFs(fs);
      }
      count=0;
      for(int i=0;i<testset.size();i++){
        Instance data = testset.getInstance(i);
        Integer gold = (Integer) data.getTarget();
        Predict<String> pres=(Predict<String>)knn.classify(data, Type.STRING, 3);
        String pred_label=pres.getLabel();
        String gold_label = knn.getLabel(gold);
       
        if(pred_label.equals(gold_label)){
          count++;
        }
        else{
        }
      }
      counts_csmax[test]=count;
      long time_ed=System.currentTimeMillis();
      time_sum+=time_ed-time_st;
      time_times++;
      System.out.println("Knn Precision("+percents_csmax[test]+"):"
      +((float)count/testset.size())+"("+count+"/"+testset.size()+")"+"  "+(time_ed-time_st)+"ms");
    }
    knn.noFeatureSelection();
    float[] percents_ig=new float[]{1.0f,0.9f,0.8f,0.7f,0.5f,0.3f,0.2f,0.1f};
    int[] counts_ig=new int[10];
    for(int test=0;test<percents_ig.length;test++){
      long time_st=System.currentTimeMillis();
      System.out.println("Testing Bayes"+percents_ig[test]+"...");
      if(test!=0){
        fs.fS_IG(tf, percents_cs[test]);
        knn.setFs(fs);
      }
      count=0;
      for(int i=0;i<testset.size();i++){
        Instance data = testset.getInstance(i);
        Integer gold = (Integer) data.getTarget();
        Predict<String> pres=(Predict<String>)knn.classify(data, Type.STRING, 3);
        String pred_label=pres.getLabel();
        String gold_label = knn.getLabel(gold);
       
        if(pred_label.equals(gold_label)){
View Full Code Here


    this.k = k;
    int count1 =0,count2=0;
    int total = prototypes.size();
    System.out.println("实例数量:"+total);
    for(int i=0;i<total;i++){
      Instance inst = prototypes.get(i);
      TPredict pred = classify(inst, 1);
      if(pred.getLabel(0).equals(inst.getTarget()))
        count1++;
      prototypes.remove(i);
      TPredict pred2 = classify(inst, 1);
      if(pred2.getLabel(0).equals(inst.getTarget()))
        count2++;
      prototypes.add(i, inst);
    }
    System.out.println("Leave-zero-out正确率:"+count1*1.0f/total);
    System.out.println("Leave-one-out正确率:"+count2*1.0f/total);
 
View Full Code Here

   */
  public TPredict classify(Instance instance, int n){
    LinkedPredict<String>  pred = new LinkedPredict<String>(k);

    for(int i = 0; i < prototypes.size(); i++){
      Instance curInst = prototypes.get(i);
//      if(((String) curInst.getSource()).contains("听#per#的歌"))
//        System.out.println("");
      float score;
      try {
        score = sim.calc(instance.getData(), curInst.getData());
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
      }
      pred.add((String) curInst.getTarget(), score,(String) curInst.getSource());
    }
    //排序
    LinkedPredict<String> newpred = pred.mergeDuplicate(useScore );
    newpred.assertSize(n);
    return newpred;
View Full Code Here

        int[] data = new int[tokens.size() - 2];
        for (int i = 0; i < data.length; i++) {
          data[i] = Integer.parseInt(tokens.get(i + 2));
        }
        Instance inst = new Instance(data);
        inst.setTarget(labelAlphabet.lookupIndex(tokens.get(1)));

        instset.add(inst);
      }
    }
View Full Code Here

          break;
        default:
          label = "S";         
        }
        int id = la.lookupIndex(label);       
        Instance inst = new Instance();
        inst.setTarget(id);
        int[] idx = JointParser.addFeature(fa, features, ysize);
        inst.setData(idx);
        instset.add(inst);
      }
      count++;
//      System.out.println(count);
    }
View Full Code Here

//      if(file.toString().contains("0030")){
//        System.out.println(file.toString());
//      }
      TreeReaderIterator ite = new TreeReaderIterator(file, charset);
      while (ite.hasNext())
        dataSet.add(new Instance(ite.next()));
    }
    return dataSet;
  }
View Full Code Here

      while (ite.hasNext())  {
        System.out.print(".");
        Tree<Node> inst = ite.next();
        List<Tree<Node>> newTreeList = getNewTree(inst);
        for(int i=0;i<newTreeList.size();i++){
          dataSet.add(new Instance(newTreeList.get(i)));
        }
      }
      System.out.print("\n");
    }
    return dataSet;
View Full Code Here

    List<File> fileList = findFiles(path, from, to, suffix);
    InstanceSet dataSet = new InstanceSet();
    for (File file : fileList) {
      TreeReaderIterator ite = new TreeReaderIterator(file, charset);
      while (ite.hasNext())  {
        dataSet.add(new Instance(ite.next()));
      }
    }
    return dataSet;
  }
View Full Code Here

  }

  private static String test(String line) throws Exception{
//    System.out.println(line);

    Instance inst = new Instance(line);
    seg.doProcess(inst);


    Loss loss = new HammingLoss();
View Full Code Here

   *         0->SHIFT
   */
  private Predict<String> estimateActions(JointParsingState state) {
    // 当前状态的特征
    ArrayList<String> features = state.getFeatures();
    Instance inst = new Instance(addFeature(fa, features, ysize));

    Predict<Integer> ret = models.classify(inst,ysize);
    ret.normalize();
    Predict<String> result =new Predict<String>(2);
    float total = 0;
View Full Code Here

TOP

Related Classes of org.fnlp.ml.types.Instance

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.