Example usage for weka.core Matrix Matrix

List of usage examples for weka.core Matrix Matrix

Introduction

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

Prototype

public Matrix(int nr, int nc) 

Source Link

Document

Constructs a matrix and initializes it with default values.

Usage

From source file:CGLSMethod.LinearRegression.java

License:Open Source License

/**
 * Calculate a linear regression using the selected attributes
 *
 * @param selectedAttributes an array of booleans where each element
 * is true if the corresponding attribute should be included in the
 * regression.//from   w  w  w .  j  a  v a 2s  . com
 * @return an array of coefficients for the linear regression model.
 * @throws Exception if an error occurred during the regression.
 */
private double[] doRegression(boolean[] selectedAttributes) throws Exception {

    if (b_Debug) {
        System.out.print("doRegression(");
        for (int i = 0; i < selectedAttributes.length; i++) {
            System.out.print(" " + selectedAttributes[i]);
        }
        System.out.println(" )");
    }
    int numAttributes = 0;
    for (int i = 0; i < selectedAttributes.length; i++) {
        if (selectedAttributes[i]) {
            numAttributes++;
        }
    }

    // Check whether there are still attributes left
    Matrix independent = null, dependent = null;
    double[] weights = null;
    if (numAttributes > 0) {
        independent = new Matrix(m_TransformedData.numInstances(), numAttributes);
        dependent = new Matrix(m_TransformedData.numInstances(), 1);
        for (int i = 0; i < m_TransformedData.numInstances(); i++) {
            Instance inst = m_TransformedData.instance(i);
            int column = 0;
            for (int j = 0; j < m_TransformedData.numAttributes(); j++) {
                if (j == m_ClassIndex) {
                    dependent.setElement(i, 0, inst.classValue());
                } else {
                    if (selectedAttributes[j]) {
                        double value = inst.value(j) - m_Means[j];

                        // We only need to do this if we want to
                        // scale the input
                        if (!m_checksTurnedOff) {
                            value /= m_StdDevs[j];
                        }
                        independent.setElement(i, column, value);
                        column++;
                    }
                }
            }
        }

        // Grab instance weights
        weights = new double[m_TransformedData.numInstances()];
        for (int i = 0; i < weights.length; i++) {
            weights[i] = m_TransformedData.instance(i).weight();
        }
    }

    // Compute coefficients (note that we have to treat the
    // intercept separately so that it doesn't get affected
    // by the ridge constant.)
    double[] coefficients = new double[numAttributes + 1];
    if (numAttributes > 0) {
        double[] coeffsWithoutIntercept = independent.regression(dependent, weights, m_Ridge);
        System.arraycopy(coeffsWithoutIntercept, 0, coefficients, 0, numAttributes);
    }
    coefficients[numAttributes] = m_ClassMean;

    // Convert coefficients into original scale
    int column = 0;
    for (int i = 0; i < m_TransformedData.numAttributes(); i++) {
        if ((i != m_TransformedData.classIndex()) && (selectedAttributes[i])) {

            // We only need to do this if we have scaled the
            // input.
            if (!m_checksTurnedOff) {
                coefficients[column] /= m_StdDevs[i];
            }

            // We have centred the input
            coefficients[coefficients.length - 1] -= coefficients[column] * m_Means[i];
            column++;
        }
    }

    return coefficients;
}

From source file:rbms.RBM.java

License:Open Source License

/**
 * Hidden Activation Probability - returns P(z|x) where p(z[i]==1|x) for each element.
 * A Bias is added (and removed) automatically.
    * @param   x_   x (without bias)/*  www .j  av  a2s .  c o  m*/
 * @return   z (without bias)
 */
public double[] prob_z(double x_[]) {
    Matrix x = new Matrix(MatrixUtils.addBias(x_), 1);
    double z[] = MatrixUtils.sigma(x.times(W).getArray()[0]);
    return MatrixUtils.removeBias(z);
}

From source file:rbms.RBM.java

License:Open Source License

/**
 * Visible Activation Probability - returns P(x|z) where p(x[j]==1|z) for each j-th element.
 * A bias is added (and removed) automatically.
 * @param   z_   z (without bias)/*from w w  w.  ja v  a 2  s  . co m*/
 * @return   x (without bias)
 */
public double[] prob_x(double z_[]) {
    Matrix z = new Matrix(MatrixUtils.addBias(z_), 1);
    double x[] = MatrixUtils.sigma(z.times(W.transpose()).getArray()[0]);
    return MatrixUtils.removeBias(x);
}

From source file:rbms.RBM.java

License:Open Source License

/**
 * Initialize W, and make _dW (for momentum) of the same dimensions.
 * @param   d   number of visible units/*www .  j  a  v  a 2s. c o  m*/
 * @param   h    number of hidden units
 */
private void initWeights(int d, int h) {

    this.W = makeW(d, h);
    this.dW_ = new Matrix(W.getRowDimension(), W.getColumnDimension()); // for momentum
}