List of usage examples for weka.core.matrix EigenvalueDecomposition getRealEigenvalues
public double[] getRealEigenvalues()
From source file:faster_pca.faster_pca.java
License:Open Source License
/** * Initializes the filter with the given input data. * * @param instances the data to process/* www. j av a2 s . com*/ * @throws Exception in case the processing goes wrong * @see #batchFinished() */ protected void setup(Instances instances) throws Exception { int i; int j; Vector<Integer> deleteCols; int[] todelete; double[][] v; Matrix corr; EigenvalueDecomposition eig; Matrix V; m_TrainInstances = new Instances(instances); // make a copy of the training data so that we can get the class // column to append to the transformed data (if necessary) m_TrainCopy = new Instances(m_TrainInstances, 0); /*m_ReplaceMissingFilter = new ReplaceMissingValues(); m_ReplaceMissingFilter.setInputFormat(m_TrainInstances); m_TrainInstances = Filter.useFilter(m_TrainInstances, m_ReplaceMissingFilter);*/ m_NominalToBinaryFilter = new NominalToBinary(); m_NominalToBinaryFilter.setInputFormat(m_TrainInstances); m_TrainInstances = Filter.useFilter(m_TrainInstances, m_NominalToBinaryFilter); // delete any attributes with only one distinct value or are all missing deleteCols = new Vector<Integer>(); /*for (i = 0; i < m_TrainInstances.numAttributes(); i++) { if (m_TrainInstances.numDistinctValues(i) <= 1) { deleteCols.addElement(i); } }*/ if (m_TrainInstances.classIndex() >= 0) { // get rid of the class column m_HasClass = true; m_ClassIndex = m_TrainInstances.classIndex(); deleteCols.addElement(new Integer(m_ClassIndex)); } // remove columns from the data if necessary if (deleteCols.size() > 0) { m_AttributeFilter = new Remove(); todelete = new int[deleteCols.size()]; for (i = 0; i < deleteCols.size(); i++) { todelete[i] = (deleteCols.elementAt(i)).intValue(); } m_AttributeFilter.setAttributeIndicesArray(todelete); m_AttributeFilter.setInvertSelection(false); m_AttributeFilter.setInputFormat(m_TrainInstances); m_TrainInstances = Filter.useFilter(m_TrainInstances, m_AttributeFilter); } // can evaluator handle the processed data ? e.g., enough attributes? getCapabilities().testWithFail(m_TrainInstances); m_NumInstances = m_TrainInstances.numInstances(); m_NumAttribs = m_TrainInstances.numAttributes(); // fillCorrelation(); fillCovariance(); // get eigen vectors/values corr = new Matrix(m_Correlation); eig = corr.eig(); V = eig.getV(); v = new double[m_NumAttribs][m_NumAttribs]; for (i = 0; i < v.length; i++) { for (j = 0; j < v[0].length; j++) { v[i][j] = V.get(i, j); } } m_Eigenvectors = v.clone(); m_Eigenvalues = eig.getRealEigenvalues().clone(); // any eigenvalues less than 0 are not worth anything --- change to 0 for (i = 0; i < m_Eigenvalues.length; i++) { if (m_Eigenvalues[i] < 0) { m_Eigenvalues[i] = 0.0; } } m_SortedEigens = Utils.sort(m_Eigenvalues); m_SumOfEigenValues = Utils.sum(m_Eigenvalues); m_TransformedFormat = determineOutputFormat(m_TrainInstances); setOutputFormat(m_TransformedFormat); m_TrainInstances = null; }
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.// ww w. jav a 2 s.c o m */ @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]; }