本文整理汇总了C#中weka.core.Instances.sumOfWeights方法的典型用法代码示例。如果您正苦于以下问题:C# Instances.sumOfWeights方法的具体用法?C# Instances.sumOfWeights怎么用?C# Instances.sumOfWeights使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weka.core.Instances
的用法示例。
在下文中一共展示了Instances.sumOfWeights方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: buildTree
/// <summary> Builds the tree structure with hold out set
///
/// </summary>
/// <param name="train">the data for which the tree structure is to be
/// generated.
/// </param>
/// <param name="test">the test data for potential pruning
/// </param>
/// <param name="keepData">is training Data to be kept?
/// </param>
/// <exception cref="Exception">if something goes wrong
/// </exception>
public virtual void buildTree(Instances train, Instances test, bool keepData)
{
Instances[] localTrain, localTest;
int i;
if (keepData)
{
m_train = train;
}
m_isLeaf = false;
m_isEmpty = false;
m_sons = null;
m_localModel = m_toSelectModel.selectModel(train, test);
m_test = new Distribution(test, m_localModel);
if (m_localModel.numSubsets() > 1)
{
localTrain = m_localModel.split(train);
localTest = m_localModel.split(test);
train = test = null;
m_sons = new ClassifierTree[m_localModel.numSubsets()];
for (i = 0; i < m_sons.Length; i++)
{
m_sons[i] = getNewTree(localTrain[i], localTest[i]);
localTrain[i] = null;
localTest[i] = null;
}
}
else
{
m_isLeaf = true;
if (Utils.eq(train.sumOfWeights(), 0))
m_isEmpty = true;
train = test = null;
}
}
示例2: selectModel
/// <summary> Selects C4.5-type split for the given dataset.</summary>
public override ClassifierSplitModel selectModel(Instances data)
{
double minResult;
//double currentResult;
BinC45Split[] currentModel;
BinC45Split bestModel = null;
NoSplit noSplitModel = null;
double averageInfoGain = 0;
int validModels = 0;
bool multiVal = true;
Distribution checkDistribution;
double sumOfWeights;
int i;
try
{
// Check if all Instances belong to one class or if not
// enough Instances to split.
checkDistribution = new Distribution(data);
noSplitModel = new NoSplit(checkDistribution);
if (Utils.sm(checkDistribution.total(), 2 * m_minNoObj) || Utils.eq(checkDistribution.total(), checkDistribution.perClass(checkDistribution.maxClass())))
return noSplitModel;
// Check if all attributes are nominal and have a
// lot of values.
System.Collections.IEnumerator enu = data.enumerateAttributes();
//UPGRADE_TODO: Method 'java.util.Enumeration.hasMoreElements' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationhasMoreElements'"
while (enu.MoveNext())
{
//UPGRADE_TODO: Method 'java.util.Enumeration.nextElement' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationnextElement'"
weka.core.Attribute attribute = (weka.core.Attribute)enu.Current;
if ((attribute.Numeric) || (Utils.sm((double) attribute.numValues(), (0.3 * (double) m_allData.numInstances()))))
{
multiVal = false;
break;
}
}
currentModel = new BinC45Split[data.numAttributes()];
sumOfWeights = data.sumOfWeights();
// For each attribute.
for (i = 0; i < data.numAttributes(); i++)
{
// Apart from class attribute.
if (i != (data).classIndex())
{
// Get models for current attribute.
currentModel[i] = new BinC45Split(i, m_minNoObj, sumOfWeights);
currentModel[i].buildClassifier(data);
// Check if useful split for current attribute
// exists and check for enumerated attributes with
// a lot of values.
if (currentModel[i].checkModel())
if ((data.attribute(i).Numeric) || (multiVal || Utils.sm((double) data.attribute(i).numValues(), (0.3 * (double) m_allData.numInstances()))))
{
averageInfoGain = averageInfoGain + currentModel[i].infoGain();
validModels++;
}
}
else
currentModel[i] = null;
}
// Check if any useful split was found.
if (validModels == 0)
return noSplitModel;
averageInfoGain = averageInfoGain / (double) validModels;
// Find "best" attribute to split on.
minResult = 0;
for (i = 0; i < data.numAttributes(); i++)
{
if ((i != (data).classIndex()) && (currentModel[i].checkModel()))
// Use 1E-3 here to get a closer approximation to the original
// implementation.
if ((currentModel[i].infoGain() >= (averageInfoGain - 1e-3)) && Utils.gr(currentModel[i].gainRatio(), minResult))
{
bestModel = currentModel[i];
minResult = currentModel[i].gainRatio();
}
}
// Check if useful split was found.
if (Utils.eq(minResult, 0))
return noSplitModel;
// Add all Instances with unknown values for the corresponding
// attribute to the distribution for the model, so that
// the complete distribution is stored with the model.
bestModel.distribution().addInstWithUnknown(data, bestModel.attIndex());
// Set the split point analogue to C45 if attribute numeric.
bestModel.SplitPoint = m_allData;
return bestModel;
//.........这里部分代码省略.........
示例3: newDistribution
/// <summary> Computes new distributions of instances for nodes
/// in tree.
///
/// </summary>
/// <exception cref="Exception">if something goes wrong
/// </exception>
private void newDistribution(Instances data)
{
Instances[] localInstances;
localModel().resetDistribution(data);
m_train = data;
if (!m_isLeaf)
{
localInstances = (Instances[]) localModel().split(data);
for (int i = 0; i < m_sons.Length; i++)
son(i).newDistribution(localInstances[i]);
}
else
{
// Check whether there are some instances at the leaf now!
if (!Utils.eq(data.sumOfWeights(), 0))
{
m_isEmpty = false;
}
}
}
示例4: test
//.........这里部分代码省略.........
System.Console.Out.WriteLine("\nClass is numeric: " + instances.classAttribute().Numeric);
System.Console.Out.WriteLine("\nClasses:\n");
for (i = 0; i < instances.numClasses(); i++)
{
System.Console.Out.WriteLine(instances.classAttribute().value_Renamed(i));
}
System.Console.Out.WriteLine("\nClass values and labels of instances:\n");
for (i = 0; i < instances.numInstances(); i++)
{
Instance inst = instances.instance(i);
System.Console.Out.Write(inst.classValue() + "\t");
System.Console.Out.Write(inst.toString(inst.classIndex()));
if (instances.instance(i).classIsMissing())
{
System.Console.Out.WriteLine("\tis missing");
}
else
{
System.Console.Out.WriteLine();
}
}
// Create random weights.
System.Console.Out.WriteLine("\nCreating random weights for instances.");
for (i = 0; i < instances.numInstances(); i++)
{
instances.instance(i).Weight = random.NextDouble();
}
// Print all instances and their weights (and the sum of weights).
System.Console.Out.WriteLine("\nInstances and their weights:\n");
System.Console.Out.WriteLine(instances.instancesAndWeights());
System.Console.Out.Write("\nSum of weights: ");
System.Console.Out.WriteLine(instances.sumOfWeights());
// Insert an attribute
secondInstances = new Instances(instances);
Attribute testAtt = new Attribute("Inserted");
secondInstances.insertAttributeAt(testAtt, 0);
System.Console.Out.WriteLine("\nSet with inserted attribute:\n");
//UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Out.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'"
System.Console.Out.WriteLine(secondInstances);
System.Console.Out.WriteLine("\nClass name: " + secondInstances.classAttribute().name());
// Delete the attribute
secondInstances.deleteAttributeAt(0);
System.Console.Out.WriteLine("\nSet with attribute deleted:\n");
//UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Out.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'"
System.Console.Out.WriteLine(secondInstances);
System.Console.Out.WriteLine("\nClass name: " + secondInstances.classAttribute().name());
// Test if headers are equal
System.Console.Out.WriteLine("\nHeaders equal: " + instances.equalHeaders(secondInstances) + "\n");
// Print data in internal format.
System.Console.Out.WriteLine("\nData (internal values):\n");
for (i = 0; i < instances.numInstances(); i++)
{
for (j = 0; j < instances.numAttributes(); j++)
{
if (instances.instance(i).isMissing(j))
{
System.Console.Out.Write("? ");
}
else
{
示例5: performIteration
/// <summary> Performs one boosting iteration.</summary>
private void performIteration(double[][] trainYs, double[][] trainFs, double[][] probs, Instances data, double origSumOfWeights)
{
if (m_Debug)
{
System.Console.Error.WriteLine("Training classifier " + (m_NumGenerated + 1));
}
// Build the new models
for (int j = 0; j < m_NumClasses; j++)
{
if (m_Debug)
{
System.Console.Error.WriteLine("\t...for class " + (j + 1) + " (" + m_ClassAttribute.name() + "=" + m_ClassAttribute.value_Renamed(j) + ")");
}
// Make copy because we want to save the weights
Instances boostData = new Instances(data);
// Set instance pseudoclass and weights
for (int i = 0; i < probs.Length; i++)
{
// Compute response and weight
double p = probs[i][j];
double z, actual = trainYs[i][j];
if (actual == 1 - m_Offset)
{
z = 1.0 / p;
if (z > Z_MAX)
{
// threshold
z = Z_MAX;
}
}
else
{
z = (- 1.0) / (1.0 - p);
if (z < - Z_MAX)
{
// threshold
z = - Z_MAX;
}
}
double w = (actual - p) / z;
// Set values for instance
Instance current = boostData.instance(i);
current.setValue(boostData.classIndex(), z);
current.Weight = current.weight() * w;
}
// Scale the weights (helps with some base learners)
double sumOfWeights = boostData.sumOfWeights();
double scalingFactor = (double) origSumOfWeights / sumOfWeights;
for (int i = 0; i < probs.Length; i++)
{
Instance current = boostData.instance(i);
current.Weight = current.weight() * scalingFactor;
}
// Select instances to train the classifier on
Instances trainData = boostData;
if (m_WeightThreshold < 100)
{
trainData = selectWeightQuantile(boostData, (double) m_WeightThreshold / 100);
}
else
{
if (m_UseResampling)
{
double[] weights = new double[boostData.numInstances()];
for (int kk = 0; kk < weights.Length; kk++)
{
weights[kk] = boostData.instance(kk).weight();
}
trainData = boostData.resampleWithWeights(m_RandomInstance, weights);
}
}
// Build the classifier
m_Classifiers[j][m_NumGenerated].buildClassifier(trainData);
}
// Evaluate / increment trainFs from the classifier
for (int i = 0; i < trainFs.Length; i++)
{
double[] pred = new double[m_NumClasses];
double predSum = 0;
for (int j = 0; j < m_NumClasses; j++)
{
pred[j] = m_Shrinkage * m_Classifiers[j][m_NumGenerated].classifyInstance(data.instance(i));
predSum += pred[j];
}
predSum /= m_NumClasses;
for (int j = 0; j < m_NumClasses; j++)
{
trainFs[i][j] += (pred[j] - predSum) * (m_NumClasses - 1) / m_NumClasses;
}
//.........这里部分代码省略.........
示例6: buildClassifier
//.........这里部分代码省略.........
// Make class numeric
Instances trainN = new Instances(train);
trainN.ClassIndex = - 1;
trainN.deleteAttributeAt(classIndex);
trainN.insertAttributeAt(new weka.core.Attribute("'pseudo class'"), classIndex);
trainN.ClassIndex = classIndex;
m_NumericClassData = new Instances(trainN, 0);
// Get class values
int numInstances = train.numInstances();
double[][] tmpArray = new double[numInstances][];
for (int i2 = 0; i2 < numInstances; i2++)
{
tmpArray[i2] = new double[m_NumClasses];
}
double[][] trainFs = tmpArray;
double[][] tmpArray2 = new double[numInstances][];
for (int i3 = 0; i3 < numInstances; i3++)
{
tmpArray2[i3] = new double[m_NumClasses];
}
double[][] trainYs = tmpArray2;
for (int j = 0; j < m_NumClasses; j++)
{
for (int k = 0; k < numInstances; k++)
{
trainYs[k][j] = (train.instance(k).classValue() == j)?1.0 - m_Offset:0.0 + (m_Offset / (double) m_NumClasses);
}
}
// Perform iterations
double[][] probs = initialProbs(numInstances);
m_NumGenerated = 0;
double sumOfWeights = train.sumOfWeights();
for (int j = 0; j < this.NumIterations; j++)
{
performIteration(trainYs, trainFs, probs, trainN, sumOfWeights);
Evaluation eval = new Evaluation(train);
eval.evaluateModel(this, test);
results[j] += eval.correct();
}
}
}
// Find the number of iterations with the lowest error
//UPGRADE_TODO: The equivalent in .NET for field 'java.lang.Double.MAX_VALUE' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
double bestResult = - System.Double.MaxValue;
for (int j = 0; j < this.NumIterations; j++)
{
if (results[j] > bestResult)
{
bestResult = results[j];
bestNumIterations = j;
}
}
if (m_Debug)
{
System.Console.Error.WriteLine("Best result for " + bestNumIterations + " iterations: " + bestResult);
}
}
// Build classifier on all the data
int numInstances2 = data.numInstances();
double[][] trainFs2 = new double[numInstances2][];
for (int i4 = 0; i4 < numInstances2; i4++)
{