List of usage examples for org.apache.commons.math3.linear RealVector getL1Norm
public double getL1Norm()
From source file:com.opengamma.strata.math.impl.matrix.CommonsMatrixAlgebra.java
@Override public double getNorm1(Matrix m) { ArgChecker.notNull(m, "m"); if (m instanceof DoubleArray) { RealVector temp = CommonsMathWrapper.wrap((DoubleArray) m); return temp.getL1Norm(); } else if (m instanceof DoubleMatrix) { RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix) m); // TODO find if commons implements this anywhere, so we are not doing it // by hand double max = 0.0; for (int col = temp.getColumnDimension() - 1; col >= 0; col--) { max = Math.max(max, temp.getColumnVector(col).getL1Norm()); }/*from w ww .ja va2s.c om*/ return max; } throw new IllegalArgumentException("Can only find norm1 of DoubleMatrix; have " + m.getClass()); }
From source file:edu.cuhk.hccl.cmd.CosineDocumentSimilarity.java
private RealVector toRealVector(Map<String, Integer> map) { RealVector vector = new ArrayRealVector(terms.size()); int i = 0;/* www .j a v a2s . c om*/ for (String term : terms) { int value = map.containsKey(term) ? map.get(term) : 0; vector.setEntry(i++, value); } return (RealVector) vector.mapDivide(vector.getL1Norm()); }
From source file:lucene.CosineDocumentSimilarity.java
RealVector toRealVector(Map<String, Integer> map) { RealVector vector = new ArrayRealVector(terms.size()); int i = 0;/* www.ja v a 2 s. c om*/ for (String term : terms) { int value = map.containsKey(term) ? map.get(term) : 0; vector.setEntry(i++, value); } return (RealVector) vector.mapDivide(vector.getL1Norm()); }
From source file:edu.stanford.cfuller.colocalization3d.fitting.P3DFitter.java
/** * Fits the distances between the two channels of a set of objects to a p3d distribution. * /*from w ww.ja v a 2 s. c om*/ * @param objects the ImageObjects whose distances will be fit * @param diffs a RealVector containing the scalar distances between the channels of the ImageObjects, in the same order. * * @return a RealVector containing the parameters for the distribution fit: first the mean parameter, second the standard deviation parameter */ public RealVector fit(List<ImageObject> objects, RealVector diffs) { P3dObjectiveFunction of = new P3dObjectiveFunction(); of.setR(diffs); final double tol = 1e-12; NelderMeadMinimizer nmm = new NelderMeadMinimizer(tol); double initialMean = diffs.getL1Norm() / diffs.getDimension(); double initialWidth = diffs.mapSubtract(initialMean) .map(new org.apache.commons.math3.analysis.function.Power(2)).getL1Norm() / diffs.getDimension(); initialWidth = Math.sqrt(initialWidth); RealVector startingPoint = new ArrayRealVector(2, 0.0); startingPoint.setEntry(0, initialMean); startingPoint.setEntry(1, initialWidth); RealVector parametersMin = null; if (this.parameters.hasKey(ROBUST_P3D_FIT_PARAM)) { double cutoff = this.parameters.getDoubleValueForKey(ROBUST_P3D_FIT_PARAM); of.setMinProb(cutoff); } return nmm.optimize(of, startingPoint); }
From source file:edu.stanford.cfuller.colocalization3d.correction.PositionCorrector.java
/** * Applies an existing correction to the positions of a set of objects, using a specified reference * and correction channel./* www .ja v a2 s . c o m*/ * * @param imageObjects A Vector containing all the ImageObjects to be corrected. * @param c The Correction object to be used, which could have been generated with determineCorrection, or loaded from disk. * @return A RealVector with one entry per ImageObject, containing the corrected scalar distance between the object in the reference channel and the channel being corrected. */ public RealVector applyCorrection(Correction c, java.util.List<ImageObject> imageObjects) { int referenceChannel = this.parameters.getIntValueForKey(REF_CH_PARAM); int channelToCorrect = this.parameters.getIntValueForKey(CORR_CH_PARAM); RealVector diffs = new ArrayRealVector(imageObjects.size(), 0.0); RealVector averageVectorDiffs = null; for (int i = 0; i < imageObjects.size(); i++) { diffs.setEntry(i, imageObjects.get(i).getScalarDifferenceBetweenChannels(referenceChannel, channelToCorrect, this.pixelToDistanceConversions)); if (i == 0) { averageVectorDiffs = imageObjects.get(i) .getVectorDifferenceBetweenChannels(referenceChannel, channelToCorrect).copy(); } else { averageVectorDiffs = averageVectorDiffs.add(imageObjects.get(i) .getVectorDifferenceBetweenChannels(referenceChannel, channelToCorrect).map(new Abs())); } } averageVectorDiffs.mapDivideToSelf(imageObjects.size()); averageVectorDiffs = averageVectorDiffs.ebeMultiply(this.pixelToDistanceConversions); java.util.logging.Logger.getLogger("edu.stanford.cfuller.colocalization3d") .info("mean separation uncorrected = " + diffs.getL1Norm() / diffs.getDimension()); java.util.logging.Logger.getLogger("edu.stanford.cfuller.colocalization3d") .info("mean separation components uncorrected = " + averageVectorDiffs.toString()); RealVector newDiffs = new ArrayRealVector(imageObjects.size(), 0.0); if (this.parameters.getBooleanValueForKey("correct_images")) { for (int i = 0; i < imageObjects.size(); i++) { try { newDiffs.setEntry(i, this.correctSingleObjectVectorDifference(c, imageObjects.get(i), referenceChannel, channelToCorrect).getNorm()); imageObjects.get(i).setCorrectionSuccessful(true); } catch (UnableToCorrectException e) { newDiffs.setEntry(i, -1.0 * Double.MAX_VALUE); imageObjects.get(i).setCorrectionSuccessful(false); } } java.util.logging.Logger.getLogger("edu.stanford.cfuller.colocalization3d") .info("mean separation corrected = " + newDiffs.getL1Norm() / newDiffs.getDimension()); } else { newDiffs = diffs; } return newDiffs; }
From source file:edu.stanford.cfuller.colocalization3d.correction.PositionCorrector.java
/** * Determines the target registration error for a correction by successively leaving out each ImageObject in a set used to make a correction, * calculating a correction from the remaining objects, and assessing the error in correcting the object left out. * // ww w . j ava2 s. co m * @param imageObjects A Vector containing all the ImageObjects to be used for the correction * or in the order it appears in a multiwavelength image file. * @return The average value of the error over all objects. */ public double determineTRE(java.util.List<ImageObject> imageObjects) { int referenceChannel = this.parameters.getIntValueForKey(REF_CH_PARAM); int channelToCorrect = this.parameters.getIntValueForKey(CORR_CH_PARAM); RealVector treVector = new ArrayRealVector(imageObjects.size(), 0.0); RealVector treXYVector = new ArrayRealVector(imageObjects.size(), 0.0); java.util.Deque<TREThread> startedThreads = new java.util.LinkedList<TREThread>(); int maxThreads = 1; if (this.parameters.hasKey(THREAD_COUNT_PARAM)) { maxThreads = this.parameters.getIntValueForKey(THREAD_COUNT_PARAM); } final int threadWaitTime_ms = 1000; for (int removeIndex = 0; removeIndex < imageObjects.size(); removeIndex++) { if (removeIndex % 10 == 0) { java.util.logging.Logger .getLogger(edu.stanford.cfuller.colocalization3d.Colocalization3DMain.LOGGER_NAME) .finer("calulating TRE: point " + (removeIndex + 1) + " of " + imageObjects.size()); } TREThread nextFit = new TREThread(imageObjects, referenceChannel, channelToCorrect, removeIndex, this); if (startedThreads.size() < maxThreads) { startedThreads.add(nextFit); nextFit.start(); } else { TREThread next = startedThreads.poll(); try { next.join(threadWaitTime_ms); } catch (InterruptedException e) { e.printStackTrace(); } while (next.isAlive()) { startedThreads.add(next); next = startedThreads.poll(); try { next.join(threadWaitTime_ms); } catch (InterruptedException e) { e.printStackTrace(); } } treVector.setEntry(next.getRemoveIndex(), next.getTre()); treXYVector.setEntry(next.getRemoveIndex(), next.getTreXY()); startedThreads.add(nextFit); nextFit.start(); } } java.util.List<Integer> unsuccessful_TRE = new java.util.ArrayList<Integer>(); while (startedThreads.size() > 0) { TREThread next = startedThreads.poll(); try { next.join(); if (next.getSuccess()) { treVector.setEntry(next.getRemoveIndex(), next.getTre()); } else { unsuccessful_TRE.add(next.getRemoveIndex()); } } catch (InterruptedException e) { e.printStackTrace(); } } RealVector treVector_mod = new ArrayRealVector(treVector.getDimension() - unsuccessful_TRE.size()); RealVector treXYVector_mod = new ArrayRealVector(treVector_mod.getDimension()); int c = 0; //unsuccessful TRE calculation results when there is incomplete coverage in the correction dataset for (int i = 0; i < treVector.getDimension(); ++i) { if (!unsuccessful_TRE.contains(i)) { treVector_mod.setEntry(c, treVector.getEntry(i)); treXYVector_mod.setEntry(c, treXYVector.getEntry(i)); ++c; } } treVector = treVector_mod; treXYVector = treXYVector_mod; double tre = treVector.getL1Norm() / treVector.getDimension(); double xy_tre = (treXYVector.getL1Norm() / treXYVector.getDimension()); java.util.logging.Logger.getLogger(edu.stanford.cfuller.colocalization3d.Colocalization3DMain.LOGGER_NAME) .info("TRE: " + tre); java.util.logging.Logger.getLogger(edu.stanford.cfuller.colocalization3d.Colocalization3DMain.LOGGER_NAME) .info("x-y TRE: " + xy_tre); return tre; }
From source file:org.apache.predictionio.examples.java.recommendations.tutorial4.FeatureBasedAlgorithm.java
public FeatureBasedModel train(PreparedData data) { Map<Integer, RealVector> userFeatures = new HashMap<Integer, RealVector>(); Map<Integer, Integer> userActions = new HashMap<Integer, Integer>(); for (Integer uid : data.userInfo.keySet()) { userFeatures.put(uid, new ArrayRealVector(data.featureCount)); userActions.put(uid, 0);/*from ww w . jav a 2s.c om*/ } for (TrainingData.Rating rating : data.ratings) { final int uid = rating.uid; final int iid = rating.iid; final double rate = rating.rating; // Skip features outside the range. if (!(params.min <= rate && rate <= params.max)) continue; final double actualRate = (rate - params.drift) * params.scale; final RealVector userFeature = userFeatures.get(uid); final RealVector itemFeature = data.itemFeatures.get(iid); userFeature.combineToSelf(1, actualRate, itemFeature); userActions.put(uid, userActions.get(uid) + 1); } // Normalize userFeatures by l-inf-norm for (Integer uid : userFeatures.keySet()) { final RealVector feature = userFeatures.get(uid); feature.mapDivideToSelf(feature.getLInfNorm()); } // Normalize itemFeatures by weight Map<Integer, RealVector> itemFeatures = new HashMap<Integer, RealVector>(); for (Integer iid : data.itemFeatures.keySet()) { final RealVector feature = data.itemFeatures.get(iid); final RealVector normalizedFeature = feature.mapDivide(feature.getL1Norm()); itemFeatures.put(iid, normalizedFeature); } return new FeatureBasedModel(userFeatures, userActions, itemFeatures); }
From source file:org.grouplens.samantha.modeler.solver.L1Regularizer.java
public double getObjective(double l1coef, RealVector var) { double l1norm = var.getL1Norm(); return l1coef * l1norm; }
From source file:org.grouplens.samantha.modeler.solver.L1Regularizer.java
public double getObjective(double l1coef, List<RealVector> vars) { double objVal = 0.0; for (RealVector realVector : vars) { double l1norm = realVector.getL1Norm(); objVal += l1norm;/*from ww w. jav a 2 s . c o m*/ } return objVal * l1coef; }
From source file:org.lenskit.mf.funksvd.FunkSVDModelBuilder.java
@Override public FunkSVDModel get() { int userCount = snapshot.getUserIds().size(); RealMatrix userFeatures = MatrixUtils.createRealMatrix(userCount, featureCount); int itemCount = snapshot.getItemIds().size(); RealMatrix itemFeatures = MatrixUtils.createRealMatrix(itemCount, featureCount); logger.debug("Learning rate is {}", rule.getLearningRate()); logger.debug("Regularization term is {}", rule.getTrainingRegularization()); logger.info("Building SVD with {} features for {} ratings", featureCount, snapshot.getRatings().size()); TrainingEstimator estimates = rule.makeEstimator(snapshot); List<FeatureInfo> featureInfo = new ArrayList<>(featureCount); // Use scratch vectors for each feature for better cache locality // Per-feature vectors are strided in the output matrices RealVector uvec = MatrixUtils.createRealVector(new double[userCount]); RealVector ivec = MatrixUtils.createRealVector(new double[itemCount]); for (int f = 0; f < featureCount; f++) { logger.debug("Training feature {}", f); StopWatch timer = new StopWatch(); timer.start();/*from w w w . j a v a 2s. c o m*/ uvec.set(initialValue); ivec.set(initialValue); FeatureInfo.Builder fib = new FeatureInfo.Builder(f); trainFeature(f, estimates, uvec, ivec, fib); summarizeFeature(uvec, ivec, fib); featureInfo.add(fib.build()); // Update each rating's cached value to accommodate the feature values. estimates.update(uvec, ivec); // And store the data into the matrix userFeatures.setColumnVector(f, uvec); assert Math.abs(userFeatures.getColumnVector(f).getL1Norm() - uvec.getL1Norm()) < 1.0e-4 : "user column sum matches"; itemFeatures.setColumnVector(f, ivec); assert Math.abs(itemFeatures.getColumnVector(f).getL1Norm() - ivec.getL1Norm()) < 1.0e-4 : "item column sum matches"; timer.stop(); logger.info("Finished feature {} in {}", f, timer); } // Wrap the user/item matrices because we won't use or modify them again return new FunkSVDModel(userFeatures, itemFeatures, snapshot.userIndex(), snapshot.itemIndex(), featureInfo); }