本文整理了Java中weka.core.Utils.normalize()
方法的一些代码示例,展示了Utils.normalize()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Utils.normalize()
方法的具体详情如下:
包路径:weka.core.Utils
类名称:Utils
方法名:normalize
[英]Normalizes the doubles in the array by their sum.
[中]通过求和对数组中的双精度进行规格化。
代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable
/**
* Normalizes the doubles in the array by their sum.
*
* @param doubles the array of double
* @exception IllegalArgumentException if sum is Zero or NaN
*/
public static void normalize(double[] doubles) {
double sum = 0;
for (double d : doubles) {
sum += d;
}
normalize(doubles, sum);
}
代码示例来源:origin: Waikato/weka-trunk
/**
* Normalizes the doubles in the array by their sum.
*
* @param doubles the array of double
* @exception IllegalArgumentException if sum is Zero or NaN
*/
public static void normalize(double[] doubles) {
double sum = 0;
for (double d : doubles) {
sum += d;
}
normalize(doubles, sum);
}
代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable
/**
* Computes probabilities from F scores
*
* @param Fs the F scores
* @return the computed probabilities
*/
private double[] probs(double[] Fs) {
double maxF = -Double.MAX_VALUE;
for (int i = 0; i < Fs.length; i++) {
if (Fs[i] > maxF) {
maxF = Fs[i];
}
}
double sum = 0;
double[] probs = new double[Fs.length];
for (int i = 0; i < Fs.length; i++) {
probs[i] = Math.exp(Fs[i] - maxF);
sum += probs[i];
}
Utils.normalize(probs, sum);
return probs;
}
代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable
/**
* Computes the p-values (probabilities for the classes) from the F-values of
* the logistic model.
*
* @param Fs the F-values
* @return the p-values
*/
protected double[] probs(double[] Fs) {
double maxF = -Double.MAX_VALUE;
for (double element : Fs) {
if (element > maxF) {
maxF = element;
}
}
double sum = 0;
double[] probs = new double[Fs.length];
for (int i = 0; i < Fs.length; i++) {
probs[i] = Math.exp(Fs[i] - maxF);
sum += probs[i];
}
Utils.normalize(probs, sum);
return probs;
}
代码示例来源:origin: Waikato/weka-trunk
/**
* Computes probabilities from F scores
*
* @param Fs the F scores
* @return the computed probabilities
*/
private double[] probs(double[] Fs) {
double maxF = -Double.MAX_VALUE;
for (int i = 0; i < Fs.length; i++) {
if (Fs[i] > maxF) {
maxF = Fs[i];
}
}
double sum = 0;
double[] probs = new double[Fs.length];
for (int i = 0; i < Fs.length; i++) {
probs[i] = Math.exp(Fs[i] - maxF);
sum += probs[i];
}
Utils.normalize(probs, sum);
return probs;
}
代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable
/**
* Converts an array containing the natural logarithms of probabilities stored
* in a vector back into probabilities. The probabilities are assumed to sum
* to one.
*
* @param a an array holding the natural logarithms of the probabilities
* @return the converted array
*/
public static double[] logs2probs(double[] a) {
double max = a[maxIndex(a)];
double sum = 0.0;
double[] result = new double[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = Math.exp(a[i] - max);
sum += result[i];
}
normalize(result, sum);
return result;
}
代码示例来源:origin: Waikato/weka-trunk
/**
* Converts an array containing the natural logarithms of probabilities stored
* in a vector back into probabilities. The probabilities are assumed to sum
* to one.
*
* @param a an array holding the natural logarithms of the probabilities
* @return the converted array
*/
public static double[] logs2probs(double[] a) {
double max = a[maxIndex(a)];
double sum = 0.0;
double[] result = new double[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = Math.exp(a[i] - max);
sum += result[i];
}
normalize(result, sum);
return result;
}
代码示例来源:origin: nz.ac.waikato.cms.moa/moa
@Override
public double[] getVotesForInstance(Instance inst) {
double[] Pr = new double[inst.numClasses()];
for (int i = 0; i < this.experts.size(); i++) {
double[] pr = this.experts.get(i).getVotesForInstance(inst);
int yHat = Utils.maxIndex(pr);
Pr[yHat] += this.weights.get(i);
} // for
Utils.normalize(Pr);
return Pr;
}
代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable
/**
* Calculates the class membership probabilities for the given test
* instance.
*
* @param instance the instance to be classified
* @return predicted class probability distribution
* @throws Exception if there is a problem generating the prediction
*/
public double [] distributionForInstance(Instance instance) throws Exception {
double[] probOfClassGivenDoc = new double[m_numClasses];
//calculate the array of log(Pr[D|C])
double[] logDocGivenClass = new double[m_numClasses];
for (int h = 0; h < m_numClasses; h++) {
logDocGivenClass[h] = probOfDocGivenClass(instance, h);
}
double max = logDocGivenClass[Utils.maxIndex(logDocGivenClass)];
for (int i = 0; i < m_numClasses; i++) {
probOfClassGivenDoc[i] = Math.exp(logDocGivenClass[i] - max) * m_probOfClass[i];
}
Utils.normalize(probOfClassGivenDoc);
return probOfClassGivenDoc;
}
代码示例来源:origin: Waikato/weka-trunk
/**
* Calculates the class membership probabilities for the given test
* instance.
*
* @param instance the instance to be classified
* @return predicted class probability distribution
* @throws Exception if there is a problem generating the prediction
*/
public double [] distributionForInstance(Instance instance) throws Exception {
double[] probOfClassGivenDoc = new double[m_numClasses];
//calculate the array of log(Pr[D|C])
double[] logDocGivenClass = new double[m_numClasses];
for (int h = 0; h < m_numClasses; h++) {
logDocGivenClass[h] = probOfDocGivenClass(instance, h);
}
double max = logDocGivenClass[Utils.maxIndex(logDocGivenClass)];
for (int i = 0; i < m_numClasses; i++) {
probOfClassGivenDoc[i] = Math.exp(logDocGivenClass[i] - max) * m_probOfClass[i];
}
Utils.normalize(probOfClassGivenDoc);
return probOfClassGivenDoc;
}
代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable
/**
* Returns the class distribution for an instance.
*/
@Override
public double[] distributionForInstance(Instance inst) throws Exception {
// Does the metric produce thresholds that need to be applied?
if (m_thresholds != null) {
double[] dist = m_IterativeClassifier.distributionForInstance(inst);
double[] newDist = new double[dist.length];
for (int i = 0; i < dist.length; i++) {
if (dist[i] >= m_thresholds[i]) {
newDist[i] = 1.0;
}
}
Utils.normalize(newDist); // Could have multiple 1.0 entries
return newDist;
} else {
return m_IterativeClassifier.distributionForInstance(inst);
}
}
代码示例来源:origin: Waikato/weka-trunk
/**
* Returns the class distribution for an instance.
*/
@Override
public double[] distributionForInstance(Instance inst) throws Exception {
// Does the metric produce thresholds that need to be applied?
if (m_thresholds != null) {
double[] dist = m_IterativeClassifier.distributionForInstance(inst);
double[] newDist = new double[dist.length];
for (int i = 0; i < dist.length; i++) {
if (dist[i] >= m_thresholds[i]) {
newDist[i] = 1.0;
}
}
Utils.normalize(newDist); // Could have multiple 1.0 entries
return newDist;
} else {
return m_IterativeClassifier.distributionForInstance(inst);
}
}
代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable
/**
* Return a class probability distribution computed from the frequency counts
* at this node
*
* @param inst the instance to get a prediction for
* @param classAtt the class attribute
* @return a class probability distribution
* @throws Exception if a problem occurs
*/
public double[] getDistribution(Instance inst, Attribute classAtt)
throws Exception {
double[] dist = new double[classAtt.numValues()];
for (int i = 0; i < classAtt.numValues(); i++) {
WeightMass w = m_classDistribution.get(classAtt.value(i));
if (w != null) {
dist[i] = w.m_weight;
} else {
dist[i] = 1.0;
}
}
Utils.normalize(dist);
return dist;
}
代码示例来源:origin: Waikato/weka-trunk
/**
* Return a class probability distribution computed from the frequency counts
* at this node
*
* @param inst the instance to get a prediction for
* @param classAtt the class attribute
* @return a class probability distribution
* @throws Exception if a problem occurs
*/
public double[] getDistribution(Instance inst, Attribute classAtt)
throws Exception {
double[] dist = new double[classAtt.numValues()];
for (int i = 0; i < classAtt.numValues(); i++) {
WeightMass w = m_classDistribution.get(classAtt.value(i));
if (w != null) {
dist[i] = w.m_weight;
} else {
dist[i] = 1.0;
}
}
Utils.normalize(dist);
return dist;
}
代码示例来源:origin: nz.ac.waikato.cms.weka/meka
@Override
public double[] convertDistribution(double r[], int c) {
double newr[] = new double[c];
for(int i = 0; i < r.length; i++) {
double d[] = MLUtils.fromBitString(m_InstancesTemplate.classAttribute().value(i));
for(int j = 0; j < d.length; j++) {
newr[j] += (d[j] * r[i]);
}
}
try {
Utils.normalize(newr);
} catch(Exception e) {
newr = new double[c];
}
return newr;
}
代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable
/**
* calculate prior probabilites for the clusters
*
* @param inst the instances
* @throws Exception if priors can't be calculated
**/
private void estimate_priors(Instances inst) throws Exception {
for (int i = 0; i < m_num_clusters; i++) {
m_priorsPrev[i] = m_priors[i];
m_priors[i] = 0.0;
}
for (int i = 0; i < inst.numInstances(); i++) {
for (int j = 0; j < m_num_clusters; j++) {
m_priors[j] += inst.instance(i).weight() * m_weights[i][j];
}
}
Utils.normalize(m_priors);
}
代码示例来源:origin: Waikato/weka-trunk
/**
* calculate prior probabilites for the clusters
*
* @param inst the instances
* @throws Exception if priors can't be calculated
**/
private void estimate_priors(Instances inst) throws Exception {
for (int i = 0; i < m_num_clusters; i++) {
m_priorsPrev[i] = m_priors[i];
m_priors[i] = 0.0;
}
for (int i = 0; i < inst.numInstances(); i++) {
for (int j = 0; j < m_num_clusters; j++) {
m_priors[j] += inst.instance(i).weight() * m_weights[i][j];
}
}
Utils.normalize(m_priors);
}
代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable
@Override
public double[] distributionForInstance(Instance instance) throws Exception {
if (m_canopies == null || m_canopies.size() == 0) {
throw new Exception("No canopies available to cluster with!");
}
double[] d = new double[numberOfClusters()];
if (m_missingValuesReplacer != null) {
m_missingValuesReplacer.input(instance);
instance = m_missingValuesReplacer.output();
}
for (int i = 0; i < m_canopies.numInstances(); i++) {
double distance = m_distanceFunction.distance(instance,
m_canopies.instance(i));
d[i] = 1.0 / (1.0 + distance);
}
Utils.normalize(d);
return d;
}
代码示例来源:origin: Waikato/weka-trunk
@Override
public double[] distributionForInstance(Instance instance) throws Exception {
if (m_canopies == null || m_canopies.size() == 0) {
throw new Exception("No canopies available to cluster with!");
}
double[] d = new double[numberOfClusters()];
if (m_missingValuesReplacer != null) {
m_missingValuesReplacer.input(instance);
instance = m_missingValuesReplacer.output();
}
for (int i = 0; i < m_canopies.numInstances(); i++) {
double distance = m_distanceFunction.distance(instance,
m_canopies.instance(i));
d[i] = 1.0 / (1.0 + distance);
}
Utils.normalize(d);
return d;
}
代码示例来源:origin: nz.ac.waikato.cms.weka/weka-stable
/**
* Returns class probabilities for an instance.
*
* @param instance the instance to compute the distribution for
* @return the class probabilities
* @throws Exception if distribution can't be computed successfully
*/
@Override
public double[] distributionForInstance(Instance inst) throws Exception {
Attribute classAtt = inst.classAttribute();
double[] pred = new double[classAtt.numValues()];
if (m_root != null) {
LeafNode l = m_root.leafForInstance(inst, null, null);
HNode actualNode = l.m_theNode;
if (actualNode == null) {
actualNode = l.m_parentNode;
}
pred = actualNode.getDistribution(inst, classAtt);
} else {
// all class values equally likely
for (int i = 0; i < classAtt.numValues(); i++) {
pred[i] = 1;
}
Utils.normalize(pred);
}
// Utils.normalize(pred);
return pred;
}
内容来源于网络,如有侵权,请联系作者删除!