本文整理汇总了C#中weka.core.Instances.add方法的典型用法代码示例。如果您正苦于以下问题:C# Instances.add方法的具体用法?C# Instances.add怎么用?C# Instances.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weka.core.Instances
的用法示例。
在下文中一共展示了Instances.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: TrainandTest
public void TrainandTest(CandidateClassifier classifierInfo, CandidateParameter cp)
{
//string dealType = classifierInfo.DealType;
Classifier cls = null;
if (TestParameters.UseTrain)
{
string modelFileName = GetModelFileName(classifierInfo.Name);
if (TestParameters.SaveModel)
{
cls = WekaUtils.TryLoadClassifier(modelFileName);
}
Instances trainInstancesNew, trainInstances;
trainInstances = m_trainInstances;
trainInstancesNew = m_trainInstancesNew;
if (cls == null)
{
if (classifierInfo.Classifier == null)
{
classifierInfo.Classifier = WekaUtils.CreateClassifier(cp.ClassifierType, m_currentTp, m_currentSl);
}
cls = classifierInfo.Classifier;
}
else
{
if (TestParameters.EnableDetailLog)
{
System.Console.WriteLine("Model is loaded.");
}
}
if (m_enableTrainSplitTest)
{
Instances trainTrainInst, trainTestInst;
DateTime splitTrainTimeEnd;
if (m_trainSplitTestNums != -1)
{
int trainTrainSize = trainInstancesNew.numInstances() - m_trainSplitTestNums;
int trainTestSize = m_trainSplitTestNums;
trainTrainInst = new Instances(trainInstancesNew, 0, trainTrainSize);
trainTestInst = new Instances(trainInstancesNew, trainTrainSize, trainTestSize);
splitTrainTimeEnd = WekaUtils.GetDateValueFromInstances(trainInstances, 0, trainTrainSize);
}
else if (m_trainSplitPercent != -1)
{
if (m_trainSplitPercent == 100.0)
{
int trainTrainSize = trainInstancesNew.numInstances();
trainTrainInst = new Instances(trainInstancesNew, 0, trainTrainSize);
trainTestInst = new Instances(trainInstancesNew, 0, trainTrainSize);
splitTrainTimeEnd = WekaUtils.GetDateValueFromInstances(trainInstances, 0, trainTrainSize);
}
else
{
int trainTrainSize = (int)Math.Round(trainInstancesNew.numInstances() * m_trainSplitPercent / 100);
int trainTestSize = trainInstancesNew.numInstances() - trainTrainSize;
trainTrainInst = new Instances(trainInstancesNew, 0, trainTrainSize);
trainTestInst = new Instances(trainInstancesNew, trainTrainSize, trainTestSize);
splitTrainTimeEnd = WekaUtils.GetDateValueFromInstances(trainInstances, 0, trainTrainSize);
}
}
else
{
trainTrainInst = new Instances(trainInstancesNew, 0);
trainTestInst = new Instances(trainInstancesNew, 0);
DateTime dt = WekaUtils.GetDateValueFromInstances(trainInstances, 0, trainInstances.numInstances() - 1);
splitTrainTimeEnd = m_trainTimeEnd.AddMinutes(-TestParameters.BatchTestMinutes);
while (splitTrainTimeEnd > dt)
{
splitTrainTimeEnd = splitTrainTimeEnd.AddMinutes(-TestParameters.BatchTestMinutes);
}
for (int i = 0; i < trainInstances.numInstances(); ++i)
{
dt = WekaUtils.GetDateValueFromInstances(trainInstances, 0, i);
if (dt <= splitTrainTimeEnd)
{
var ins = new DenseInstance(trainInstancesNew.instance(i));
trainTrainInst.add(ins);
}
else
{
var ins = new DenseInstance(trainInstancesNew.instance(i));
trainTestInst.add(ins);
}
}
}
cls = WekaUtils.TrainInstances(trainTrainInst, TestParameters.SaveModel ? modelFileName : null, cls);
//m_classifierQueue[dealType].Enqueue(new ClassifierInfo(cls, splitTrainTimeEnd));
//foreach (var i in m_classifierQueue[dealType])
//{
// var e = WekaUtils.TestInstances(trainTestInst, i.Cls);
// i.TotalCost = i.TotalCost * m_classifierQueueFactor + e.totalCost();
// i.TotalNum = (int)(i.TotalNum * m_classifierQueueFactor) + (int)e.numInstances();
//}
//.........这里部分代码省略.........
示例3: CreateInstancesWithoutClass
/// <summary>
/// Create an instances structure without classes for unsupervised methods
/// </summary>
/// <returns>a weka Instances object</returns>
public Instances CreateInstancesWithoutClass()
{
weka.core.FastVector atts = new FastVector();
int columnNo = 0;
// Descriptors loop
for (int i = 0; i < ParentScreening.ListDescriptors.Count; i++)
{
if (ParentScreening.ListDescriptors[i].IsActive() == false) continue;
atts.addElement(new weka.core.Attribute(ParentScreening.ListDescriptors[i].GetName()));
columnNo++;
}
weka.core.FastVector attVals = new FastVector();
Instances data1 = new Instances("MyRelation", atts, 0);
foreach (cWell CurrentWell in this.ListActiveWells)
{
double[] vals = new double[data1.numAttributes()];
int IdxRealCol = 0;
for (int Col = 0; Col < ParentScreening.ListDescriptors.Count; Col++)
{
if (ParentScreening.ListDescriptors[Col].IsActive() == false) continue;
vals[IdxRealCol++] = CurrentWell.ListSignatures[Col].GetValue();
}
data1.add(new DenseInstance(1.0, vals));
}
return data1;
}
示例4: CreateInstancesWithoutClass
public Instances CreateInstancesWithoutClass(cExtendedTable Input)
{
weka.core.FastVector atts = new FastVector();
int columnNo = 0;
// Descriptors loop
for (int i = 0; i < Input.Count; i++)
{
//if (ParentScreening.ListDescriptors[i].IsActive() == false) continue;
atts.addElement(new weka.core.Attribute(Input[i].Name));
columnNo++;
}
// weka.core.FastVector attVals = new FastVector();
Instances data1 = new Instances("MyRelation", atts, 0);
for (int IdxRow = 0; IdxRow < Input[0].Count; IdxRow++)
{
double[] vals = new double[data1.numAttributes()];
for (int Col = 0; Col < columnNo; Col++)
{
// if (Glo .ListDescriptors[Col].IsActive() == false) continue;
vals[Col] = Input[Col][IdxRow];// double.Parse(dt.Rows[IdxRow][Col].ToString());
}
data1.add(new DenseInstance(1.0, vals));
}
return data1;
}
示例5: CreateInstancesWithClasses
/// <summary>
/// Create an instances structure with classes for supervised methods
/// </summary>
/// <param name="NumClass"></param>
/// <returns></returns>
public Instances CreateInstancesWithClasses(List<bool> ListClassSelected)
{
weka.core.FastVector atts = new FastVector();
int columnNo = 0;
for (int i = 0; i < ParentScreening.ListDescriptors.Count; i++)
{
if (ParentScreening.ListDescriptors[i].IsActive() == false) continue;
atts.addElement(new weka.core.Attribute(ParentScreening.ListDescriptors[i].GetName()));
columnNo++;
}
weka.core.FastVector attVals = new FastVector();
foreach (var item in cGlobalInfo.ListWellClasses)
{
attVals.addElement(item.Name);
}
atts.addElement(new weka.core.Attribute("ClassAttribute", attVals));
Instances data1 = new Instances("MyRelation", atts, 0);
int IdxWell = 0;
foreach (cWell CurrentWell in this.ListActiveWells)
{
if (!ListClassSelected[CurrentWell.GetCurrentClassIdx()]) continue;
double[] vals = new double[data1.numAttributes()];
int IdxCol = 0;
for (int Col = 0; Col < ParentScreening.ListDescriptors.Count; Col++)
{
if (ParentScreening.ListDescriptors[Col].IsActive() == false) continue;
vals[IdxCol++] = CurrentWell.ListSignatures[Col].GetValue();
}
vals[columnNo] = CurrentWell.GetCurrentClassIdx();
data1.add(new DenseInstance(1.0, vals));
IdxWell++;
}
data1.setClassIndex((data1.numAttributes() - 1));
return data1;
}
示例6: CreateInstancesWithClassesWithPlateBasedDescriptor
/// <summary>
/// Create an instances structure with classes for supervised methods
/// </summary>
/// <param name="NumClass"></param>
/// <returns></returns>
public Instances CreateInstancesWithClassesWithPlateBasedDescriptor(int NumberOfClass)
{
weka.core.FastVector atts = new FastVector();
int columnNo = 0;
for (int i = 0; i < ParentScreening.ListPlateBaseddescriptorNames.Count; i++)
{
atts.addElement(new weka.core.Attribute(ParentScreening.ListPlateBaseddescriptorNames[i]));
columnNo++;
}
weka.core.FastVector attVals = new FastVector();
for (int i = 0; i < NumberOfClass; i++)
attVals.addElement("Class" + (i).ToString());
atts.addElement(new weka.core.Attribute("Class", attVals));
Instances data1 = new Instances("MyRelation", atts, 0);
int IdxWell = 0;
foreach (cWell CurrentWell in this.ListActiveWells)
{
if (CurrentWell.GetCurrentClassIdx() == -1) continue;
double[] vals = new double[data1.numAttributes()];
int IdxCol = 0;
for (int Col = 0; Col < ParentScreening.ListPlateBaseddescriptorNames.Count; Col++)
{
vals[IdxCol++] = CurrentWell.ListPlateBasedDescriptors[Col].GetValue();
}
vals[columnNo] = CurrentWell.GetCurrentClassIdx();
data1.add(new DenseInstance(1.0, vals));
IdxWell++;
}
data1.setClassIndex((data1.numAttributes() - 1));
return data1;
}
示例7: test
/// <summary> Method for testing this class.
///
/// </summary>
/// <param name="argv">should contain one element: the name of an ARFF file
/// </param>
//@ requires argv != null;
//@ requires argv.length == 1;
//@ requires argv[0] != null;
public static void test(System.String[] argv)
{
Instances instances, secondInstances, train, test, empty;
//Instance instance;
//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'"
System.Random random = new System.Random((System.Int32) 2);
//UPGRADE_ISSUE: Class hierarchy differences between 'java.io.Reader' and 'System.IO.StreamReader' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1186'"
System.IO.StreamReader reader;
int start, num;
//double newWeight;
FastVector testAtts, testVals;
int i, j;
try
{
if (argv.Length > 1)
{
throw (new System.Exception("Usage: Instances [<filename>]"));
}
// Creating set of instances from scratch
testVals = new FastVector(2);
testVals.addElement("first_value");
testVals.addElement("second_value");
testAtts = new FastVector(2);
testAtts.addElement(new Attribute("nominal_attribute", testVals));
testAtts.addElement(new Attribute("numeric_attribute"));
instances = new Instances("test_set", testAtts, 10);
instances.add(new Instance(instances.numAttributes()));
instances.add(new Instance(instances.numAttributes()));
instances.add(new Instance(instances.numAttributes()));
instances.ClassIndex = 0;
System.Console.Out.WriteLine("\nSet of instances created from scratch:\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(instances);
if (argv.Length == 1)
{
System.String filename = argv[0];
//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'"
reader = new System.IO.StreamReader(filename, System.Text.Encoding.Default);
// Read first five instances and print them
System.Console.Out.WriteLine("\nFirst five instances from file:\n");
instances = new Instances(reader, 1);
instances.ClassIndex = instances.numAttributes() - 1;
i = 0;
while ((i < 5) && (instances.readInstance(reader)))
{
i++;
}
//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(instances);
// Read all the instances in the file
//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'"
reader = new System.IO.StreamReader(filename, System.Text.Encoding.Default);
instances = new Instances(reader);
// Make the last attribute be the class
instances.ClassIndex = instances.numAttributes() - 1;
// Print header and instances.
System.Console.Out.WriteLine("\nDataset:\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(instances);
System.Console.Out.WriteLine("\nClass index: " + instances.classIndex());
}
// Test basic methods based on class index.
System.Console.Out.WriteLine("\nClass name: " + instances.classAttribute().name());
System.Console.Out.WriteLine("\nClass index: " + instances.classIndex());
System.Console.Out.WriteLine("\nClass is nominal: " + instances.classAttribute().Nominal);
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
{
//.........这里部分代码省略.........
示例8: createWhyInstances
private Instances createWhyInstances()
{
FastVector fvWhy = createWhyFastVector();
Instances whyInstances = new Instances("WhyInstances", fvWhy, listSecondaryWhyCandidates.Count);
foreach (Token candidate in listSecondaryWhyCandidates)
{
if (candidate.Value == null) continue;
Instance whyInstance = createSingleWhyInstance(fvWhy, candidate);
whyInstance.setDataset(whyInstances);
whyInstances.add(whyInstance);
}
whyInstances.setClassIndex(fvWhy.size() - 1);
return whyInstances;
}
示例9: copyInstances
/// <summary> Copies instances from one set to the end of another
/// one.
///
/// </summary>
/// <param name="source">the source of the instances
/// </param>
/// <param name="from">the position of the first instance to be copied
/// </param>
/// <param name="dest">the destination for the instances
/// </param>
/// <param name="num">the number of instances to be copied
/// </param>
//@ requires 0 <= from && from <= numInstances() - num;
//@ requires 0 <= num;
protected internal virtual void copyInstances(int from, Instances dest, int num)
{
for (int i = 0; i < num; i++)
{
dest.add(instance(from + i));
}
}
示例10: 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;
}
示例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: 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;
}
示例13: 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());
}
}
示例14: CreateInstanceOnFly
private static Instances CreateInstanceOnFly(double[] a, double[] b)
{
FastVector atts;
Instances data;
double[] vals;
// 1. set up attributes
atts = new FastVector();
// - numeric
atts.addElement(new Attribute("att1"));
atts.addElement(new Attribute("att2"));
// 2. create Instances object
data = new Instances("MyRelation", atts, 0);
for (int i = 0; i < a.Length; ++i)
{
// 3. fill with data
// first instance
vals = new double[data.numAttributes()];
// - numeric
vals[0] = a[i];
// - nominal
vals[1] = b[i];
data.add(new weka.core.DenseInstance(1.0, vals));
}
return data;
}
示例15: CreateInstanceForNClasses
/// <summary>
/// Create a single instance for WEKA
/// </summary>
/// <param name="NClasses">Number of classes</param>
/// <returns>the weka instances</returns>
public Instances CreateInstanceForNClasses(cInfoClass InfoClass)
{
List<double> AverageList = new List<double>();
for (int i = 0; i < Parent.ListDescriptors.Count; i++)
if (Parent.ListDescriptors[i].IsActive()) AverageList.Add(GetAverageValuesList()[i]);
weka.core.FastVector atts = new FastVector();
List<string> NameList = Parent.ListDescriptors.GetListNameActives();
for (int i = 0; i < NameList.Count; i++)
atts.addElement(new weka.core.Attribute(NameList[i]));
weka.core.FastVector attVals = new FastVector();
for (int i = 0; i < InfoClass.NumberOfClass; i++)
attVals.addElement("Class" + i);
atts.addElement(new weka.core.Attribute("Class__", attVals));
Instances data1 = new Instances("SingleInstance", atts, 0);
double[] newTable = new double[AverageList.Count + 1];
Array.Copy(AverageList.ToArray(), 0, newTable, 0, AverageList.Count);
//newTable[AverageList.Count] = 1;
data1.add(new DenseInstance(1.0, newTable));
data1.setClassIndex((data1.numAttributes() - 1));
return data1;
}