Advertisement
brajabp

Untitled

Sep 19th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. package biz.k11i.xgboost.demo;
  2.  
  3. import biz.k11i.xgboost.Predictor;
  4. import biz.k11i.xgboost.util.FVec;
  5.  
  6. import java.io.*;
  7. import java.util.*;
  8.  
  9. public class ModelsML {
  10.     public static void main(String[] args) throws IOException {
  11.         String path = "D:\\PROJECTS\\xgboost-predictor-java\\xgboost-predictor-examples\\src\\main\\resources\\biz\\k11i\\xgboost\\demo\\model\\";
  12.         String filePath = path + "testData.txt";
  13.  
  14.         List<AbstractMap.SimpleEntry<Integer, FVec>> data = loadData(filePath);
  15.         Predictor predictor = new Predictor(new FileInputStream(path + "modelsml.model"));
  16.  
  17.         Calendar predictTime = Calendar.getInstance();
  18.         predict(predictor, data);
  19.         logTimeElapsed(predictTime, true);
  20.     }
  21.  
  22.     /**
  23.      * Predicts probability and calculate its logarithmic loss using {@link Predictor#predict(FVec)}.
  24.      *
  25.      * @param predictor Predictor
  26.      * @param data      test data
  27.      */
  28.     static void predict(Predictor predictor, List<AbstractMap.SimpleEntry<Integer, FVec>> data) {
  29.         for (AbstractMap.SimpleEntry<Integer, FVec> pair : data) {
  30.  
  31.             double[] predicted = predictor.predict(pair.getValue());
  32.  
  33.             double predValue = predicted[0];
  34.             int rownum = pair.getKey();
  35.  
  36.             System.out.println("\"" + rownum + "\" " + predValue);
  37.         }
  38.     }
  39.  
  40.     /**
  41.      * Loads test data.
  42.      *
  43.      * @return test data
  44.      */
  45.     static List<AbstractMap.SimpleEntry<Integer, FVec>> loadData(String filePath) throws IOException {
  46.         List<AbstractMap.SimpleEntry<Integer, FVec>> result = new ArrayList<>();
  47.  
  48.         File f = new File(filePath);
  49.         FileInputStream in = new FileInputStream(f);
  50.         BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
  51.  
  52.         String line;
  53.         int _x = 0;
  54.         while ((line = reader.readLine()) != null) {
  55.             if (_x++ == 0) continue;
  56.             String[] values = line.split(" ");
  57.  
  58.             int featureLength = values.length - 1;
  59.             float[] data = new float[featureLength];
  60.  
  61.             for (int i = 1; i < values.length; i++) {
  62.                 data[i - 1] = Float.valueOf(values[i]);
  63.             }
  64.  
  65.             result.add(new AbstractMap.SimpleEntry<>(Integer.parseInt(values[0].substring(1, values[0].length()-1)), FVec.Transformer.fromArray(data, false)));
  66.         }
  67.  
  68.         return result;
  69.     }
  70.  
  71.     private static void logTimeElapsed(Calendar startTime, boolean logTime) {
  72.         if (!logTime) return;
  73.  
  74.         Calendar cal = Calendar.getInstance();
  75.         System.out.println(cal.getTimeInMillis() + " - " + startTime.getTimeInMillis() + " : Time spent --> " + (cal.getTimeInMillis() - startTime.getTimeInMillis()) + " ms");
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement