List of usage examples for weka.core.matrix Matrix timesEquals
public Matrix timesEquals(double s)
From source file:net.sf.jclal.activelearning.singlelabel.querystrategy.VarianceReductionQueryStrategy.java
License:Open Source License
/** * * Analyzes how informative is an instance. * * @param instance The instance to query. * @return The utility of the instance.//from w ww .j av a 2s . c om */ @Override public double utilityInstance(Instance instance) { Instances unlabeled = getUnlabelledData().getDataset(); if (unlabelledSize != unlabeled.numInstances()) { unlabelledSize = unlabeled.numInstances(); //it is initialized q_sub_i int n = unlabeled.numInstances(); double[] q = new double[n]; //1. q_sub_i = 1/n, i = 1, 2, ..., n //Arrays.fill(q, 1.0 / n); //further on it fills, to optimize //it is initialized pi_sub_i //2. pi_sub_i double[] piSubI = getPiSubI(unlabeled); //to create the Fisher matrix int dimensionMatrix = unlabeled.numAttributes() - 1; int classIndex = unlabeled.classIndex(); Matrix matrixFisher = null; try { matrixFisher = new Matrix(dimensionMatrix, dimensionMatrix); } catch (Exception ex) { Logger.getLogger(VarianceReductionQueryStrategy.class.getName()).log(Level.SEVERE, null, ex); } for (int i = 0; i < piSubI.length; i++) { double mult = piSubI[i] * (1 - piSubI[i]); //the values of the instance are had double[] atributos = unlabeled.instance(i).toDoubleArray(); //the attribute class is eliminated, only the features are left double[] vectorX = DatasetUtils.copyFeatures(atributos, classIndex); Matrix current = null; try { current = new Matrix(vectorX.length, vectorX.length); } catch (Exception ex) { Logger.getLogger(VarianceReductionQueryStrategy.class.getName()).log(Level.SEVERE, null, ex); } productVector(current, vectorX); //it multiplies current * multi current.timesEquals(mult); //it adds current to matrixFisher //plusEquals saves the result in matrixFisher matrixFisher.plusEquals(current); } double factorRegularizationValue = getFactorRegularization(); Matrix identity = Matrix.identity(dimensionMatrix, dimensionMatrix); identity.timesEquals(factorRegularizationValue); //the result joins to matrixFisher matrixFisher.plusEquals(identity); //do eigen decomposition EigenvalueDecomposition eigen = matrixFisher.eig(); //in case of file, the matrix v takes the matrix file from eigen //in this case eigen cant not be destroy for the moment Matrix v = eigen.getV(); double[] landa = eigen.getRealEigenvalues(); double epsilonValue = getEpsilon(); //variable copies of q to know if there has been some change double[] copiaQ = new double[q.length]; Arrays.fill(copiaQ, 1.0 / n); //while it finds change in q, it keeps on iterating currentEpsilonIteration = 0; do { ++currentEpsilonIteration; //the value of q is updated //in the first iteration it fills with 1.0/n System.arraycopy(copiaQ, 0, q, 0, q.length); //process of finding f_sub_i double[] f = new double[landa.length]; for (int j = 0; j < f.length; j++) { f[j] = 0; for (int i = 0; i < n; i++) { double mult = q[i] * piSubI[i] * (1 - piSubI[i]); //the values of the instance are had double[] atributos = unlabeled.instance(i).toDoubleArray(); //the attribute class is eliminated, only the features are left double[] vectorX = DatasetUtils.copyFeatures(atributos, classIndex); //it multiplies vector_x with vector_columna of V //vector_x it is: 1 X n //vector_de_V it is: n X 1 //result: a number double multVectores = 0; for (int k = 0; k < vectorX.length; k++) { multVectores += vectorX[k] * v.get(k, j); } //the result rises up to the square multVectores *= multVectores; //it joins to f[j] f[j] += mult * multVectores; } } //the first process of finding q of the current iteration for (int i = 0; i < n; i++) { double mult = copiaQ[i] * copiaQ[i] * piSubI[i] * (1 - piSubI[i]); //the values of the instance are had double[] atributos = unlabeled.instance(i).toDoubleArray(); //the attribute class is eliminated, only the features are left double[] vectorX = DatasetUtils.copyFeatures(atributos, classIndex); //the following is realized double sumatoria = 0; for (int j = 0; j < landa.length; j++) { //it multiplies vector_x with vector_columna of V //vector_x is: 1 X n //vector_de_V is: n X 1 //result: a number double multVectores = 0; for (int k = 0; k < vectorX.length; k++) { multVectores += vectorX[k] * v.get(k, j); } //the result multiplies with landa[j] multVectores *= landa[j]; //it rises up to the square multVectores *= multVectores; //it splits between the square of f [j] multVectores /= f[j] * f[j]; //the sumatoria is added sumatoria += multVectores; } //the value of copia_q [i] is: mult * sumatoria copiaQ[i] = mult * sumatoria; } //the second step to find q in the iteration /*the sum must be out, if it was inside and with copia_q then *one would give priority to the last instance and the last one * would be always chosen */ double suma = 0; for (int j = 0; j < n; j++) { suma += copiaQ[j]; } for (int i = 0; i < n; i++) { copiaQ[i] = copiaQ[i] / suma; } } while (change(q, copiaQ, epsilonValue)); //the values are saved tempValues = new double[copiaQ.length]; System.arraycopy(copiaQ, 0, tempValues, 0, copiaQ.length); } int indice = unlabeled.indexOf(instance); return tempValues[indice]; }