本文整理匯總了Java中org.apache.commons.math3.linear.RealVector類的典型用法代碼示例。如果您正苦於以下問題:Java RealVector類的具體用法?Java RealVector怎麽用?Java RealVector使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
RealVector類屬於org.apache.commons.math3.linear包,在下文中一共展示了RealVector類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: centerKernelMatrix
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
private static RealMatrix
centerKernelMatrix(final RealMatrix kernelMatrix) {
// get size of kernelMatrix
final int n = kernelMatrix.getRowDimension();
// get mean values for each row/column
final RealVector columnMeans =
MatrixFunctions.columnMeans(kernelMatrix);
final double matrixMean = MatrixFunctions.mean(kernelMatrix);
RealMatrix centeredKernelMatrix = kernelMatrix.copy();
for (int k = 0; k < n; k++) {
centeredKernelMatrix.setRowVector(k,
centeredKernelMatrix.getRowVector(k).subtract(columnMeans));
centeredKernelMatrix.setColumnVector(k, centeredKernelMatrix
.getColumnVector(k).subtract(columnMeans));
}
centeredKernelMatrix = centeredKernelMatrix.scalarAdd(matrixMean);
return centeredKernelMatrix;
}
示例2: score
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
private NoveltyScores score(final RealMatrix kernelMatrix) {
// projected test samples:
final RealMatrix projectionVectors =
kernelMatrix.transpose().multiply(m_projection);
// differences to the target value:
final RealMatrix diff = projectionVectors.subtract(
MatrixFunctions.ones(kernelMatrix.getColumnDimension(), 1)
.scalarMultiply(m_targetPoints.getEntry(0, 0)));
// distances to the target value:
final RealVector scoresVector = MatrixFunctions.sqrt(MatrixFunctions
.rowSums(MatrixFunctions.multiplyElementWise(diff, diff)));
return new NoveltyScores(scoresVector.toArray(), projectionVectors);
}
示例3: unmarshall
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
private RealVector unmarshall(Document doc, int limit) {
if (!metadata.isBinary()) {
throw new UnsupportedOperationException("Can't consume non-binary models.");
}
try {
final Binary binary = doc.get(VECTOR_FIELD_NAME, Binary.class);
final byte[] b = binary.getData();
if (metadata.getLoaderId().equalsIgnoreCase("legacy")) {
return BinaryCodecs.legacyUnmarshall(b, limit, metadata.isSparse(), metadata.getDimensions());
}
else {
return BinaryCodecs.unmarshall(b, metadata.isSparse(), metadata.getDimensions());
}
}
catch (Exception e) {
logger.error("Error unmarshalling vector", e);
}
return null;
}
示例4: sim
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
@Override
public double sim(RealVector r1, RealVector r2, boolean sparse) {
if (r1.getDimension() != r2.getDimension()) {
return 0;
}
double min = 0.0;
double sum = 0.0;
for (int i = 0; i < r1.getDimension(); ++i) {
if (r1.getEntry(i) > r2.getEntry(i)) {
min += r2.getEntry(i);
} else {
min += r1.getEntry(i);
}
sum += r1.getEntry(i) + r2.getEntry(i);
}
if (sum == 0) {
return 0;
}
double result = 2 * min / sum;
return Math.abs(result);
}
示例5: sim
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
@Override
public double sim(RealVector r1, RealVector r2, boolean sparse) {
if (r1.getDimension() != r2.getDimension()) {
return 0;
}
double max = 0;
double tmp;
for (int i = 0; i < r1.getDimension(); ++i) {
tmp = Math.abs((r1.getEntry(i) - r2.getEntry(i)));
max = (tmp > max ? tmp : max);
}
double result = 1 / (1 + (max == Double.NaN ? 0 : max));
return Math.abs(result);
}
示例6: sim
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
@Override
public double sim(RealVector r1, RealVector r2, boolean sparse) {
if (r1.getDimension() != r2.getDimension()) {
return 0;
}
double alpha = 0.99;
double divergence = 0.0;
for (int i = 0; i < r1.getDimension(); ++i) {
if (r1.getEntry(i) > 0.0 && r2.getEntry(i) > 0.0) {
divergence += r1.getEntry(i) * Math.log(r1.getEntry(i) / ((1 - alpha) * r1.getEntry(i) + alpha * r2.getEntry(i)));
}
}
double result = (1 - (divergence / Math.sqrt(2 * Math.log(2))));
return Math.abs(result);
}
示例7: sim
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
@Override
public double sim(RealVector r1, RealVector r2, boolean sparse) {
if (r1.getDimension() != r2.getDimension()) {
return 0;
}
double min = 0.0;
double max = 0.0;
for (int i = 0; i <r1.getDimension(); ++i) {
if (r1.getEntry(i) > r2.getEntry(i)) {
min +=r2.getEntry(i);
max += r1.getEntry(i);
} else {
min += r1.getEntry(i);
max += r2.getEntry(i);
}
}
if (max == 0) {
return 0;
}
return Math.abs(min / max);
}
示例8: unmarshall
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
/**
* 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;
}
示例9: getRelatedness
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
public Map<String, Double> getRelatedness(String one, List<String> many, boolean translated) {
List<? extends AnalyzedTerm> analyzedTerms = doAnalyze(one, many);
Map<String, RealVector> vectors;
if(translated) {
vectors = vectorSpace.getTranslatedVectors((List<MutableTranslatedTerm>) analyzedTerms);
} else {
vectors = vectorSpace.getVectors((List<AnalyzedTerm>) analyzedTerms);
}
Map<String, Double> results = new LinkedHashMap<>();
RealVector oneVector = vectors.get(one);
for (String m : many) {
RealVector mVector = vectors.get(m);
if (oneVector != null && mVector != null) {
double score = func.sim(oneVector, mVector, vectorSpace.getMetadata().isSparse());
results.put(m, score);
} else {
results.put(m, 0d);
}
}
return results;
}
示例10: getVectors
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
@Override
public Map<String, RealVector> getVectors(List<AnalyzedTerm> terms) {
if (terms == null) {
throw new IllegalArgumentException("terms can't be null");
}
Set<String> allTerms = new HashSet<>();
terms.forEach(t -> allTerms.addAll(t.getAnalyzedTokens()));
collectVectors(allTerms, getMetadata().getDimensions());
Map<String, RealVector> vectors = new HashMap<>();
for (AnalyzedTerm term : terms) {
RealVector vector = composeVectors(term.getAnalyzedTokens(), getTermComposer());
vectors.put(term.getTerm(), vector);
}
return vectors;
}
示例11: compose
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
@Override
public RealVector compose(List<RealVector> vectors) {
logger.trace("Composing {} vectors", vectors.size());
if (vectors.isEmpty()) {
return null;
} else if (vectors.size() == 1) {
return vectors.get(0);
} else {
RealVector sum = vectors.get(0).add(vectors.get(1));
for (int i = 2; i < vectors.size(); i++) {
sum = sum.add(vectors.get(i));
}
return sum;
}
}
示例12: getComposedTranslatedVectorsTest
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
@Test
public void getComposedTranslatedVectorsTest() {
IndraAnalyzer ptAnalyzer = new IndraAnalyzer("PT", ModelMetadata.createTranslationVersion(vectorSpace.getMetadata()));
List<String> terms = Arrays.asList("mãe computador", "pai avaliação");
List<MutableTranslatedTerm> analyzedTerms = terms.stream().map(t -> new MutableTranslatedTerm(t,
ptAnalyzer.analyze(t))).collect(Collectors.toList());
analyzedTerms.get(0).putAnalyzedTranslatedTokens("mãe", Arrays.asList("mother", "mom", "matriarch"));
analyzedTerms.get(0).putAnalyzedTranslatedTokens("computador", Arrays.asList("machine", "computer"));
analyzedTerms.get(1).putAnalyzedTranslatedTokens("pai", Arrays.asList("father", "dad", "patriarch"));
analyzedTerms.get(1).putAnalyzedTranslatedTokens("avaliação", Arrays.asList("test", "evaluation"));
Map<String, RealVector> vectorPairs = vectorSpace.getTranslatedVectors(analyzedTerms);
Assert.assertEquals(vectorPairs.size(), 2);
Assert.assertEquals(vectorPairs.get(terms.get(0)), MockCachedVectorSpace.TWO_VECTOR);
Assert.assertEquals(vectorPairs.get(terms.get(1)), MockCachedVectorSpace.NEGATIVE_TWO_VECTOR);
}
示例13: MixModel
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
public MixModel(MethyModel tumor, MethyModel normal, RealVector thetas, int nBetas, int MYTHREADS) throws InterruptedException {
int nFeatures=tumor.getNaRatio().getDimension();
this.nBetas = nBetas;
RealVector betas = new ArrayRealVector(nBetas);
for (int i=0; i<nBetas; i++) {
betas.setEntry(i,i/(nBetas-1.0));
}
mixDens = new RealMatrix[nFeatures];
ExecutorService executor = Executors.newFixedThreadPool(MYTHREADS);
for(int i = 0; i < nFeatures; i++) {
double tumorAlpha = tumor.getAlpha().getEntry(i);
double tumorBeta = tumor.getBeta().getEntry(i);
BetaDistribution tumorDist = new BetaDistribution(tumorAlpha,tumorBeta);
double normalAlpha = normal.getAlpha().getEntry(i);
double normalBeta = normal.getBeta().getEntry(i);
BetaDistribution normalDist = new BetaDistribution(normalAlpha,normalBeta);
Runnable worker = new CalMixDens(tumorDist,normalDist,thetas,betas,nPoints,i,mixDens);
executor.execute(worker);
}
executor.shutdown();
while (!executor.isTerminated()) {
Thread.sleep(10000);
}
}
示例14: makeDataMatrix
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
private RealMatrix makeDataMatrix(List<double[]> X, double[] meanX) {
if (meanX == null) {
return makeDataMatrix(X);
}
final int m = X.size();
final int n = X.get(0).length;
RealMatrix M = MatrixUtils.createRealMatrix(n, m);
RealVector mean = MatrixUtils.createRealVector(meanX);
int i = 0;
for (double[] x : X) {
RealVector xi = MatrixUtils.createRealVector(x).subtract(mean);
M.setColumnVector(i, xi);
i++;
}
return M;
}
示例15: compute
import org.apache.commons.math3.linear.RealVector; //導入依賴的package包/類
/**
* Runs the regression model for the given dependent and independent variables
* The Y and X variables must be transformed, if necessary, to meet Gauss Markov assumptions
* @param y the dependent variable, which may be a transformed version of the raw data
* @param x the independent variable(s), which may be a transformed version of the raw data
*/
protected void compute(RealVector y, RealMatrix x) {
final int n = frame.rows().count();
final int p = regressors.size() + (hasIntercept() ? 1 : 0);
final int dfModel = regressors.size();
final RealMatrix betaMatrix = computeBeta(y, x);
final RealVector betaCoefficients = betaMatrix.getColumnVector(0);
final RealVector betaVariance = betaMatrix.getColumnVector(1);
this.tss = computeTSS(y);
this.ess = tss - rss;
this.fValue = (ess / dfModel) / (rss / (n - p));
this.fValueProbability = 1d - new FDistribution(dfModel, n-p).cumulativeProbability(fValue);
this.rSquared = 1d - (rss / tss);
this.rSquaredAdj = 1d - (rss * (n - (hasIntercept() ? 1 : 0))) / (tss * (n - p));
this.computeParameterStdErrors(betaVariance);
this.computeParameterSignificance(betaCoefficients);
}