/** * initializes the attribute indices. */ protected void initializeAttributeIndices() { //m_AttributeIndices.setUpper(m_Data.numAttributes() - 1); m_ActiveIndices = new boolean[m_Data.numAttributes()]; for (int i = 0; i < m_ActiveIndices.length; i++) m_ActiveIndices[i] = true; //m_AttributeIndices.isInRange(i); }
public String toStringArff() { StringBuilder text = new StringBuilder(); text.append(ARFF_RELATION).append(" ") .append(Utils.quote(getRelationName())).append("\n\n"); for (int i = 0; i < numAttributes(); i++) { text.append(attribute(i).toString()).append("\n"); } text.append("\n").append(ARFF_DATA).append("\n"); text.append(toString()); return text.toString(); } }
/** * Completes the hashset with attributes indices. */ private void computeAttributesIndices() { this.hsAttributesIndices = new HashMap<String, Integer>(); // iterates through all existing attributes // and sets an unique identifier for each one of them for (int i = 0; i < this.numAttributes(); i++) { hsAttributesIndices.put(this.attribute(i).name(), i); } }
/** * Returns the dataset as a string in ARFF format. Strings are quoted if * they contain whitespace characters, or if they are a question mark. * * @return the dataset in ARFF format as a string */ public String toString() { StringBuffer text = new StringBuffer(); text.append(ARFF_RELATION).append(" "). append(Utils.quote(this.instanceInformation.getRelationName())).append("\n\n"); for (int i = 0; i < numAttributes(); i++) { text.append(attribute(i).toString()).append("\n"); } text.append("\n").append(ARFF_DATA).append("\n"); text.append(stringWithoutHeader()); return text.toString(); }
public Attribute attribute(String name) { for (int i = 0; i < numAttributes(); i++) { if (attribute(i).name().equals(name)) { return attribute(i); } } return null; }
public Attribute attribute(String name) { for (int i = 0; i < numAttributes(); i++) { if (attribute(i).name().equals(name)) { return attribute(i); } } return null; }
protected void reset() { try { if (this.fileReader != null) this.fileReader.close(); fileSource.reset(); } catch (IOException ioe) { throw new RuntimeException("FileStream restart failed.", ioe); } if (!getNextFileReader()) { hitEndOfStream = true; throw new RuntimeException("FileStream is empty."); } this.instances = new Instances(this.fileReader, 1, -1); this.instances.setClassIndex(this.instances.numAttributes() - 1); }
protected void reset() { try { if (this.fileReader != null) this.fileReader.close(); fileSource.reset(); } catch (IOException ioe) { throw new RuntimeException("FileStream restart failed.", ioe); } if (!getNextFileReader()) { hitEndOfStream = true; throw new RuntimeException("FileStream is empty."); } this.instances = new Instances(this.fileReader, 1, -1); this.instances.setClassIndex(this.instances.numAttributes() - 1); }
private void reset() throws IOException { if (this.fileReader != null) { this.fileReader.close(); } InputStream fileStream = new FileInputStream(this.arffFileOption.getFile()); this.fileProgressMonitor = new InputStreamProgressMonitor( fileStream); this.fileReader = new BufferedReader(new InputStreamReader( this.fileProgressMonitor)); this.instances = new Instances(this.fileReader, 1, this.classIndexOption.getValue()); if (this.classIndexOption.getValue() < 0) { this.instances.setClassIndex(this.instances.numAttributes() - 1); } else if (this.classIndexOption.getValue() > 0) { this.instances.setClassIndex(this.classIndexOption.getValue() - 1); } this.numInstancesRead = 0; this.lastInstanceRead = null; }
private void reset() throws IOException, FileNotFoundException { if (this.fileReader != null) { this.fileReader.close(); } InputStream fileStream = new FileInputStream(this.arffFileOption.getFile()); this.fileProgressMonitor = new InputStreamProgressMonitor( fileStream); this.fileReader = new BufferedReader(new InputStreamReader( this.fileProgressMonitor)); this.instances = new Instances(this.fileReader, 1, this.classIndexOption.getValue()); if (this.classIndexOption.getValue() < 0) { this.instances.setClassIndex(this.instances.numAttributes() - 1); } else if (this.classIndexOption.getValue() > 0) { this.instances.setClassIndex(this.classIndexOption.getValue() - 1); } this.numInstancesRead = 0; this.lastInstanceRead = null; }
@Override public void restart() { try { if (this.fileReader != null) { this.fileReader.close(); } InputStream fileStream = new FileInputStream(this.arffFileOption.getFile()); this.fileProgressMonitor = new InputStreamProgressMonitor( fileStream); this.fileReader = new BufferedReader(new InputStreamReader( this.fileProgressMonitor)); int classIndex = this.classIndexOption.getValue(); this.instances = new Instances(this.fileReader, 1, classIndex); if (classIndex < 0) { this.instances.setClassIndex(this.instances.numAttributes() - 1); } else if (this.classIndexOption.getValue() > 0) { this.instances.setClassIndex(this.classIndexOption.getValue() - 1); } this.numInstancesRead = 0; this.lastInstanceRead = null; this.hitEndOfFile = !readNextInstanceFromFile(); } catch (IOException ioe) { throw new RuntimeException("ArffFileStream restart failed.", ioe); } this.clusterEvents = new ArrayList<ClusterEvent>(); }
public Clustering(List<? extends Instance> points){ HashMap<Integer, Integer> labelMap = classValues(points); int dim = points.get(0).dataset().numAttributes()-1;
/** * Finds the closest point in the hyper rectangle to a given point. Change the * given point to this closest point by clipping of at all the dimensions to * be clipped of. If the point is inside the rectangle it stays unchanged. The * return value is true if the point was not changed, so the the return value * is true if the point was inside the rectangle. * * @param node The current KDTreeNode in whose hyperrectangle the closest * point is to be found. * @param x a point * @return true if the input point stayed unchanged. */ protected boolean clipToInsideHrect(KDTreeNode node, Instance x) { boolean inside = true; for (int i = 0; i < m_Instances.numAttributes(); i++) { // TODO treat nominals differently!?? if (x.value(i) < node.m_NodeRanges[i][MIN]) { x.setValue(i, node.m_NodeRanges[i][MIN]); inside = false; } else if (x.value(i) > node.m_NodeRanges[i][MAX]) { x.setValue(i, node.m_NodeRanges[i][MAX]); inside = false; } } return inside; }
/** * Initializes the ranges of a subset of the instances of this dataset. * Therefore m_Ranges is not set. * * @param instList list of indexes of the subset * @return the ranges * @throws Exception if something goes wrong */ public double[][] initializeRanges(int[] instList) throws Exception { if (m_Data == null) throw new Exception("No instances supplied."); int numAtt = m_Data.numAttributes(); double[][] ranges = new double [numAtt][3]; if (m_Data.numInstances() <= 0) { initializeRangesEmpty(numAtt, ranges); return ranges; } else { // initialize ranges using the first instance updateRangesFirst(m_Data.instance(instList[0]), numAtt, ranges); // update ranges, starting from the second for (int i = 1; i < instList.length; i++) { updateRanges(m_Data.instance(instList[i]), numAtt, ranges); } } return ranges; }
/** * Weka instances information. * * @param instances the instances * @return the weka.core. instances */ public weka.core.Instances wekaInstancesInformation(Instances instances) { weka.core.Instances wekaInstances; ArrayList<weka.core.Attribute> attInfo = new ArrayList<weka.core.Attribute>(); for (int i = 0; i < instances.numAttributes(); i++) { attInfo.add(wekaAttribute(i, instances.attribute(i))); } wekaInstances = new weka.core.Instances(instances.getRelationName(), attInfo, 0); if (instances.instanceInformation.numOutputAttributes() == 1){ wekaInstances.setClassIndex(instances.classIndex()); } else { //Assign a classIndex to a MultiLabel instance for compatibility reasons wekaInstances.setClassIndex(instances.instanceInformation.numOutputAttributes()-1); //instances.numAttributes()-1); //Last } //System.out.println(attInfo.get(3).name()); //System.out.println(attInfo.get(3).isNominal()); //System.out.println(wekaInstances.attribute(3).name()); //System.out.println(wekaInstances.attribute(3).isNominal()); return wekaInstances; }
@Override protected boolean getNextFileReader() { boolean ret = super.getNextFileReader(); if (ret) { this.instances = new Instances(this.fileReader, 1, -1); if (this.classIndexOption.getValue() < 0) { this.instances.setClassIndex(this.instances.numAttributes() - 1); } else if (this.classIndexOption.getValue() > 0) { this.instances.setClassIndex(this.classIndexOption.getValue() - 1); } } return ret; }
@Override protected boolean getNextFileReader() { boolean ret = super.getNextFileReader(); if (ret) { this.instances = new Instances(this.fileReader, 1, -1); if (this.classIndexOption.getValue() < 0) { this.instances.setClassIndex(this.instances.numAttributes() - 1); } else if (this.classIndexOption.getValue() > 0) { this.instances.setClassIndex(this.classIndexOption.getValue() - 1); } } return ret; }
public Clustering getClusteringResult() { Clustering clustering = null; weka.core.Instances wekaInstances= this.instanceConverter.wekaInstances(instances); try { clusterer.buildClusterer(wekaInstances); int numClusters = clusterer.numberOfClusters(); Instances dataset = getDataset(instances.numAttributes(), numClusters); List<Instance> newInstances = new ArrayList<Instance>() ; //Instances(dataset); for (int i = 0; i < wekaInstances.numInstances(); i++) { weka.core.Instance inst = wekaInstances.get(i); int cnum = clusterer.clusterInstance(inst); Instance newInst = new DenseInstance(instances.instance(cnum)); newInst.insertAttributeAt(inst.numAttributes()); newInst.setDataset(dataset); newInst.setClassValue(cnum); newInstances.add(newInst); } clustering = new Clustering(newInstances); } catch (Exception e) { e.printStackTrace(); } instances = null; return clustering; }
public Instances getDataset(int numdim, int numclass) { FastVector attributes = new FastVector(); for (int i = 0; i < numdim; i++) { attributes.addElement(new Attribute("att" + (i + 1))); } if (numclass > 0) { FastVector classLabels = new FastVector(); for (int i = 0; i < numclass; i++) { classLabels.addElement("class" + (i + 1)); } attributes.addElement(new Attribute("class", classLabels)); } Instances myDataset = new Instances("horizion", attributes, 0); if (numclass > 0) { myDataset.setClassIndex(myDataset.numAttributes() - 1); } return myDataset; }
/** * GenerateMultilabelHeader. * * @param si single-label Instances */ protected MultilabelInstancesHeader generateMultilabelHeader(Instances si) { Instances mi = new Instances(si, 0, 0); mi.setClassIndex(-1); mi.deleteAttributeAt(mi.numAttributes() - 1); FastVector bfv = new FastVector(); bfv.addElement("0"); bfv.addElement("1"); for (int i = 0; i < this.m_L; i++) { mi.insertAttributeAt(new Attribute("class" + i, bfv), i); } this.multilabelStreamTemplate = mi; this.multilabelStreamTemplate.setRelationName("SYN_Z" + this.labelCardinalityOption.getValue() + "L" + this.m_L + "X" + m_A + "S" + metaRandomSeedOption.getValue() + ": -C " + this.m_L); this.multilabelStreamTemplate.setClassIndex(this.m_L); return new MultilabelInstancesHeader(multilabelStreamTemplate, m_L); }