Example usage for weka.core Matrix transpose

List of usage examples for weka.core Matrix transpose

Introduction

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

Prototype

public final Matrix transpose() 

Source Link

Document

Returns the transpose of a matrix.

Usage

From source file:org.opentox.jaqpot3.qsar.util.WekaInstancesProcess.java

License:Open Source License

public static Matrix getLeverageDoAMatrix(Instances inst) throws JaqpotException {
    Matrix omega = null;/* ww  w.  j av a 2 s.  co  m*/
    if (inst != null) {
        int noInstances = inst.numInstances();
        int noAttributes = inst.numAttributes();
        double[][] dataArray = new double[noInstances][noAttributes];
        for (int i = 0; i < noInstances; i++) {
            dataArray[i] = inst.instance(i).toDoubleArray();
        }
        Matrix dataMatrix = new Matrix(dataArray);
        omega = (dataMatrix.transpose().times(dataMatrix)).inverse();
    }
    return omega;
}

From source file:org.opentox.jaqpot3.qsar.util.WekaInstancesProcess.java

License:Open Source License

public static Instances getLeverageDoAPredictedInstances(Instances processedinst, Instances inst,
        String datasetUri, Model model) throws JaqpotException {

    double gamma = model.getActualModel().getGamma();
    Matrix matrix = model.getActualModel().getDataMatrix();

    try {/*from  w  ww. j  a  va2 s  . c  o m*/
        AttributeCleanup removeCompounds = new AttributeCleanup(false, string);
        processedinst = removeCompounds.filter(processedinst);
    } catch (Exception ex) {
    }

    List<String> excludeAttributes = model.getActualModel().getExcludeAttributesDoA();
    if (excludeAttributes.size() > 0) {
        Attribute attr;
        List<Integer> indices = new ArrayList();
        for (String temp : excludeAttributes) {
            attr = processedinst.attribute(temp);
            if (attr != null) {
                indices.add(attr.index());
            }
        }
        processedinst = removeInstancesAttributes(processedinst, indices);

    }

    int numInstances = processedinst.numInstances();
    int numAttributes = processedinst.numAttributes();
    double[] indicator = new double[numInstances];

    //calculate DoA
    Matrix x = null;
    for (int i = 0; i < numInstances; i++) {
        x = new Matrix(processedinst.instance(i).toDoubleArray(), numAttributes);
        indicator[i] = Math.max(0, (gamma - x.transpose().times(matrix).times(x).get(0, 0)) / gamma);
    }

    //add new DoA attribute
    Add attributeAdder = new Add();
    attributeAdder.setAttributeIndex("last");
    attributeAdder.setAttributeName("DoA" + datasetUri);
    try {
        attributeAdder.setInputFormat(inst);
        inst = Filter.useFilter(inst, attributeAdder);
    } catch (Exception ex) {
        String message = "Exception while trying to add prediction feature to Instances";
        throw new JaqpotException(message, ex);
    }

    int newNumInstances = inst.numInstances();
    int newNumAttributes = inst.numAttributes();

    //set the DoA values
    for (int i = 0; i < newNumInstances; i++) {
        inst.instance(i).setValue(newNumAttributes - 1, indicator[i]);
    }

    return inst;
}

From source file:rbms.RBM.java

License:Open Source License

/**
 * Epoch - Run X through one epcho of CD of the RBM.
 * @param   X_0   The input matrix (includes bias column).
 * @return   the contrastive divergence (CD) for this epoch.
 *
 * <verbatim>//from w w w . java 2  s.c  o  m
 * x_0 = x
 *
 *  for k = 0,...,K-1
 *     z_k = sample up
 *     x_k+1 = sample down
 *
 *  e+ = pz|x_0 
 *  e- = pz|x_K
 *
 *  CD = e+ - e-
 * </verbatim>
 *
 * Note: should be binary for hidden states, can be probabilities for visible states.
 */
public Matrix epoch(Matrix X_0) {

    int N = X_0.getArray().length;

    // POSITIVE
    Matrix Z_0 = prob_Z(X_0); // sample up                                    
    Matrix E_pos = X_0.transpose().times(Z_0); // positive energy, H_1 * V_1

    // NEGATIVE
    Matrix X_1 = prob_X(Z_0); // go down -- can either sample down 
    //Matrix X_1 = Mat.threshold(prob_X(Z_0),0.5);                     // ... or just go down
    Matrix pZ_1 = prob_Z(X_1); // go back up again
    Matrix E_neg = X_1.transpose().times(pZ_1); // negative energy, P(Z_1) * X_1

    // CALCULATE ERROR (Optional!)
    //double _Err = Mat.meanSquaredError(X_0.getArray(),X_1.getArray());         // @note: this take some milliseconds to calculate
    //System.out.println(""+_Err);

    // CONTRASTIVE DIVERGENCE
    Matrix CD = ((E_pos.minusEquals(E_neg)).times(1. / N)); // CD = difference between energies

    return CD;

}

From source file:rbms.RBM.java

License:Open Source License

public Matrix sample_epoch(Matrix X_0) {

    int N = X_0.getArray().length;

    // POSITIVE/* w  w  w.  jav  a 2 s  .  c o m*/
    Matrix Z_0 = sample_Z(X_0); // sample up                                    
    Matrix E_pos = X_0.transpose().times(Z_0); // positive energy, H_1 * V_1

    // NEGATIVE
    Matrix X_1 = sample_X(Z_0); // go down -- can either sample down 
    //Matrix X_1 = Mat.threshold(prob_X(Z_0),0.5);                     // ... or just go down
    Matrix pZ_1 = prob_Z(X_1); // go back up again
    Matrix E_neg = X_1.transpose().times(pZ_1); // negative energy, P(Z_1) * X_1

    // CALCULATE ERROR (Optional!)
    double _Err = MatrixUtils.meanSquaredError(X_0.getArray(), X_1.getArray()); // @note: this take some milliseconds to calculate
    System.out.println("" + _Err);

    // CONTRASTIVE DIVERGENCE
    Matrix CD = ((E_pos.minusEquals(E_neg)).times(1. / N)); // CD = difference between energies

    return CD;
}