Refine search
/** * Stack two Instances together row-wise. */ public static final Instances combineInstances(Instances D1, Instances D2) { Instances D = new Instances(D1); for(int i = 0; i < D2.numInstances(); i++) { D.add(D2.instance(i)); } return D; }
System.out.println("--------------------------"); double[] instanceValue1 = new double[dataRaw.numAttributes()]; instanceValue1[0] = dataRaw.attribute(0).addStringValue("This is a string!"); instanceValue1[1] = 0; dataRaw.add(new DenseInstance(1.0, instanceValue1)); System.out.println("--------------------------"); double[] instanceValue2 = new double[dataRaw.numAttributes()]; instanceValue2[0] = dataRaw.attribute(0).addStringValue("This is second string!"); instanceValue2[1] = 1; dataRaw.add(new DenseInstance(1.0, instanceValue2));
Instances dataUnlabeled = new Instances("TestInstances", atts, 0); dataUnlabeled.add(newInst); dataUnlabeled.setClassIndex(dataUnlabeled.numAttributes() - 1); double classif = ibk.classifyInstance(dataUnlabeled.firstInstance());
//load model String rootPath="/some/where/"; Classifier cls = (Classifier) weka.core.SerializationHelper.read(rootPath+"tree.model"); //predict instance class values Instances originalTrain= //load or create Instances to predict //which instance to predict class value int s1=0; //perform your prediction double value=cls.classifyInstance(originalTrain.instance(s1)); //get the name of the class value String prediction=originalTrain.classAttribute().value((int)value); System.out.println("The predicted value of instance "+ Integer.toString(s1)+ ": "+prediction);
DataSource source = new DataSource(new File("mycsvinputfile")); System.out.println(source.getStructure()); Instances data = source.getDataSet(); // setting class attribute if the data format does not provide this information // For example, the XRFF format saves the class attribute information as well if (data.classIndex() == -1) data.setClassIndex(data.numAttributes() - 1); //initialize svm classifier LibSVM svm = new LibSVM(); svm.buildClassifier(data);
/** * Returns a random number generator. The initial seed of the random number * generator depends on the given seed and the hash code of a string * representation of a instances chosen based on the given seed. * * @param seed the given seed * @return the random number generator */ public Random getRandomNumberGenerator(long seed) { Random r = new Random(seed); r.setSeed(instance(r.nextInt(numInstances())).toStringNoWeight().hashCode() + seed); return r; }
/** * bag class for getting the result of the loaded classifier */ private static class LoadedClassifier { private AbstractClassifier newClassifier = null; private Instances newHeader = null; }
/** * Add missing values to a dataset. * * @param data the instances to add missing values to * @param level the level of missing values to add (if positive, this is the * probability that a value will be set to missing, if negative all * but one value will be set to missing (not yet implemented)) * @param predictorMissing if true, predictor attributes will be modified */ protected void addMissing(Instances data, int level, boolean predictorMissing) { Random random = new Random(1); for (int i = 0; i < data.numInstances(); i++) { Instance current = data.instance(i); for (int j = 0; j < data.numAttributes(); j++) { if (predictorMissing) { if (random.nextInt(100) < level) { current.setMissing(j); } } } } }
@Override public void buildClassifier(Instances Train) throws Exception { testCapabilities(Train); this.m_NumClasses = Train.classIndex(); int indices[] = MLUtils.gen_indices(m_NumClasses); MLUtils.randomize(indices,new Random(m_S)); if(getDebug()) System.out.print(":- Chain ("); root = new Tink(indices,0,Train); if (getDebug()) System.out.println(" ) -:"); }
/** * Shuffles the instances in the set so that they are ordered randomly. * * @param random a random number generator */ public void randomize(Random random) { for (int j = numInstances() - 1; j > 0; j--) { swap(j, random.nextInt(j + 1)); } }
/** * Adds random instances to the dataset. * * @param dataset the dataset * @param numInstances the number of instances * @param random a random number generator */ protected void addRandomInstances( Instances dataset, int numInstances, Random random ) { int n = dataset.numAttributes(); double [] v = new double[ n ]; for( int i = 0; i < numInstances; i++ ) { for( int j = 0; j < n; j++ ) { Attribute att = dataset.attribute( j ); if( att.isNumeric() ) { v[ j ] = random.nextDouble(); } else if ( att.isNominal() ) { v[ j ] = random.nextInt( att.numValues() ); } } dataset.add( new DenseInstance( 1, v ) ); } }
/** * generates a class association rule out of a given premise. It randomly * chooses a class label as consequence. * * @param itemArray the (randomly constructed) premise of the class * association rule * @return a class association rule stored in a RuleItem */ public final RuleItem addCons(int[] itemArray) { ItemSet premise = new ItemSet(itemArray); int[] cons = new int[itemArray.length]; for (int i = 0; i < itemArray.length; i++) { cons[i] = -1; } cons[m_instances.classIndex()] = m_randNum.nextInt((m_instances .attribute(m_instances.classIndex())).numValues()); ItemSet consequence = new ItemSet(cons); RuleItem current = new RuleItem(); current.m_premise = premise; current.m_consequence = consequence; return current; }
/** * Initializes the format for the dataset produced. Must be called before the * generateExample or generateExamples methods are used. Re-initializes the * random number generator with the given seed. * * @return the format for the dataset * @throws Exception if the generating of the format failed * @see #getSeed() */ @Override public Instances defineDataFormat() throws Exception { ArrayList<Attribute> atts; m_Random = new Random(getSeed()); m_NoiseRandom = new Random(getSeed()); // number of examples is the same as given per option setNumExamplesAct(getNumExamples()); // initialize dataset format atts = new ArrayList<Attribute>(); atts.add(new Attribute("x")); atts.add(new Attribute("y")); m_DatasetFormat = new Instances(getRelationNameToUse(), atts, 0); return m_DatasetFormat; }
newInst = new DenseInstance(model.numAttributes()); newInst.setDataset(model); for (int i = 0, j = 0; i < model.numAttributes(); i++) { if (model.attribute(i).isNumeric()) { if (j >= m_Elements.length) throw new Exception("Datatypes are not compatible."); newInst.setValue(i, m_Elements[j++]); if (model.attribute(i).isNominal()) { int newVal = (int) (random.nextDouble() * (double) (model.attribute(i).numValues())); if (newVal == (int) model.attribute(i).numValues()) newVal -= 1; newInst.setValue(i, newVal);
double value = random.nextDouble(); if (format.attribute(i).isNumeric()) { attributes[i] = value; } else { if (format.attribute(i).isNominal()) { attributes[i] = (value > 0.5) ? 1.0 : 0.0; } else { example = new DenseInstance(1.0, attributes); example.setDataset(format); example.setClassMissing();
/** * Generate an example of the dataset. * * @param format the dataset format * @param randomG the random number generator * @param stdDev the standard deviation to use * @param center the centers * @param cName the class value * @return the instance generated examples one by one is not possible, because * voting is chosen */ private Instance generateInstance(Instances format, Random randomG, double stdDev, double[] center, String cName) { Instance example; int numAtts = getNumAttributes(); if (getClassFlag()) { numAtts++; } double[] data = new double[numAtts]; for (int i = 0; i < getNumAttributes(); i++) { data[i] = randomG.nextGaussian() * stdDev + center[i]; } if (getClassFlag()) { data[format.classIndex()] = format.classAttribute().indexOfValue(cName); } example = new DenseInstance(1.0, data); example.setDataset(format); return example; }
/** * Updates the perturbed values for the plots when the jitter value is changed */ private void updatePturb() { double xj = 0; double yj = 0; for (int j = 0; j < m_plots.size(); j++) { PlotData2D temp_plot = (m_plots.get(j)); for (int i = 0; i < temp_plot.m_plotInstances.numInstances(); i++) { if (temp_plot.m_plotInstances.instance(i).isMissing(m_xIndex) || temp_plot.m_plotInstances.instance(i).isMissing(m_yIndex)) { } else { if (m_JitterVal > 0) { xj = m_JRand.nextGaussian(); yj = m_JRand.nextGaussian(); } temp_plot.m_pointLookup[i][2] = pturbX(temp_plot.m_pointLookup[i][0], xj); temp_plot.m_pointLookup[i][3] = pturbY(temp_plot.m_pointLookup[i][1], yj); } } } }
private Instance makeOutputInstance(Instances output, Instance source) { double[] newVals = new double[output.numAttributes()]; for (int i = 0; i < newVals.length; i++) { newVals[i] = Utils.missingValue(); } for (int i = 0; i < source.numAttributes(); i++) { if (!source.isMissing(i)) { Attribute s = source.attribute(i); int outputIndex = output.attribute(s.name()).index(); if (s.isNumeric()) { newVals[outputIndex] = source.value(s); } else if (s.isString()) { String sVal = source.stringValue(s); newVals[outputIndex] = output.attribute(outputIndex).addStringValue( sVal); } else if (s.isRelationValued()) { Instances rVal = source.relationalValue(s); newVals[outputIndex] = output.attribute(outputIndex) .addRelation(rVal); } else if (s.isNominal()) { String nomVal = source.stringValue(s); newVals[outputIndex] = output.attribute(outputIndex).indexOfValue( nomVal); } } } Instance newInst = new DenseInstance(source.weight(), newVals); newInst.setDataset(output); return newInst; }
/** * Transforms the instance in the prediction process before given to the internal multi-label * or multi-target classifier. The instance is passed having the original set of labels, these * must be replaced with the transformed labels (attributes) so that the internla classifier * can predict them. * * @param x The instance to transform. Consists of features and labels. * @return The transformed instance. Consists of features and transformed labels. */ @Override public Instance transformInstance(Instance x) throws Exception{ Instances tmpInst = new Instances(x.dataset()); tmpInst.delete(); tmpInst.add(x); Instances features = this.extractPart(tmpInst, false); Instances labels = new Instances(this.m_PatternInstances); labels.add(new DenseInstance(labels.numAttributes())); Instances result = Instances.mergeInstances(labels, features); result.setClassIndex(labels.numAttributes()); return result.instance(0); }
@Override public void buildClassifier(Instances D) throws Exception { testCapabilities(D); int L = D.classIndex(); if(getDebug()) System.out.print("Creating "+L+" models ("+m_Classifier.getClass().getName()+"): "); m_MultiClassifiers = AbstractClassifier.makeCopies(m_Classifier,L); m_Templates = new Instances[L]; for(int j = 0; j < L; j++) { //Select only class attribute 'j' m_Templates[j] = MLUtils.keepAttributesAt(new Instances(D),new int[]{j},L); m_Templates[j].setClassIndex(0); //Build the classifier for that class m_MultiClassifiers[j].buildClassifier(m_Templates[j]); if(getDebug()) System.out.print(" " + (m_Templates[j].classAttribute().name())); m_Templates[j] = new Instances(m_Templates[j], 0); } }