本文整理汇总了C#中weka.core.Instances.numInstances方法的典型用法代码示例。如果您正苦于以下问题:C# Instances.numInstances方法的具体用法?C# Instances.numInstances怎么用?C# Instances.numInstances使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weka.core.Instances
的用法示例。
在下文中一共展示了Instances.numInstances方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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)
{
String results = "";
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 data = new weka.core.Instances(buffReader); //source.getDataSet();
// setting class attribute if the data format does not provide this information
// For example, the XRFF format saves the class attribute information as well
if (data.classIndex() == -1)
data.setClassIndex(data.numAttributes() - 1);
weka.classifiers.Classifier cl;
for (int i = 3; i < data.numInstances(); i++) {
cl = new weka.classifiers.bayes.NaiveBayes();
//cl = new weka.classifiers.trees.J48();
//cl = new weka.classifiers.lazy.IB1();
//cl = new weka.classifiers.functions.MultilayerPerceptron();
((weka.classifiers.functions.MultilayerPerceptron)cl).setHiddenLayers("12");
weka.core.Instances subset = new weka.core.Instances(data,0,i);
cl.buildClassifier(subset);
weka.classifiers.Evaluation eval = new weka.classifiers.Evaluation(subset);
eval.crossValidateModel(cl, subset, 3, new java.util.Random(1));
results = results + eval.pctCorrect(); // For accuracy measurement
/* For Mathews Correlation Coefficient */
//double TP = eval.numTruePositives(1);
//double FP = eval.numFalsePositives(1);
//double TN = eval.numTrueNegatives(1);
//double FN = eval.numFalseNegatives(1);
//double correlationCoeff = ((TP*TN)-(FP*FN))/Math.Sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN));
//results = results + correlationCoeff;
if (i != data.numInstances()-1)
results = results + ", ";
if(i == data.numInstances()-1)
Debug.Log("Player: " + playerID + ", Num Maps: " + data.numInstances() + ", AUC: " + eval.areaUnderROC(1));
}
} catch (java.lang.Exception ex)
{
Debug.LogError(ex.getMessage());
}
// Write values to file for a matlab read
// For accuracy
StreamWriter writer = new StreamWriter("DataForMatlab/"+playerID+"_CrossFoldValidations_NeuralNet.txt");
//StreamWriter writer = new StreamWriter("DataForMatlab/"+playerID+"_CrossFoldCorrCoeff.txt"); // For mathews cc
writer.WriteLine(results);
writer.Close();
Debug.Log(playerID + " has been written to file");
}
示例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
/// <summary> Evaluates the classifier on a given set of instances. Note that
/// the data must have exactly the same format (e.g. order of
/// attributes) as the data used to train the classifier! Otherwise
/// the results will generally be meaningless.
///
/// </summary>
/// <param name="classifier">machine learning classifier
/// </param>
/// <param name="data">set of test instances for evaluation
/// </param>
/// <returns> the predictions
/// </returns>
/// <throws> Exception if model could not be evaluated </throws>
/// <summary> successfully
/// </summary>
public virtual double[] evaluateModel(Classifier classifier, Instances data)
{
double[] predictions = new double[data.numInstances()];
for (int i = 0; i < data.numInstances(); i++)
{
predictions[i] = evaluateModelOnce((Classifier) classifier, data.instance(i));
}
return predictions;
}
示例4: 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: ;
}
示例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: 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;
}
示例7: PerformTraining
/// <summary>
/// Build the learning model for classification
/// </summary>
/// <param name="InstancesList">list of instances </param>
/// <param name="NumberofClusters">Number of Clusters</param>
/// <param name="TextBoxForFeedback">Text box for the results (can be NULL)</param>
/// <param name="PanelForVisualFeedback">Panel to display visual results if avalaible (can be NULL)</param>
public Classifier PerformTraining(FormForClassificationInfo WindowForClassificationParam, Instances InstancesList, /*int NumberofClusters,*/ RichTextBox TextBoxForFeedback,
Panel PanelForVisualFeedback, out weka.classifiers.Evaluation ModelEvaluation, bool IsCellular)
{
// weka.classifiers.Evaluation ModelEvaluation = null;
// FormForClassificationInfo WindowForClassificationParam = new FormForClassificationInfo(GlobalInfo);
ModelEvaluation = null;
// if (WindowForClassificationParam.ShowDialog() != System.Windows.Forms.DialogResult.OK) return null;
// weka.classifiers.Evaluation ModelEvaluation = new Evaluation(
cParamAlgo ClassifAlgoParams = WindowForClassificationParam.GetSelectedAlgoAndParameters();
if (ClassifAlgoParams == null) return null;
//this.Cursor = Cursors.WaitCursor;
// cParamAlgo ClassificationAlgo = WindowForClassificationParam.GetSelectedAlgoAndParameters();
cListValuesParam Parameters = ClassifAlgoParams.GetListValuesParam();
//Classifier this.CurrentClassifier = null;
// -------------------------- Classification -------------------------------
// create the instances
// InstancesList = this.ListInstances;
this.attValsWithoutClasses = new FastVector();
if (IsCellular)
for (int i = 0; i < cGlobalInfo.ListCellularPhenotypes.Count; i++)
this.attValsWithoutClasses.addElement(cGlobalInfo.ListCellularPhenotypes[i].Name);
else
for (int i = 0; i < cGlobalInfo.ListWellClasses.Count; i++)
this.attValsWithoutClasses.addElement(cGlobalInfo.ListWellClasses[i].Name);
InstancesList.insertAttributeAt(new weka.core.Attribute("Class", this.attValsWithoutClasses), InstancesList.numAttributes());
//int A = Classes.Count;
for (int i = 0; i < Classes.Count; i++)
InstancesList.get(i).setValue(InstancesList.numAttributes() - 1, Classes[i]);
InstancesList.setClassIndex(InstancesList.numAttributes() - 1);
weka.core.Instances train = new weka.core.Instances(InstancesList, 0, InstancesList.numInstances());
if (PanelForVisualFeedback != null)
PanelForVisualFeedback.Controls.Clear();
#region List classifiers
#region J48
if (ClassifAlgoParams.Name == "J48")
{
this.CurrentClassifier = new weka.classifiers.trees.J48();
((J48)this.CurrentClassifier).setMinNumObj((int)Parameters.ListDoubleValues.Get("numericUpDownMinInstLeaf").Value);
((J48)this.CurrentClassifier).setConfidenceFactor((float)Parameters.ListDoubleValues.Get("numericUpDownConfFactor").Value);
((J48)this.CurrentClassifier).setNumFolds((int)Parameters.ListDoubleValues.Get("numericUpDownNumFolds").Value);
((J48)this.CurrentClassifier).setUnpruned((bool)Parameters.ListCheckValues.Get("checkBoxUnPruned").Value);
((J48)this.CurrentClassifier).setUseLaplace((bool)Parameters.ListCheckValues.Get("checkBoxLaplacianSmoothing").Value);
((J48)this.CurrentClassifier).setSeed((int)Parameters.ListDoubleValues.Get("numericUpDownSeedNumber").Value);
((J48)this.CurrentClassifier).setSubtreeRaising((bool)Parameters.ListCheckValues.Get("checkBoxSubTreeRaising").Value);
// CurrentClassif.SetJ48Tree((J48)this.CurrentClassifier, Classes.Length);
this.CurrentClassifier.buildClassifier(train);
// display results training
// display tree
if (PanelForVisualFeedback != null)
{
GViewer GraphView = DisplayTree(GlobalInfo, ((J48)this.CurrentClassifier), IsCellular).gViewerForTreeClassif;
GraphView.Size = new System.Drawing.Size(PanelForVisualFeedback.Width, PanelForVisualFeedback.Height);
GraphView.Anchor = (AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
PanelForVisualFeedback.Controls.Clear();
PanelForVisualFeedback.Controls.Add(GraphView);
}
}
#endregion
#region Random Tree
else if (ClassifAlgoParams.Name == "RandomTree")
{
this.CurrentClassifier = new weka.classifiers.trees.RandomTree();
if ((bool)Parameters.ListCheckValues.Get("checkBoxMaxDepthUnlimited").Value)
((RandomTree)this.CurrentClassifier).setMaxDepth(0);
else
((RandomTree)this.CurrentClassifier).setMaxDepth((int)Parameters.ListDoubleValues.Get("numericUpDownMaxDepth").Value);
((RandomTree)this.CurrentClassifier).setSeed((int)Parameters.ListDoubleValues.Get("numericUpDownSeed").Value);
((RandomTree)this.CurrentClassifier).setMinNum((double)Parameters.ListDoubleValues.Get("numericUpDownMinWeight").Value);
if ((bool)Parameters.ListCheckValues.Get("checkBoxIsBackfitting").Value)
{
((RandomTree)this.CurrentClassifier).setNumFolds((int)Parameters.ListDoubleValues.Get("numericUpDownBackFittingFolds").Value);
}
else
{
((RandomTree)this.CurrentClassifier).setNumFolds(0);
}
this.CurrentClassifier.buildClassifier(train);
//string StringForTree = ((RandomTree)this.CurrentClassifier).graph().Remove(0, ((RandomTree)this.CurrentClassifier).graph().IndexOf("{") + 2);
//.........这里部分代码省略.........
示例8: Instances
/// <summary> Constructor copying all instances and references to
/// the header information from the given set of instances.
///
/// </summary>
/// <param name="instances">the set to be copied
/// </param>
public Instances(Instances dataset):this(dataset, dataset.numInstances())
{
dataset.copyInstances(0, this, dataset.numInstances());
}
示例9: 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());
}
}
示例10: resample
/// <summary> Creates a new dataset of the same size using random sampling
/// with replacement.
///
/// </summary>
/// <param name="random">a random number generator
/// </param>
/// <returns> the new dataset
/// </returns>
public virtual Instances resample(System.Random random)
{
Instances newData = new Instances(this, numInstances());
while (newData.numInstances() < numInstances())
{
newData.add(instance(random.Next(numInstances())));
}
return newData;
}
示例11: split
/// <summary> Splits the given set of instances into subsets.
///
/// </summary>
/// <exception cref="Exception">if something goes wrong
/// </exception>
public Instances[] split(Instances data)
{
Instances[] instances = new Instances[m_numSubsets];
double[] weights;
double newWeight;
Instance instance;
int subset, i, j;
for (j = 0; j < m_numSubsets; j++)
instances[j] = new Instances((Instances) data, data.numInstances());
for (i = 0; i < data.numInstances(); i++)
{
instance = ((Instances) data).instance(i);
weights = GetWeights(instance);
subset = whichSubset(instance);
if (subset > - 1)
instances[subset].add(instance);
else
for (j = 0; j < m_numSubsets; j++)
if (Utils.gr(weights[j], 0))
{
newWeight = weights[j] * instance.weight();
instances[j].add(instance);
instances[j].lastInstance().Weight = newWeight;
}
}
for (j = 0; j < m_numSubsets; j++)
instances[j].compactify();
return instances;
}
示例12: 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;
}
//.........这里部分代码省略.........
示例13: buildClassifier
/// <summary> Builds the boosted classifier</summary>
public virtual void buildClassifier(Instances data)
{
m_RandomInstance = new Random(m_Seed);
Instances boostData;
int classIndex = data.classIndex();
if (data.classAttribute().Numeric)
{
throw new Exception("LogitBoost can't handle a numeric class!");
}
if (m_Classifier == null)
{
throw new System.Exception("A base classifier has not been specified!");
}
if (!(m_Classifier is WeightedInstancesHandler) && !m_UseResampling)
{
m_UseResampling = true;
}
if (data.checkForStringAttributes())
{
throw new Exception("Cannot handle string attributes!");
}
if (m_Debug)
{
System.Console.Error.WriteLine("Creating copy of the training data");
}
m_NumClasses = data.numClasses();
m_ClassAttribute = data.classAttribute();
// Create a copy of the data
data = new Instances(data);
data.deleteWithMissingClass();
// Create the base classifiers
if (m_Debug)
{
System.Console.Error.WriteLine("Creating base classifiers");
}
m_Classifiers = new Classifier[m_NumClasses][];
for (int j = 0; j < m_NumClasses; j++)
{
m_Classifiers[j] = Classifier.makeCopies(m_Classifier, this.NumIterations);
}
// Do we want to select the appropriate number of iterations
// using cross-validation?
int bestNumIterations = this.NumIterations;
if (m_NumFolds > 1)
{
if (m_Debug)
{
System.Console.Error.WriteLine("Processing first fold.");
}
// Array for storing the results
double[] results = new double[this.NumIterations];
// Iterate throught the cv-runs
for (int r = 0; r < m_NumRuns; r++)
{
// Stratify the data
data.randomize(m_RandomInstance);
data.stratify(m_NumFolds);
// Perform the cross-validation
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++)
{
//.........这里部分代码省略.........
示例14: 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;
}
示例15: buildClassifier
/// <summary> Generates the classifier.
///
/// </summary>
/// <param name="instances">set of instances serving as training data
/// </param>
/// <exception cref="Exception">if the classifier has not been generated successfully
/// </exception>
public override void buildClassifier(Instances instances)
{
//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 bestVal = System.Double.MaxValue, currVal;
//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 bestPoint = - System.Double.MaxValue, sum;
int bestAtt = - 1, numClasses;
if (instances.checkForStringAttributes())
{
throw new Exception("Can't handle string attributes!");
}
double[][] bestDist = new double[3][];
for (int i = 0; i < 3; i++)
{
bestDist[i] = new double[instances.numClasses()];
}
m_Instances = new Instances(instances);
m_Instances.deleteWithMissingClass();
if (m_Instances.numInstances() == 0)
{
throw new System.ArgumentException("No instances without missing " + "class values in training file!");
}
if (instances.numAttributes() == 1)
{
throw new System.ArgumentException("Attribute missing. Need at least one " + "attribute other than class attribute!");
}
if (m_Instances.classAttribute().Nominal)
{
numClasses = m_Instances.numClasses();
}
else
{
numClasses = 1;
}
// For each attribute
bool first = true;
for (int i = 0; i < m_Instances.numAttributes(); i++)
{
if (i != m_Instances.classIndex())
{
// Reserve space for distribution.
double[][] tmpArray = new double[3][];
for (int i2 = 0; i2 < 3; i2++)
{
tmpArray[i2] = new double[numClasses];
}
m_Distribution = tmpArray;
// Compute value of criterion for best split on attribute
if (m_Instances.attribute(i).Nominal)
{
currVal = findSplitNominal(i);
}
else
{
currVal = findSplitNumeric(i);
}
if ((first) || (currVal < bestVal))
{
bestVal = currVal;
bestAtt = i;
bestPoint = m_SplitPoint;
for (int j = 0; j < 3; j++)
{
Array.Copy(m_Distribution[j], 0, bestDist[j], 0, numClasses);
}
}
// First attribute has been investigated
first = false;
}
}
// Set attribute, split point and distribution.
m_AttIndex = bestAtt;
m_SplitPoint = bestPoint;
m_Distribution = bestDist;
if (m_Instances.classAttribute().Nominal)
{
for (int i = 0; i < m_Distribution.Length; i++)
{
double sumCounts = Utils.sum(m_Distribution[i]);
if (sumCounts == 0)
{
// This means there were only missing attribute values
//.........这里部分代码省略.........