Java Code Examples for weka.core.FastVector#addElement()
The following examples show how to use
weka.core.FastVector#addElement() .
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: RuleGeneration.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * generates a consequence of length 1 for an association rule. * @param instances the instances under consideration * @param attNum an item that does not occur in the premise * @param consequences FastVector that possibly already contains other consequences of length 1 * @return FastVector with consequences of length 1 */ public static FastVector singleConsequence(Instances instances, int attNum, FastVector consequences){ ItemSet consequence; for (int i = 0; i < instances.numAttributes(); i++) { if( i == attNum){ for (int j = 0; j < instances.attribute(i).numValues(); j++) { consequence = new ItemSet(instances.numInstances()); consequence.m_items = new int[instances.numAttributes()]; for (int k = 0; k < instances.numAttributes(); k++) consequence.m_items[k] = -1; consequence.m_items[i] = j; consequences.addElement(consequence); } } } return consequences; }
Example 2
Source File: AprioriItemSet.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * Converts the header info of the given set of instances into a set of item * sets (singletons). The ordering of values in the header file determines the * lexicographic order. * * @param instances the set of instances whose header info is to be used * @return a set of item sets, each containing a single item * @exception Exception if singletons can't be generated successfully */ public static FastVector singletons(Instances instances, boolean treatZeroAsMissing) throws Exception { FastVector setOfItemSets = new FastVector(); ItemSet current; for (int i = 0; i < instances.numAttributes(); i++) { if (instances.attribute(i).isNumeric()) throw new Exception("Can't handle numeric attributes!"); int j = (treatZeroAsMissing) ? 1 : 0; for (; j < instances.attribute(i).numValues(); j++) { current = new AprioriItemSet(instances.numInstances()); current.m_items = new int[instances.numAttributes()]; for (int k = 0; k < instances.numAttributes(); k++) current.m_items[k] = -1; current.m_items[i] = j; setOfItemSets.addElement(current); } } return setOfItemSets; }
Example 3
Source File: ItemSet.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * Converts the header info of the given set of instances into a set of item * sets (singletons). The ordering of values in the header file determines the * lexicographic order. * * @param instances the set of instances whose header info is to be used * @return a set of item sets, each containing a single item * @exception Exception if singletons can't be generated successfully */ public static FastVector singletons(Instances instances) throws Exception { FastVector setOfItemSets = new FastVector(); ItemSet current; for (int i = 0; i < instances.numAttributes(); i++) { if (instances.attribute(i).isNumeric()) throw new Exception("Can't handle numeric attributes!"); for (int j = 0; j < instances.attribute(i).numValues(); j++) { current = new ItemSet(instances.numInstances()); current.m_items = new int[instances.numAttributes()]; for (int k = 0; k < instances.numAttributes(); k++) current.m_items[k] = -1; current.m_items[i] = j; setOfItemSets.addElement(current); } } return setOfItemSets; }
Example 4
Source File: CaRuleGeneration.java From tsml with GNU General Public License v3.0 | 6 votes |
/** * generates a consequence of length 1 for a class association rule. * @param instances the instances under consideration * @return FastVector with consequences of length 1 */ public static FastVector singleConsequence(Instances instances){ ItemSet consequence; FastVector consequences = new FastVector(); for (int j = 0; j < (instances.classAttribute()).numValues(); j++) { consequence = new ItemSet(instances.numInstances()); int[] consequenceItems = new int[instances.numAttributes()]; consequence.setItem(consequenceItems); for (int k = 0; k < instances.numAttributes(); k++) consequence.setItemAt(-1,k); consequence.setItemAt(j,instances.classIndex()); consequences.addElement(consequence); } return consequences; }
Example 5
Source File: WekaTee.java From uncc2014watsonsim with GNU General Public License v2.0 | 6 votes |
/** * When the score changes, rewrite the file. * This is really rare in practice, so don't bother optimizing it. */ private static void dump_from_scratch(Collection<String> names, Timestamp start_time) throws IOException { saved_schema_version = names.size(); FastVector attributes = new FastVector(); // Answer score names for (String name: names) attributes.addElement(new Attribute(name)); Instances data = new Instances("Watsonsim captured question stream", attributes, 0); // Save the results to a file saver = new ArffSaver(); saver.setStructure(data); saver.setRetrieval(Saver.INCREMENTAL); saver.setFile(new File("data/weka-log." + start_time + ".arff")); for (Score row : dataset) saver.writeIncremental(new Instance(1.0, row.getEach(names))); }
Example 6
Source File: PLSFilter.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Determines the output format based on the input format and returns * this. In case the output format cannot be returned immediately, i.e., * immediateOutputFormat() returns false, then this method will be called * from batchFinished(). * * @param inputFormat the input format to base the output format on * @return the output format * @throws Exception in case the determination goes wrong * @see #hasImmediateOutputFormat() * @see #batchFinished() */ protected Instances determineOutputFormat(Instances inputFormat) throws Exception { // generate header FastVector atts = new FastVector(); String prefix = getAlgorithm().getSelectedTag().getReadable(); for (int i = 0; i < getNumComponents(); i++) atts.addElement(new Attribute(prefix + "_" + (i+1))); atts.addElement(new Attribute("Class")); Instances result = new Instances(prefix, atts, 0); result.setClassIndex(result.numAttributes() - 1); return result; }
Example 7
Source File: BayesNetGenerator.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Generate random connected Bayesian network with discrete nodes * having all the same cardinality. * * @throws Exception if something goes wrong */ public void generateRandomNetwork () throws Exception { if (m_otherBayesNet == null) { // generate from scratch Init(m_nNrOfNodes, m_nCardinality); generateRandomNetworkStructure(m_nNrOfNodes, m_nNrOfArcs); generateRandomDistributions(m_nNrOfNodes, m_nCardinality); } else { // read from file, just copy parent sets and distributions m_nNrOfNodes = m_otherBayesNet.getNrOfNodes(); m_ParentSets = m_otherBayesNet.getParentSets(); m_Distributions = m_otherBayesNet.getDistributions(); random = new Random(m_nSeed); // initialize m_Instances FastVector attInfo = new FastVector(m_nNrOfNodes); // generate value strings for (int iNode = 0; iNode < m_nNrOfNodes; iNode++) { int nValues = m_otherBayesNet.getCardinality(iNode); FastVector nomStrings = new FastVector(nValues + 1); for (int iValue = 0; iValue < nValues; iValue++) { nomStrings.addElement(m_otherBayesNet.getNodeValue(iNode, iValue)); } Attribute att = new Attribute(m_otherBayesNet.getNodeName(iNode), nomStrings); attInfo.addElement(att); } m_Instances = new Instances(m_otherBayesNet.getName(), attInfo, 100); m_Instances.setClassIndex(m_nNrOfNodes - 1); } }
Example 8
Source File: EditableBayesNet.java From tsml with GNU General Public License v3.0 | 5 votes |
AddArcAction(int nParent, FastVector children) { try { m_nParent = nParent; m_children = new FastVector(); m_CPT = new Estimator[children.size()][]; for (int iChild = 0; iChild < children.size(); iChild++) { int nChild = (Integer) children.elementAt(iChild); m_children.addElement(nChild); SerializedObject so = new SerializedObject(m_Distributions[nChild]); m_CPT[iChild] = (Estimator[]) so.getObject(); } } catch (Exception e) { e.printStackTrace(); } }
Example 9
Source File: EditableBayesNet.java From tsml with GNU General Public License v3.0 | 5 votes |
/** XML helper function for selecting elements under a node with a given name * @param item XMLNode to select items from * @param sElement name of the element to return */ FastVector selectElements(Node item, String sElement) throws Exception { NodeList children = item.getChildNodes(); FastVector nodelist = new FastVector(); for (int iNode = 0; iNode < children.getLength(); iNode++) { Node node = children.item(iNode); if ((node.getNodeType() == Node.ELEMENT_NODE) && node.getNodeName().equals(sElement)) { nodelist.addElement(node); } } return nodelist; }
Example 10
Source File: CostCurve.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * generates the header * * @return the header */ private Instances makeHeader() { FastVector fv = new FastVector(); fv.addElement(new Attribute(PROB_COST_FUNC_NAME)); fv.addElement(new Attribute(NORM_EXPECTED_COST_NAME)); fv.addElement(new Attribute(THRESHOLD_NAME)); return new Instances(RELATION_NAME, fv, 100); }
Example 11
Source File: RDG1.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Generates a new rule for the decision list * and classifies the new example. * * @param random random number generator * @param example the instance to classify * @return a list of tests * @throws Exception if dataset format not defined */ private FastVector generateTestList(Random random, Instance example) throws Exception { Instances format = getDatasetFormat(); if (format == null) throw new Exception("Dataset format not defined."); int numTests = getNumAttributes() - getNumIrrelevant(); FastVector TestList = new FastVector(numTests); boolean[] irrelevant = getAttList_Irr(); for (int i = 0; i < getNumAttributes(); i++) { if (!irrelevant[i]) { Test newTest = null; Attribute att = example.attribute(i); if (att.isNumeric()) { double newSplit = random.nextDouble(); boolean newNot = newSplit < example.value(i); newTest = new Test(i, newSplit, format, newNot); } else { newTest = new Test(i, example.value(i), format, false); } TestList.addElement (newTest); } } return TestList; }
Example 12
Source File: CheckClassifier.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Checks whether the scheme can handle zero training instances. * * @param nominalPredictor if true use nominal predictor attributes * @param numericPredictor if true use numeric predictor attributes * @param stringPredictor if true use string predictor attributes * @param datePredictor if true use date predictor attributes * @param relationalPredictor if true use relational predictor attributes * @param multiInstance whether multi-instance is needed * @param classType the class type (NUMERIC, NOMINAL, etc.) * @return index 0 is true if the test was passed, index 1 is true if test * was acceptable */ protected boolean[] canHandleZeroTraining( boolean nominalPredictor, boolean numericPredictor, boolean stringPredictor, boolean datePredictor, boolean relationalPredictor, boolean multiInstance, int classType) { print("handle zero training instances"); printAttributeSummary( nominalPredictor, numericPredictor, stringPredictor, datePredictor, relationalPredictor, multiInstance, classType); print("..."); FastVector accepts = new FastVector(); accepts.addElement("train"); accepts.addElement("value"); int numTrain = 0, numTest = getNumInstances(), numClasses = 2, missingLevel = 0; boolean predictorMissing = false, classMissing = false; return runBasicTest( nominalPredictor, numericPredictor, stringPredictor, datePredictor, relationalPredictor, multiInstance, classType, missingLevel, predictorMissing, classMissing, numTrain, numTest, numClasses, accepts); }
Example 13
Source File: CheckClusterer.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Checks basic prediction of the scheme, for simple non-troublesome * datasets. * * @param nominalPredictor if true use nominal predictor attributes * @param numericPredictor if true use numeric predictor attributes * @param stringPredictor if true use string predictor attributes * @param datePredictor if true use date predictor attributes * @param relationalPredictor if true use relational predictor attributes * @param multiInstance whether multi-instance is needed * @return index 0 is true if the test was passed, index 1 is true if test * was acceptable */ protected boolean[] canPredict( boolean nominalPredictor, boolean numericPredictor, boolean stringPredictor, boolean datePredictor, boolean relationalPredictor, boolean multiInstance) { print("basic predict"); printAttributeSummary( nominalPredictor, numericPredictor, stringPredictor, datePredictor, relationalPredictor, multiInstance); print("..."); FastVector accepts = new FastVector(); accepts.addElement("unary"); accepts.addElement("binary"); accepts.addElement("nominal"); accepts.addElement("numeric"); accepts.addElement("string"); accepts.addElement("date"); accepts.addElement("relational"); accepts.addElement("multi-instance"); accepts.addElement("not in classpath"); int numTrain = getNumInstances(), missingLevel = 0; boolean predictorMissing = false; return runBasicTest(nominalPredictor, numericPredictor, stringPredictor, datePredictor, relationalPredictor, multiInstance, missingLevel, predictorMissing, numTrain, accepts); }
Example 14
Source File: CheckClassifier.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Checks whether nominal schemes can handle more than two classes. * If a scheme is only designed for two-class problems it should * throw an appropriate exception for multi-class problems. * * @param nominalPredictor if true use nominal predictor attributes * @param numericPredictor if true use numeric predictor attributes * @param stringPredictor if true use string predictor attributes * @param datePredictor if true use date predictor attributes * @param relationalPredictor if true use relational predictor attributes * @param multiInstance whether multi-instance is needed * @param numClasses the number of classes to test * @return index 0 is true if the test was passed, index 1 is true if test * was acceptable */ protected boolean[] canHandleNClasses( boolean nominalPredictor, boolean numericPredictor, boolean stringPredictor, boolean datePredictor, boolean relationalPredictor, boolean multiInstance, int numClasses) { print("more than two class problems"); printAttributeSummary( nominalPredictor, numericPredictor, stringPredictor, datePredictor, relationalPredictor, multiInstance, Attribute.NOMINAL); print("..."); FastVector accepts = new FastVector(); accepts.addElement("number"); accepts.addElement("class"); int numTrain = getNumInstances(), numTest = getNumInstances(), missingLevel = 0; boolean predictorMissing = false, classMissing = false; return runBasicTest(nominalPredictor, numericPredictor, stringPredictor, datePredictor, relationalPredictor, multiInstance, Attribute.NOMINAL, missingLevel, predictorMissing, classMissing, numTrain, numTest, numClasses, accepts); }
Example 15
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 16
Source File: CheckEstimator.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Checks basic missing value handling of the scheme. If the missing * values cause an exception to be thrown by the scheme, this will be * recorded. * * @param attrTypes attribute types that can be estimated * @param classType the class type (NUMERIC, NOMINAL, etc.) * @param attributeMissing true if the missing values may be in * the attributes * @param classMissing true if the missing values may be in the class * @param missingLevel the percentage of missing values * @return index 0 is true if the test was passed, index 1 is true if test * was acceptable */ protected boolean[] canHandleMissing(AttrTypes attrTypes, int classType, boolean attributeMissing, boolean classMissing, int missingLevel) { if (missingLevel == 100) print("100% "); print("missing"); if (attributeMissing) { print(" attribute"); if (classMissing) print(" and"); } if (classMissing) print(" class"); print(" values"); printAttributeSummary(attrTypes, classType); print("..."); FastVector accepts = new FastVector(); accepts.addElement("missing"); accepts.addElement("value"); accepts.addElement("train"); int numTrain = getNumInstances(), numTest = getNumInstances(), numClasses = 2; int numAtts = 1, attrIndex = 0; return runBasicTest(attrTypes, numAtts, attrIndex, classType, missingLevel, attributeMissing, classMissing, numTrain, numTest, numClasses, accepts); }
Example 17
Source File: EvaluationUtils.java From tsml with GNU General Public License v3.0 | 5 votes |
/** * Generate a bunch of predictions ready for processing, by performing a * evaluation on a test set assuming the classifier is already trained. * * @param classifier the pre-trained Classifier to evaluate * @param test the test dataset * @exception Exception if an error occurs */ public FastVector getTestPredictions(Classifier classifier, Instances test) throws Exception { FastVector predictions = new FastVector(); for (int i = 0; i < test.numInstances(); i++) { if (!test.instance(i).classIsMissing()) { predictions.addElement(getPrediction(classifier, test.instance(i))); } } return predictions; }
Example 18
Source File: CheckKernel.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Checks basic missing value handling of the scheme. If the missing * values cause an exception to be thrown by the scheme, this will be * recorded. * * @param nominalPredictor if true use nominal predictor attributes * @param numericPredictor if true use numeric predictor attributes * @param stringPredictor if true use string predictor attributes * @param datePredictor if true use date predictor attributes * @param relationalPredictor if true use relational predictor attributes * @param multiInstance whether multi-instance is needed * @param classType the class type (NUMERIC, NOMINAL, etc.) * @param predictorMissing true if the missing values may be in * the predictors * @param classMissing true if the missing values may be in the class * @param missingLevel the percentage of missing values * @return index 0 is true if the test was passed, index 1 is true if test * was acceptable */ protected boolean[] canHandleMissing( boolean nominalPredictor, boolean numericPredictor, boolean stringPredictor, boolean datePredictor, boolean relationalPredictor, boolean multiInstance, int classType, boolean predictorMissing, boolean classMissing, int missingLevel) { if (missingLevel == 100) print("100% "); print("missing"); if (predictorMissing) { print(" predictor"); if (classMissing) print(" and"); } if (classMissing) print(" class"); print(" values"); printAttributeSummary( nominalPredictor, numericPredictor, stringPredictor, datePredictor, relationalPredictor, multiInstance, classType); print("..."); FastVector accepts = new FastVector(); accepts.addElement("missing"); accepts.addElement("value"); accepts.addElement("train"); int numTrain = getNumInstances(), numClasses = 2; return runBasicTest(nominalPredictor, numericPredictor, stringPredictor, datePredictor, relationalPredictor, multiInstance, classType, missingLevel, predictorMissing, classMissing, numTrain, numClasses, accepts); }
Example 19
Source File: PrincipalComponents.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Set the format for the transformed data * @return a set of empty Instances (header only) in the new format * @throws Exception if the output format can't be set */ private Instances setOutputFormat() throws Exception { if (m_eigenvalues == null) { return null; } double cumulative = 0.0; FastVector attributes = new FastVector(); for (int i = m_numAttribs - 1; i >= 0; i--) { StringBuffer attName = new StringBuffer(); // build array of coefficients double[] coeff_mags = new double[m_numAttribs]; for (int j = 0; j < m_numAttribs; j++) coeff_mags[j] = -Math.abs(m_eigenvectors[j][m_sortedEigens[i]]); int num_attrs = (m_maxAttrsInName > 0) ? Math.min(m_numAttribs, m_maxAttrsInName) : m_numAttribs; // this array contains the sorted indices of the coefficients int[] coeff_inds; if (m_numAttribs > 0) { // if m_maxAttrsInName > 0, sort coefficients by decreasing magnitude coeff_inds = Utils.sort(coeff_mags); } else { // if m_maxAttrsInName <= 0, use all coeffs in original order coeff_inds = new int[m_numAttribs]; for (int j=0; j<m_numAttribs; j++) coeff_inds[j] = j; } // build final attName string for (int j = 0; j < num_attrs; j++) { double coeff_value = m_eigenvectors[coeff_inds[j]][m_sortedEigens[i]]; if (j > 0 && coeff_value >= 0) attName.append("+"); attName.append(Utils.doubleToString(coeff_value,5,3) +m_trainInstances.attribute(coeff_inds[j]).name()); } if (num_attrs < m_numAttribs) attName.append("..."); attributes.addElement(new Attribute(attName.toString())); cumulative+=m_eigenvalues[m_sortedEigens[i]]; if ((cumulative / m_sumOfEigenValues) >= m_coverVariance) { break; } } if (m_hasClass) { attributes.addElement(m_trainHeader.classAttribute().copy()); } Instances outputFormat = new Instances(m_trainInstances.relationName()+"_principal components", attributes, 0); // set the class to be the last attribute if necessary if (m_hasClass) { outputFormat.setClassIndex(outputFormat.numAttributes()-1); } m_outputNumAtts = outputFormat.numAttributes(); return outputFormat; }
Example 20
Source File: MergeManyValues.java From tsml with GNU General Public License v3.0 | 4 votes |
/** * Input an instance for filtering. The instance is processed * and made available for output immediately. * * @param instance the input instance * @return true if the filtered instance may now be * collected with output(). * @throws IllegalStateException if no input format has been set. */ public boolean input(Instance instance) { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (m_NewBatch) { resetQueue(); m_NewBatch = false; } Attribute att = getInputFormat().attribute(m_AttIndex.getIndex()); FastVector newVals = new FastVector(att.numValues() - 1); for (int i = 0; i < att.numValues(); i++) { boolean inMergeList = false; if(att.value(i).equalsIgnoreCase(m_Label)){ //don't want to add this one. inMergeList = true; }else{ inMergeList = m_MergeRange.isInRange(i); } if(!inMergeList){ //add it. newVals.addElement(att.value(i)); } } newVals.addElement(m_Label); Attribute temp = new Attribute(att.name(), newVals); Instance newInstance = (Instance)instance.copy(); if (!newInstance.isMissing(m_AttIndex.getIndex())) { String currValue = newInstance.stringValue(m_AttIndex.getIndex()); if(temp.indexOfValue(currValue) == -1) newInstance.setValue(m_AttIndex.getIndex(), temp.indexOfValue(m_Label)); else newInstance.setValue(m_AttIndex.getIndex(), temp.indexOfValue(currValue)); } push(newInstance); return true; }