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


   * @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

   * @param instances   the data to process
   * @return            the modified data
   * @throws Exception  in case the processing goes wrong
   */
  protected Instances processSIMPLS(Instances instances) 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 (!isFirstBatchDone()) {
      // init
      X       = getX(instances);
      X_trans = X.transpose();
      Y       = getY(instances);
      A       = X_trans.times(Y);
      M       = X_trans.times(X);
      C       = Matrix.identity(instances.numAttributes() - 1, instances.numAttributes() - 1);
      W       = new Matrix(instances.numAttributes() - 1, getNumComponents());
      P       = new Matrix(instances.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       = 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)));
  setVector(w, W, h);
 
  // 3. ph=Mh*wh, store ph in P as column
  p       = M.times(w);
  p_trans = p.transpose();
  setVector(p, P, h);
 
  // 4. qh=Ah'*wh, store qh in Q as column
  q = A_trans.times(w);
  setVector(q, Q, h);
 
  // 5. vh=Ch*ph, vh=vh/||vh||
  v       = C.times(p);
  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
      m_SIMPLS_W = W;
      T          = X.times(m_SIMPLS_W);
      X_new      = T;
      m_SIMPLS_B = W.times(Q.transpose());
     
      if (getPerformPrediction())
  y = T.times(P.transpose()).times(m_SIMPLS_B);
      else
  y = getY(instances);

      result = toInstances(getOutputFormat(), X_new, y);
    }
View Full Code Here

    PrecomputedKernelMatrixKernel pc = new PrecomputedKernelMatrixKernel();

    // load kernel matrix
    try {
      pc.setKernelMatrix(
         new Matrix(
            new InputStreamReader(ClassLoader.getSystemResourceAsStream(
                  "weka/classifiers/data/test.matrix"))));
    } catch (Exception e) {
      e.printStackTrace();
    }
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.