• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java Matrix类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中weka.core.matrix.Matrix的典型用法代码示例。如果您正苦于以下问题:Java Matrix类的具体用法?Java Matrix怎么用?Java Matrix使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



Matrix类属于weka.core.matrix包,在下文中一共展示了Matrix类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: loadMatrix

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * Loads the correlation/covariance matrix from a file.
 * 
 * @throws IOException if a problem occurs
 */
protected void loadMatrix() throws IOException {
  File f = new File(m_pathToMatrix);

  if (!f.exists()) {
    throw new IOException("The matrix file '" + m_pathToMatrix
      + "' does not seem to exist on the file system!");
  }

  BufferedReader br = null;
  try {
    br = new BufferedReader(new FileReader(m_pathToMatrix));
    try {
      m_matrix = new Matrix(br);
    } catch (Exception e) {
      throw new IOException(e);
    }
    br.close();
    br = null;
  } finally {
    if (br != null) {
      br.close();
    }
  }
}
 
开发者ID:mydzigear,项目名称:repo.kmeanspp.silhouette_score,代码行数:30,代码来源:PreConstructedPCA.java


示例2: classifyInstance

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * Classifies a given instance.
 * 
 * @param inst the instance to be classified
 * @return the classification
 * @throws Exception if instance could not be classified successfully
 */
@Override
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;

}
 
开发者ID:mydzigear,项目名称:repo.kmeanspp.silhouette_score,代码行数:26,代码来源:GaussianProcesses.java


示例3: computeStdDev

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * 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_deltaSquared;

  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;
}
 
开发者ID:mydzigear,项目名称:repo.kmeanspp.silhouette_score,代码行数:26,代码来源:GaussianProcesses.java


示例4: logDensity

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * 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
 */
@Override
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);
}
 
开发者ID:mydzigear,项目名称:repo.kmeanspp.silhouette_score,代码行数:34,代码来源:GaussianProcesses.java


示例5: classifyInstance

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * 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;

}
 
开发者ID:dsibournemouth,项目名称:autoweka,代码行数:27,代码来源:GaussianProcesses.java


示例6: computeStdDev

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * 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;
}
 
开发者ID:dsibournemouth,项目名称:autoweka,代码行数:26,代码来源:GaussianProcesses.java


示例7: logDensity

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * 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);
}
 
开发者ID:dsibournemouth,项目名称:autoweka,代码行数:32,代码来源:GaussianProcesses.java


示例8: getKernel

import weka.core.matrix.Matrix; //导入依赖的package包/类
/** Creates a default PrecomputedKernelMatrixKernell */
public Kernel getKernel() {
  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();
  }

  return pc;
}
 
开发者ID:dsibournemouth,项目名称:autoweka,代码行数:17,代码来源:PrecomputedKernelMatrixKernelTest.java


示例9: productVector

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * Compute the product among the matrix and the vector
 *
 * @param current
 *            The matrix.
 * @param vectorX
 *            The vector.
 */
public void productVector(Matrix current, double[] vectorX) {

	for (int m = 0; m < vectorX.length; m++) {
		for (int nn = 0; nn < vectorX.length; nn++) {
			current.set(m, nn, vectorX[m] * vectorX[nn]);
		}
	}
}
 
开发者ID:ogreyesp,项目名称:JCLAL,代码行数:17,代码来源:VarianceReductionQueryStrategy.java


示例10: getHeatMapForMatrix

import weka.core.matrix.Matrix; //导入依赖的package包/类
public static Image getHeatMapForMatrix(Matrix matrix,
  List<String> rowAttNames) {

  double[][] m = matrix.getArray();

  // generate the heat map
  // need to reverse the order of the rows
  double[][] mm = new double[m.length][];
  for (int i = 0; i < m.length; i++) {
    mm[m.length - 1 - i] = m[i];
  }
  String[] xLabels = new String[rowAttNames.size()];
  String[] yLabels = new String[rowAttNames.size()];
  for (int i = 0; i < rowAttNames.size(); i++) {
    xLabels[i] = rowAttNames.get(i);
    yLabels[rowAttNames.size() - 1 - i] = rowAttNames.get(i);
  }
  HeatChart map = new HeatChart(mm, true);
  map.setTitle("Correlation matrix heat map");
  map.setCellSize(new java.awt.Dimension(30, 30));
  map.setHighValueColour(java.awt.Color.RED);
  map.setLowValueColour(java.awt.Color.BLUE);
  map.setXValues(xLabels);
  map.setYValues(yLabels);

  return map.getChartImage();
}
 
开发者ID:mydzigear,项目名称:repo.kmeanspp.silhouette_score,代码行数:28,代码来源:CorrelationMatrixRowReduceTask.java


示例11: getHeatMapForMatrix

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * Generates a heat map from a matrix of correlations
 * 
 * @param matrix a Matrix (expected to hold correlation values between -1 and
 *          1)
 * @param rowAttNames a list of labels for the columns/rows
 * @return an Image holding the heat map
 */
public static Image getHeatMapForMatrix(Matrix matrix,
  List<String> rowAttNames) {

  double[][] m = matrix.getArray();

  // generate the heat map
  // need to reverse the order of the rows
  double[][] mm = new double[m.length][];
  for (int i = 0; i < m.length; i++) {
    mm[m.length - 1 - i] = m[i];
  }
  String[] xLabels = new String[rowAttNames.size()];
  String[] yLabels = new String[rowAttNames.size()];
  for (int i = 0; i < rowAttNames.size(); i++) {
    xLabels[i] = rowAttNames.get(i);
    yLabels[rowAttNames.size() - 1 - i] = rowAttNames.get(i);
  }
  HeatChart map = new HeatChart(mm, true);
  map.setTitle("Correlation matrix heat map");
  map.setCellSize(new java.awt.Dimension(30, 30));
  map.setHighValueColour(java.awt.Color.RED);
  map.setLowValueColour(java.awt.Color.BLUE);
  map.setXValues(xLabels);
  map.setYValues(yLabels);

  return map.getChartImage();
}
 
开发者ID:mydzigear,项目名称:repo.kmeanspp.silhouette_score,代码行数:36,代码来源:ChartUtils.java


示例12: calculateStdErrorOfCoef

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * Returns the standard errors of slope and intercept for a simple linear
 * regression model: y = a + bx. The first element is the standard error of
 * slope, the second element is standard error of intercept.
 * 
 * @param data (the data set)
 * @param chosen (chosen x-attribute)
 * @param slope (slope determined by simple linear regression model)
 * @param intercept (intercept determined by simple linear regression model)
 * @param df (number of instances - 2)
 * 
 * @return array of standard errors of slope and intercept
 * @throws Exception if there is a missing class value in data
 */
public static double[] calculateStdErrorOfCoef(Instances data,
  Attribute chosen, double slope, double intercept, int df) throws Exception {
  // calculate sum of squared residuals, mean squared error
  double ssr = calculateSSR(data, chosen, slope, intercept);
  double mse = ssr / df;

  /*
   * put data into 2-D array with 2 columns first column is value of chosen
   * attribute second column is constant (1's)
   */
  double[][] array = new double[data.numInstances()][2];
  for (int i = 0; i < data.numInstances(); i++) {
    array[i][0] = data.instance(i).value(chosen);
    array[i][1] = 1.0;
  }

  /*
   * linear algebra calculation: covariance matrix = mse * (XtX)^-1 diagonal
   * of covariance matrix is square of standard error of coefficients
   */
  Matrix X = new Matrix(array);
  Matrix Xt = X.transpose();
  Matrix XtX = Xt.times(X);
  Matrix inverse = XtX.inverse();
  Matrix cov = inverse.times(mse);

  double[] result = new double[2];
  for (int i = 0; i < 2; i++) {
    result[i] = Math.sqrt(cov.get(i, i));
  }

  return result;
}
 
开发者ID:mydzigear,项目名称:repo.kmeanspp.silhouette_score,代码行数:48,代码来源:RegressionAnalysis.java


示例13: getStandardDeviation

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * 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;
}
 
开发者ID:mydzigear,项目名称:repo.kmeanspp.silhouette_score,代码行数:20,代码来源:GaussianProcesses.java


示例14: calculateCovariance

import weka.core.matrix.Matrix; //导入依赖的package包/类
/** Calculate covariance and value means */
private void calculateCovariance() {

  double sumValues = 0, sumConds = 0;
  for (int i = 0; i < m_Values.size(); i++) {
    sumValues += m_Values.elementAt(i).doubleValue()
      * m_Weights.elementAt(i).doubleValue();
    sumConds += m_CondValues.elementAt(i).doubleValue()
      * 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 = m_Values.elementAt(i).doubleValue();
    double y = m_CondValues.elementAt(i).doubleValue();
    double weight = 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);
}
 
开发者ID:mydzigear,项目名称:repo.kmeanspp.silhouette_score,代码行数:32,代码来源:NNConditionalEstimator.java


示例15: normalKernel

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * 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);
}
 
开发者ID:mydzigear,项目名称:repo.kmeanspp.silhouette_score,代码行数:17,代码来源:MahalanobisEstimator.java


示例16: estimate

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * 
 * @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 (double weight : weights) {
      if (Double.isNaN(weight) || Double.isInfinite(weight)) {
        throw new IllegalArgumentException(
          "Invalid numbers in the weight vector");
      }
      sum += weight;
    }

    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);
}
 
开发者ID:mydzigear,项目名称:repo.kmeanspp.silhouette_score,代码行数:38,代码来源:MultivariateGaussianEstimator.java


示例17: getLogDeterminant

import weka.core.matrix.Matrix; //导入依赖的package包/类
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;
}
 
开发者ID:mydzigear,项目名称:repo.kmeanspp.silhouette_score,代码行数:14,代码来源:MultivariateGaussianEstimator.java


示例18: EM

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * Performs the expectation maximization (EM) algorithm to find the maximum 
 * likelihood estimate (or posterior mode if ridge prior is being used)
 * for the multivariate normal parameters of a dataset with missing values. 
 * @param data          preprocessed dataset with missing values
 * @param t_obs         the complete data sufficient statistics for the observed values
 * @return theta        the maximum likelihood estimate for the parameters of the multivariate normal distribution
 * @throws Exception    if processing goes wrong
 */
private Matrix EM(Instances data, Matrix t_obs) throws Exception {
  
  int p = m_numAttributes; // number of columns
  Matrix theta = new Matrix(p+1, p+1); // parameter matrix
  
  // if numIterations is -1, change to largest int
  int numIterations = m_numIterations;
  if (numIterations < 0) {
    numIterations = Integer.MAX_VALUE;
  }
  
  // starting theta value (means and variances of each column, correlations left at zero)
  // values are standardized so means are 0 and variances are 1
  theta.set(0, 0, -1);
  for (int i = 1; i < data.numAttributes(); i++) {
    theta.set(0, i, 0); // mu_i
    theta.set(i, 0, 0);
    theta.set(i, i, 1); // sigma_ii
  }
  
  double likelihood = logLikelihood(data, theta);
  double deltaLikelihood = Double.MAX_VALUE;
  for (int i = 0; i < numIterations && deltaLikelihood > m_LogLikelihoodThreshold; i++) {
    theta = doEMIteration(data, theta, t_obs);
    double newLikelihood = logLikelihood(data, theta);
    deltaLikelihood = newLikelihood - likelihood;
    likelihood = newLikelihood;
  }
  
  return theta;
}
 
开发者ID:dsibournemouth,项目名称:autoweka,代码行数:41,代码来源:EMImputation.java


示例19: swp

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * Performs the normal sweep operation. 
 * @param g           a matrix
 * @param k           the pivot position
 * @return h          the matrix after being swept on position k
 * @throws Exception  if processing goes wrong
 */
private static Matrix swp(Matrix g, int k) throws Exception {
  
  try {
    return doSweep(g, k, 1); // call actual sweep function with proper parameters
  } catch (Exception e) {
    throw e;
  }
}
 
开发者ID:dsibournemouth,项目名称:autoweka,代码行数:16,代码来源:EMImputation.java


示例20: rsw

import weka.core.matrix.Matrix; //导入依赖的package包/类
/**
 * Performs the reverse sweep operation. 
 * @param g           a matrix
 * @param k           the pivot position
 * @return h          the matrix after being swept on position k
 * @throws Exception  if processing goes wrong
 */
private static Matrix rsw(Matrix g, int k) throws Exception {
  
  try {
    return doSweep(g, k, -1); // call actual sweep function with proper parameters
  } catch (Exception e) {
    throw e;
  }
}
 
开发者ID:dsibournemouth,项目名称:autoweka,代码行数:16,代码来源:EMImputation.java



注:本文中的weka.core.matrix.Matrix类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java PolicyResolverFactory类代码示例发布时间:2022-05-23
下一篇:
Java SQLExceptionConversionDelegate类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap