List of usage examples for weka.core Instances Instances
public Instances(String name, ArrayList<Attribute> attInfo, int capacity)
From source file:CJWeka.java
License:Open Source License
public Object addInstance(Object args) throws Exception { if (!(args instanceof String)) { throw new RuntimeException("Invalid type for execute"); }/*from w w w . ja v a 2 s . c o m*/ StringBuffer retbuf = new StringBuffer(""); // function code goes in here String floatstring = (String) args; // convert floatstring to float/double array to instance String[] flostr = floatstring.split(" "); int nvalues = flostr.length; // add instance to ii if (my_attributes.isEmpty()) { // create attributes for all instances for (int j = 0; j < nvalues - 1; j++) { Attribute a = new Attribute(Integer.toString(j)); my_attributes.add(a); } classvals.add("0"); classvals.add("1"); /* classvals.add("2"); classvals.add("3"); classvals.add("4"); classvals.add("5"); classvals.add("6"); classvals.add("7");*/ Attribute cls = new Attribute("class", classvals); my_attributes.add(cls); ii = new Instances("my_instances", my_attributes, 0); } ii.setClassIndex(nvalues - 1); Instance inst = this.floatstringToInst(floatstring, ii, true); ii.add(inst); retbuf.append(ii.numInstances()); // return number of Instances in ii return retbuf.toString(); }
From source file:TextDirectoryToArff.java
License:Open Source License
public Instances createDataset(String directoryPath) throws Exception { FastVector atts = new FastVector(2); atts.addElement(new Attribute("filename", (FastVector) null)); atts.addElement(new Attribute("contents", (FastVector) null)); Instances data = new Instances("text_files_in_" + directoryPath, atts, 0); File dir = new File(directoryPath); String[] files = dir.list();/*from w ww . j ava2s .co m*/ for (int i = 0; i < files.length; i++) { if (files[i].endsWith(".txt")) { try { double[] newInst = new double[2]; newInst[0] = (double) data.attribute(0).addStringValue(files[i]); File txt = new File(directoryPath + File.separator + files[i]); InputStreamReader is; is = new InputStreamReader(new FileInputStream(txt)); StringBuffer txtStr = new StringBuffer(); int c; while ((c = is.read()) != -1) { txtStr.append((char) c); } newInst[1] = (double) data.attribute(1).addStringValue(txtStr.toString()); data.add(new Instance(1.0, newInst)); } catch (Exception e) { //System.err.println("failed to convert file: " + directoryPath + File.separator + files[i]); } } } return data; }
From source file:WekaClassify.java
License:Open Source License
public void SplitRun(Instances anInstance, double split) throws Exception { anInstance.randomize(new java.util.Random(0)); int trainSize = (int) Math.round(anInstance.numInstances() * split); int testSize = anInstance.numInstances() - trainSize; Instances train = new Instances(anInstance, 0, trainSize); Instances test = new Instances(anInstance, trainSize, testSize); NumberFormat percentFormat = NumberFormat.getPercentInstance(); percentFormat.setMaximumFractionDigits(1); String result = percentFormat.format(split); System.out.println("Evaluating with : Split percentage : " + result); m_Evaluation.evaluateModel(m_Classifier, anInstance); }
From source file:PCADetector.java
License:Apache License
public Instances getInstances() { int numAtts = m_oriDataMatrix.size(); if (numAtts < 0) return null; ArrayList<Attribute> atts = new ArrayList<Attribute>(numAtts); for (int att = 0; att < numAtts; att++) { atts.add(new Attribute(Integer.toString(att), att)); }//w w w.j av a 2 s . com int numInstances = m_oriDataMatrix.get(0).size(); if (numInstances <= 0) return null; Instances dataset = new Instances("MetricInstances", atts, numInstances); for (int inst = 0; inst < numInstances; inst++) { Instance newInst = new DenseInstance(numAtts); for (int att = 0; att < numAtts; att++) { newInst.setValue(att, m_oriDataMatrix.get(att).get(inst)); } dataset.add(newInst); } return dataset; }
From source file:aaa.util.test.CreateArff.java
License:Open Source License
/** * Generates the Instances object and outputs it in ARFF format to stdout. * * @param args ignored//from w ww . ja v a 2 s.c o m * @throws Exception if generation of instances fails */ public static void main(String[] args) throws Exception { ArrayList<Attribute> atts; ArrayList<Attribute> attsRel; ArrayList<String> attVals; ArrayList<String> attValsRel; Instances data; Instances dataRel; double[] vals; double[] valsRel; int i; // 1. set up attributes atts = new ArrayList<Attribute>(); // - numeric atts.add(new Attribute("att1")); // - nominal attVals = new ArrayList<String>(); for (i = 0; i < 5; i++) attVals.add("val" + (i + 1)); atts.add(new Attribute("att2", attVals)); // - string atts.add(new Attribute("att3", (ArrayList<String>) null)); // - date atts.add(new Attribute("att4", "yyyy-MM-dd")); // - relational attsRel = new ArrayList<Attribute>(); // -- numeric attsRel.add(new Attribute("att5.1")); // -- nominal attValsRel = new ArrayList<String>(); for (i = 0; i < 5; i++) attValsRel.add("val5." + (i + 1)); attsRel.add(new Attribute("att5.2", attValsRel)); dataRel = new Instances("att5", attsRel, 0); atts.add(new Attribute("att5", dataRel, 0)); // 2. create Instances object data = new Instances("MyRelation", atts, 0); // 3. fill with data // first instance vals = new double[data.numAttributes()]; // - numeric vals[0] = Math.PI; // - nominal vals[1] = attVals.indexOf("val3"); // - string vals[2] = data.attribute(2).addStringValue("This is a string!"); // - date vals[3] = data.attribute(3).parseDate("2001-11-09"); // - relational dataRel = new Instances(data.attribute(4).relation(), 0); // -- first instance valsRel = new double[2]; valsRel[0] = Math.PI + 1; valsRel[1] = attValsRel.indexOf("val5.3"); dataRel.add(new DenseInstance(1.0, valsRel)); // -- second instance valsRel = new double[2]; valsRel[0] = Math.PI + 2; valsRel[1] = attValsRel.indexOf("val5.2"); dataRel.add(new DenseInstance(1.0, valsRel)); vals[4] = data.attribute(4).addRelation(dataRel); // add data.add(new DenseInstance(1.0, vals)); // second instance vals = new double[data.numAttributes()]; // important: needs NEW array! // - numeric vals[0] = Math.E; // - nominal vals[1] = attVals.indexOf("val1"); // - string vals[2] = data.attribute(2).addStringValue("And another one!"); // - date vals[3] = data.attribute(3).parseDate("2000-12-01"); // - relational dataRel = new Instances(data.attribute(4).relation(), 0); // -- first instance valsRel = new double[2]; valsRel[0] = Math.E + 1; valsRel[1] = attValsRel.indexOf("val5.4"); dataRel.add(new DenseInstance(1.0, valsRel)); // -- second instance valsRel = new double[2]; valsRel[0] = Math.E + 2; valsRel[1] = attValsRel.indexOf("val5.1"); dataRel.add(new DenseInstance(1.0, valsRel)); vals[4] = data.attribute(4).addRelation(dataRel); // add data.add(new DenseInstance(1.0, vals)); // 4. output data System.out.println(data); }
From source file:activeSegmentation.feature.FeatureExtraction.java
License:Open Source License
/** * Create training instances out of the user markings * @return set of instances (feature vectors in Weka format) *///from w w w . j av a 2 s . co m @Override public void createTrainingInstance(List<String> classLabels, int classes, List<Vector<ArrayList<Roi>>> examples) { ArrayList<Attribute> attributes = createFeatureHeader(); attributes.add(new Attribute(Common.CLASS, addClasstoHeader(classes, classLabels))); // create initial set of instances trainingData = new Instances(Common.INSTANCE_NAME, attributes, 1); // Set the index of the class attribute trainingData.setClassIndex(filterManager.getNumOfFeatures()); for (int classIndex = 0; classIndex < classes; classIndex++) { int nl = 0; // Read all lists of examples for (int sliceNum = 1; sliceNum <= filterManager.getOriginalImageSize(); sliceNum++) for (int j = 0; j < examples.get(sliceNum - 1).get(classIndex).size(); j++) { Roi r = examples.get(sliceNum - 1).get(classIndex).get(j); nl += addRectangleRoiInstances(trainingData, classIndex, sliceNum, r); } IJ.log("# of pixels selected as " + classLabels.get(classIndex) + ": " + nl); } }
From source file:activeSegmentation.feature.FeatureExtraction.java
License:Open Source License
/** * Add training samples from a rectangular roi * /*from w w w . j a v a 2s .c o m*/ * @param trainingData set of instances to add to * @param classIndex class index value * @param sliceNum number of 2d slice being processed * @param r shape roi * @return number of instances added */ private Instances addRectangleRoiInstances(int sliceNum, List<String> classLabels, int classes) { Instances testingData; ArrayList<Attribute> attributes = createFeatureHeader(); attributes.add(new Attribute(Common.CLASS, addClasstoHeader(classes, classLabels))); System.out.println(attributes.toString()); // create initial set of instances testingData = new Instances(Common.INSTANCE_NAME, attributes, 1); // Set the index of the class attribute testingData.setClassIndex(filterManager.getNumOfFeatures()); for (int x = 0; x < originalImage.getWidth(); x++) { for (int y = 0; y < originalImage.getHeight(); y++) { testingData.add(filterManager.createInstance(x, y, 0, sliceNum)); } } // increase number of instances for this class System.out.println("SIZe" + testingData.size()); System.out.println(testingData.get(1).toString()); return testingData; }
From source file:activeSegmentation.learning.WekaDataSet.java
License:Open Source License
/** * Creates a Weka Dataset from a portion of the IDataset object * * @param dataset The dataset//from w w w .ja va 2 s .co m * @param first The position of the first instance to copy * @param toCopy The number of instances to copy */ public WekaDataSet(IDataSet dataset, int first, int toCopy) { this.dataset = new Instances(dataset.getDataset(), first, toCopy); }
From source file:adams.data.conversion.ReportToWekaInstance.java
License:Open Source License
/** * Performs the actual conversion./*from w w w .j a v a2s . com*/ * * @return the converted data * @throws Exception if something goes wrong with the conversion */ protected Object doConvert() throws Exception { Report report; Instance result; ArrayList atts; ArrayList attValues; int i; double[] values; report = (Report) m_Input; // generate header if (m_Header == null) { atts = new ArrayList(); for (i = 0; i < m_Fields.length; i++) { switch (m_Fields[i].getDataType()) { case NUMERIC: atts.add(new Attribute(m_Fields[i].getName())); break; case BOOLEAN: attValues = new ArrayList(); attValues.add("false"); attValues.add("true"); atts.add(new Attribute(m_Fields[i].getName(), attValues)); break; default: atts.add(new Attribute(m_Fields[i].getName(), (List) null)); break; } } m_Header = new Instances(getClass().getName(), atts, 0); } // generate instance values = new double[m_Header.numAttributes()]; for (i = 0; i < m_Fields.length; i++) { if (report.hasValue(m_Fields[i])) { switch (m_Fields[i].getDataType()) { case NUMERIC: values[i] = report.getDoubleValue(m_Fields[i]); break; case BOOLEAN: if (report.getBooleanValue(m_Fields[i])) values[i] = 1; else values[i] = 0; break; default: values[i] = m_Header.attribute(i).addStringValue("" + report.getValue(m_Fields[i])); break; } } else { values[i] = weka.core.Utils.missingValue(); } } result = new DenseInstance(1.0, values); result.setDataset(m_Header); return result; }
From source file:adams.data.conversion.SpreadSheetToWekaInstances.java
License:Open Source License
/** * Performs the actual conversion.// w ww .ja v a 2 s . c om * * @return the converted data * @throws Exception if something goes wrong with the conversion */ @Override protected Object doConvert() throws Exception { Instances result; SpreadSheet sheet; DenseInstance inst; ArrayList<Attribute> atts; HashSet<String> unique; ArrayList<String> labels; Row row; Cell cell; int i; int n; double[] values; Collection<ContentType> types; ContentType type; boolean added; int[] classIndices; sheet = (SpreadSheet) m_Input; // create header atts = new ArrayList<>(); for (i = 0; i < sheet.getColumnCount(); i++) { added = false; types = sheet.getContentTypes(i); if (types.contains(ContentType.DOUBLE)) types.remove(ContentType.LONG); if (types.contains(ContentType.LONG)) { types.add(ContentType.DOUBLE); types.remove(ContentType.LONG); } if (types.size() == 1) { type = (ContentType) types.toArray()[0]; if (type == ContentType.DOUBLE) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent())); added = true; } else if (type == ContentType.DATE) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), Constants.TIMESTAMP_FORMAT)); added = true; } else if (type == ContentType.TIME) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), Constants.TIME_FORMAT)); added = true; } } if (!added) { unique = new HashSet<>(); for (n = 0; n < sheet.getRowCount(); n++) { row = sheet.getRow(n); cell = row.getCell(i); if ((cell != null) && !cell.isMissing()) unique.add(cell.getContent()); } if ((unique.size() > m_MaxLabels) || (m_MaxLabels < 1)) { atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), (FastVector) null)); } else { labels = new ArrayList<>(unique); Collections.sort(labels); atts.add(new Attribute(sheet.getHeaderRow().getCell(i).getContent(), labels)); } } } result = new Instances(Environment.getInstance().getProject(), atts, sheet.getRowCount()); if (sheet.hasName()) result.setRelationName(sheet.getName()); // add data for (n = 0; n < sheet.getRowCount(); n++) { row = sheet.getRow(n); values = new double[result.numAttributes()]; for (i = 0; i < result.numAttributes(); i++) { cell = row.getCell(i); values[i] = weka.core.Utils.missingValue(); if ((cell != null) && !cell.isMissing()) { if (result.attribute(i).type() == Attribute.DATE) { if (cell.isTime()) values[i] = cell.toTime().getTime(); else values[i] = cell.toDate().getTime(); } else if (result.attribute(i).isNumeric()) { values[i] = Utils.toDouble(cell.getContent()); } else if (result.attribute(i).isString()) { values[i] = result.attribute(i).addStringValue(cell.getContent()); } else { values[i] = result.attribute(i).indexOfValue(cell.getContent()); } } } inst = new DenseInstance(1.0, values); result.add(inst); } if (sheet instanceof Dataset) { classIndices = ((Dataset) sheet).getClassAttributeIndices(); if (classIndices.length > 0) result.setClassIndex(classIndices[0]); } return result; }