Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package biz.k11i.xgboost.demo;
- import biz.k11i.xgboost.Predictor;
- import biz.k11i.xgboost.util.FVec;
- import java.io.*;
- import java.util.*;
- public class ModelsML {
- public static void main(String[] args) throws IOException {
- String path = "D:\\PROJECTS\\xgboost-predictor-java\\xgboost-predictor-examples\\src\\main\\resources\\biz\\k11i\\xgboost\\demo\\model\\";
- String filePath = path + "testData.txt";
- List<AbstractMap.SimpleEntry<Integer, FVec>> data = loadData(filePath);
- Predictor predictor = new Predictor(new FileInputStream(path + "modelsml.model"));
- Calendar predictTime = Calendar.getInstance();
- predict(predictor, data);
- logTimeElapsed(predictTime, true);
- }
- /**
- * Predicts probability and calculate its logarithmic loss using {@link Predictor#predict(FVec)}.
- *
- * @param predictor Predictor
- * @param data test data
- */
- static void predict(Predictor predictor, List<AbstractMap.SimpleEntry<Integer, FVec>> data) {
- for (AbstractMap.SimpleEntry<Integer, FVec> pair : data) {
- double[] predicted = predictor.predict(pair.getValue());
- double predValue = predicted[0];
- int rownum = pair.getKey();
- System.out.println("\"" + rownum + "\" " + predValue);
- }
- }
- /**
- * Loads test data.
- *
- * @return test data
- */
- static List<AbstractMap.SimpleEntry<Integer, FVec>> loadData(String filePath) throws IOException {
- List<AbstractMap.SimpleEntry<Integer, FVec>> result = new ArrayList<>();
- File f = new File(filePath);
- FileInputStream in = new FileInputStream(f);
- BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
- String line;
- int _x = 0;
- while ((line = reader.readLine()) != null) {
- if (_x++ == 0) continue;
- String[] values = line.split(" ");
- int featureLength = values.length - 1;
- float[] data = new float[featureLength];
- for (int i = 1; i < values.length; i++) {
- data[i - 1] = Float.valueOf(values[i]);
- }
- result.add(new AbstractMap.SimpleEntry<>(Integer.parseInt(values[0].substring(1, values[0].length()-1)), FVec.Transformer.fromArray(data, false)));
- }
- return result;
- }
- private static void logTimeElapsed(Calendar startTime, boolean logTime) {
- if (!logTime) return;
- Calendar cal = Calendar.getInstance();
- System.out.println(cal.getTimeInMillis() + " - " + startTime.getTimeInMillis() + " : Time spent --> " + (cal.getTimeInMillis() - startTime.getTimeInMillis()) + " ms");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement