Example usage for weka.core.matrix Matrix minus

List of usage examples for weka.core.matrix Matrix minus

Introduction

In this page you can find the example usage for weka.core.matrix Matrix minus.

Prototype

public Matrix minus(Matrix B) 

Source Link

Document

C = A - B

Usage

From source file:adams.data.instancesanalysis.pls.PLS1.java

License:Open Source License

/**
 * Performs predictions on the data./*from  www. j  a va2 s  .c o m*/
 *
 * @param data   the input data
 * @return      the predicted data
 */
protected Instances predict(Instances data) {
    Instances result;
    Instances tmpInst;
    int i;
    int j;
    Matrix x;
    Matrix X;
    Matrix T;
    Matrix t;

    result = new Instances(getOutputFormat());

    for (i = 0; i < data.numInstances(); i++) {
        // work on each instance
        tmpInst = new Instances(data, 0);
        tmpInst.add((Instance) data.instance(i).copy());
        x = MatrixHelper.getX(tmpInst);
        X = new Matrix(1, getNumComponents());
        T = new Matrix(1, getNumComponents());

        for (j = 0; j < getNumComponents(); j++) {
            MatrixHelper.setVector(x, X, j);
            // 1. step: tj = xj * wj
            t = x.times(MatrixHelper.getVector(m_W, j));
            MatrixHelper.setVector(t, T, j);
            // 2. step: xj+1 = xj - tj*pj^T (tj is 1x1 matrix!)
            x = x.minus(MatrixHelper.getVector(m_P, j).transpose().times(t.get(0, 0)));
        }

        switch (m_PredictionType) {
        case ALL:
            tmpInst = MatrixHelper.toInstances(getOutputFormat(), T, T.times(m_b_hat));
            break;
        case NONE:
        case EXCEPT_CLASS:
            tmpInst = MatrixHelper.toInstances(getOutputFormat(), T, MatrixHelper.getY(tmpInst));
            break;
        default:
            throw new IllegalStateException("Unhandled prediction type: " + m_PredictionType);
        }

        result.add(tmpInst.instance(0));

    }

    return result;
}

From source file:adams.data.instancesanalysis.pls.PLS1.java

License:Open Source License

/**
 * Transforms the data, initializes if necessary.
 *
 * @param data   the data to use/*from  ww  w  . ja v  a2s. c om*/
 */
protected Instances doTransform(Instances data, Map<String, Object> params) throws Exception {
    Matrix X, X_trans;
    Matrix y;
    Matrix W, w;
    Matrix T, t, t_trans;
    Matrix P, p, p_trans;
    double b;
    Matrix b_hat;
    int j;
    Matrix tmp;
    Instances result;

    // initialization
    if (!isInitialized()) {
        // split up data
        X = MatrixHelper.getX(data);
        y = MatrixHelper.getY(data);
        X_trans = X.transpose();

        // init
        W = new Matrix(data.numAttributes() - 1, getNumComponents());
        P = new Matrix(data.numAttributes() - 1, getNumComponents());
        T = new Matrix(data.numInstances(), getNumComponents());
        b_hat = new Matrix(getNumComponents(), 1);

        for (j = 0; j < getNumComponents(); j++) {
            // 1. step: wj
            w = X_trans.times(y);
            MatrixHelper.normalizeVector(w);
            MatrixHelper.setVector(w, W, j);

            // 2. step: tj
            t = X.times(w);
            t_trans = t.transpose();
            MatrixHelper.setVector(t, T, j);

            // 3. step: ^bj
            b = t_trans.times(y).get(0, 0) / t_trans.times(t).get(0, 0);
            b_hat.set(j, 0, b);

            // 4. step: pj
            p = X_trans.times(t).times(1 / t_trans.times(t).get(0, 0));
            p_trans = p.transpose();
            MatrixHelper.setVector(p, P, j);

            // 5. step: Xj+1
            X = X.minus(t.times(p_trans));
            y = y.minus(t.times(b));
        }

        // W*(P^T*W)^-1
        tmp = W.times(((P.transpose()).times(W)).inverse());

        // factor = W*(P^T*W)^-1 * b_hat
        m_r_hat = tmp.times(b_hat);

        // save matrices
        m_P = P;
        m_W = W;
        m_b_hat = b_hat;

        result = predict(data);
    }
    // prediction
    else {
        result = predict(data);
    }

    return result;
}

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 ww  w.java  2 s.  c o 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:meka.classifiers.multilabel.PLST.java

License:Open Source License

/**
 * The method to transform the labels into another set of latent labels,
 * typically a compression method is used, e.g., Boolean matrix decomposition
 * in the case of MLC-BMaD, or matrix multiplication based on SVD for PLST.
 *
 * @param D the instances to transform into new instances with transformed labels. The
 * Instances consist of features and original labels.
 * @return The resulting instances. Instances consist of features and transformed labels.
 *///from ww  w  . jav  a  2  s  .c  om
@Override
public Instances transformLabels(Instances D) throws Exception {
    Instances features = this.extractPart(D, false);
    Instances labels = this.extractPart(D, true);

    Matrix labelMatrix = MatrixUtils.instancesToMatrix(labels);

    // first, lets do the preprocessing as in the original implementation
    double[] averages = new double[labels.numAttributes()];

    for (int i = 0; i < labels.numAttributes(); i++) {
        double[] column = labels.attributeToDoubleArray(i);
        double sum = 0.0;
        for (int j = 0; j < column.length; j++) {
            if (column[j] == 1.0) {
                sum += 1.0;
            } else {
                sum += -1;
                // The algorithm needs 1/-1 coding, so let's
                // change the matrix here
                labelMatrix.set(j, i, -1.0);
            }
        }
        averages[i] = sum / column.length;
    }

    double[][] shiftMatrix = new double[1][labels.numAttributes()];

    shiftMatrix[0] = averages;

    // remember shift for prediction
    this.m_Shift = new Matrix(shiftMatrix);

    double[][] shiftTrainMatrix = new double[labels.numInstances()][labels.numAttributes()];

    for (int i = 0; i < labels.numInstances(); i++) {
        shiftTrainMatrix[i] = averages;
    }

    Matrix trainShift = new Matrix(shiftTrainMatrix);

    SingularValueDecomposition svd = new SingularValueDecomposition(labelMatrix.minus(trainShift));

    // The paper uses U here, but the implementation by the authors uses V, so
    // we used V here too.
    m_v = svd.getV();

    //remove columns so only size are left
    double[][] newArr = new double[m_v.getRowDimension()][this.getSize()];

    for (int i = 0; i < newArr.length; i++) {
        for (int j = 0; j < newArr[i].length; j++) {
            newArr[i][j] = m_v.getArray()[i][j];
        }
    }

    m_v = new Matrix(newArr);

    // now the multiplication (last step of the algorithm)
    Matrix compressed = MatrixUtils.instancesToMatrix(labels).times(this.m_v);

    // and transform it to Instances
    ArrayList<Attribute> attinfos = new ArrayList<Attribute>();

    for (int i = 0; i < compressed.getColumnDimension(); i++) {

        Attribute att = new Attribute("att" + i);
        attinfos.add(att);
    }

    // create pattern instances (also used in prediction) note: this is a regression
    // problem now, labels are not binary
    this.m_PatternInstances = new Instances("compressedlabels", attinfos, compressed.getRowDimension());

    // fill result Instances
    Instances result = Instances.mergeInstances(MatrixUtils.matrixToInstances(compressed, m_PatternInstances),
            features);

    result.setClassIndex(this.getSize());
    return result;
}

From source file:org.mitre.ccv.CompleteCompositionVectorMain.java

License:Open Source License

/**
 * Performs Affinity Propagation Clustering
 * //from w  w  w .  j a  v  a  2 s .  c  om
 * @param dm
 * @param filename
 */
public AffinityPropagation cluster(DistanceMatrix dm, int type) {
    double[][] mVals = dm.getClonedDistances();
    Matrix m = new Matrix(mVals);
    if (distCalc == 1 || distCalc == 3) {
        m = m.times(-1.0);
    } else if (distCalc == 2) {
        Matrix o = new Matrix(m.getRowDimension(), m.getColumnDimension(), 1.0);
        m = o.minus(m);
    }

    int total = m.getColumnDimension();
    double values[] = new double[(total * total - total) / 2];
    int count = 0;
    for (int i = 1; i < total; i++) {
        for (int j = i + 1; j < total - 1; j++) {
            values[count] = m.get(i, j);
            count++;
        }

    }

    double preference = getPreference(values, type);

    AffinityPropagation ap = new AffinityPropagation(m, 5000, 300, 0.9, preference);

    return ap;
}

From source file:org.mitre.clustering.AffinityPropagation.java

License:Open Source License

public void run() throws Exception {
    /*//from www. ja  v  a  2 s . c om
     * Allocate space for messages
     */
    Matrix ds;
    try {
        ds = getDiag(s);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    A = new Matrix(count, count, 0.0);
    R = new Matrix(count, count, 0.0);
    int t = 1;

    /*
     * Run parralel affinity prop. updates
     */
    Matrix e = new Matrix(count, convits, 0);
    Boolean dn = false;
    int i = -1; // adjusted due to difference in MATLAB indexing
    Matrix st;
    if (!symmetric) {
        st = s;
    } else {
        st = s.transpose();
    }

    while (!dn) {
        i = i + 1;

        A = A.transpose();
        R = R.transpose();
        /*
         * Compute responsibilities
         */
        for (int ii = 0; ii < count; ii++) {
            Matrix old = R.getMatrix(0, count - 1, ii, ii);
            Matrix AS = A.getMatrix(0, count - 1, ii, ii).plus(st.getMatrix(0, count - 1, ii, ii));

            int I = getMaxIndex(AS);
            double Y = AS.get(I, 0);
            /*
             * Need to use -Double.MAX_VALUE here, as this will return
             * the smallest NEGATIVE number, as opposed to smallest absolute
             * floating point number
             */
            AS.set(I, 0, -Double.MAX_VALUE);

            int I2 = getMaxIndex(AS);
            double Y2 = AS.get(I2, 0);

            /*
             * Set R
             */
            Matrix yMat = new Matrix(count, 1, Y);
            Matrix stVect = st.getMatrix(0, count - 1, ii, ii);
            R.setMatrix(0, count - 1, ii, ii, stVect.minus(yMat));
            R.set(I, ii, st.get(I, ii) - Y2);
            Matrix rVect = R.getMatrix(0, count - 1, ii, ii);
            rVect = rVect.times(1 - lam).plus(old.times(lam));
            R.setMatrix(0, count - 1, ii, ii, rVect);
        }
        A = A.transpose();
        R = R.transpose();

        /*
         * Compute availabilities
         */
        for (int jj = 0; jj < count; jj++) {
            Matrix old = A.getMatrix(0, count - 1, jj, jj);
            Matrix Rp = new Matrix(count, 1, 0);
            for (int c = 0; c < count; c++) {
                if (R.get(c, jj) < 0.0) {
                    Rp.set(c, 0, 0.0);
                } else {
                    Rp.set(c, 0, R.get(c, jj));
                }
            }

            Rp.set(jj, 0, R.get(jj, jj));
            Matrix sumM = new Matrix(count, 1, getSum(Rp));
            A.setMatrix(0, count - 1, jj, jj, sumM.minus(Rp));

            double dA = A.get(jj, jj);

            Matrix aVect = A.getMatrix(0, count - 1, jj, jj);
            for (int c = 0; c < count; c++) {
                if (aVect.get(c, 0) > 0) {
                    aVect.set(c, 0, 0.0);
                }
            }
            A.setMatrix(0, count - 1, jj, jj, aVect);
            A.set(jj, jj, dA);

            aVect = A.getMatrix(0, count - 1, jj, jj);
            aVect = aVect.times(1 - lam).plus(old.times(lam));
            A.setMatrix(0, count - 1, jj, jj, aVect);

        }

        /*
         * Check for convergence
         */
        Matrix E = null;
        try {
            E = getDiag(A).plus(getDiag(R));
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        for (int c = 0; c < count; c++) {
            if (E.get(c, 0) > 0) {
                E.set(c, 0, 1.0);
            } else {
                E.set(c, 0, 0.0);
            }
        }

        int index = i % convits;

        e.setMatrix(0, count - 1, index, index, E);

        K = getSum(E);

        if (i >= convits || i >= maxits) {
            Matrix se = new Matrix(count, 1);
            for (int c = 0; c < count; c++) {
                double sum = 0;
                for (int c2 = 0; c2 < convits; c2++) {
                    sum += e.get(c, c2);
                }
                se.set(c, 0, sum);
            }

            Matrix seC = matrixIsEqual(se, convits);
            Matrix seZ = matrixIsEqual(se, 0.0);

            double value = getSum(seC.plus(seZ));

            if (value == count) {
                unconverged = false;
            } else {
                unconverged = true;
            }

            if ((!unconverged && K > 0) || i == maxits) {
                dn = true;
            }

        }

    }

    /*
     * Identify exemplars
     */
    Matrix diag = null;
    try {
        diag = getDiag(A).plus(getDiag(R));
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    exemplars = new Vector<Integer>();

    for (int c = 0; c < count; c++) {
        if (diag.get(c, 0) > 0) {
            exemplars.add(c);
        }
    }

}