weka.core.matrix.Matrix Java Examples

The following examples show how to use weka.core.matrix.Matrix. 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: GaussianProcesses.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns natural logarithm of density estimate for given value based on given instance.
 *   
 * @param instance the instance to make the prediction for.
 * @param value the value to make the prediction for.
 * @return the natural logarithm of the density estimate
 * @exception Exception if the density cannot be computed
 */
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;
  // center around estimate
  value = value - estimate;
  double z = -Math.log(sigma * Math.sqrt(2 * Math.PI)) 
    - value * value /(2.0*sigma*sigma); 
  
  return z + Math.log(m_Alin);
}
 
Example #2
Source File: LinearModel.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public void fitModel()
	{
//B = (XtX)-1XtY		
		XtXinv=Xt.times(X);
		XtXinv=XtXinv.inverse();
		Matrix temp= XtXinv.times(Xt),t2,t3;
//B should be m x 1		
		B=temp.times(Y);
		paras=B.getColumnPackedCopy();
		H_Diagonal=new double[n];
//		(XtX)-1Xt is mxn, so can just work the diagonals with Xt  	 
		double sum=0;
		for(int i=0;i<n;i++)
		{
			t2=X.getMatrix(i,i,0,m-1);
			t3=t2.transpose();
//			System.out.println("Row mult t2 rows ="+t2.getRowDimension()+" columns = "+t2.getColumnDimension());
			t3=XtXinv.times(t3);
			t3=t2.times(t3);
			H_Diagonal[i]=t3.get(0,0);
			sum+=H_Diagonal[i];
		}
		
	}
 
Example #3
Source File: GaussianProcesses.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Classifies a given instance.
 * 
 * @param inst
 *            the instance to be classified
 * @return the classification
 * @throws Exception
 *             if instance could not be classified successfully
 */
public double classifyInstance(Instance inst) throws Exception {

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

}
 
Example #4
Source File: GaussianProcesses.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Computes standard deviation for given instance, without
 * transforming target back into original space.
 */
protected double computeStdDev(Instance inst, Matrix k) throws Exception {

  double kappa = m_kernel.eval(-1, -1, inst) + m_delta * m_delta;

  double s = 0;
  int n = m_L.length;
  for (int i = 0; i < n; i++) {
    double t = 0;
    for (int j = 0; j < n; j++) {
      t -= k.get(j,0) * (i>j? m_L[i][j] : m_L[j][i]);
    }			
    s += t * k.get(i,0);
  }

  double sigma = m_delta;
  if (kappa > s) {
    sigma = Math.sqrt(kappa - s);
  }

  return sigma;
}
 
Example #5
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 #6
Source File: PLSFilter.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * determines the dominant eigenvector for the given matrix and returns it
 * 
 * @param m		the matrix to determine the dominant eigenvector for
 * @return		the dominant eigenvector
 */
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);
  
  return result;
}
 
Example #7
Source File: NNConditionalEstimator.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/** Calculate covariance and value means */
 private void calculateCovariance() {
   
   double sumValues = 0, sumConds = 0;
   for(int i = 0; i < m_Values.size(); i++) {
     sumValues += ((Double)m_Values.elementAt(i)).doubleValue()
* ((Double)m_Weights.elementAt(i)).doubleValue();
     sumConds += ((Double)m_CondValues.elementAt(i)).doubleValue()
* ((Double)m_Weights.elementAt(i)).doubleValue();
   }
   m_ValueMean = sumValues / m_SumOfWeights;
   m_CondMean = sumConds / m_SumOfWeights;
   double c00 = 0, c01 = 0, c10 = 0, c11 = 0;
   for(int i = 0; i < m_Values.size(); i++) {
     double x = ((Double)m_Values.elementAt(i)).doubleValue();
     double y = ((Double)m_CondValues.elementAt(i)).doubleValue();
     double weight = ((Double)m_Weights.elementAt(i)).doubleValue();
     c00 += (x - m_ValueMean) * (x - m_ValueMean) * weight;
     c01 += (x - m_ValueMean) * (y - m_CondMean) * weight;
     c11 += (y - m_CondMean) * (y - m_CondMean) * weight;
   }
   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);
 }
 
Example #8
Source File: MahalanobisEstimator.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns value for normal kernel
 *
 * @param x the argument to the kernel function
 * @param variance the variance
 * @return the value for a normal kernel
 */
private double normalKernel(double x) {
  
  Matrix thisPoint = new Matrix(1, 2);
  thisPoint.set(0, 0, x);
  thisPoint.set(0, 1, m_ConstDelta);
  return Math.exp(-thisPoint.times(m_CovarianceInverse).
      times(thisPoint.transpose()).get(0, 0) 
      / 2) / (Math.sqrt(TWO_PI) * m_Determinant);
}
 
Example #9
Source File: MatrixUtils.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Derivative of the sigmoid function applied to Jama Matrix
 */
public static final Jama.Matrix dsigma(Jama.Matrix A) {
	double A_[][] = A.getArray();
	double X[][] = new double[A_.length][A_[0].length];
	for(int i = 0; i < A_.length; i++) {
		for(int j = 0; j < A_[i].length; j++) {
			X[i][j] = dsigma(A_[i][j]);
		}
	}
	return new Jama.Matrix(X);
}
 
Example #10
Source File: MultivariateGaussianEstimator.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 
 * @see weka.estimators.MultivariateEstimator#estimate(double[][], double[])
 */
@Override
public void estimate(double[][] observations, double[] weights) {
  double[] means;
  double[][] cov;

  if (weights != null) {
    double sum = 0;
    for (int i = 0; i < weights.length; i++) {
      if (Double.isNaN(weights[i]) || Double.isInfinite(weights[i])) {
        throw new IllegalArgumentException(
            "Invalid numbers in the weight vector");
      }
      sum += weights[i];
    }

    if (Math.abs(sum - 1.0) > 1e-10) {
      throw new IllegalArgumentException("Weights do not sum to one");
    }

    means = weightedMean(observations, weights, 0);
    cov = weightedCovariance(observations, weights, means);

  } else {
    // Compute mean vector
    means = mean(observations);
    cov = covariance(observations, means);
  }

  CholeskyDecomposition chol = new CholeskyDecomposition(new Matrix(cov));

  // Become the newly fitted distribution.
  recalculate(means, cov, chol);
}
 
Example #11
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 #12
Source File: LinearModel.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public LinearModel(double[][] data,double[] response)	
	{
		m=data.length;
		n=data[0].length;
		y = response;
//This way round for consistency with other constructor
		Xt=new Matrix(data);
//		System.out.println("Xt = \n"+Xt);
		X = Xt.transpose();
//		System.out.println("X = \n"+X);
		Y=new Matrix(y,y.length);
	}
 
Example #13
Source File: LinearModel.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public LinearModel(Instances data)
	{
//Form X and Y from Instances		
		n=data.numInstances();
		m=data.numAttributes();	//includes the constant term
		y = data.attributeToDoubleArray(data.classIndex());
		Y=new Matrix(y,y.length);
		double[][] xt = new double[m][n];
		for(int i=0;i<n;i++)
			xt[0][i]=1;
		for(int i=1;i<m;i++)
			xt[i]=data.attributeToDoubleArray(i-1);
		Xt=new Matrix(xt);
		X=Xt.transpose();
	}
 
Example #14
Source File: LinearModel.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public  double[] formTestPredictions(Instances testData)
	{
//Form X matrix from testData
		int rows=testData.numInstances();
		int cols=testData.numAttributes();	//includes the constant term
		predicted=new double[rows];
		if(cols!=m)
		{
			System.out.println("Error: Mismatch in attribute lengths in form test Train ="+m+" Test ="+cols);
			System.exit(0);
		}
		double[][] xt = new double[cols][rows];
		for(int i=0;i<rows;i++)
			xt[0][i]=1;
		for(int i=1;i<cols;i++)
			xt[i]=testData.attributeToDoubleArray(i-1);
		Matrix testX=new Matrix(xt);
		testX=testX.transpose();
		
		for(int i=0;i<rows;i++)
		{
			//Find predicted
			predicted[i]=paras[0];
			for(int j=1;j<paras.length;j++)
				predicted[i]+=paras[j]*testX.get(i,j);
		}
		return predicted;
	
	}
 
Example #15
Source File: PLST.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
    * Transforms the predictions of the internal classifier back to the original labels.
    *
    * @param y The predictions that should be transformed back. The array consists only of
    * the predictions as they are returned from the internal classifier.
    * @return The transformed predictions.
    */
   @Override
   public double[] transformPredictionsBack(double[] y){
// y consists of predictions and maxindex, we need only predictions
double[] predictions = new double[y.length/2];

for (int i = 0; i < predictions.length; i++){
    predictions[i] = y[predictions.length+i];
}

double[][] dataArray = new double[1][predictions.length];

dataArray[0] = predictions;

Matrix yMat = new Matrix(dataArray);
       
Matrix multiplied = yMat.times(this.m_v.transpose()).plus(m_Shift);

double[] res = new double[multiplied.getColumnDimension()];

// change back from -1/1 coding to 0/1
for (int i = 0; i < res.length; i++) {
    res[i] = multiplied.getArray()[0][i]<0.0 ? 0.0 : 1.0;
}

return res;
   }
 
Example #16
Source File: MatrixUtils.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Helper method that transforma an Instances object to a Matrix object.
 *
 * @param inst The Instances to transform.
 * @return  The resulting Matrix object.
 */
public static Matrix instancesToMatrix(Instances inst){
	double[][] darr = new double[inst.numInstances()][inst.numAttributes()];
	for (int i =0 ; i < inst.numAttributes(); i++) {
		for (int j = 0; j < inst.attributeToDoubleArray(i).length; j++) {
			darr[j][i] = inst.attributeToDoubleArray(i)[j];
		}
	}
	return new Matrix(darr);
}
 
Example #17
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 #18
Source File: MatrixUtils.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
public static Jama.Matrix addBias(Jama.Matrix M) {
	double[][] M_ = M.getArray();
	final double[][] C = new double[M_.length][M_[0].length+1];
	for (int i = 0; i < M_.length; i++) {
		C[i][0] = 1.0;
		for(int j = 0; j < M_[i].length; j++) {
			C[i][j+1] = M_[i][j];
		}
	}
	return new Jama.Matrix(C);
}
 
Example #19
Source File: sIB.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/** 
    * Create a new empty <code>Partition</code> instance.
    */
   public Partition() {
     Pt_x = new int[m_numInstances];
     for (int i = 0; i < m_numInstances; i++) {
Pt_x[i] = -1;
     }
     Pt = new double[m_numCluster];
     Py_t = new Matrix(m_numAttributes, m_numCluster);
     counter = 0;
   }
 
Example #20
Source File: sIB.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Put an instance into a new cluster and update.
 * @param instIdx instance to be updated
 * @param newt index of the new cluster this instance has been assigned to
 * @param T the current working partition
 * @param Px an array of prior probabilities of the instances
 */
private void updateAssignment(int instIdx, int newt, Partition T, double Px, Matrix Py_x) {    
  T.Pt_x[instIdx] = newt;
  
  // update probability of attributes in the cluster 
  double mass = Px + T.Pt[newt];
  double pi1 = Px / mass;
  double pi2 = T.Pt[newt] / mass;
  for (int i = 0; i < m_numAttributes; i++) {
    T.Py_t.set(i, newt, pi1 * Py_x.get(i, instIdx) + pi2 * T.Py_t.get(i, newt));
  }

  T.Pt[newt] = mass;
}
 
Example #21
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 #22
Source File: sIB.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Transpose the document-term matrix to term-document matrix
  * @param data instances with document-term info
  * @return a term-document matrix transposed from the input dataset
  */
 private Matrix getTransposedMatrix(Instances data) {
   double[][] temp = new double[data.numAttributes()][data.numInstances()];
   for (int i = 0; i < data.numInstances(); i++) {
     Instance inst = data.instance(i);
     for (int v = 0; v < inst.numValues(); v++) {
temp[inst.index(v)][i] = inst.valueSparse(v);
     }
   }
   Matrix My_x = new Matrix(temp);
   return My_x;
 }
 
Example #23
Source File: sIB.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
private Matrix getTransposedNormedMatrix(Instances data) {
   Matrix matrix = new Matrix(data.numAttributes(), data.numInstances());
   for(int i = 0; i < data.numInstances(); i++){
     double[] vals = data.instance(i).toDoubleArray();
     double sum = Utils.sum(vals);
     for (int v = 0; v < vals.length; v++) {
vals[v] /= sum;
matrix.set(v, i, vals[v]);
     }      
   }
   return matrix;
 }
 
Example #24
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 #25
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 #26
Source File: GaussianProcesses.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gives standard deviation of the prediction at the given instance.
 * 
 * @param inst
 *            the instance to get the standard deviation for
 * @return the standard deviation
 * @throws Exception
 *             if computation fails
 */
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;
}
 
Example #27
Source File: MatrixUtils.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Multiply - multiply vectors a and b together.
 */
public static double[] multiply(final double[] a, final double[] b) throws Exception {
	Jama.Matrix a_ = new Jama.Matrix(a,1);
	Jama.Matrix b_ = new Jama.Matrix(b,1);
	Jama.Matrix c_ = a_.arrayTimes(b_);
	return c_.getArray()[0];
   }
 
Example #28
Source File: PaceMatrix.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/** Generate matrix with standard-normally distributed random elements
     @param m    Number of rows.
     @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();
     }
   }
   return A;
 }
 
Example #29
Source File: MatrixUtils.java    From meka with GNU General Public License v3.0 4 votes vote down vote up
public static void printMatrix(double M_[][]) {
	Jama.Matrix M = new Jama.Matrix(M_);
	M.print(5,3);
}
 
Example #30
Source File: MatrixUtils.java    From meka with GNU General Public License v3.0 4 votes vote down vote up
public static void printDim(Jama.Matrix M) {
	printDim(M.getArray());
}