DataSource source = new DataSource(new File("mycsvinputfile")); System.out.println(source.getStructure()); Instances data = 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); //initialize svm classifier LibSVM svm = new LibSVM(); svm.buildClassifier(data);
Instances dataUnlabeled = new Instances("TestInstances", atts, 0); dataUnlabeled.add(newInst); dataUnlabeled.setClassIndex(dataUnlabeled.numAttributes() - 1); double classif = ibk.classifyInstance(dataUnlabeled.firstInstance());
Operator() { ArrayList<Attribute> a = new ArrayList<Attribute>(); for (int i=0; i<attrs.length-1; i++) { a.add(new Attribute(attrs[i])); // numeric } ArrayList<String> d = new ArrayList<String>(); d.add("false"); d.add("true"); a.add(new Attribute(attrs[attrs.length-1], d)); // nominal attribute data = new Instances("Buh", a, 0); data.setClassIndex(attrs.length-1); // the CLASS } }
Instances training_data = new Instances(new BufferedReader( new FileReader("tmp-file-duplicate-pairs.arff"))); training_data.setClassIndex(training_data.numAttributes() - 1);
/** * Transform - transform dataset D for this node. * this.j defines the current node index, e.g., 3 * this.paY[] defines parents, e.g., [1,4] * we should remove the rest, e.g., [0,2,5,...,L-1] * @return dataset we should remove all variables from D EXCEPT current node, and parents. */ public Instances transform(Instances D) throws Exception { int L = D.classIndex(); d = D.numAttributes() - L; int keep[] = A.append(this.paY,j); // keep all parents and self! Arrays.sort(keep); int remv[] = A.invert(keep,L); // i.e., remove the rest < L Arrays.sort(remv); map = new int[L]; for(int j = 0; j < L; j++) { map[j] = Arrays.binarySearch(keep,j); } Instances D_ = F.remove(new Instances(D),remv, false); D_.setClassIndex(map[this.j]); return D_; }
@Override public void buildClassifier(Instances D) throws Exception { testCapabilities(D); int L = D.classIndex(); if(getDebug()) System.out.print("Creating "+L+" models ("+m_Classifier.getClass().getName()+"): "); m_MultiClassifiers = AbstractClassifier.makeCopies(m_Classifier,L); m_Templates = new Instances[L]; for(int j = 0; j < L; j++) { //Select only class attribute 'j' m_Templates[j] = MLUtils.keepAttributesAt(new Instances(D),new int[]{j},L); m_Templates[j].setClassIndex(0); //Build the classifier for that class m_MultiClassifiers[j].buildClassifier(m_Templates[j]); if(getDebug()) System.out.print(" " + (m_Templates[j].classAttribute().name())); m_Templates[j] = new Instances(m_Templates[j], 0); } }
/** Need to remove non-nominal attributes, set class index */ protected void setUp() throws Exception { super.setUp(); // class index m_Instances.setClassIndex(1); // remove attributes that are not nominal/numeric int i = 0; while (i < m_Instances.numAttributes()) { if ( !m_Instances.attribute(i).isNominal() && !m_Instances.attribute(i).isNumeric() ) m_Instances.deleteAttributeAt(i); else i++; } }
protected Instances getSummaryInsts(Instances orig, String[] args) throws Exception { List<String> attNames = new ArrayList<String>(); for (int i = 0; i < orig.numAttributes(); i++) { attNames.add(orig.attribute(i).name()); } CSVToARFFHeaderMapTask arffTask = new CSVToARFFHeaderMapTask(); arffTask.setOptions(args); for (int i = 0; i < orig.numInstances(); i++) { arffTask.processRow(orig.instance(i).toString(), attNames); } Instances withSummary = arffTask.getHeader(); CSVToARFFHeaderReduceTask arffReduce = new CSVToARFFHeaderReduceTask(); List<Instances> instList = new ArrayList<Instances>(); instList.add(withSummary); withSummary = arffReduce.aggregate(instList); withSummary.setClassIndex(orig.classIndex()); return withSummary; }
protected Instances getData2() throws Exception { Instances data2 = new Instances(new StringReader(DATA2)); data2.setClassIndex(1); return data2; }
ArrayList<String> newVals) { Instances miningSchemaI = getFieldsAsInstances(); if (miningSchemaI.attribute(index).isNominal()) { throw new IllegalArgumentException("[MiningSchema] convertNumericAttToNominal: attribute is " + "already nominal!"); newValues.add(newVals.get(i)); for (int i = 0; i < miningSchemaI.numAttributes(); i++) { Attribute tempA = miningSchemaI.attribute(i); if (i == index) { Attribute newAtt = new Attribute(tempA.name(), newValues); attInfo.add(newAtt); } else { attInfo.add(tempA); Instances newI = new Instances("miningSchema", attInfo, 0); if (m_fieldInstancesStructure.classIndex() >= 0) { newI.setClassIndex(m_fieldInstancesStructure.classIndex());
JFrame1 form = new JFrame1(); form.setVisible(true); form.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent pce) { // Handle the change here String pth = (String) pce.getNewValue(); BufferedReader datafile = readDataFile(pth); Instances data = new Instances(datafile); data.setClassIndex(data.numAttributes() - 1); (...) } });
/** * Transform - transform dataset D for this node. * this.j defines the current node index, e.g., 3 * this.paY[] defines parents, e.g., [1,4] * we should remove the rest, e.g., [0,2,5,...,L-1] * @return dataset we should remove all variables from D EXCEPT current node, and parents. */ public Instances transform(Instances D) throws Exception { int L = D.classIndex(); d = D.numAttributes() - L; int keep[] = A.append(this.paY,j); // keep all parents and self! Arrays.sort(keep); int remv[] = A.invert(keep,L); // i.e., remove the rest < L Arrays.sort(remv); map = new int[L]; for(int j = 0; j < L; j++) { map[j] = Arrays.binarySearch(keep,j); } Instances D_ = F.remove(new Instances(D),remv, false); D_.setClassIndex(map[this.j]); return D_; }
@Override public void buildClassifier(Instances D) throws Exception { testCapabilities(D); int L = D.classIndex(); if(getDebug()) System.out.print("Creating "+L+" models ("+m_Classifier.getClass().getName()+"): "); m_MultiClassifiers = AbstractClassifier.makeCopies(m_Classifier,L); m_InstancesTemplates = new Instances[L]; for(int j = 0; j < L; j++) { //Select only class attribute 'j' Instances D_j = F.keepLabels(new Instances(D),L,new int[]{j}); D_j.setClassIndex(0); //Build the classifier for that class m_MultiClassifiers[j].buildClassifier(D_j); if(getDebug()) System.out.print(" " + (D_j.classAttribute().name())); m_InstancesTemplates[j] = new Instances(D_j, 0); } }
/** Need to remove non-nominal attributes, set class index */ protected void setUp() throws Exception { super.setUp(); // class index m_Instances.setClassIndex(1); // remove attributes that are not nominal/numeric int i = 0; while (i < m_Instances.numAttributes()) { if ( !m_Instances.attribute(i).isNominal() && !m_Instances.attribute(i).isNumeric() ) m_Instances.deleteAttributeAt(i); else i++; } }
BufferedReader reader = new BufferedReader(new FileReader("/some/where/file.arff")); ArffReader arff = new ArffReader(reader, 1000); Instances data = arff.getStructure(); data.setClassIndex(data.numAttributes() - 1); Instance inst; while ((inst = arff.readInstance(data)) != null) { data.add(inst); }
// rootPath should be where the .arff file is held // filename should hold the complete name of the .arff file public static Instances instanceData(String rootPath, String filename) throws Exception { // initialize source DataSource source = null; Instances data = null; source = new DataSource(rootPath + filename); data = source.getDataSet(); <br> // set the class to the last attribute of the data (may need to tweak) if (data.classIndex() == -1) data.setClassIndex(data.numAttributes() -1 ); return data; } </br>
protected Instances getData2() throws Exception { Instances data2 = new Instances(new StringReader(DATA2)); data2.setClassIndex(1); return data2; }
ArrayList<String> newVals) { Instances miningSchemaI = getFieldsAsInstances(); if (miningSchemaI.attribute(index).isNominal()) { throw new IllegalArgumentException("[MiningSchema] convertNumericAttToNominal: attribute is " + "already nominal!"); newValues.add(newVals.get(i)); for (int i = 0; i < miningSchemaI.numAttributes(); i++) { Attribute tempA = miningSchemaI.attribute(i); if (i == index) { Attribute newAtt = new Attribute(tempA.name(), newValues); attInfo.add(newAtt); } else { attInfo.add(tempA); Instances newI = new Instances("miningSchema", attInfo, 0); if (m_fieldInstancesStructure.classIndex() >= 0) { newI.setClassIndex(m_fieldInstancesStructure.classIndex());
/** * Load an arbitrary arff file * * @return Instances * @throws Exception IO error. */ public static Instances loadArff(String path) throws Exception { Instances data = new Instances(new FileReader(path)); data.setClassIndex(data.numAttributes() - 1); return data; } /**
/** * Tests the copying of the header of a dataset. * * @see Instances#Instances(Instances, int) */ public void testHeaderCopy() { Instances data; data = new Instances(m_Instances, 0); assertEquals("# of attributes differ", m_Instances.numAttributes(), data.numAttributes()); assertEquals("class index differs", m_Instances.classIndex(), data.classIndex()); assertEquals("Unexpected instances", 0, data.numInstances()); m_Instances.setClassIndex(m_Instances.numAttributes() - 1); data = new Instances(m_Instances, 0); assertEquals("class index differs", m_Instances.classIndex(), data.classIndex()); }