List of usage examples for weka.core.matrix Matrix identity
public static Matrix identity(int m, int n)
From source file:adams.data.instancesanalysis.pls.SIMPLS.java
License:Open Source License
/** * Transforms the data, initializes if necessary. * * @param data the data to use//from w ww . j a v a2 s.co m */ protected Instances doTransform(Instances data, Map<String, Object> params) throws Exception { Matrix A, A_trans; Matrix M; Matrix X, X_trans; Matrix X_new; Matrix Y, y; Matrix C, c; Matrix Q, q; Matrix W, w; Matrix P, p, p_trans; Matrix v, v_trans; Matrix T; Instances result; int h; if (!isInitialized()) { // init X = MatrixHelper.getX(data); X_trans = X.transpose(); Y = MatrixHelper.getY(data); A = X_trans.times(Y); M = X_trans.times(X); C = Matrix.identity(data.numAttributes() - 1, data.numAttributes() - 1); W = new Matrix(data.numAttributes() - 1, getNumComponents()); P = new Matrix(data.numAttributes() - 1, getNumComponents()); Q = new Matrix(1, getNumComponents()); for (h = 0; h < getNumComponents(); h++) { // 1. qh as dominant EigenVector of Ah'*Ah A_trans = A.transpose(); q = MatrixHelper.getDominantEigenVector(A_trans.times(A)); // 2. wh=Ah*qh, ch=wh'*Mh*wh, wh=wh/sqrt(ch), store wh in W as column w = A.times(q); c = w.transpose().times(M).times(w); w = w.times(1.0 / StrictMath.sqrt(c.get(0, 0))); MatrixHelper.setVector(w, W, h); // 3. ph=Mh*wh, store ph in P as column p = M.times(w); p_trans = p.transpose(); MatrixHelper.setVector(p, P, h); // 4. qh=Ah'*wh, store qh in Q as column q = A_trans.times(w); MatrixHelper.setVector(q, Q, h); // 5. vh=Ch*ph, vh=vh/||vh|| v = C.times(p); MatrixHelper.normalizeVector(v); v_trans = v.transpose(); // 6. Ch+1=Ch-vh*vh', Mh+1=Mh-ph*ph' C = C.minus(v.times(v_trans)); M = M.minus(p.times(p_trans)); // 7. Ah+1=ChAh (actually Ch+1) A = C.times(A); } // finish if (getNumCoefficients() > 0) slim(W); m_W = W; T = X.times(m_W); X_new = T; m_B = W.times(Q.transpose()); switch (m_PredictionType) { case ALL: y = T.times(P.transpose()).times(m_B); break; case NONE: case EXCEPT_CLASS: y = MatrixHelper.getY(data); break; default: throw new IllegalStateException("Unhandled prediction type: " + m_PredictionType); } result = MatrixHelper.toInstances(getOutputFormat(), X_new, y); } else { X = MatrixHelper.getX(data); X_new = X.times(m_W); switch (m_PredictionType) { case ALL: y = X.times(m_B); break; case NONE: case EXCEPT_CLASS: y = MatrixHelper.getY(data); break; default: throw new IllegalStateException("Unhandled prediction type: " + m_PredictionType); } result = MatrixHelper.toInstances(getOutputFormat(), X_new, y); } return result; }
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./*www . j a v a2 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]; }
From source file:org.knime.knip.suise.node.boundarymodel.BoundaryModel.java
License:Open Source License
private double calcLMDL(double epsilon) { // create sample matrix Matrix V = new Matrix(m_contourData.numFeatures(), m_contourData.numVectors()); double[] vec; for (int i = 0; i < m_contourData.numVectors(); i++) { vec = m_contourData.getVector(i); for (int j = 0; j < vec.length; j++) { V.set(j, i, vec[j]);/*from ww w . j a va2s . com*/ } } // estimate of the covariance matrix Matrix W = V.times(V.transpose()); W.times(m_contourData.numFeatures() / (epsilon * epsilon * m_contourData.numVectors())); W = Matrix.identity(m_contourData.numFeatures(), m_contourData.numFeatures()).plus(W); return Utils.log2(W.det()) * (m_contourData.numFeatures() + m_contourData.numVectors()) / 2; }
From source file:org.knime.knip.suise.node.boundarymodel.contourdata.ContourCluster.java
License:Open Source License
private double calcLMDL() { // create sample matrix Matrix V = new Matrix(m_cdata.numFeatures(), m_samples.size()); double[] vec; for (int i = 0; i < m_samples.size(); i++) { vec = m_cdata.get(m_samples.get(i)[0], m_samples.get(i)[1]); for (int j = 0; j < vec.length; j++) { V.set(j, i, vec[j]);//from www .j a va2 s . c om } } double epsilon = 5; // estimate of the covariance matrix Matrix W = V.times(V.transpose()); W.times(m_cdata.numFeatures() / (epsilon * epsilon * m_samples.size())); W = Matrix.identity(m_cdata.numFeatures(), m_cdata.numFeatures()).plus(W); return Utils.log2(W.det()) * (m_cdata.numFeatures() + m_samples.size()) / 2; }