List of usage examples for weka.core Instance toDoubleArray
public double[] toDoubleArray();
From source file:classification.classifiers.LDA.java
License:Open Source License
/** * Modification to make the LDA classifier be like a classifier from WEKA(R). * // ww w .j a v a2 s .c om * @param newInstance * @return * @throws Exception */ public double classifyInstance(Instance newInstance) throws Exception { double[] instance = new double[newInstance.numAttributes()]; instance = newInstance.toDoubleArray(); int numClass = predict(instance); // System.out.println(numClass); double predictedClass = valueClass[numClass]; // Double predictedClass = ; return predictedClass; }
From source file:cluster.ABC.ClusterUtils.java
License:Open Source License
/** Normalizes the values of a normal Instance in L2 norm * * @author Sugato Basu//from w ww. j av a2 s. co m * @param inst Instance to be normalized */ public static void normalizeInstance(Instance inst) throws Exception { double norm = 0; double values[] = inst.toDoubleArray(); if (inst instanceof SparseInstance) { System.err.println("Is SparseInstance, using normalizeSparseInstance function instead"); normalizeSparseInstance(inst); } for (int i = 0; i < values.length; i++) { if (i != inst.classIndex()) { // don't normalize the class index norm += values[i] * values[i]; } } norm = Math.sqrt(norm); for (int i = 0; i < values.length; i++) { if (i != inst.classIndex()) { // don't normalize the class index values[i] /= norm; } } for (int i = 0; i < inst.numAttributes(); i++) { inst.setValue(i, values[i]); } //inst.setValueArray(values); }
From source file:cn.ict.zyq.bestConf.bestConf.BestConf.java
License:Open Source License
public static void testCOMT2() throws Exception { BestConf bestconf = new BestConf(); Instances trainingSet = DataIOFile.loadDataFromArffFile("data/trainingBestConf0.arff"); trainingSet.setClassIndex(trainingSet.numAttributes() - 1); Instances samplePoints = LHSInitializer.getMultiDimContinuous(bestconf.getAttributes(), InitialSampleSetSize, false); samplePoints.insertAttributeAt(trainingSet.classAttribute(), samplePoints.numAttributes()); samplePoints.setClassIndex(samplePoints.numAttributes() - 1); COMT2 comt = new COMT2(samplePoints, COMT2Iteration); comt.buildClassifier(trainingSet);//from w w w . jav a2s. c o m Evaluation eval = new Evaluation(trainingSet); eval.evaluateModel(comt, trainingSet); System.err.println(eval.toSummaryString()); Instance best = comt.getInstanceWithPossibleMaxY(samplePoints.firstInstance()); Instances bestInstances = new Instances(trainingSet, 2); bestInstances.add(best); DataIOFile.saveDataToXrffFile("data/trainingBestConf_COMT2.arff", bestInstances); //now we output the training set with the class value updated as the predicted value Instances output = new Instances(trainingSet, trainingSet.numInstances()); Enumeration<Instance> enu = trainingSet.enumerateInstances(); while (enu.hasMoreElements()) { Instance ins = enu.nextElement(); double[] values = ins.toDoubleArray(); values[values.length - 1] = comt.classifyInstance(ins); output.add(ins.copy(values)); } DataIOFile.saveDataToXrffFile("data/trainingBestConf0_predict.xrff", output); }
From source file:com.yahoo.labs.samoa.instances.WekaToSamoaInstanceConverter.java
License:Apache License
/** * Samoa instance from weka instance.// w w w . j a v a 2 s . co m * * @param inst the inst * @return the instance */ public Instance samoaInstance(weka.core.Instance inst) { Instance samoaInstance; if (inst instanceof weka.core.SparseInstance) { double[] attributeValues = new double[inst.numValues()]; int[] indexValues = new int[inst.numValues()]; for (int i = 0; i < inst.numValues(); i++) { if (inst.index(i) != inst.classIndex()) { attributeValues[i] = inst.valueSparse(i); indexValues[i] = inst.index(i); } } samoaInstance = new SparseInstance(inst.weight(), attributeValues, indexValues, inst.numAttributes()); } else { samoaInstance = new DenseInstance(inst.weight(), inst.toDoubleArray()); //samoaInstance.deleteAttributeAt(inst.classIndex()); } if (this.samoaInstanceInformation == null) { this.samoaInstanceInformation = this.samoaInstancesInformation(inst.dataset()); } samoaInstance.setDataset(samoaInstanceInformation); samoaInstance.setClassValue(inst.classValue()); return samoaInstance; }
From source file:core.ClusterEvaluationEX.java
License:Open Source License
/** * num??/*from ww w .ja v a2 s.c o m*/ * Returns the Centroids * */ public Instances getCentroids(int num) { FastVector atts = new FastVector(); Attribute clusterID = new Attribute("clusterID"); atts.addElement(clusterID); Instances data = new Instances("centroids", atts, m_numClusters); for (int i = 0; i < ID.numAttributes() - 1; i++) { Attribute att = new Attribute("Subject" + String.valueOf(i)); atts.addElement(att); } double[] map = new double[m_numClusters]; double[] temp = new double[m_clusterAssignments.length]; System.arraycopy(m_clusterAssignments, 0, temp, 0, m_clusterAssignments.length); int n = map.length; for (int i = 0; i < m_clusterAssignments.length; i++) { double id = temp[i]; if (id == -1) continue; boolean flag = true; for (int j = 0; j < temp.length; j++) { if (temp[j] == id) { temp[j] = -1; } } if (flag && n != -1) { map[map.length - n] = id + num; n--; } else if (n != -1) { continue; } else { break; } } for (int i = 0; i < map.length; i++) { double id = map[i]; double[] averatts = new double[ID.numAttributes()]; int count = 0; for (int j = 0; j < ID.numInstances(); j++) { Instance iter = ID.instance(j); if (iter.value(0) == id) { averatts = CommonMethords.add(averatts, iter.toDoubleArray()); count++; } } averatts = CommonMethords.calAver(averatts, count); Instance ins = new Instance(1, averatts); data.add(ins); } return data; }
From source file:data.Bag.java
License:Open Source License
/** * Constructor./*from w ww .j a va 2 s. c om*/ * * @param instance * A Weka's Instance to be transformed into a Bag. * @throws Exception * To be handled in an upper level. * */ public Bag(Instance instance) throws Exception { super(instance); m_AttValues = instance.toDoubleArray(); m_Weight = instance.weight(); m_Dataset = instance.dataset(); }
From source file:de.upb.timok.utils.DatasetTransformationUtils.java
License:Open Source License
public static List<double[]> instancesToDoubles(Instances instances, boolean chopClassAttribute) { final List<double[]> result = new ArrayList<>(); for (int i = 0; i < instances.size(); i++) { final Instance instance = instances.get(i); double[] temp = instance.toDoubleArray(); if (chopClassAttribute) { temp = Arrays.copyOfRange(temp, 0, temp.length - 1); }//from w w w . j av a 2s . co m result.add(temp); } return result; }
From source file:DiversifyQuery.DivTopK.java
/** * * @param candidate/*from w ww . jav a2s.c o m*/ * @param timeSeriesIns * @return */ public static double subsequenceDistance(double[] candidate, Instance timeSeriesIns) { double[] timeSeries = timeSeriesIns.toDoubleArray(); return subsequenceDistance(candidate, timeSeries); }
From source file:edu.oregonstate.eecs.mcplan.abstraction.PairDataset.java
License:Open Source License
public static <S, X extends FactoredRepresentation<S>, A extends VirtualConstructor<A>> ArrayList<PairInstance> makePairDataset( final RandomGenerator rng, final int max_pairwise_instances, final Instances single) { final ReservoirSampleAccumulator<PairInstance> negative = new ReservoirSampleAccumulator<PairInstance>(rng, max_pairwise_instances);/* w w w.j a v a 2 s. c o m*/ final ReservoirSampleAccumulator<PairInstance> positive = new ReservoirSampleAccumulator<PairInstance>(rng, max_pairwise_instances); for (int i = 0; i < single.size(); ++i) { for (int j = i + 1; j < single.size(); ++j) { final Instance ii = single.get(i); final Instance ij = single.get(j); final int label; if (ii.classValue() == ij.classValue()) { label = 1; if (positive.acceptNext()) { final PairInstance pair_instance = new PairInstance(ii.toDoubleArray(), ij.toDoubleArray(), label); positive.addPending(pair_instance); } } else { label = 0; if (negative.acceptNext()) { final PairInstance pair_instance = new PairInstance(ii.toDoubleArray(), ij.toDoubleArray(), label); negative.addPending(pair_instance); } } } } final ArrayList<PairInstance> result = new ArrayList<PairInstance>(negative.n() + positive.n()); result.addAll(negative.samples()); result.addAll(positive.samples()); return result; }
From source file:edu.oregonstate.eecs.mcplan.ml.Memorizer.java
License:Open Source License
@Override public void buildClassifier(final Instances data) throws Exception { Nclasses_ = data.numClasses();// w w w . j a va2s . com class_idx_ = data.classIndex(); for (final Instance i : data) { final double[] x = i.toDoubleArray(); final int c = (int) x[class_idx_]; x[class_idx_] = 0; m_.put(new ArrayHolder(x), c); } }