/** * Vector deserialization. */ public static RealVector unmarshall(byte[] bytes, boolean sparse, int dimensions) throws IOException { RealVector realVector = !sparse ? new ArrayRealVector(dimensions) : new OpenMapRealVector(dimensions); if (!sparse) { try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes))) { for (int i = 0; i < dimensions; i++) { realVector.setEntry(i, dis.readDouble()); } } } else { try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes))) { while (true) { try { realVector.setEntry(dis.readInt(), dis.readDouble()); } catch (EOFException e) { break; } } } } return realVector; }
public void getTopK(int testIndex, int numPred, ArrayList<String> predictions, ArrayList<Double> confidenceScores) { int size = docs[0].vector.getDimension(); SparseRealVector onesVector = new OpenMapRealVector(size); for(int i=0; i<size; i++) { onesVector.setEntry(i, 1); } List<Prediction> sortedPredictions = new ArrayList<Prediction>(); // descending order of jaccard similarity for(int i=0; i<info.noTrainingDocs; i++) { double jacSim = getJaccardSimilarity(docs[i], docs[info.noTrainingDocs + testIndex], onesVector); Prediction pred = new Prediction(new Integer(i).toString(), jacSim); if(jacSim > 0.0) { sortedPredictions.add(pred); } //System.out.println(i+" "+jacSim); } Collections.sort(sortedPredictions, new PredictionComparator()); //System.out.println("no of predictions = "+sortedPredictions.size()); for(int j=0; j<numPred && j<sortedPredictions.size(); j++) { predictions.add(sortedPredictions.get(j).predictionIndex); confidenceScores.add(sortedPredictions.get(j).confidenceScore); } }
public DocVector(Map<String,Integer> terms) { this.terms = terms; this.vector = new OpenMapRealVector(terms.size()); }
@SuppressWarnings("deprecation") public DocVector(Map<String,Integer> terms) { this.terms = terms; this.vector = new OpenMapRealVector(terms.size()); }