当前位置: 首页>>代码示例>>Java>>正文


Java Matrix.times方法代码示例

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


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

示例1: 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

示例2: convertInstance

import weka.core.matrix.Matrix; //导入方法依赖的package包/类
/**
 * Transform an instance in original (unnormalized) format
 * @param instance an instance in the original (unnormalized) format
 * @return a transformed instance
 * @throws Exception if instance can't be transformed
 */
public Instance convertInstance(Instance instance) throws Exception {
  if (m_s == null) {
    throw new Exception("convertInstance: Latent Semantic Analysis not " +
                         "performed yet.");
  }
  
  // array to hold new attribute values
  double [] newValues = new double[m_outputNumAttributes];
  
  // apply filters so new instance is in same format as training instances
  Instance tempInstance = (Instance)instance.copy();
  if (!instance.dataset().equalHeaders(m_trainHeader)) {
    throw new Exception("Can't convert instance: headers don't match: " +
    "LatentSemanticAnalysis");
  }
  // replace missing values
  m_replaceMissingFilter.input(tempInstance);
  m_replaceMissingFilter.batchFinished();
  tempInstance = m_replaceMissingFilter.output();
  // normalize
  if (m_normalize) {
    m_normalizeFilter.input(tempInstance);
    m_normalizeFilter.batchFinished();
    tempInstance = m_normalizeFilter.output();
  }
  // convert nominal attributes to binary
  m_nominalToBinaryFilter.input(tempInstance);
  m_nominalToBinaryFilter.batchFinished();
  tempInstance = m_nominalToBinaryFilter.output();
  // remove class/other attributes
  if (m_attributeFilter != null) {
    m_attributeFilter.input(tempInstance);
    m_attributeFilter.batchFinished();
    tempInstance = m_attributeFilter.output();
  }
  
  // record new attribute values
  if (m_hasClass) { // copy class value
    newValues[m_outputNumAttributes - 1] = instance.classValue();
  }
  double [][] oldInstanceValues = new double[1][m_numAttributes];
  oldInstanceValues[0] = tempInstance.toDoubleArray();
  Matrix instanceVector = new Matrix(oldInstanceValues); // old attribute values
  instanceVector = instanceVector.times(m_transformationMatrix); // new attribute values
  for (int i = 0; i < m_actualRank; i++) {
    newValues[i] = instanceVector.get(0, i);
  }
  
  // return newly transformed instance
  if (instance instanceof SparseInstance) {
    return new SparseInstance(instance.weight(), newValues);
  } else {
    return new Instance(instance.weight(), newValues);
  }
}
 
开发者ID:williamClanton,项目名称:jbossBA,代码行数:62,代码来源:LatentSemanticAnalysis.java


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