@Override protected boolean readNextInstanceFromFile() { try { if (this.instances.readInstance(this.fileReader)) { this.lastInstanceRead = new InstanceExample(this.instances.instance(0)); this.instances.delete(); // keep instances clean return true; } if (this.fileReader != null) { this.fileReader.close(); this.fileReader = null; } return false; } catch (IOException ioe) { throw new RuntimeException( "ArffFileStream failed to read instance from stream.", ioe); } }
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); }
@Override public void trainOnInstanceImpl(Instance inst) { if (inst.classValue() > C) C = (int)inst.classValue(); if (this.window == null) { this.window = new Instances(inst.dataset()); } if (this.limitOption.getValue() <= this.window.numInstances()) { this.window.delete(0); } this.window.add(inst); }
public Attribute attribute(String name) { for (int i = 0; i < numAttributes(); i++) { if (attribute(i).name().equals(name)) { return attribute(i); } } return null; }
/** * Sets the indices of relevant features. * This method also sets the irrelevant ones since * it is the set complement. * * @param indicesRelevants */ public void setIndicesRelevants(int[] indicesRelevants) { this.indicesRelevants = indicesRelevants; // -1 to skip the class attribute int numIrrelevantFeatures = this.numAttributes() - this.indicesRelevants.length - 1; this.indicesIrrelevants = new int[numIrrelevantFeatures]; // Infers and sets the set of irrelevant features int index = 0; int indexRel = 0; for(int i = 0; i < numAttributes(); i++){ if(i != classIndex()) { while (indexRel < indicesRelevants.length - 1 && i > indicesRelevants[indexRel]) indexRel++; if (indicesRelevants[indexRel] != i){ indicesIrrelevants[index] = i; index++; } } } } }
public void initHeader(Instances dataset) { int numLabels = this.numOldLabelsOption.getValue(); Attribute target = dataset.classAttribute(); List<String> possibleValues = new ArrayList<String>(); int n = target.numValues(); for (int i = 0; i < n; i++) { possibleValues.add(target.value(i)); } ArrayList<Attribute> attrs = new ArrayList<Attribute>(numLabels + dataset.numAttributes()); for (int i = 0; i < numLabels; i++) { attrs.add(new Attribute(target.name() + "_" + i, possibleValues)); } for (int i = 0; i < dataset.numAttributes(); i++) { Attribute attr = dataset.attribute(i); Attribute newAttribute = null; if (attr.isNominal() == true) { newAttribute = new Attribute(attr.name(), attr.getAttributeValues()); } if (attr.isNumeric() == true) { newAttribute = new Attribute(attr.name()); } if (newAttribute != null) { attrs.add(newAttribute); } } this.header = new Instances("extended_" + dataset.getRelationName(), attrs, 0); this.header.setClassIndex(numLabels + dataset.classIndex()); }
double candidateWeight = 0.0; Random random = new Random(1); Instances randData = new Instances(chunk); randData.randomize(random); if (randData.classAttribute().isNominal()) { randData.stratify(numFolds); Instances train = randData.trainCV(numFolds, n, random); Instances test = randData.testCV(numFolds, n); for (int num = 0; num < train.numInstances(); num++) { learner.trainOnInstance(train.instance(num));
int classIndex = this.ltm.classIndex(); for (int c = 0; c <= this.maxClassValue; c++){ List<double[]> classSamples = new ArrayList<>(); for (int i = this.ltm.numInstances()-1; i >-1 ; i--) { if (this.ltm.get(i).classValue() == c) { classSamples.add(this.ltm.get(i).toDoubleArray()); this.ltm.delete(i); double[] attributes = new double[this.ltm.numAttributes()]; System.arraycopy(centroid, 0, attributes, 1, this.ltm.numAttributes() - 1); Instance inst = new InstanceImpl(1, attributes); inst.setDataset(this.ltm); this.ltm.add(inst);
InputStreamProgressMonitor fileProgressMonitor = new InputStreamProgressMonitor(fileStream); Reader fileReader = new BufferedReader(new InputStreamReader(fileProgressMonitor)); Instances instances = new Instances(fileReader, 1, this.classIndexOption.getValue()); for (int i = 0; i < instances.numAttributes()-ignoredAttributes.size(); i++) { Double[] values = {Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY,0.0}; valuesMinMaxDiff.add(values); while (instances.readInstance(fileReader)) { Instance instance = instances.instance(0); int a = 0; for (int i = 0; i < instances.numAttributes(); i++) { if(!ignoredAttributes.contains(i)){ double value = instance.value(i); instances.delete();
fileProgressMonitor = new InputStreamProgressMonitor(fileStream); fileReader = new BufferedReader(new InputStreamReader(fileProgressMonitor)); instances = new Instances(fileReader, 1, this.classIndexOption.getValue()); if (classIndexOption.getValue() < 0) { instances.setClassIndex(instances.numAttributes() - 1); } else if (classIndexOption.getValue() > 0) { instances.setClassIndex(classIndexOption.getValue() - 1); for (int i = 0; i < rawAttributeList.length; i++) { int attribute = ((IntOption)rawAttributeList[i]).getValue(); if(1 <= attribute && attribute <= instances.numAttributes()) attributes.add(attribute-1); else "Attribute option "+attribute +" will be ignored. Filestream only has " +instances.numAttributes()+" attributes."); for (int i = 0; i < instances.numAttributes(); i++) { if(!instances.attribute(i).isNumeric() && i != instances.classIndex()){ attributes.add(i); numAttsOption = new IntOption("numAtts", 'a',"", instances.numAttributes() - removeAttributes.length); for (int i = 0; i < removeAttributes.length; i++) { System.out.println((removeAttributes[i]+1)+" " +instances.attribute(removeAttributes[i]).name()); filteredDataset = new Instances(instances);
Instances second = this.driftStream.getHeader(); FastVector newAttributes = new FastVector(); for (int i = 0; i < first.numAttributes() - 1; i++) { newAttributes.addElement(first.attribute(i)); for (int i = 0; i < second.numAttributes() - 1; i++) { newAttributes.addElement(second.attribute(i)); if (first.numClasses() < second.numClasses()) { classLabels = second.classAttribute(); } else { classLabels = first.classAttribute(); this.streamHeader = new InstancesHeader(new Instances( getCLICreationString(InstanceStream.class), newAttributes, 0)); this.streamHeader.setClassIndex(this.streamHeader.numAttributes() - 1);
@Override public String toString() { StringBuilder text = new StringBuilder(); for (int i = 0; i < numInstances(); i++) { text.append(instance(i).toString()); if (i < numInstances() - 1) { text.append('\n'); } } return text.toString(); }
/** * Stratify. * * @param numFolds the num folds */ public void stratify(int numFolds) { if (classAttribute().isNominal()) { // sort by class int index = 1; while (index < numInstances()) { Instance instance1 = instance(index - 1); for (int j = index; j < numInstances(); j++) { Instance instance2 = instance(j); if ((instance1.classValue() == instance2.classValue()) || (instance1.classIsMissing() && instance2.classIsMissing())) { swap(index, j); index++; } } index++; } stratStep(numFolds); } }
/** * 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); }
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(); } }
result.append(m_headerInfo.classAttribute().value(c)).append("\t"). append(Double.toString(m_probOfClass[c])).append("\n"); result.append(m_headerInfo.classAttribute().value(c)).append("\t"); for (int w = 0; w < m_headerInfo.numAttributes(); w++) { if (w == m_headerInfo.classIndex()) { continue; result.append(m_headerInfo.attribute(w).name()).append("\t"); for (int c = 0; c < m_numClasses; c++) { double value = m_wordTotalForClass[c].getValue(w);
/** * 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 public void trainOnInstanceImpl(Instance inst) { if (inst.classValue() > C) { C = (int) inst.classValue(); } if (this.window == null) { this.window = new Instances(inst.dataset()); } for (int i = 0; i < this.window.size(); i++) { if (this.classifierRandom.nextDouble() > this.prob) { this.window.delete(i); } } this.window.add(inst); }
double leftAttSum[] = new double[m_Instances.numAttributes()], rightAttSum[] = new double[m_Instances.numAttributes()], leftAttSqSum[] = new double[m_Instances.numAttributes()], rightAttSqSum[] = new double[m_Instances.numAttributes()], rightSqMean, leftSqMean, leftSqSum, rightSqSum, minSum = Double.POSITIVE_INFINITY, val; for (int dim = 0; dim < m_Instances.numAttributes(); dim++) { || dim == m_Instances.classIndex()) continue; for (int j = 0; j < m_Instances.numAttributes(); j++) { if (j == m_Instances.classIndex()) continue; val = m_Instances.instance(m_InstList[i]).value(j); if (m_NormalizeNodeWidth) { if (Double.isNaN(universe[j][MIN]) Instance inst = m_Instances.instance(m_InstList[i]); leftSqSum = rightSqSum = 0.0; for (int j = 0; j < m_Instances.numAttributes(); j++) { if (j == m_Instances.classIndex()) continue; val = inst.value(j); splitVal = (m_Instances.instance(m_InstList[i]).value(dim) + m_Instances .instance(m_InstList[i + 1]).value(dim)) / 2; else