Java Code Examples for weka.core.Instances#attribute()
The following examples show how to use
weka.core.Instances#attribute() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChangeDateFormat.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * Set the output format. Changes the format of the specified date * attribute. */ private void setOutputFormat() { // Create new attributes FastVector newAtts = new FastVector(getInputFormat().numAttributes()); for (int j = 0; j < getInputFormat().numAttributes(); j++) { Attribute att = getInputFormat().attribute(j); if (j == m_AttIndex.getIndex()) { newAtts.addElement(new Attribute(att.name(), getDateFormat().toPattern())); } else { newAtts.addElement(att.copy()); } } // Create new header Instances newData = new Instances(getInputFormat().relationName(), newAtts, 0); newData.setClassIndex(getInputFormat().classIndex()); m_OutputAttribute = newData.attribute(m_AttIndex.getIndex()); setOutputFormat(newData); }
Example 2
Source File: FPGrowth.java From tsml with GNU General Public License v3.0 | 6 votes |
private ArrayList<Item> parseRulesMustContain(Instances data) { ArrayList<Item> result = new ArrayList<Item>(); String[] split = m_rulesMustContain.trim().split(","); for (int i = 0; i < split.length; i++) { String attName = split[i].trim(); Attribute att = data.attribute(attName); if (att == null) { System.err.println("[FPGrowth] : WARNING - can't find attribute " + attName + " in the data."); } else { BinaryItem tempI = null; try { tempI = new BinaryItem(att, m_positiveIndex - 1); } catch (Exception e) { // this should never happen e.printStackTrace(); } result.add(tempI); } } return result; }
Example 3
Source File: IncrementalPerformance.java From meka with GNU General Public License v3.0 | 6 votes |
/** * Creates a panel displaying the data. * * @param data the plot data * @return the panel * @throws Exception if plot generation fails */ protected VisualizePanel createPanel(Instances data) throws Exception { VisualizePanel result = new ThresholdVisualizePanel(); PlotData2D plot = new PlotData2D(data); plot.setPlotName("Incremental performance"); plot.m_displayAllPoints = true; boolean[] connectPoints = new boolean [data.numInstances()]; for (int cp = 1; cp < connectPoints.length; cp++) connectPoints[cp] = true; plot.setConnectPoints(connectPoints); result.addPlot(plot); if (data.attribute(SAMPLES) != null) result.setXIndex(data.attribute(SAMPLES).index()); if (data.attribute(ACCURACY) != null) result.setYIndex(data.attribute(ACCURACY).index()); return result; }
Example 4
Source File: ARAMNetworkClass.java From meka with GNU General Public License v3.0 | 6 votes |
/** * Generates the classifier. * * @param instances set of instances serving as training data * @exception Exception if the classifier has not been generated * successfully */ //public int[] getneuronsactivated(); // public double[] getneuronsactivity(); public void testCapabilities(Instances D) throws Exception { // get the classifier's capabilities, enable all class attributes and do the usual test Capabilities cap = getCapabilities(); cap.enableAllClasses(); //getCapabilities().testWithFail(D); // get the capabilities again, test class attributes individually int L = D.classIndex(); for(int j = 0; j < L; j++) { Attribute c = D.attribute(j); cap.testWithFail(c,true); } }
Example 5
Source File: SuvervisedFilterPreprocessor.java From AILibs with GNU Affero General Public License v3.0 | 6 votes |
@Override public Instances apply(final Instances data) throws PreprocessingException { if (!this.prepared) { throw new IllegalStateException("Cannot apply preprocessor before it has been prepared!"); } try { Instances inst = this.selector.reduceDimensionality(data); if (inst.classIndex() >= 0) { inst = WekaUtil.removeClassAttribute(inst); } for (int i = 0; i < inst.numAttributes(); i++) { Attribute a = inst.attribute(i); inst.renameAttribute(a, this.getClass().getSimpleName() + "_" + a.name()); } return inst; } catch (Exception e) { throw new PreprocessingException(e); } }
Example 6
Source File: ACF_PACF.java From tsml with GNU General Public License v3.0 | 5 votes |
@Override protected Instances determineOutputFormat(Instances inputFormat) throws Exception { //Check capabilities for the filter. Can only handle real valued, no missing. getCapabilities().testWithFail(inputFormat); seriesLength=inputFormat.numAttributes(); if(inputFormat.classIndex()>=0) seriesLength--; if(maxLag>seriesLength-endTerms) maxLag=seriesLength-endTerms; if(maxLag<0) maxLag=inputFormat.numAttributes()-1; //Set up instances size and format. ArrayList<Attribute> atts=new ArrayList<>(); String name; for(int i=0;i<maxLag;i++){ name = "ACF_"+i; atts.add(new Attribute(name)); } for(int i=0;i<maxLag;i++){ name = "PACF_"+i; atts.add(new Attribute(name)); } if(inputFormat.classIndex()>=0){ //Classification set, set class //Get the class values Attribute target =inputFormat.attribute(inputFormat.classIndex()); ArrayList<String> vals=new ArrayList<>(target.numValues()); for(int i=0;i<target.numValues();i++) vals.add(target.value(i)); atts.add(new Attribute(inputFormat.attribute(inputFormat.classIndex()).name(),vals)); } Instances result = new Instances("PACF"+inputFormat.relationName(),atts,inputFormat.numInstances()); if(inputFormat.classIndex()>=0) result.setClassIndex(result.numAttributes()-1); return result; }
Example 7
Source File: Differences.java From tsml with GNU General Public License v3.0 | 5 votes |
protected Instances determineOutputFormat(Instances inputFormat) throws Exception { //Check all attributes are real valued, otherwise throw exception for(int i=0;i<inputFormat.numAttributes();i++) if(inputFormat.classIndex()!=i) { if (!inputFormat.attribute(i).isNumeric()) throw new Exception("Non numeric attribute not allowed in Moments"); } //Set up instances size and format. ArrayList<Attribute> atts = new ArrayList<>(); String name; for(int i=0;i<inputFormat.numAttributes()-order-1;i++){ name = attName+"Difference"+order+"_"+(i+1); atts.add(new Attribute(name)); } if(inputFormat.classIndex()>=0){ //Classification set, set class //Get the class values as a fast vector Attribute target =inputFormat.attribute(inputFormat.classIndex()); ArrayList<String> vals=new ArrayList<>(); for(int i=0;i<target.numValues();i++) vals.add(target.value(i)); atts.add(new Attribute(inputFormat.attribute(inputFormat.classIndex()).name(),vals)); } Instances result = new Instances("Difference"+order+inputFormat.relationName(),atts,inputFormat.numInstances()); if(inputFormat.classIndex()>=0){ result.setClassIndex(result.numAttributes()-1); } return result; }
Example 8
Source File: PowerCepstrum.java From tsml with GNU General Public License v3.0 | 5 votes |
@Override protected Instances determineOutputFormat(Instances inputFormat) throws Exception { //Set up instances size and format. int length=(fftFilter.findLength(inputFormat)); length/=2; ArrayList<Attribute> atts=new ArrayList<>(); String name; for(int i=0;i<length;i++){ name = "PowerSpectrum_"+i; atts.add(new Attribute(name)); } if(inputFormat.classIndex()>=0){ //Classification set, set class //Get the class values as a fast vector Attribute target =inputFormat.attribute(inputFormat.classIndex()); ArrayList<String> vals=new ArrayList<>(target.numValues()); for(int i=0;i<target.numValues();i++) vals.add(target.value(i)); atts.add(new Attribute(inputFormat.attribute(inputFormat.classIndex()).name(),vals)); } Instances result = new Instances("Cepstrum"+inputFormat.relationName(),atts,inputFormat.numInstances()); if(inputFormat.classIndex()>=0) result.setClassIndex(result.numAttributes()-1); return result; }
Example 9
Source File: FilteredClassifier.java From meka with GNU General Public License v3.0 | 5 votes |
/** * TestCapabilities. * Make sure the training data is suitable. * @param D the data */ public void testCapabilities(Instances D) throws Exception { // get the classifier's capabilities, enable all class attributes and do the usual test Capabilities cap = getCapabilities(); cap.enableAllClasses(); // get the capabilities again, test class attributes individually int L = D.classIndex(); for(int j = 0; j < L; j++) { Attribute c = D.attribute(j); cap.testWithFail(c,true); } }
Example 10
Source File: AnchorpointsCreationTest.java From AILibs with GNU Affero General Public License v3.0 | 5 votes |
@Test public void anchorpointsAreCreatedAndHaveTheValues() throws IOException, InvalidAnchorPointsException, AlgorithmException, InterruptedException, ClassNotFoundException, DatasetCreationException { int[] xValues = new int[] { 2, 4, 8, 16, 32, 64 }; Instances dataset = null; OpenmlConnector client = new OpenmlConnector(); try { DataSetDescription description = client.dataGet(42); File file = client.datasetGet(description); DataSource source = new DataSource(file.getCanonicalPath()); dataset = source.getDataSet(); dataset.setClassIndex(dataset.numAttributes() - 1); Attribute targetAttribute = dataset.attribute(description.getDefault_target_attribute()); dataset.setClassIndex(targetAttribute.index()); } catch (Exception e) { throw new IOException("Could not load data set from OpenML!", e); } // final LearningCurveExtrapolationMethod extrapolationMethod, final ISupervisedLearner<I, D> learner, final D dataset, final double trainsplit, final int[] anchorPoints, // final ISamplingAlgorithmFactory<?, D, ? extends ASamplingAlgorithm<D>> samplingAlgorithmFactory, final long seed WekaInstances simpleDataset = new WekaInstances(dataset); LearningCurveExtrapolator extrapolator = new LearningCurveExtrapolator((x, y, ds) -> { Assert.assertArrayEquals(x, xValues); for (int i = 0; i < y.length; i++) { Assert.assertTrue(y[i] > 0.0d); } return null; }, new WekaClassifier(new J48()), simpleDataset, 0.7d, xValues, new SystematicSamplingFactory<>(), 1l); extrapolator.extrapolateLearningCurve(); }
Example 11
Source File: AbstractMultiSearch.java From meka with GNU General Public License v3.0 | 5 votes |
/** * TestCapabilities. * Make sure the training data is suitable. * @param D the data */ public void testCapabilities(Instances D) throws Exception { // get the classifier's capabilities, enable all class attributes and do the usual test Capabilities cap = getCapabilities(); cap.enableAllClasses(); //getCapabilities().testWithFail(D); // get the capabilities again, test class attributes individually int L = D.classIndex(); for(int j = 0; j < L; j++) { Attribute c = D.attribute(j); cap.testWithFail(c,true); } }
Example 12
Source File: CollectiveInstances.java From collective-classification-weka-package with GNU General Public License v3.0 | 5 votes |
/** * randomly initializes the class labels in the given set according to the * class distribution in the training set * @param train the training instances to retrieve the class * distribution from * @param instances the instances to initialize * @param from the first instance to initialize * @param count the number of instances to initialize * @return the initialize instances * @throws Exception if something goes wrong */ public Instances initializeLabels( Instances train, Instances instances, int from, int count ) throws Exception { int i; AttributeStats stats; Attribute classAttr; double percentage; // reset flip count m_FlippedLabels = 0; // explicitly set labels to "missing" for (i = from; i < from + count; i++) instances.instance(i).setClassMissing(); // determining the percentage of the first class stats = train.attributeStats(train.classIndex()); percentage = (double) stats.nominalCounts[0] / (double) stats.totalCount; // set lables classAttr = instances.attribute(instances.classIndex()); for (i = from; i < from + count; i++) { // random class if (m_Random.nextDouble() < percentage) instances.instance(i).setClassValue(classAttr.value(0)); else instances.instance(i).setClassValue(classAttr.value(1)); } return instances; }
Example 13
Source File: PACF.java From tsml with GNU General Public License v3.0 | 5 votes |
@Override protected Instances determineOutputFormat(Instances inputFormat) throws Exception { //Check capabilities for the filter. Can only handle real valued, no missing. getCapabilities().testWithFail(inputFormat); seriesLength=inputFormat.numAttributes(); if(inputFormat.classIndex()>=0) seriesLength--; if(maxLag>seriesLength-endTerms) maxLag=seriesLength-endTerms; if(maxLag<0) maxLag=inputFormat.numAttributes()-1; //Set up instances size and format. ArrayList<Attribute> atts=new ArrayList<>(); String name; for(int i=0;i<maxLag;i++){ name = "PACF_"+i; atts.add(new Attribute(name)); } if(inputFormat.classIndex()>=0){ //Classification set, set class //Get the class values Attribute target =inputFormat.attribute(inputFormat.classIndex()); ArrayList<String> vals=new ArrayList<>(target.numValues()); for(int i=0;i<target.numValues();i++) vals.add(target.value(i)); atts.add(new Attribute(inputFormat.attribute(inputFormat.classIndex()).name(),vals)); } Instances result = new Instances("PACF"+inputFormat.relationName(),atts,inputFormat.numInstances()); if(inputFormat.classIndex()>=0) result.setClassIndex(result.numAttributes()-1); return result; }
Example 14
Source File: Copy.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Sets the format of the input instances. * * @param instanceInfo an Instances object containing the input instance * structure (any instances contained in the object are ignored - only the * structure is required). * @return true if the outputFormat may be collected immediately * @throws Exception if a problem occurs setting the input format */ public boolean setInputFormat(Instances instanceInfo) throws Exception { super.setInputFormat(instanceInfo); m_CopyCols.setUpper(instanceInfo.numAttributes() - 1); // Create the output buffer Instances outputFormat = new Instances(instanceInfo, 0); m_SelectedAttributes = m_CopyCols.getSelection(); for (int i = 0; i < m_SelectedAttributes.length; i++) { int current = m_SelectedAttributes[i]; // Create a copy of the attribute with a different name Attribute origAttribute = instanceInfo.attribute(current); outputFormat.insertAttributeAt((Attribute)origAttribute.copy("Copy of " + origAttribute.name()), outputFormat.numAttributes()); } // adapt locators int[] newIndices = new int[instanceInfo.numAttributes() + m_SelectedAttributes.length]; for (int i = 0; i < instanceInfo.numAttributes(); i++) newIndices[i] = i; for (int i = 0; i < m_SelectedAttributes.length; i++) newIndices[instanceInfo.numAttributes() + i] = m_SelectedAttributes[i]; initInputLocators(instanceInfo, newIndices); setOutputFormat(outputFormat); return true; }
Example 15
Source File: TwoWayNominalSplit.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Gets the string describing the comparision the split depends on for a particular * branch. i.e. the right hand side of the description of the split. * * @param branchNum the branch of the split * @param dataset the dataset that the split is based on * @return a string describing the comparison */ public String comparisonString(int branchNum, Instances dataset) { Attribute att = dataset.attribute(attIndex); if (att.numValues() != 2) return ((branchNum == 0 ? "= " : "!= ") + att.value(trueSplitValue)); else return ("= " + (branchNum == 0 ? att.value(trueSplitValue) : att.value(trueSplitValue == 0 ? 1 : 0))); }
Example 16
Source File: TSBF.java From tsml with GNU General Public License v3.0 | 5 votes |
Instances formatProbabilityInstances(double[][] probs,Instances data){ int numClasses=data.numClasses(); int numFeatures=(numClasses-1)*numSubSeries; //Set up instances size and format. FastVector atts=new FastVector(); String name; for(int j=0;j<numFeatures;j++){ name = "ProbFeature"+j; atts.addElement(new Attribute(name)); } //Get the class values as a fast vector Attribute target =data.attribute(data.classIndex()); FastVector vals=new FastVector(target.numValues()); for(int j=0;j<target.numValues();j++) vals.addElement(target.value(j)); atts.addElement(new Attribute(data.attribute(data.classIndex()).name(),vals)); //create blank instances with the correct class value Instances result = new Instances("SubsequenceIntervals",atts,data.numInstances()); result.setClassIndex(result.numAttributes()-1); for(int i=0;i<data.numInstances();i++){ double cval=data.instance(i).classValue(); DenseInstance in=new DenseInstance(result.numAttributes()); in.setValue(result.numAttributes()-1,cval); int pos=0; for(int j=0;j<numSubSeries;j++){ for(int k=0;k<numClasses-1;k++) in.setValue(pos++, probs[j+numSubSeries*i][k]); } result.add(in); } return result; }
Example 17
Source File: TweetToSparseFeatureVector.java From AffectiveTweets with GNU General Public License v3.0 | 4 votes |
/** * Processes a batch of tweets. * * @param tweetInstances the input tweets */ public void tweetsToVectors(Instances tweetInstances) { // The vocabulary is created only in the first execution if (!this.isFirstBatchDone()){ this.attributeCount = new Object2IntOpenHashMap<String>(); // the Dictionary of the brown Clusters if(this.clustNgramMaxDim>0){ this.brownDict=new Object2ObjectOpenHashMap<String,String>(); try { FileInputStream fin = new FileInputStream(wordClustFile); GZIPInputStream gzis = new GZIPInputStream(fin); InputStreamReader xover = new InputStreamReader(gzis); BufferedReader bf = new BufferedReader(xover); String line; while ((line = bf.readLine()) != null) { String pair[] = line.split("\t"); // the word in the clusters are stemmed brownDict.put(this.m_stemmer.stem(pair[1]), pair[0]); } bf.close(); xover.close(); gzis.close(); fin.close(); } catch (IOException e) { // do not create clusters attributes this.clustNgramMaxDim=0; } } // Initializes NegationEvaluator if(this.negateTokens) this.initiliazeNegationEvaluator(); } // Loads the POS tagger model if(this.posNgramMaxDim>0 && this.tagger==null){ this.initializeTagger(); } this.procTweets = new ObjectArrayList<Object2IntMap<String>>(); // reference to the content of the message, users index start from zero Attribute attrCont = tweetInstances.attribute(this.m_textIndex.getIndex()); for (ListIterator<Instance> it = tweetInstances.listIterator(); it .hasNext();) { Instance inst = it.next(); String content = inst.stringValue(attrCont); if(this.toLowerCase) content=content.toLowerCase(); Object2IntMap<String> docVec=calculateDocVec(content); // Add the frequencies of the different words this.procTweets.add(docVec); // The attribute space is calculated only the first time we run the filter. // This avoids adding new features for the test data if (!this.isFirstBatchDone()) { // if the attribute is new we add it to the attribute list, otherwise we // increment the count for(String docAtt:docVec.keySet()){ if(this.attributeCount.containsKey(docAtt)){ int prevFreq=this.attributeCount.getInt(docAtt); this.attributeCount.put(docAtt,prevFreq+1); } else{ this.attributeCount.put(docAtt,1); } } } } }
Example 18
Source File: AutoTestAdjust.java From bestconf with Apache License 2.0 | 4 votes |
public Instances runExp(Instances samplePoints, String perfAttName){ Instances retVal = null; if(samplePoints.attribute(perfAttName) == null){ Attribute performance = new Attribute(perfAttName); samplePoints.insertAttributeAt(performance, samplePoints.numAttributes()); } int pos = samplePoints.numInstances(); int count = 0; for (int i = 0; i < pos; i++) { Instance ins = samplePoints.get(i); HashMap hm = new HashMap(); int tot = 0; for (int j = 0; j < ins.numAttributes(); j++) { hm.put(ins.attribute(j).name(), ins.value(ins.attribute(j))); } boolean testRet; if (Double.isNaN(ins.value(ins.attribute(ins.numAttributes() - 1)))) { testRet = this.startTest(hm, i, isInterrupt); double y = 0; if (!testRet) {// the setting does not work, we skip it y = -1; count++; if (count >= targetTestErrorNum) { System.out.println("There must be somthing wrong with the system. Please check and restart....."); System.exit(1); } } else { y = getPerformanceByType(performanceType); count = 0; } ins.setValue(samplePoints.numAttributes() - 1, y); writePerfstoFile(ins); } else { continue; } } retVal = samplePoints; retVal.setClassIndex(retVal.numAttributes()-1); return retVal; }
Example 19
Source File: Id3.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Method for building an Id3 tree. * * @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 = Utils.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]); } } }
Example 20
Source File: AbstractShowThresholdCurve.java From meka with GNU General Public License v3.0 | 3 votes |
/** * Sets the combobox indices. * * @param data the threshold curve data * @param panel the panel * @throws Exception if setting of indices fails */ protected void setComboBoxIndices(Instances data, ThresholdVisualizePanel panel) throws Exception { if (data.attribute(getDefaultXColumn()) != null) panel.setXIndex(data.attribute(getDefaultXColumn()).index()); if (data.attribute(getDefaultYColumn()) != null) panel.setYIndex(data.attribute(getDefaultYColumn()).index()); }