/// <summary> Sets the format of the input instances. /// /// </summary> /// <param name="instanceInfo">an Instances object containing the input instance /// structure (any instances contained in the object are ignored - only the /// structure is required). /// </param> /// <returns> true if the outputFormat may be collected immediately /// </returns> /// <exception cref="Exception">if the format couldn't be set successfully /// </exception> public override bool setInputFormat(Instances instanceInfo) { base.setInputFormat(instanceInfo); m_SelectCols.Upper = instanceInfo.numAttributes() - 1; // Create the output buffer FastVector attributes = new FastVector(); int outputClass = - 1; m_SelectedAttributes = m_SelectCols.Selection; int inStrKeepLen = 0; int[] inStrKeep = new int[m_SelectedAttributes.Length]; for (int i = 0; i < m_SelectedAttributes.Length; i++) { int current = m_SelectedAttributes[i]; if (instanceInfo.classIndex() == current) { outputClass = attributes.size(); } Attribute keep = (Attribute) instanceInfo.attribute(current).copy(); if (keep.type() == Attribute.STRING) { inStrKeep[inStrKeepLen++] = current; } attributes.addElement(keep); } m_InputStringIndex = new int[inStrKeepLen]; Array.Copy(inStrKeep, 0, m_InputStringIndex, 0, inStrKeepLen); Instances outputFormat = new Instances(instanceInfo.relationName(), attributes, 0); outputFormat.ClassIndex = outputClass; setOutputFormat(outputFormat); return true; }
/// <summary> Calculates the cumulative margin distribution for the set of /// predictions, returning the result as a set of Instances. The /// structure of these Instances is as follows:<p> <ul> /// <li> <b>Margin</b> contains the margin value (which should be plotted /// as an x-coordinate) /// <li> <b>Current</b> contains the count of instances with the current /// margin (plot as y axis) /// <li> <b>Cumulative</b> contains the count of instances with margin /// less than or equal to the current margin (plot as y axis) /// </ul> <p> /// /// </summary> /// <returns> datapoints as a set of instances, null if no predictions /// have been made. /// </returns> public virtual Instances getCurve(FastVector predictions) { if (predictions.size() == 0) { return null; } Instances insts = makeHeader(); double[] margins = getMargins(predictions); int[] sorted = Utils.sort(margins); int binMargin = 0; int totalMargin = 0; insts.add(makeInstance(- 1, binMargin, totalMargin)); for (int i = 0; i < sorted.Length; i++) { double current = margins[sorted[i]]; double weight = ((NominalPrediction) predictions.elementAt(sorted[i])).weight(); totalMargin = (int) (totalMargin + weight); binMargin = (int) (binMargin + weight); if (true) { insts.add(makeInstance(current, binMargin, totalMargin)); binMargin = 0; } } return insts; }
/// <summary> Calculates the performance stats for the desired class and return /// results as a set of Instances. /// /// </summary> /// <param name="classIndex">index of the class of interest. /// </param> /// <returns> datapoints as a set of instances. /// </returns> public virtual Instances getCurve(FastVector predictions, int classIndex) { if ((predictions.size() == 0) || (((NominalPrediction) predictions.elementAt(0)).distribution().Length <= classIndex)) { return null; } ThresholdCurve tc = new ThresholdCurve(); Instances threshInst = tc.getCurve(predictions, classIndex); Instances insts = makeHeader(); int fpind = threshInst.attribute(ThresholdCurve.FP_RATE_NAME).index(); int tpind = threshInst.attribute(ThresholdCurve.TP_RATE_NAME).index(); int threshind = threshInst.attribute(ThresholdCurve.THRESHOLD_NAME).index(); double[] vals; double fpval, tpval, thresh; for (int i = 0; i < threshInst.numInstances(); i++) { fpval = threshInst.instance(i).value_Renamed(fpind); tpval = threshInst.instance(i).value_Renamed(tpind); thresh = threshInst.instance(i).value_Renamed(threshind); vals = new double[3]; vals[0] = 0; vals[1] = fpval; vals[2] = thresh; insts.add(new Instance(1.0, vals)); vals = new double[3]; vals[0] = 1; vals[1] = 1.0 - tpval; vals[2] = thresh; insts.add(new Instance(1.0, vals)); } return insts; }
public void WriteFile(List <List <string> > numericDataset, string file, List <string> atrNames, List <string> targetValues, bool isTargetNumeric) { weka.core.FastVector targetVals = new weka.core.FastVector(); weka.core.Instances dataRel; for (int i = 0; i < targetValues.Count; i++) { targetVals.addElement(targetValues[i]); } weka.core.Instances data; weka.core.FastVector atts = new weka.core.FastVector(); // fill and prepare the dataset for the arrf file for (int j = 0; j < insts.numAttributes(); j++) { if (j == insts.numAttributes() - 1 && isTargetNumeric == false) // target value can be nominal { atts.addElement(new weka.core.Attribute(atrNames[j], targetVals)); } else { atts.addElement(new weka.core.Attribute(atrNames[j])); } } data = new weka.core.Instances("MyRelation", atts, 0); for (int i = 0; i < insts.numInstances(); i++) { double[] vals = new double[insts.numAttributes()]; for (int j = 0; j < insts.numAttributes(); j++) { if (j == insts.numAttributes() - 1 && isTargetNumeric == false) // target value can be nominal { vals[j] = targetVals.indexOf(numericDataset[j][i]); } else { vals[j] = Convert.ToDouble(numericDataset[j][i]); } } data.add(new weka.core.DenseInstance(1.0, vals)); } if (File.Exists(file)) { File.Delete(file); } var saver = new weka.core.converters.ArffSaver(); saver.setInstances(data); saver.setFile(new java.io.File(file)); // files are saved into {AppFolder}/bin/Debug folder. You can find two files in this path. saver.writeBatch(); }
/// <summary> Calculates the performance stats for the default class and return /// results as a set of Instances. The /// structure of these Instances is as follows:<p> <ul> /// <li> <b>Probability Cost Function </b> /// <li> <b>Normalized Expected Cost</b> /// <li> <b>Threshold</b> contains the probability threshold that gives /// rise to the previous performance values. /// </ul> <p> /// /// </summary> /// <seealso cref="TwoClassStats"> /// </seealso> /// <param name="classIndex">index of the class of interest. /// </param> /// <returns> datapoints as a set of instances, null if no predictions /// have been made. /// </returns> public virtual Instances getCurve(FastVector predictions) { if (predictions.size() == 0) { return null; } return getCurve(predictions, ((NominalPrediction) predictions.elementAt(0)).distribution().Length - 1); }
/// <summary> Pulls all the margin values out of a vector of NominalPredictions. /// /// </summary> /// <param name="predictions">a FastVector containing NominalPredictions /// </param> /// <returns> an array of margin values. /// </returns> private double[] getMargins(FastVector predictions) { // sort by predicted probability of the desired class. double[] margins = new double[predictions.size()]; for (int i = 0; i < margins.Length; i++) { NominalPrediction pred = (NominalPrediction) predictions.elementAt(i); margins[i] = pred.margin(); } return margins; }
public void TestCreationOfEnums() { // Arrange var values = new weka.core.FastVector(); values.addElement("beech"); values.addElement("oak"); values.addElement("ash"); var attribute = new weka.core.Attribute("trees", values); var repo = new EnumRepository(); // Act var enumValue = repo.GetEnumValue(attribute, 0); // Assert var fields = enumValue.GetType().GetFields(); Assert.AreEqual(3, fields.Length); Assert.AreEqual("beech", fields[0].Name); }
public int InitializeClassifier(string[] atributes, string[] gestures, string classAttribute, string modelLocation) { java.io.ObjectInputStream ois = new java.io.ObjectInputStream(new java.io.FileInputStream(modelLocation)); m_cl = (weka.classifiers.Classifier)ois.readObject(); //Declare the feature vector weka.core.FastVector fvWekaFeatureVector = new weka.core.FastVector(atributes.Length + 1); for (int i = 0; i < atributes.Length; i++) { weka.core.Attribute aux = new weka.core.Attribute(atributes[i]); fvWekaFeatureVector.addElement(aux); } //Declare the class weka.core.Attribute along with its values weka.core.FastVector fvClassValues = new weka.core.FastVector(gestures.Length); for (int i = 0; i < gestures.Length; i++) { weka.core.Attribute z1 = new weka.core.Attribute(atributes[i]); fvClassValues.addElement(gestures[i]); } //fvClassValues.addElement("yes"); //fvClassValues.addElement("no"); weka.core.Attribute ClassAttribute = new weka.core.Attribute(classAttribute, fvClassValues); fvWekaFeatureVector.addElement(ClassAttribute); dataSet = new weka.core.Instances("TestRel", fvWekaFeatureVector, 10); dataSet.setClassIndex(atributes.Length); testInstance = new weka.core.Instance(atributes.Length + 1); return(1); }
/// <summary> Sets the format of the input instances. /// /// </summary> /// <param name="instanceInfo">an Instances object containing the input instance /// structure (any instances contained in the object are ignored - only the /// structure is required). /// </param> /// <exception cref="UnsupportedAttributeTypeException">if the specified attribute /// is neither numeric or nominal. /// </exception> public override bool setInputFormat(Instances instanceInfo) { base.setInputFormat(instanceInfo); m_AttIndex.Upper=instanceInfo.numAttributes() - 1; if (!Numeric && !Nominal) { throw new Exception("Can only handle numeric " + "or nominal attributes."); } m_Values.Upper=instanceInfo.attribute(m_AttIndex.Index).numValues() - 1; if (Nominal && m_ModifyHeader) { instanceInfo = new Instances(instanceInfo, 0); // copy before modifying Attribute oldAtt = instanceInfo.attribute(m_AttIndex.Index); int[] selection = m_Values.Selection; FastVector newVals = new FastVector(); for (int i = 0; i < selection.Length; i++) { newVals.addElement(oldAtt.value_Renamed(selection[i])); } instanceInfo.deleteAttributeAt(m_AttIndex.Index); instanceInfo.insertAttributeAt(new Attribute(oldAtt.name(), newVals), m_AttIndex.Index); m_NominalMapping = new int[oldAtt.numValues()]; for (int i = 0; i < m_NominalMapping.Length; i++) { bool found = false; for (int j = 0; j < selection.Length; j++) { if (selection[j] == i) { m_NominalMapping[i] = j; found = true; break; } } if (!found) { m_NominalMapping[i] = - 1; } } } setOutputFormat(instanceInfo); return true; }
private weka.core.FastVector generateStateAttributes(int rIdx) { var atts = new weka.core.FastVector(); // Get all the features of this class. foreach (var method in typeof(LimitFeatureGenerator).GetMethods()) { var attributes = method.GetCustomAttributes(typeof(Feature), true); if (attributes.Length == 0) continue; // Get the feature attribute on this method. var attr = ((Feature)attributes[0]); if(rIdx < (int)attr.MinRound || rIdx > (int)attr.MaxRound) continue; // Get the name for this column in the CSV file. string name = attr.Name; switch (attr.FType) { case FeatureType.Boolean: { var attVals = new weka.core.FastVector(); attVals.addElement(Boolean.FalseString); attVals.addElement(Boolean.TrueString); atts.addElement(new weka.core.Attribute(name, attVals)); }break; case FeatureType.Enum: { var attVals = new weka.core.FastVector(); var vals = Enum.GetNames(attr.EnumType); for(int i = 0; i < vals.Length; i++) attVals.addElement(vals[i]); atts.addElement(new weka.core.Attribute(name, attVals)); }break; case FeatureType.Nominal: { var attVals = new weka.core.FastVector(); var vals = attr.NominalValues; for(int i = 0; i < vals.Length; i++) attVals.addElement(vals[i]); atts.addElement(new weka.core.Attribute(name, attVals)); }break; case FeatureType.String: { atts.addElement(new weka.core.Attribute(name, (weka.core.FastVector)null)); }break; case FeatureType.Discrete: case FeatureType.Continuous: { atts.addElement(new weka.core.Attribute(name)); }break; default: throw new Exception("Unknown attribute type: " + attr.FType.ToString()); } } return atts; }
public weka.core.Instances GenerateClassifierInstances(int rIdx) { var atts = generateStateAttributes(rIdx); var classVals = new weka.core.FastVector(); classVals.addElement("Fold"); classVals.addElement("Call"); classVals.addElement("Raise"); atts.addElement(new weka.core.Attribute("Action", classVals)); var data = new weka.core.Instances(((Rounds)rIdx).ToString().ToLower() + "_data", atts, 0); data.setClassIndex(data.numAttributes() - 1); return data; }
private double[] getProbabilities(FastVector predictions, int classIndex) { // sort by predicted probability of the desired class. double[] probs = new double[predictions.size()]; for (int i = 0; i < probs.Length; i++) { NominalPrediction pred = (NominalPrediction) predictions.elementAt(i); probs[i] = pred.distribution()[classIndex]; } return probs; }
/// <summary> Creates an Instances object with the attributes we will be calculating. /// /// </summary> /// <returns> the Instances structure. /// </returns> private Instances makeHeader() { FastVector fv = new FastVector(); fv.addElement(new Attribute("Margin")); fv.addElement(new Attribute("Current")); fv.addElement(new Attribute("Cumulative")); return new Instances("MarginCurve", fv, 100); }
/// <summary> Calculates the performance stats for the desired class and return /// results as a set of Instances. /// /// </summary> /// <param name="classIndex">index of the class of interest. /// </param> /// <returns> datapoints as a set of instances. /// </returns> public virtual Instances getCurve(FastVector predictions, int classIndex) { if ((predictions.size() == 0) || (((NominalPrediction) predictions.elementAt(0)).distribution().Length <= classIndex)) { return null; } double totPos = 0, totNeg = 0; double[] probs = getProbabilities(predictions, classIndex); // Get distribution of positive/negatives for (int i = 0; i < probs.Length; i++) { NominalPrediction pred = (NominalPrediction) predictions.elementAt(i); if (pred.actual() == weka.classifiers.evaluation.Prediction_Fields.MISSING_VALUE) { System.Console.Error.WriteLine(GetType().FullName + " Skipping prediction with missing class value"); continue; } if (pred.weight() < 0) { System.Console.Error.WriteLine(GetType().FullName + " Skipping prediction with negative weight"); continue; } if (pred.actual() == classIndex) { totPos += pred.weight(); } else { totNeg += pred.weight(); } } Instances insts = makeHeader(); int[] sorted = Utils.sort(probs); TwoClassStats tc = new TwoClassStats(totPos, totNeg, 0, 0); double threshold = 0; double cumulativePos = 0; double cumulativeNeg = 0; for (int i = 0; i < sorted.Length; i++) { if ((i == 0) || (probs[sorted[i]] > threshold)) { tc.TruePositive = tc.TruePositive - cumulativePos; tc.FalseNegative = tc.FalseNegative + cumulativePos; tc.FalsePositive = tc.FalsePositive - cumulativeNeg; tc.TrueNegative = tc.TrueNegative + cumulativeNeg; threshold = probs[sorted[i]]; insts.add(makeInstance(tc, threshold)); cumulativePos = 0; cumulativeNeg = 0; if (i == sorted.Length - 1) { break; } } NominalPrediction pred = (NominalPrediction) predictions.elementAt(sorted[i]); if (pred.actual() == weka.classifiers.evaluation.Prediction_Fields.MISSING_VALUE) { System.Console.Error.WriteLine(GetType().FullName + " Skipping prediction with missing class value"); continue; } if (pred.weight() < 0) { System.Console.Error.WriteLine(GetType().FullName + " Skipping prediction with negative weight"); continue; } if (pred.actual() == classIndex) { cumulativePos += pred.weight(); } else { cumulativeNeg += pred.weight(); } /* System.out.println(tc + " " + probs[sorted[i]] + " " + (pred.actual() == classIndex)); */ /*if ((i != (sorted.length - 1)) && ((i == 0) || (probs[sorted[i]] != probs[sorted[i - 1]]))) { insts.add(makeInstance(tc, probs[sorted[i]])); }*/ } return insts; }
private Instances makeHeader() { FastVector fv = new FastVector(); fv.addElement(new Attribute(TRUE_POS_NAME)); fv.addElement(new Attribute(FALSE_NEG_NAME)); fv.addElement(new Attribute(FALSE_POS_NAME)); fv.addElement(new Attribute(TRUE_NEG_NAME)); fv.addElement(new Attribute(FP_RATE_NAME)); fv.addElement(new Attribute(TP_RATE_NAME)); fv.addElement(new Attribute(PRECISION_NAME)); fv.addElement(new Attribute(RECALL_NAME)); fv.addElement(new Attribute(FALLOUT_NAME)); fv.addElement(new Attribute(FMEASURE_NAME)); fv.addElement(new Attribute(THRESHOLD_NAME)); return new Instances(RELATION_NAME, fv, 100); }
/// <summary> Includes a whole bunch of predictions in the confusion matrix. /// /// </summary> /// <param name="predictions">a FastVector containing the NominalPredictions /// to include /// </param> /// <exception cref="Exception">if no valid prediction was made (i.e. /// unclassified). /// </exception> public virtual void addPredictions(FastVector predictions) { for (int i = 0; i < predictions.size(); i++) { addPrediction((NominalPrediction) predictions.elementAt(i)); } }
private Instances makeHeader() { FastVector fv = new FastVector(); fv.addElement(new Attribute(PROB_COST_FUNC_NAME)); fv.addElement(new Attribute(NORM_EXPECTED_COST_NAME)); fv.addElement(new Attribute(THRESHOLD_NAME)); return new Instances(RELATION_NAME, fv, 100); }