Package weka.core.matrix

Examples of weka.core.matrix.Matrix


  protected void initVars(Instances data) {
    super.initVars(data);

    try {
      if (m_KernelMatrix == null) {
        m_KernelMatrix = new Matrix(new FileReader(m_KernelMatrixFile));
        //        System.err.println("Read kernel matrix.");
      }
    } catch (Exception e) {
      System.err.println("Problem reading matrix from " + m_KernelMatrixFile);
    }
View Full Code Here


      @param n    Number of colums.
      @return An m-by-n matrix with random elements.  */
  public static Matrix randomNormal( int m, int n ) {
    Random random = new Random();
    
    Matrix A = new Matrix(m,n);
    double[][] X = A.getArray();
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
  X[i][j] = random.nextGaussian();
      }
    }
View Full Code Here

    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);

    m_ReplaceMissingFilter = new ReplaceMissingValues();
    m_ReplaceMissingFilter.setInputFormat(m_TrainInstances);
    m_TrainInstances = Filter.useFilter(m_TrainInstances, m_ReplaceMissingFilter);

    if (m_Normalize) {
      m_NormalizeFilter = new Normalize();
      m_NormalizeFilter.setInputFormat(m_TrainInstances);
      m_TrainInstances = Filter.useFilter(m_TrainInstances, m_NormalizeFilter);
    }

    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] = ((Integer)(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();

    // 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 = (double[][]) v.clone();
    m_Eigenvalues  = (double[]) eig.getRealEigenvalues().clone();

    // any eigenvalues less than 0 are not worth anything --- change to 0
View Full Code Here

   * @return    the data without class attribute
   */
  protected Matrix getX(Instances instances) {
    double[][]  x;
    double[]  values;
    Matrix  result;
    int    i;
    int    n;
    int    j;
    int    clsIndex;
   
    clsIndex = instances.classIndex();
    x        = new double[instances.numInstances()][];
   
    for (i = 0; i < instances.numInstances(); i++) {
      values = instances.instance(i).toDoubleArray();
      x[i]   = new double[values.length - 1];
     
      j = 0;
      for (n = 0; n < values.length; n++) {
  if (n != clsIndex) {
    x[i][j] = values[n];
    j++;
  }
      }
    }
   
    result = new Matrix(x);
   
    return result;
  }
View Full Code Here

   * @return    the data without the class attribute
   */
  protected Matrix getX(Instance instance) {
    double[][]  x;
    double[]  values;
    Matrix  result;
   
    x = new double[1][];
    values = instance.toDoubleArray();
    x[0] = new double[values.length - 1];
    System.arraycopy(values, 0, x[0], 0, values.length - 1);
   
    result = new Matrix(x);
   
    return result;
  }
View Full Code Here

   * @param instances  the data to work on
   * @return    the class attribute
   */
  protected Matrix getY(Instances instances) {
    double[][]  y;
    Matrix  result;
    int    i;
   
    y = new double[instances.numInstances()][1];
    for (i = 0; i < instances.numInstances(); i++)
      y[i][0] = instances.instance(i).classValue();
   
    result = new Matrix(y);
   
    return result;
  }
View Full Code Here

   * @param instance  the instance to work on
   * @return    the class attribute
   */
  protected Matrix getY(Instance instance) {
    double[][]  y;
    Matrix  result;
   
    y = new double[1][1];
    y[0][0] = instance.classValue();
   
    result = new Matrix(y);
   
    return result;
  }
View Full Code Here

   * @param m    the matrix to work on
   * @param columnIndex  the column to return
   * @return    the column as n x 1 matrix
   */
  protected Matrix columnAsVector(Matrix m, int columnIndex) {
    Matrix  result;
    int    i;
   
    result = new Matrix(m.getRowDimension(), 1);
   
    for (i = 0; i < m.getRowDimension(); i++)
      result.set(i, 0, m.get(i, columnIndex));
   
    return result;
  }
View Full Code Here

   */
  protected Matrix getDominantEigenVector(Matrix m) {
    EigenvalueDecomposition  eigendecomp;
    double[]      eigenvalues;
    int        index;
    Matrix      result;
   
    eigendecomp = m.eig();
    eigenvalues = eigendecomp.getRealEigenvalues();
    index       = Utils.maxIndex(eigenvalues);
    result  = columnAsVector(eigendecomp.getV(), index);
View Full Code Here

   * @param instances   the data to process
   * @return            the modified data
   * @throws Exception  in case the processing goes wrong
   */
  protected Instances processPLS1(Instances instances) throws Exception {
    Matrix  X, X_trans, x;
    Matrix  y;
    Matrix  W, w;
    Matrix  T, t, t_trans;
    Matrix  P, p, p_trans;
    double  b;
    Matrix  b_hat;
    int    i;
    int    j;
    Matrix  X_new;
    Matrix  tmp;
    Instances  result;
    Instances  tmpInst;

    // initialization
    if (!isFirstBatchDone()) {
      // split up data
      X       = getX(instances);
      y       = getY(instances);
      X_trans = X.transpose();
     
      // init
      W     = new Matrix(instances.numAttributes() - 1, getNumComponents());
      P     = new Matrix(instances.numAttributes() - 1, getNumComponents());
      T     = new Matrix(instances.numInstances(), getNumComponents());
      b_hat = new Matrix(getNumComponents(), 1);
     
      for (j = 0; j < getNumComponents(); j++) {
  // 1. step: wj
  w = X_trans.times(y);
  normalizeVector(w);
  setVector(w, W, j);
 
  // 2. step: tj
  t       = X.times(w);
  t_trans = t.transpose();
  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((double) 1 / t_trans.times(t).get(0, 0));
  p_trans = p.transpose();
  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());
     
      // X_new = X*W*(P^T*W)^-1
      X_new = getX(instances).times(tmp);
     
      // factor = W*(P^T*W)^-1 * b_hat
      m_PLS1_RegVector = tmp.times(b_hat);
  
      // save matrices
      m_PLS1_P     = P;
      m_PLS1_W     = W;
      m_PLS1_b_hat = b_hat;
     
      if (getPerformPrediction())
        result = toInstances(getOutputFormat(), X_new, y);
      else
        result = toInstances(getOutputFormat(), X_new, getY(instances));
    }
    // prediction
    else {
      result = new Instances(getOutputFormat());
     
      for (i = 0; i < instances.numInstances(); i++) {
  // work on each instance
  tmpInst = new Instances(instances, 0);
  tmpInst.add((Instance) instances.instance(i).copy());
  x = getX(tmpInst);
  X = new Matrix(1, getNumComponents());
  T = new Matrix(1, getNumComponents());
 
  for (j = 0; j < getNumComponents(); j++) {
    setVector(x, X, j);
    // 1. step: tj = xj * wj
    t = x.times(getVector(m_PLS1_W, j));
View Full Code Here

TOP

Related Classes of weka.core.matrix.Matrix

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.