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(); }
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; }
/// <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; }
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); }
/// <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); }
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); }