Package weka.core.matrix

Examples of weka.core.matrix.Matrix


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


    }
    covariance[i1][i2] = element;
  }
      }

      Matrix matrix = new Matrix(covariance);
      weka.core.matrix.EigenvalueDecomposition eigen =
  new weka.core.matrix.EigenvalueDecomposition(matrix);
      double[] eigenValues = eigen.getRealEigenvalues();

      // find index of the largest eigenvalue
      int index=0;
      double largest = eigenValues[0];
      for (int i=1; i<eigenValues.length; i++) {
  if (eigenValues[i]>largest) {
    index=i;
    largest = eigenValues[i];
  }
      }

      // calculate the first principle component
      double[] FPC = new double[k];
      Matrix eigenVector = eigen.getV();
      double[][] vectorArray = eigenVector.getArray();
      for (int i=0; i<FPC.length; i++) {
  FPC[i] = vectorArray[i][index];
      }

      // calculate the first principle component scores
View Full Code Here

    }
    c00 /= (m_SumOfWeights - 1.0);
    c01 /= (m_SumOfWeights - 1.0);
    c10 = c01;
    c11 /= (m_SumOfWeights - 1.0);
    m_Covariance = new Matrix(2, 2);
    m_Covariance.set(0, 0, c00);
    m_Covariance.set(0, 1, c01);
    m_Covariance.set(1, 0, c10);
    m_Covariance.set(1, 1, c11);
  }
View Full Code Here

          }
        }
      }
    }
   
    m_t = new Matrix(insts.numInstances(), 1);   
    double [] tt = new double[n];
    for (int i = 0; i < n; i++) {
      tt[i] = insts.instance(i).classValue() - m_avg_target;
    }
View Full Code Here

   
    // Filter instance
    inst = filterInstance(inst);

    // Build K vector
    Matrix k = new Matrix(m_NumTrain, 1);
    for (int i = 0; i < m_NumTrain; i++) {
      k.set(i, 0, m_kernel.eval(-1, i, inst));
    }

    double result = k.transpose().times(m_t).get(0, 0) + m_avg_target;
    result = (result - m_Blin) / m_Alin;

    return result;

  }
View Full Code Here

  public double[][] predictIntervals(Instance inst, double confidenceLevel) throws Exception {

    inst = filterInstance(inst);

    // Build K vector (and Kappa)
    Matrix k = new Matrix(m_NumTrain, 1);
    for (int i = 0; i < m_NumTrain; i++) {
      k.set(i, 0, m_kernel.eval(-1, i, inst));
    }

    double estimate = k.transpose().times(m_t).get(0, 0) + m_avg_target;

    double sigma = computeStdDev(inst, k);

    confidenceLevel = 1.0 - ((1.0 - confidenceLevel) / 2.0);
View Full Code Here

  public double getStandardDeviation(Instance inst) throws Exception {

    inst = filterInstance(inst);

    // Build K vector (and Kappa)
    Matrix k = new Matrix(m_NumTrain, 1);
    for (int i = 0; i < m_NumTrain; i++) {
      k.set(i, 0, m_kernel.eval(-1, i, inst));
    }

    return computeStdDev(inst, k) / m_Alin;
  }
View Full Code Here

  public double logDensity(Instance inst, double value) throws Exception {
   
    inst = filterInstance(inst);

    // Build K vector (and Kappa)
    Matrix k = new Matrix(m_NumTrain, 1);
    for (int i = 0; i < m_NumTrain; i++) {
      k.set(i, 0, m_kernel.eval(-1, i, inst));
    }
   
    double estimate = k.transpose().times(m_t).get(0, 0) + m_avg_target;

    double sigma = computeStdDev(inst, k);
   
    // transform to GP space
    value = value * m_Alin + m_Blin;
View Full Code Here

  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

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

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

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

    // 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

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.