List of usage examples for weka.core Attribute index
public finalint index()
From source file:ID3Chi.java
License:Open Source License
/** * Method for building an ID3Chi tree./*from w ww. j a va 2 s. c o m*/ * * @param data * the training data * @exception Exception * if decision tree can't be built successfully */ private void makeTree(Instances data) throws Exception { // Check if no instances have reached this node. /* if (data.numInstances() == 0) { m_Attribute = null; m_ClassValue = Instance.missingValue(); m_Distribution = new double[data.numClasses()]; return; } /**/ if (data.numInstances() == 0) { SetNullDistribution(data); } // Compute attribute with maximum information gain. double[] infoGains = new double[data.numAttributes()]; Enumeration attEnum = data.enumerateAttributes(); double entropyOfAllData = computeEntropy(data); while (attEnum.hasMoreElements()) { Attribute att = (Attribute) attEnum.nextElement(); infoGains[att.index()] = computeInfoGain(data, att, entropyOfAllData); } m_Attribute = data.attribute(Utils.maxIndex(infoGains)); double chiSquare = computeChiSquare(data, m_Attribute); int degreesOfFreedom = m_Attribute.numValues() - 1; ChiSquaredDistribution chi = new ChiSquaredDistribution(degreesOfFreedom); double threshold = chi.inverseCumulativeProbability(m_confidenceLevel); // Make leaf if information gain is zero. // Otherwise create successors. if (Utils.eq(infoGains[m_Attribute.index()], 0)) { MakeALeaf(data); } else { // Discard unknown values for selected attribute //data.deleteWithMissing(m_Attribute); Instances[] subset = splitData(data, m_Attribute); if (CheckIfCanApplyChiSquare(subset) && (chiSquare <= threshold)) { MakeALeaf(data); return; } m_Successors = new ID3Chi[m_Attribute.numValues()]; for (int j = 0; j < m_Attribute.numValues(); j++) { m_Successors[j] = new ID3Chi(this.m_confidenceLevel); m_Successors[j].m_Ratio = (double) subset[j].numInstances() / (double) data.numInstances(); m_Successors[j].makeTree(subset[j]); } } }
From source file:adams.flow.sink.WekaInstanceViewer.java
License:Open Source License
/** * Displays the token (the panel and dialog have already been created at * this stage)./* w w w . j a v a2 s .c o m*/ * * @param token the token to display */ @Override protected void display(Token token) { InstanceContainerManager manager; InstanceContainer cont; weka.core.Instance winst; weka.core.Attribute att; String id; adams.data.instance.Instance inst; if (token.getPayload() instanceof weka.core.Instance) { winst = (weka.core.Instance) token.getPayload(); inst = new adams.data.instance.Instance(); inst.set(winst); if (!m_ID.isEmpty()) { att = winst.dataset().attribute(m_ID); if (att != null) { if (att.isNominal() || att.isString()) id = winst.stringValue(att.index()); else id = "" + winst.value(att.index()); inst.setID(id); } } } else { inst = (adams.data.instance.Instance) token.getPayload(); if (inst.hasReport() && inst.getReport().hasValue(m_ID)) inst.setID("" + inst.getReport().getValue(new Field(m_ID, DataType.UNKNOWN))); } manager = m_InstancePanel.getContainerManager(); cont = manager.newContainer(inst); manager.startUpdate(); manager.add(cont); m_Updater.update(m_InstancePanel, cont); }
From source file:adams.flow.transformer.WekaExperimentEvaluation.java
License:Open Source License
/** * Evaluates the experiment data./* www . j a va2 s .c o m*/ * * @param data the data to evaluate * @throws Exception If something goes wrong, like loading * data fails or comparison field invalid */ protected void evaluateExperiment(Instances data) throws Exception { Tester ttester; StringBuilder outBuff; int compareCol; int tType; String tmpStr; weka.core.Attribute att; // setup testing algorithm ttester = getTester(data); // evaluate experiment tmpStr = m_ComparisonField.getField(); att = data.attribute(tmpStr); if (att == null) throw new Exception("Cannot find comparison field '" + tmpStr + "' in data!"); compareCol = att.index(); tType = m_TestBase; outBuff = new StringBuilder(); if (m_OutputHeader) { outBuff.append(ttester.header(compareCol)); outBuff.append("\n"); } try { if (tType < ttester.getNumResultsets()) outBuff.append(ttester.multiResultsetFull(tType, compareCol)); else if (tType == ttester.getNumResultsets()) outBuff.append(ttester.multiResultsetSummary(compareCol)); else outBuff.append(ttester.multiResultsetRanking(compareCol)); outBuff.append("\n"); } catch (Exception ex) { outBuff.append(ex.getMessage() + "\n"); } // broadcast evaluation m_OutputToken = new Token(outBuff.toString()); }
From source file:adams.ml.data.InstancesView.java
License:Open Source License
/** * Returns whether the specified column is a class attribute. * * @param name they name of the column to query * @return true if column a class attribute *//*from w w w . jav a 2 s .c om*/ @Override public boolean isClassAttributeByName(String name) { Attribute att; att = m_Data.attribute(name); return (att != null) && (att.index() == m_Data.classIndex()); }
From source file:adams.ml.data.InstancesView.java
License:Open Source License
/** * Sets the class attribute status for a column. * * @param name the name of the column to set the class attribute status for * @param isClass if true then the column will be flagged as class * attribute, otherwise the flag will get removed * @return true if successfully updated *//*from ww w . j a v a 2s . c om*/ @Override public boolean setClassAttributeByName(String name, boolean isClass) { Attribute att; att = m_Data.attribute(name); return (att != null) && setClassAttribute(att.index(), isClass); }
From source file:app.RunApp.java
License:Open Source License
/** * Action when table attributes clicked with left mouse * //from ww w. j ava 2s .c om * @param evt Event */ private void tableAttributesLeftMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableAttributesLeftMouseClicked if (tabsAttributes.getSelectedIndex() == 0) { int selected = tableAttributesLeft.getSelectedRow(); String attr = tableAttributesLeft.getValueAt(selected, 0).toString(); Instances instances = dataset.getDataSet(); Attribute currentAttr = instances.attribute(attr); double[] attributeValues = instances.attributeToDoubleArray(currentAttr.index()); HeapSort.sort(attributeValues); attributesBoxDiagram2.getChart().setTitle(currentAttr.name()); attributesBoxDiagram2.getChart().getXYPlot().clearAnnotations(); ChartUtils.updateXYChart(attributesBoxDiagram2, HeapSort.getSortedArray()); } }
From source file:asap.PostProcess.java
private void formatPredictions(Instances instances, double[] predictions, String[] columnNames, int predictionsColumnIndex, String predictionsColumnName, String columnSeparator, String outputFilename, boolean writeColumnsHeaderLine) { PerformanceCounters.startTimer("formatPredictions"); System.out.println("Formatting predictions to file " + outputFilename + "..."); File outputFile = new File(outputFilename); PrintWriter writer;//w w w . j a v a 2s. c om try { outputFile.getParentFile().mkdirs(); outputFile.createNewFile(); writer = new PrintWriter(outputFile, "UTF-8"); } catch (IOException ex) { Logger.getLogger(PostProcess.class.getName()).log(Level.SEVERE, null, ex); return; } StringBuilder sb = new StringBuilder(); DecimalFormat df = new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.US)); df.setMaximumFractionDigits(3); int i = -1; if (!writeColumnsHeaderLine) { i = 0; } for (; i < instances.numInstances(); i++) { sb.delete(0, sb.length()); for (int j = 0; j < columnNames.length; j++) { if (j > 0) { sb.append(columnSeparator); } if (j == predictionsColumnIndex) { if (i < 0) { sb.append(predictionsColumnName); } else { sb.append(df.format(predictions[i])); } sb.append(columnSeparator); } if (i < 0) { sb.append(columnNames[j]); } else { if (columnNames[j].toLowerCase().contains("id")) { Attribute attribute = instances.attribute(columnNames[j]); if (attribute != null) { sb.append((int) instances.instance(i).value(attribute.index())); } else { sb.append(0); } } else { Attribute attribute = instances.attribute(columnNames[j]); if (attribute != null) { sb.append(instances.instance(i).value(attribute.index())); } else { sb.append(df.format(0d)); } } } } if (columnNames.length == predictionsColumnIndex) { sb.append(columnSeparator); if (i < 0) { sb.append(predictionsColumnName); } else { sb.append(df.format(predictions[i])); } } writer.println(sb); } writer.flush(); writer.close(); System.out.println("\tdone."); PerformanceCounters.stopTimer("formatPredictions"); }
From source file:ca.uottawa.balie.WekaLearner.java
License:Open Source License
/** * Creates a new classification algorithm. * //from www . j a va2s . c om * @param pi_Attributes Array of attributes * @param pi_ClassAttributes Class attribute */ public WekaLearner(WekaAttribute[] pi_Attributes, String[] pi_ClassAttributes) { m_ClassAttributes = pi_ClassAttributes; m_AttributeLabels = new String[pi_Attributes.length]; // Create the Weka Attributes m_WekaAttributes = new FastVector(pi_Attributes.length + 1); for (int i = 0; i != pi_Attributes.length; ++i) { if (pi_Attributes[i].Label().equals(CLASS_LABEL)) { throw new Error("Attribute cannot be named \"Class\" (reserved)"); } m_AttributeLabels[i] = pi_Attributes[i].Label(); if (pi_Attributes[i].IsNumeric()) { Attribute anAttribute = new Attribute(pi_Attributes[i].Label()); m_WekaAttributes.addElement(anAttribute); } else { Attribute anAttribute = new Attribute(pi_Attributes[i].Label(), pi_Attributes[i].Values()); m_WekaAttributes.addElement(anAttribute); } } // Create The class attribute FastVector fvClass = new FastVector(pi_ClassAttributes.length); for (int i = 0; i != pi_ClassAttributes.length; ++i) { fvClass.addElement(pi_ClassAttributes[i]); } Attribute aClass = new Attribute(CLASS_LABEL, fvClass); m_WekaAttributes.addElement(aClass); m_TrainingSet = new Instances("", m_WekaAttributes, 0); m_TrainingSet.setClassIndex(aClass.index()); m_TestingSet = new Instances("", m_WekaAttributes, 0); m_TestingSet.setClassIndex(aClass.index()); // create an empty confusion matrix.. will be populated at evaluation time m_ConfusionMatrix = new double[0][0]; m_bDoubleOnly = false; }
From source file:cerebro.Id3.java
License:Open Source License
/** * Method for building an Id3 tree./*from w ww . j av a 2 s . c o m*/ * * @param data the training data * @exception Exception if decision tree can't be built successfully */ private void makeTree(Instances data) throws Exception { // Check if no instances have reached this node. if (data.numInstances() == 0) { m_Attribute = null; m_ClassValue = Instance.missingValue(); m_Distribution = new double[data.numClasses()]; return; } // Compute attribute with maximum information gain. double[] infoGains = new double[data.numAttributes()]; Enumeration attEnum = data.enumerateAttributes(); while (attEnum.hasMoreElements()) { Attribute att = (Attribute) attEnum.nextElement(); infoGains[att.index()] = computeInfoGain(data, att); } m_Attribute = data.attribute(Utils.maxIndex(infoGains)); // Make leaf if information gain is zero. // Otherwise create successors. if (Utils.eq(infoGains[m_Attribute.index()], 0)) { m_Attribute = null; m_Distribution = new double[data.numClasses()]; Enumeration instEnum = data.enumerateInstances(); while (instEnum.hasMoreElements()) { Instance inst = (Instance) instEnum.nextElement(); m_Distribution[(int) inst.classValue()]++; } Utils.normalize(m_Distribution); m_ClassValue = Utils.maxIndex(m_Distribution); m_ClassAttribute = data.classAttribute(); } else { Instances[] splitData = splitData(data, m_Attribute); m_Successors = new Id3[m_Attribute.numValues()]; for (int j = 0; j < m_Attribute.numValues(); j++) { m_Successors[j] = new Id3(); m_Successors[j].makeTree(splitData[j]); } } }
From source file:cezeri.utils.FactoryInstance.java
public static Instances getSubsetData(Instances data, String[] attList) { Instances temp = new Instances(data); for (int i = 0; i < data.numAttributes(); i++) { if (!temp.attribute(0).equals(temp.classAttribute())) { temp.deleteAttributeAt(0);/*w ww .ja va 2s .co m*/ } } double[][] m = new double[attList.length + 1][data.numInstances()]; for (int i = 0; i < attList.length; i++) { int n = attList.length - 1 - i; String str = attList[n]; Attribute t = data.attribute(str); double[] d = data.attributeToDoubleArray(t.index()); m[n] = d; temp.insertAttributeAt(t, 0); } m[attList.length] = data.attributeToDoubleArray(data.classIndex()); m = CMatrix.getInstance(m).transpose().get2DArrayDouble(); FastVector att = new FastVector(); for (int i = 0; i < temp.numAttributes(); i++) { att.addElement(temp.attribute(i)); } Instances ret = new Instances(temp.relationName(), att, m.length); for (int i = 0; i < m.length; i++) { Instance ins = new Instance(m[0].length); for (int j = 0; j < m[0].length; j++) { ins.setValue(j, m[i][j]); } ret.add(ins); } ret.setClassIndex(temp.classIndex()); return ret; }