MCPcopy Create free account
hub / github.com/WinVector/Logistic / MapReduceScore

Class MapReduceScore

src/com/winvector/logistic/demo/MapReduceScore.java:29–102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27
28/// @SuppressWarnings("deprecation") // Tool depreciated 0.21.0 a known issue: https://issues.apache.org/jira/browse/MAPREDUCE-2084
29public class MapReduceScore extends Configured implements Tool {
30
31 @Override
32 public int run(final String[] args) throws Exception {
33 if(args.length!=3) {
34 final Log log = LogFactory.getLog(MapReduceScore.class);
35 log.info(Licenses.licenses);
36 log.fatal("use: MapReduceScore model.ser testFile resultDir");
37 return -1;
38 }
39 final String modelFileName = args[0];
40 final String testFileName = args[1];
41 final String resultFileName = args[2];
42 run(modelFileName,testFileName,resultFileName);
43 return 0;
44 }
45
46 public double run(final String modelFileName, final String testFileName, final String resultFileName) throws Exception {
47 final Log log = LogFactory.getLog(MapReduceScore.class);
48 final Random rand = new Random();
49 final String tmpPrefix = "TMPAC_" + rand.nextLong();
50 final Configuration mrConfig = getConf();
51 log.info("start");
52 log.info("reading model: " + modelFileName);
53 final Model model;
54 {
55 final Path modelPath = new Path(modelFileName);
56 final FSDataInputStream fdi = modelPath.getFileSystem(mrConfig).open(modelPath);
57 final ObjectInputStream ois = new ObjectInputStream(fdi);
58 model = (Model)ois.readObject();
59 ois.close();
60 }
61 log.info("model:\n" + model.config.formatSoln(model.coefs));
62 final Path testFile = new Path(testFileName);
63 final Path resultFile = new Path(resultFileName);
64 log.info("scoring data: " + testFile);
65 log.info("writing: " + resultFile);
66 final SigmoidLossMultinomial underlying = new SigmoidLossMultinomial(model.config.dim(),model.config.noutcomes());
67 final WritableVariableList lConfig = WritableVariableList.copy(model.config.def());
68 final String headerLine = WritableUtils.readFirstLine(mrConfig,testFile);
69 final Pattern sepPattern = Pattern.compile("\t");
70 final LineBurster burster = new HBurster(sepPattern,headerLine,false);
71 mrConfig.set(MapRedScan.BURSTERSERFIELD,SerialUtils.serializableToString(burster));
72 final StringBuilder b = new StringBuilder();
73 b.append("predict" + "." + model.config.def().resultColumn + "\t");
74 b.append("predict" + "." + model.config.def().resultColumn + "." + "score" + "\t");
75 for(int i=0;i<model.config.noutcomes();++i) {
76 final String cat = model.config.outcome(i);
77 b.append("predict" + "." + model.config.def().resultColumn + "." + cat + "." + "score" + "\t");
78 }
79 b.append(headerLine);
80 mrConfig.set(MapRedScore.IDEALHEADERFIELD,b.toString());
81 final MapRedScore sc = new MapRedScore(underlying,lConfig,model.config.useIntercept(),mrConfig,testFile);
82 sc.score(model.coefs,resultFile);
83 final MapRedAccuracy ac = new MapRedAccuracy(underlying,lConfig,model.config.useIntercept(),tmpPrefix,mrConfig,testFile);
84 final long[] testAccuracy = ac.score(model.coefs);
85 final double accuracy = testAccuracy[0]/(double)testAccuracy[1];
86 log.info("test accuracy: " + testAccuracy[0] + "/" + testAccuracy[1] + "\t" + accuracy);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected