Java Code Examples for weka.core.matrix.Matrix#getRowDimension()

The following examples show how to use weka.core.matrix.Matrix#getRowDimension() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: sIB.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Compute the sIB score
  * @param m a term-cluster matrix, with m[i, j] is the probability of term i given cluster j  
  * @param Pt an array of cluster prior probabilities
  * @return the sIB score which indicates the quality of the partition
  */
 private double sIB_local_MI(Matrix m, double[] Pt) {
   double Hy = 0.0, Ht = 0.0;
   for (int i = 0; i < Pt.length; i++) {
     Ht += Pt[i] * Math.log(Pt[i]);
   }
   Ht = -Ht;
   
   for (int i = 0; i < m_numAttributes; i++) {
     double Py = 0.0;
     for (int j = 0; j < m_numCluster; j++) {
Py += m.get(i, j) * Pt[j];	
     }     
     if(Py == 0) continue;
     Hy += Py * Math.log(Py);
   }
   Hy = -Hy;
   
   double Hyt = 0.0, tmp = 0.0;
   for (int i = 0; i < m.getRowDimension(); i++) {
     for (int j = 0; j < m.getColumnDimension(); j++) {
if ((tmp = m.get(i, j)) == 0 || Pt[j] == 0) {
  continue;
}
tmp *= Pt[j];
Hyt += tmp * Math.log(tmp);
     }
   }
   return Hy + Ht + Hyt;
 }
 
Example 2
Source File: sIB.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Compute the MI between instances and attributes
  * @param m the term-document matrix
  * @param input object that describes the statistics about the training data
  */
 private void MI(Matrix m, Input input){    
   int minDimSize = m.getColumnDimension() < m.getRowDimension() ? m.getColumnDimension() : m.getRowDimension();
   if(minDimSize < 2){
     System.err.println("Warning : This is not a JOINT distribution");
     input.Hx = Entropy (m);
     input.Hy = 0;
     input.Ixy = 0;
     return;
   }
   
   input.Hx = Entropy(input.Px);
   input.Hy = Entropy(input.Py);
   
   double entropy = input.Hx + input.Hy;    
   for (int i=0; i < m_numInstances; i++) {
     Instance inst = m_data.instance(i);
     for (int v = 0; v < inst.numValues(); v++) {
double tmp = m.get(inst.index(v), i);
if(tmp <= 0) continue;
entropy += tmp * Math.log(tmp);
     }
   }
   input.Ixy = entropy;
   if(m_verbose) {
     System.out.println("Ixy = " + input.Ixy);
   }
 }
 
Example 3
Source File: sIB.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Compute the entropy score based on a matrix
  * @param p a matrix with non-negative and normalized probabilities
  * @return the entropy value
  */
 private double Entropy(Matrix p) {
   double mi = 0;
   for (int i = 0; i < p.getRowDimension(); i++) {
     for (int j = 0; j < p.getColumnDimension(); j++) {
if(p.get(i, j) == 0){
  continue;
}
mi += p.get(i, j) + Math.log(p.get(i, j)); 
     }
   }
   mi = -mi;
   return mi;
 }
 
Example 4
Source File: PLSFilter.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * normalizes the given vector (inplace) 
 * 
 * @param v		the vector to normalize
 */
protected void normalizeVector(Matrix v) {
  double	sum;
  int		i;
  
  // determine length
  sum = 0;
  for (i = 0; i < v.getRowDimension(); i++)
    sum += v.get(i, 0) * v.get(i, 0);
  sum = StrictMath.sqrt(sum);
  
  // normalize content
  for (i = 0; i < v.getRowDimension(); i++)
    v.set(i, 0, v.get(i, 0) / sum);
}
 
Example 5
Source File: MultivariateGaussianEstimator.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
private double getLogDeterminant(Matrix L) {
  double logDeterminant;
  double detL = 0;
  int n = L.getRowDimension();
  double[][] matrixAsArray = L.getArray();
  for (int i = 0; i < n; i++) {

    detL += Math.log(matrixAsArray[i][i]);
  }
  logDeterminant = detL * 2;

  return logDeterminant;
}
 
Example 6
Source File: MatrixUtils.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Helper method that transforms a Matrix object to an Instances object.
 *
 * @param mat The Matrix to transform.
 * @param patternInst the Instances template to use
 * @return  The resulting Instances object.
 */
public static Instances matrixToInstances(Matrix mat, Instances patternInst){
	Instances result = new Instances(patternInst);
	for (int i = 0; i < mat.getRowDimension(); i++) {
		double[] row =  mat.getArray()[i];
		DenseInstance denseInst = new DenseInstance(1.0, row);
		result.add(denseInst);
	}

	return result;
}
 
Example 7
Source File: PaceMatrix.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/** Construct a PaceMatrix from a Matrix 
    @param X    Matrix 
*/
public PaceMatrix( Matrix X ) {
  super( X.getRowDimension(), X.getColumnDimension() );
  A = X.getArray();
}