本文整理汇总了C#中weka.core.Instances.instance方法的典型用法代码示例。如果您正苦于以下问题:C# Instances.instance方法的具体用法?C# Instances.instance怎么用?C# Instances.instance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weka.core.Instances
的用法示例。
在下文中一共展示了Instances.instance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Classify
public static string Classify(bool useRubine, float duration, bool righthandedness, List<float> SpeakerAngles, PointCollection pointHist, StylusPointCollection S, List<List<int>> hist, List<List<int>> ihist)
{
// Convert all parameters to format used in GestureTests
List<Vector2> InterpretedPoints = new List<Vector2>();
List<Vector2> StylusPoints = new List<Vector2>();
List<Vector2> VelocityHistory = new List<Vector2>();
List<Vector2> InverseVelocityHistory = new List<Vector2>();
foreach(Point P in pointHist)
InterpretedPoints.Add(new Vector2((float)P.X,(float)P.Y));
foreach(StylusPoint P in S)
StylusPoints.Add(new Vector2((float)P.X,(float)P.Y));
for (int i = 0; i < hist[0].Count; i++)
{
VelocityHistory.Add(new Vector2(hist[0][i], hist[1][i]));
InverseVelocityHistory.Add(new Vector2(ihist[0][i], ihist[1][i]));
}
// Create a new Sample, compute the features, and classify
GS = new GestureSample(GestureTests.Types.GestureType.unknown, righthandedness,duration,SpeakerAngles,InterpretedPoints,StylusPoints,VelocityHistory,InverseVelocityHistory);
GS.ComputeFeatures(GestureFeatures.PointsStroke);
if (useRubine)
return EC.Recognizer.Classify(GS).ToString();
WriteARFF();
Instances test = new Instances(new java.io.FileReader("outfile.arff"));
test.setClassIndex(0);
double clsLabel = cls.classifyInstance(test.instance(0));
test.instance(0).setClassValue(clsLabel);
// Return the appropriate label
return ((GestureType2D)((int)clsLabel+1)).ToString();
}
示例2: Main
public static void Main(string[] args) {
try {
int runs = 1;
string algo = "";
string data = "";
if(args.Length>0) runs = Convert.ToInt32(args[0]);
if(args.Length>1) algo = args[1];
if(args.Length>2) data = args[2];
Stopwatch read = new Stopwatch(),
build = new Stopwatch(),
classify = new Stopwatch();
for (int cnt=0; cnt<runs; cnt++) {
read.Start();
Instances train = new Instances(new java.io.FileReader(data+"train.arff"));
train.setClassIndex(train.numAttributes() - 1);
Instances test = new Instances(new java.io.FileReader(data+"test.arff"));
test.setClassIndex(test.numAttributes() - 1);
read.Stop();
Classifier[] clList = {
new weka.classifiers.bayes.NaiveBayes(),
new weka.classifiers.trees.RandomForest(),
new weka.classifiers.trees.J48(),
new weka.classifiers.functions.MultilayerPerceptron(),
new weka.classifiers.rules.ConjunctiveRule(),
new weka.classifiers.functions.SMO()
};
build.Start();
foreach (Classifier classifier in clList) {
if(algo.Equals("") || algo.Equals("All") || classifier.getClass().getSimpleName().Equals(algo))
classifier.buildClassifier(train);
}
build.Stop();
classify.Start();
foreach (Classifier classifier in clList) {
if(algo.Equals("") || algo.Equals("All") || classifier.getClass().getSimpleName().Equals(algo)) {
int numCorrect = 0;
for (int i = 0; i < test.numInstances(); i++)
{
if (classifier.classifyInstance(test.instance(i)) == test.instance(i).classValue())
numCorrect++;
}
//Console.Write(classifier.getClass().getSimpleName() + "\t" + numCorrect + " out of " + test.numInstances() + " correct (" +(100.0 * numCorrect / test.numInstances()) + "%)");
}
}
classify.Stop();
}
Console.WriteLine("{\""+ algo + "\"," + read.ElapsedMilliseconds + "," + build.ElapsedMilliseconds + "," + classify.ElapsedMilliseconds + "," + (read.ElapsedMilliseconds+build.ElapsedMilliseconds+classify.ElapsedMilliseconds)+"};");
if(args.Length>3) Console.ReadLine();
} catch (java.lang.Exception e){
e.printStackTrace();
}
}
示例3: evaluateModel
//.........这里部分代码省略.........
catch (Exception e)
{
Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
throw;
}
finally
{
objectStream.Close();
//fs.Close();
}
objectInputStream.Close();
}
// backup of fully setup classifier for cross-validation
classifierBackup = Classifier.makeCopy(classifier);
// Build the classifier if no object file provided
if ((classifier is UpdateableClassifier) && (testFileName.Length != 0) && (costMatrix == null) && (trainFileName.Length != 0))
{
// Build classifier incrementally
trainingEvaluation.Priors = train;
testingEvaluation.Priors = train;
trainTimeStart = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;
if (objectInputFileName.Length == 0)
{
classifier.buildClassifier(train);
}
while (train.readInstance(trainReader))
{
trainingEvaluation.updatePriors(train.instance(0));
testingEvaluation.updatePriors(train.instance(0));
((UpdateableClassifier) classifier).updateClassifier(train.instance(0));
train.delete(0);
}
trainTimeElapsed = (System.DateTime.Now.Ticks - 621355968000000000) / 10000 - trainTimeStart;
trainReader.Close();
}
else if (objectInputFileName.Length == 0)
{
// Build classifier in one go
tempTrain = new Instances(train);
trainingEvaluation.Priors = tempTrain;
testingEvaluation.Priors = tempTrain;
trainTimeStart = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;
classifier.buildClassifier(tempTrain);
trainTimeElapsed = (System.DateTime.Now.Ticks - 621355968000000000) / 10000 - trainTimeStart;
}
// Save the classifier if an object output file is provided
if (objectOutputFileName.Length != 0)
{
//UPGRADE_TODO: Constructor 'java.io.FileOutputStream.FileOutputStream' was converted to 'System.IO.FileStream.FileStream' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioFileOutputStreamFileOutputStream_javalangString'"
System.IO.Stream os = new System.IO.FileStream(objectOutputFileName, System.IO.FileMode.Create);
if (objectOutputFileName.EndsWith(".gz"))
{
//UPGRADE_ISSUE: Constructor 'java.util.zip.GZIPOutputStream.GZIPOutputStream' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javautilzipGZIPOutputStream'"
os = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(os);
}
//UPGRADE_TODO: Class 'java.io.ObjectOutputStream' was converted to 'System.IO.BinaryWriter' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioObjectOutputStream'"
System.IO.BinaryWriter objectOutputStream = new System.IO.BinaryWriter(os);
//UPGRADE_TODO: Method 'java.io.ObjectOutputStream.writeObject' was converted to 'SupportClass.Serialize' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioObjectOutputStreamwriteObject_javalangObject'"
示例4: toPrintClassifications
/// <summary> Prints the predictions for the given dataset into a String variable.
///
/// </summary>
/// <param name="classifier the">classifier to use
/// </param>
/// <param name="train the">training data
/// </param>
/// <param name="testFileName the">name of the test file
/// </param>
/// <param name="classIndex the">class index
/// </param>
/// <param name="attributesToOutput the">indices of the attributes to output
/// </param>
/// <returns> the generated predictions for the attribute range
/// </returns>
/// <throws> Exception if test file cannot be opened </throws>
protected internal static System.String toPrintClassifications(Classifier classifier, Instances train, System.String testFileName, int classIndex, Range attributesToOutput)
{
System.Text.StringBuilder text = new System.Text.StringBuilder();
if (testFileName.Length != 0)
{
System.IO.StreamReader testReader = null;
try
{
//UPGRADE_TODO: The differences in the expected value of parameters for constructor 'java.io.BufferedReader.BufferedReader' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
//UPGRADE_WARNING: At least one expression was used more than once in the target code. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1181'"
//UPGRADE_TODO: Constructor 'java.io.FileReader.FileReader' was converted to 'System.IO.StreamReader' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
testReader = new System.IO.StreamReader(new System.IO.StreamReader(testFileName, System.Text.Encoding.Default).BaseStream, new System.IO.StreamReader(testFileName, System.Text.Encoding.Default).CurrentEncoding);
}
catch (System.Exception e)
{
//UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
throw new System.Exception("Can't open file " + e.Message + '.');
}
Instances test = new Instances(testReader, 1);
if (classIndex != - 1)
{
test.ClassIndex = classIndex - 1;
}
else
{
test.ClassIndex = test.numAttributes() - 1;
}
int i = 0;
while (test.readInstance(testReader))
{
Instance instance = test.instance(0);
Instance withMissing = (Instance) instance.copy();
withMissing.Dataset = test;
double predValue = ((Classifier) classifier).classifyInstance(withMissing);
if (test.classAttribute().Numeric)
{
if (Instance.isMissingValue(predValue))
{
text.Append(i + " missing ");
}
else
{
text.Append(i + " " + predValue + " ");
}
if (instance.classIsMissing())
{
text.Append("missing");
}
else
{
text.Append(instance.classValue());
}
text.Append(" " + attributeValuesString(withMissing, attributesToOutput) + "\n");
}
else
{
if (Instance.isMissingValue(predValue))
{
text.Append(i + " missing ");
}
else
{
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
text.Append(i + " " + test.classAttribute().value_Renamed((int) predValue) + " ");
}
if (Instance.isMissingValue(predValue))
{
text.Append("missing ");
}
else
{
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
text.Append(classifier.distributionForInstance(withMissing)[(int) predValue] + " ");
}
text.Append(instance.toString(instance.classIndex()) + " " + attributeValuesString(withMissing, attributesToOutput) + "\n");
}
test.delete(0);
i++;
}
testReader.Close();
}
return text.ToString();
}
示例5: resetDistribution
/// <summary> Sets distribution associated with model.</summary>
public override void resetDistribution(Instances data)
{
Instances insts = new Instances(data, data.numInstances());
for (int i = 0; i < data.numInstances(); i++)
{
if (whichSubset(data.instance(i)) > - 1)
{
insts.add(data.instance(i));
}
}
Distribution newD = new Distribution(insts, this);
newD.addInstWithUnknown(data, m_attIndex);
m_distribution = newD;
}
示例6: handleNumericAttribute
/// <summary> Creates split on numeric attribute.
///
/// </summary>
/// <exception cref="Exception">if something goes wrong
/// </exception>
private void handleNumericAttribute(Instances trainInstances)
{
int firstMiss;
int next = 1;
int last = 0;
int splitIndex = - 1;
double currentInfoGain;
double defaultEnt;
double minSplit;
Instance instance;
int i;
// Current attribute is a numeric attribute.
m_distribution = new Distribution(2, trainInstances.numClasses());
// Only Instances with known values are relevant.
System.Collections.IEnumerator enu = trainInstances.enumerateInstances();
i = 0;
//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'"
instance = (Instance) enu.Current;
if (instance.isMissing(m_attIndex))
break;
m_distribution.add(1, instance);
i++;
}
firstMiss = i;
// Compute minimum number of Instances required in each
// subset.
minSplit = 0.1 * (m_distribution.total()) / ((double) trainInstances.numClasses());
if (Utils.smOrEq(minSplit, m_minNoObj))
minSplit = m_minNoObj;
else if (Utils.gr(minSplit, 25))
minSplit = 25;
// Enough Instances with known values?
if (Utils.sm((double) firstMiss, 2 * minSplit))
return ;
// Compute values of criteria for all possible split
// indices.
defaultEnt = infoGainCrit.oldEnt(m_distribution);
while (next < firstMiss)
{
if (trainInstances.instance(next - 1).value_Renamed(m_attIndex) + 1e-5 < trainInstances.instance(next).value_Renamed(m_attIndex))
{
// Move class values for all Instances up to next
// possible split point.
m_distribution.shiftRange(1, 0, trainInstances, last, next);
// Check if enough Instances in each subset and compute
// values for criteria.
if (Utils.grOrEq(m_distribution.perBag(0), minSplit) && Utils.grOrEq(m_distribution.perBag(1), minSplit))
{
currentInfoGain = infoGainCrit.splitCritValue(m_distribution, m_sumOfWeights, defaultEnt);
if (Utils.gr(currentInfoGain, m_infoGain))
{
m_infoGain = currentInfoGain;
splitIndex = next - 1;
}
m_index++;
}
last = next;
}
next++;
}
// Was there any useful split?
if (m_index == 0)
return ;
// Compute modified information gain for best split.
m_infoGain = m_infoGain - (Utils.log2(m_index) / m_sumOfWeights);
if (Utils.smOrEq(m_infoGain, 0))
return ;
// Set instance variables' values to values for
// best split.
m_numSubsets = 2;
m_splitPoint = (trainInstances.instance(splitIndex + 1).value_Renamed(m_attIndex) + trainInstances.instance(splitIndex).value_Renamed(m_attIndex)) / 2;
// In case we have a numerical precision problem we need to choose the
// smaller value
if (m_splitPoint == trainInstances.instance(splitIndex + 1).value_Renamed(m_attIndex))
{
m_splitPoint = trainInstances.instance(splitIndex).value_Renamed(m_attIndex);
}
// Restore distributioN for best split.
//.........这里部分代码省略.........
示例7: buildClassifier
public override void buildClassifier(Instances insts)
{
if (insts.checkForStringAttributes())
{
throw new Exception("Cannot handle string attributes!");
}
if (insts.numClasses() > 2)
{
throw new System.Exception("Can only handle two-class datasets!");
}
if (insts.classAttribute().Numeric)
{
throw new Exception("Can't handle a numeric class!");
}
// Filter data
m_Train = new Instances(insts);
m_Train.deleteWithMissingClass();
m_ReplaceMissingValues = new ReplaceMissingValues();
m_ReplaceMissingValues.setInputFormat(m_Train);
m_Train = Filter.useFilter(m_Train, m_ReplaceMissingValues);
m_NominalToBinary = new NominalToBinary();
m_NominalToBinary.setInputFormat(m_Train);
m_Train = Filter.useFilter(m_Train, m_NominalToBinary);
/** Randomize training data */
//UPGRADE_TODO: The differences in the expected value of parameters for constructor 'java.util.Random.Random' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
m_Train.randomize(new System.Random((System.Int32) m_Seed));
/** Make space to store perceptrons */
m_Additions = new int[m_MaxK + 1];
m_IsAddition = new bool[m_MaxK + 1];
m_Weights = new int[m_MaxK + 1];
/** Compute perceptrons */
m_K = 0;
for (int it = 0; it < m_NumIterations; it++)
{
for (int i = 0; i < m_Train.numInstances(); i++)
{
Instance inst = m_Train.instance(i);
if (!inst.classIsMissing())
{
int prediction = makePrediction(m_K, inst);
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
int classValue = (int) inst.classValue();
if (prediction == classValue)
{
m_Weights[m_K]++;
}
else
{
m_IsAddition[m_K] = (classValue == 1);
m_Additions[m_K] = i;
m_K++;
m_Weights[m_K]++;
}
if (m_K == m_MaxK)
{
//UPGRADE_NOTE: Labeled break statement was changed to a goto statement. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1012'"
goto out_brk;
}
}
}
}
//UPGRADE_NOTE: Label 'out_brk' was added. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1011'"
out_brk: ;
}
示例8: shiftRange
/// <summary> Shifts all instances in given range from one bag to another one.
///
/// </summary>
/// <exception cref="Exception">if something goes wrong
/// </exception>
public void shiftRange(int from, int to, Instances source, int startIndex, int lastPlusOne)
{
int classIndex;
double weight;
Instance instance;
int i;
for (i = startIndex; i < lastPlusOne; i++)
{
instance = (Instance) source.instance(i);
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
classIndex = (int) instance.classValue();
weight = instance.weight();
m_perClassPerBag[from][classIndex] -= weight;
m_perClassPerBag[to][classIndex] += weight;
m_perBag[from] -= weight;
m_perBag[to] += weight;
}
}
示例9: mergeInstances
/// <summary> Merges two sets of Instances together. The resulting set will have
/// all the attributes of the first set plus all the attributes of the
/// second set. The number of instances in both sets must be the same.
///
/// </summary>
/// <param name="first">the first set of Instances
/// </param>
/// <param name="second">the second set of Instances
/// </param>
/// <returns> the merged set of Instances
/// </returns>
/// <exception cref="IllegalArgumentException">if the datasets are not the same size
/// </exception>
public static Instances mergeInstances(Instances first, Instances second)
{
if (first.numInstances() != second.numInstances())
{
throw new System.ArgumentException("Instance sets must be of the same size");
}
// Create the vector of merged attributes
FastVector newAttributes = new FastVector();
for (int i = 0; i < first.numAttributes(); i++)
{
newAttributes.addElement(first.attribute(i));
}
for (int i = 0; i < second.numAttributes(); i++)
{
newAttributes.addElement(second.attribute(i));
}
// Create the set of Instances
Instances merged = new Instances(first.relationName() + '_' + second.relationName(), newAttributes, first.numInstances());
// Merge each instance
for (int i = 0; i < first.numInstances(); i++)
{
merged.add(first.instance(i).mergeInstance(second.instance(i)));
}
return merged;
}
示例10: delRange
/// <summary> Deletes all instances in given range from given bag.
///
/// </summary>
/// <exception cref="Exception">if something goes wrong
/// </exception>
public void delRange(int bagIndex, Instances source, int startIndex, int lastPlusOne)
{
double sumOfWeights = 0;
int classIndex;
Instance instance;
int i;
for (i = startIndex; i < lastPlusOne; i++)
{
instance = (Instance) source.instance(i);
//UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
classIndex = (int) instance.classValue();
sumOfWeights = sumOfWeights + instance.weight();
m_perClassPerBag[bagIndex][classIndex] -= instance.weight();
m_perClass[classIndex] -= instance.weight();
}
m_perBag[bagIndex] -= sumOfWeights;
totaL -= sumOfWeights;
}
示例11: resampleWithWeights
/// <summary> Creates a new dataset of the same size using random sampling
/// with replacement according to the given weight vector. The
/// weights of the instances in the new dataset are set to one.
/// The length of the weight vector has to be the same as the
/// number of instances in the dataset, and all weights have to
/// be positive.
///
/// </summary>
/// <param name="random">a random number generator
/// </param>
/// <param name="weights">the weight vector
/// </param>
/// <returns> the new dataset
/// </returns>
/// <exception cref="IllegalArgumentException">if the weights array is of the wrong
/// length or contains negative weights.
/// </exception>
public virtual Instances resampleWithWeights(System.Random random, double[] weights)
{
if (weights.Length != numInstances())
{
throw new System.ArgumentException("weights.length != numInstances.");
}
Instances newData = new Instances(this, numInstances());
if (numInstances() == 0)
{
return newData;
}
double[] probabilities = new double[numInstances()];
double sumProbs = 0, sumOfWeights = Utils.sum(weights);
for (int i = 0; i < numInstances(); i++)
{
sumProbs += random.NextDouble();
probabilities[i] = sumProbs;
}
Utils.normalize(probabilities, sumProbs / sumOfWeights);
// Make sure that rounding errors don't mess things up
probabilities[numInstances() - 1] = sumOfWeights;
int k = 0; int l = 0;
sumProbs = 0;
while ((k < numInstances() && (l < numInstances())))
{
if (weights[l] < 0)
{
throw new System.ArgumentException("Weights have to be positive.");
}
sumProbs += weights[l];
while ((k < numInstances()) && (probabilities[k] <= sumProbs))
{
newData.add(instance(l));
newData.instance(k).Weight = 1;
k++;
}
l++;
}
return newData;
}
示例12: classifyTest
// Test the classification result of each map that a user played,
// with the data available as if they were playing through it
public static void classifyTest(String dataString, String playerID)
{
try {
java.io.StringReader stringReader = new java.io.StringReader(dataString);
java.io.BufferedReader buffReader = new java.io.BufferedReader(stringReader);
/* NOTE THAT FOR NAIVE BAYES ALL WEIGHTS CAN BE = 1*/
//weka.core.converters.ConverterUtils.DataSource source = new weka.core.converters.ConverterUtils.DataSource("iris.arff");
weka.core.Instances thisData = new weka.core.Instances(buffReader); //source.getDataSet();
if (thisData.classIndex() == -1)
thisData.setClassIndex(thisData.numAttributes() - 1);
weka.core.Instances thisUniqueData = new weka.core.Instances(thisData);
if (thisUniqueData.classIndex() == -1)
thisUniqueData.setClassIndex(thisUniqueData.numAttributes() - 1);
thisUniqueData.delete();
if (allUniqueData == null) {
allUniqueData = new weka.core.Instances(thisData);
if (allUniqueData.classIndex() == -1)
allUniqueData.setClassIndex(allUniqueData.numAttributes() - 1);
allUniqueData.delete();
}
weka.core.InstanceComparator com = new weka.core.InstanceComparator(false);
for (int i = 0; i < thisData.numInstances(); i++)
{
bool dup = false;
for (int j = 0; j < allUniqueData.numInstances(); j++)
{
if (com.compare(thisData.instance(i),allUniqueData.instance(j)) == 0)
{
Debug.Log("Duplicate found!");
dup = true;
break;
}
}
if (!dup)
allUniqueData.add(thisData.instance(i));
else
dupInstances++;
}
for (int i = 0; i < thisData.numInstances(); i++)
{
bool dup = false;
for (int j = 0; j < thisUniqueData.numInstances(); j++)
{
if (com.compare(thisData.instance(i),thisUniqueData.instance(j)) == 0)
{
Debug.Log("Duplicate found!");
dup = true;
break;
}
}
if (!dup)
thisUniqueData.add(thisData.instance(i));
else
dupInstancesSamePlayer++;
}
//Debug.Log("All Data Instance Count = " + thisData.numInstances());
//Debug.Log("Unique Data Instance Count = " + thisUniqueData.numInstances());
//Debug.Log("Done!");
} catch (java.lang.Exception ex)
{
Debug.LogError(ex.getMessage());
}
}
示例13: 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;
}
//.........这里部分代码省略.........
示例14: buildClassifier
//.........这里部分代码省略.........
for (int i = 0; i < m_NumFolds; i++)
{
// Get train and test folds
Instances train = data.trainCV(m_NumFolds, i, m_RandomInstance);
Instances test = data.testCV(m_NumFolds, i);
// 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);
}
示例15: selectWeightQuantile
/// <summary> Select only instances with weights that contribute to
/// the specified quantile of the weight distribution
///
/// </summary>
/// <param name="data">the input instances
/// </param>
/// <param name="quantile">the specified quantile eg 0.9 to select
/// 90% of the weight mass
/// </param>
/// <returns> the selected instances
/// </returns>
protected internal virtual Instances selectWeightQuantile(Instances data, double quantile)
{
int numInstances = data.numInstances();
Instances trainData = new Instances(data, numInstances);
double[] weights = new double[numInstances];
double sumOfWeights = 0;
for (int i = 0; i < numInstances; i++)
{
weights[i] = data.instance(i).weight();
sumOfWeights += weights[i];
}
double weightMassToSelect = sumOfWeights * quantile;
int[] sortedIndices = Utils.sort(weights);
// Select the instances
sumOfWeights = 0;
for (int i = numInstances - 1; i >= 0; i--)
{
Instance instance = (Instance) data.instance(sortedIndices[i]).copy();
trainData.add(instance);
sumOfWeights += weights[sortedIndices[i]];
if ((sumOfWeights > weightMassToSelect) && (i > 0) && (weights[sortedIndices[i]] != weights[sortedIndices[i - 1]]))
{
break;
}
}
if (m_Debug)
{
System.Console.Error.WriteLine("Selected " + trainData.numInstances() + " out of " + numInstances);
}
return trainData;
}