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);
System.out.println("--------------------------"); double[] instanceValue1 = new double[dataRaw.numAttributes()]; instanceValue1[0] = dataRaw.attribute(0).addStringValue("This is a string!"); instanceValue1[1] = 0; System.out.println("--------------------------"); double[] instanceValue2 = new double[dataRaw.numAttributes()]; instanceValue2[0] = dataRaw.attribute(0).addStringValue("This is second string!"); instanceValue2[1] = 1;
protected void initMinMax(Instances data) { m_Min = new double[data.numAttributes()]; m_Max = new double[data.numAttributes()]; for (int i = 0; i < data.numAttributes(); i++) { m_Min[i] = m_Max[i] = Double.NaN; } for (int i = 0; i < data.numInstances(); i++) { updateMinMax(data.instance(i)); } }
/** * Determines the output format for the filter. */ @Override protected Instances determineOutputFormat(Instances inputFormat) { // Create attributes ArrayList<Attribute> atts = new ArrayList<Attribute>(); m_allInsts = new LinkedList<Instance>(); for (int i = 0; i < getInputFormat().numInstances(); i++) { Instances bag = getInputFormat().instance(i).relationalValue( BAG_ATTRIBUTE); for (int j = 0; j < bag.numInstances(); j++) { m_allInsts.add(bag.instance(j)); } } for (int i = 0; i < m_allInsts.size(); i++) { atts.add(new Attribute("" + i)); } atts.add(inputFormat.attribute(LABEL_ATTRIBUTE)); // class // TODO set relation name properly Instances returner = new Instances("", atts, 0); returner.setClassIndex(returner.numAttributes() - 1); return returner; }
/** * Makes the format for the level-1 data. * * @param instances the level-0 format * @return the format for the meta data * @throws Exception if an error occurs */ protected Instances metaFormat(Instances instances) throws Exception { FastVector attributes = new FastVector(); Instances metaFormat; for (int i = 0; i<instances.numAttributes(); i++) { if ( i != instances.classIndex() ) { attributes.addElement(instances.attribute(i)); } } FastVector nomElements = new FastVector(2); nomElements.addElement("0"); nomElements.addElement("1"); attributes.addElement(new Attribute("PredConf",nomElements)); metaFormat = new Instances("Meta format", attributes, 0); metaFormat.setClassIndex(metaFormat.numAttributes()-1); return metaFormat; }
public int getNode(String sNodeName) { int iNode = 0; while (iNode < m_root.m_bayesNet.m_Instances.numAttributes()) { if (m_root.m_bayesNet.m_Instances.attribute(iNode).name() .equals(sNodeName)) { return iNode; } iNode++; } // throw new Exception("Could not find node [[" + sNodeName + "]]"); return -1; }
/** 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++; } }
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); (...) } });
/** * Tests default setup. */ public void testTypical() { Instances result = useFilter(); // Number of attributes and instances shouldn't change assertEquals(m_Instances.numAttributes(), result.numAttributes()); assertEquals(m_Instances.numInstances(), result.numInstances()); Attribute mergedAtt = result.attribute(4); // All values should be merged for this attribute assertTrue("Attribute 5 has all values merged in result", mergedAtt .value(0).equals("a_or_b_or_c_or_d")); }
m_estimator = new NaiveBayes(); Instances trainingData = new Instances(instances); if (m_remove != null) { trainingData = Filter.useFilter(instances, m_remove); Instances header = m_estimator.getHeader(); int index = 0; for (int i = 0; i < header.numAttributes(); i++) { if (i != header.classIndex()) { m_estimatorLookup.put(header.attribute(i).name(), estimators[index]); index++; new Instances(getOutputFormat(), instances.numInstances()); for (int i = 0; i < instances.numInstances(); i++) { Instance current = instances.instance(i); Instance instNew = convertInstance(current);
@Override public void validate(Instances data) throws InvalidInputDataException { if (!((data.attribute(0).isString() && data.classIndex() == 1) || (data.attribute(1).isString() && data.classIndex() == 0))) { throw new InvalidInputDataException( "An ARFF is required with a string attribute and a class attribute"); } if (data.numAttributes() != 2) { throw new InvalidInputDataException( "There must be exactly two attributes: 1) Text 2) Label. " + "The given data consists of " + data.numAttributes() + " attributes."); } }
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; }
/** * Updates the minimum, maximum, sum, sumSquare values for all the attributes * * @param instance the new instance */ private void updateMinMax(Instance instance){ for (int j = 0; j < m_Train.numAttributes(); j++) { if(m_Train.classIndex() == j || m_Train.attribute(j).isNominal()) continue; if (instance.value(j) < m_MinArray[j]) m_MinArray[j] = instance.value(j); if (instance.value(j) > m_MaxArray[j]) m_MaxArray[j] = instance.value(j); } }
public GroupFeature(List<FeatureExtractor> features) { this.features = ImmutableList.copyOf(features); ImmutableList.Builder<Attribute> result = ImmutableList.builder(); for (FeatureExtractor fe: this.features) { for (Attribute att: fe.attributes()) { result.add((Attribute)att.copy()); } } _attributes = result.build(); _instances = new Instances("FOO", newArrayList(_attributes), 0); result = ImmutableList.builder(); for (int i = 0; i < _instances.numAttributes(); i++) { result.add(_instances.attribute(i)); } _attributes = result.build(); }
public void testTypical() { m_Filter = getFilter(); Instances result = useFilter(); assertEquals(m_Instances.numAttributes(), result.numAttributes()); assertEquals(m_Instances.numInstances(), result.numInstances()); // the discretized attribute must be nominal assertTrue(result.attribute(m_AttIndex).isNominal()); }
@Override public Instance transformInstance(Instance x) throws Exception{ Instances tmpInst = new Instances(x.dataset()); tmpInst.delete(); tmpInst.add(x); Instances features = this.extractPart(tmpInst, false); Instances pseudoLabels = new Instances(this.compressedTemplateInst); Instance tmpin = pseudoLabels.instance(0); pseudoLabels.delete(); pseudoLabels.add(tmpin); for ( int i = 0; i< pseudoLabels.classIndex(); i++) { pseudoLabels.instance(0).setMissing(i); } Instances newDataSet = Instances.mergeInstances(pseudoLabels, features); newDataSet.setClassIndex(pseudoLabels.numAttributes()); return newDataSet.instance(0); }
/** * performs a typical test */ public void testTypical() { Instances icopy = new Instances(m_Instances); m_Filter = getFilter(); Instances result = useFilter(); assertEquals(result.numAttributes(), icopy.numInstances() + 1); }
/** * GetXfromD - Extract attributes as a double X[][] from Instances D. * TODO: getXfromInstances would be a better name. */ public static double[][] getXfromD(Instances D) { int N = D.numInstances(); int L = D.classIndex(); int d = D.numAttributes()-L; //System.out.println("d="+d); double X[][] = new double[N][d]; for(int i = 0; i < N; i++) { for(int k = 0; k < d; k++) { X[i][k] = D.instance(i).value(k+L); } } return X; }
/** Need to remove attributes that are not nominal/numeric */ protected void setUp() throws Exception { super.setUp(); // 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.attribute(i).isDate() ) m_Instances.deleteAttributeAt(i); else i++; } }
/** * * @param bayesNet * @param instances the instances to work with * @throws Exception if something goes wrong */ public void buildStructure (BayesNet bayesNet, Instances instances) throws Exception { for (int iAttribute = 0; iAttribute < instances.numAttributes(); iAttribute++) { if (iAttribute != instances.classIndex()) { bayesNet.getParentSet(iAttribute).addParent(instances.classIndex(), instances); } } } // buildStructure