List of usage examples for weka.core Instances add
@Override public boolean add(Instance instance)
From source file:facebookpostpuller.SpecificUserModel.java
public void convertToArff(File file) throws Exception { FastVector atts;//from w w w . ja va 2 s. c o m FastVector attVals; Instances data; double[] vals; file = new File(file + ".arff"); atts = new FastVector(); atts.addElement(new Attribute(("name"), (FastVector) null)); // 5/27/2014 atts.addElement(new Attribute(("message"), (FastVector) null)); attVals = new FastVector(); attVals.addElement("13-17"); attVals.addElement("18-24"); attVals.addElement("25-34"); attVals.addElement("35-44"); attVals.addElement("45-54"); atts.addElement(new Attribute("age-group", attVals)); data = new Instances("predict_age", atts, 0); Iterator it = posts.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry) it.next(); vals = new double[data.numAttributes()]; User user = (User) pairs.getValue(); String name = user.getName(); // 5/27/2014 String message = ((Post) (pairs.getKey())).getMessage(); Preprocess pre = new Preprocess(); message = pre.emoticons(message); message = pre.emoji(message); message = pre.url(message); //StringFilter filter = new StringFilter(message); vals[0] = data.attribute(0).addStringValue(name); // 5/27/2014 vals[1] = data.attribute(1).addStringValue(message); if (ageGroup.equals("13-17")) { vals[2] = attVals.indexOf("13-17"); } else if (ageGroup.equals("18-24")) { vals[2] = attVals.indexOf("18-24"); } else if (ageGroup.equals("25-34")) { vals[2] = attVals.indexOf("25-34"); } else if (ageGroup.equals("35-44")) { vals[2] = attVals.indexOf("35-44"); } else if (ageGroup.equals("45-54")) { // Modified 6/11/2014 vals[2] = attVals.indexOf("45-54"); } data.add(new Instance(1.0, vals)); it.remove(); } ArffSaver saver = new ArffSaver(); saver.setInstances(data); saver.setFile(file); saver.writeBatch(); }
From source file:fantail.algorithms.RankingByPairwiseComparison.java
License:Open Source License
@Override public double[] recommendRanking(Instance testInst) throws Exception { Instances tempData = new Instances(testInst.dataset(), 0); tempData.add((Instance) testInst.copy()); // remove the relation att tempData.setClassIndex(-1);/* w w w .ja va2 s . c o m*/ tempData.deleteAttributeAt(tempData.numAttributes() - 1); tempData = Filter.useFilter(tempData, m_Add); tempData.setClassIndex(tempData.numAttributes() - 1); double predRanking[] = new double[m_NumLabels]; for (int i = 0; i < predRanking.length; i++) { predRanking[i] = m_NumLabels - 1; } for (int i = 0; i < m_Classifiers.size(); i++) { double predIndex = m_Classifiers.get(i).classifyInstance(tempData.instance(0)); String algoPair = m_AlgoPairs.get(i); String[] parts = algoPair.split("\\|"); int trueIndex = Integer.parseInt(parts[(int) predIndex]); predRanking[trueIndex] -= 1; } predRanking = Tools.doubleArrayToRanking(predRanking); return predRanking; }
From source file:fantail.algorithms.RankingByPairwiseComparison.java
License:Open Source License
public double[] recommendRanking2(Instance testInst) throws Exception { Instances tempData = new Instances(testInst.dataset(), 0); tempData.add((Instance) testInst.copy()); // remove the relation att tempData.setClassIndex(-1);// www. j av a 2 s .c o m tempData.deleteAttributeAt(tempData.numAttributes() - 1); tempData = Filter.useFilter(tempData, m_Add); tempData.setClassIndex(tempData.numAttributes() - 1); double predRanking[] = new double[m_NumLabels]; for (int i = 0; i < m_Classifiers.size(); i++) { double predIndex = m_Classifiers.get(i).classifyInstance(tempData.instance(0)); double predProb = m_Classifiers.get(i).distributionForInstance(tempData.instance(0))[0]; String algoPair = m_AlgoPairs.get(i); String[] parts = algoPair.split("\\|"); int trueIndex = Integer.parseInt(parts[(int) predIndex]); predRanking[trueIndex] -= predProb; } return Tools.doubleArrayToRanking(predRanking); }
From source file:FeatureSelection.ReliefFAttributeEval.java
License:Open Source License
public Instances createReliefInput(ArrayList<ArrayList<Double>> dataset, String[] featureNames_Arr, ArrayList<Double> labels) { this.featureNames_Arr = featureNames_Arr; // create attributes FastVector fv = new FastVector(); for (int i = 0; i <= featureNames_Arr.length; i++) { if (i == featureNames_Arr.length) { fv.addElement(new Attribute("@@class@@")); continue; }//from w w w .j av a 2s . co m fv.addElement(new Attribute(featureNames_Arr[i])); } // transform dataset so that each line represents each window - add // class label as well ArrayList<ArrayList<Double>> ReliefInput = new ArrayList<ArrayList<Double>>(); for (int i = 0; i < dataset.get(0).size(); i++) { ArrayList<Double> featT = new ArrayList<Double>(); for (int j = 0; j < dataset.size(); j++) { featT.add(dataset.get(j).get(i)); } featT.add(labels.get(i)); ReliefInput.add(featT); } // transform dataset into Instances type Instances ReliefInstances = new Instances("Features", fv, dataset.size()); for (int i = 0; i < ReliefInput.size(); i++) { double[] vals = CollectionUtilities.listToArray(ReliefInput.get(i)); Instance instWeka = new Instance(vals.length); for (int j = 0; j < vals.length; j++) { instWeka.setValue(j, vals[j]); } ReliefInstances.add(instWeka); } ReliefInstances.setClassIndex(ReliefInstances.numAttributes() - 1); return ReliefInstances; }
From source file:ffnn.TucilWeka.java
public static Instances createInstances(int max) { //List of Attributes dan List of Class untuk Header //Jumlah attributes: 4 jika tanpa class, 5 jika dengan class ArrayList<Attribute> attrs = new ArrayList<Attribute>(5); ArrayList<String> classVal = new ArrayList<String>(); //Menambahkkan class yang mungkin ke List classVal.add("Iris-setosa"); classVal.add("Iris-versicolor"); classVal.add("Iris-virginica"); //Menambahkan attributes ke List Attribute sepallength = new Attribute("sepallength"); attrs.add(sepallength); //Numeric Attributes Attribute sepalwidth = new Attribute("sepalwidth"); attrs.add(sepalwidth); //Numeric Attributes Attribute petallength = new Attribute("petallength"); attrs.add(petallength); //Numeric Attributes Attribute petalwidth = new Attribute("petalwidth"); attrs.add(petalwidth); //Numeric Attributes Attribute classValue = new Attribute("@@class@@", classVal); attrs.add(classValue); //String Attributes //Pembuatan/*from w ww. ja v a 2s . c o m*/ //Constructor dengan param Nama, List of Attribute, size Instances dataRaw = new Instances("irisNew", attrs, 0); //Instances kosong dataRaw.setClassIndex(dataRaw.numAttributes() - 1); Scanner scan = new Scanner(System.in); for (int i = 0; i < max; i++) { //Weka mennyimpan instance sebagai double double temp; Instance inst = new DenseInstance(dataRaw.numAttributes()); System.out.println("Sepallength:"); temp = scan.nextDouble(); inst.setValue(sepallength, temp); System.out.println("Sepalwidth:"); temp = scan.nextDouble(); inst.setValue(sepalwidth, temp); System.out.println("Petallegth:"); temp = scan.nextDouble(); inst.setValue(petallength, temp); System.out.println("Petalwidth:"); temp = scan.nextDouble(); inst.setValue(petalwidth, temp); //System.out.println("Masukan kelima:"); //temp = scan.nextDouble(); //0 -> setosa, 1 -> vesicolor, 2-> virginica //instS.setValue(classValue, temp); //tidak dibutuhkan sebenarnya //Menambahkan instance ke instances dataRaw.add(inst); } return dataRaw; }
From source file:fiit.gpminerstatic.Main.java
public static void main(String args[]) { ArrayList<Attribute> attributes = new ArrayList<Attribute>(); for (int i = 0; i < 1000; i++) { attributes.add(new Attribute(String.valueOf(i))); }/* w ww . ja v a 2 s . c o m*/ // load data from file into instances SessionsFileStream stream = new SessionsFileStream( "g:\\workspace_GPMiner\\data\\alef_sessions_aggregated.csv"); Instances instances = new Instances("Instances", attributes, 1000); Enumeration<Instance> enumer = instances.enumerateInstances(); while (enumer.hasMoreElements()) { instances.add(enumer.nextElement()); } try { // make global patterns with fpgrowth alghoritm FPGrowth fp = new FPGrowth(); fp.buildAssociations(instances); AssociationRules assocRules = fp.getAssociationRules(); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:fk.stardust.localizer.machinelearn.WekaFaultLocalizer.java
License:Open Source License
@Override public Ranking<T> localize(final ISpectra<T> spectra) { // == 1. Create Weka training instance final List<INode<T>> nodes = new ArrayList<>(spectra.getNodes()); // nominal true/false values final List<String> tf = new ArrayList<String>(); tf.add("t");/*from w w w. ja v a2s. c o m*/ tf.add("f"); // create an attribute for each component final Map<INode<T>, Attribute> attributeMap = new HashMap<INode<T>, Attribute>(); final ArrayList<Attribute> attributeList = new ArrayList<Attribute>(); // NOCS: Weka needs ArrayList.. for (final INode<T> node : nodes) { final Attribute attribute = new Attribute(node.toString(), tf); attributeList.add(attribute); attributeMap.put(node, attribute); } // create class attribute (trace success) final Attribute successAttribute = new Attribute("success", tf); attributeList.add(successAttribute); // create weka training instance final Instances trainingSet = new Instances("TraceInfoInstances", attributeList, 1); trainingSet.setClassIndex(attributeList.size() - 1); // == 2. add traces to training set // add an instance for each trace for (final ITrace<T> trace : spectra.getTraces()) { final Instance instance = new DenseInstance(nodes.size() + 1); instance.setDataset(trainingSet); for (final INode<T> node : nodes) { instance.setValue(attributeMap.get(node), trace.isInvolved(node) ? "t" : "f"); } instance.setValue(successAttribute, trace.isSuccessful() ? "t" : "f"); trainingSet.add(instance); } // == 3. use prediction to localize faults // build classifier try { final Classifier classifier = this.buildClassifier(this.classifierName, this.classifierOptions, trainingSet); final Ranking<T> ranking = new Ranking<>(); System.out.println("begin classifying"); int classified = 0; final Instance instance = new DenseInstance(nodes.size() + 1); instance.setDataset(trainingSet); for (final INode<T> node : nodes) { instance.setValue(attributeMap.get(node), "f"); } instance.setValue(successAttribute, "f"); for (final INode<T> node : nodes) { classified++; if (classified % 1000 == 0) { System.out.println(String.format("Classified %d nodes.", classified)); } // contain only the current node in the network instance.setValue(attributeMap.get(node), "t"); // predict with which probability this setup leads to a failing network final double[] distribution = classifier.distributionForInstance(instance); ranking.rank(node, distribution[1]); // reset involvment for node instance.setValue(attributeMap.get(node), "f"); } return ranking; } catch (final Exception e) { // NOCS: Weka throws only raw exceptions throw new RuntimeException(e); } }
From source file:fr.ign.cogit.geoxygene.util.conversion.ConversionToARFF.java
License:Open Source License
/** * Permet de convertir des FeatureCollections en fichier ARFF * //from w w w .j a v a2 s. c o m * @param featColl la collection en entre * @param outFilePath le fichier en sortie * @throws ParseException * @throws IOException */ public static void export(IFeatureCollection<IFeature> featColl, String outFilePath) throws ParseException, IOException { ArrayList<Attribute> atts = new ArrayList<Attribute>(); Instances data; double[] vals; int i; // 1. Prparation des attributs IFeature feat = featColl.get(0); FeatureType ft = (FeatureType) feat.getFeatureType(); List<GF_AttributeType> lAttributeTypes = ft.getFeatureAttributes(); int nbAttributes = lAttributeTypes.size(); for (i = 0; i < nbAttributes; i++) { GF_AttributeType attT = lAttributeTypes.get(i); if (attT.getValueType().equalsIgnoreCase("String")) { atts.add(new Attribute(attT.getMemberName(), (List<String>) null)); } else { atts.add(new Attribute(attT.getMemberName())); } } // 2 on cre l'instance data = new Instances("MyRelation", atts, 0); // 3 on ajoute les donnes int nbElem = featColl.size(); for (i = 0; i < nbElem; i++) { feat = featColl.get(i); vals = new double[nbAttributes]; for (int j = 0; j < nbAttributes; j++) { GF_AttributeType attT = lAttributeTypes.get(j); if (attT.getValueType().equalsIgnoreCase("String")) { vals[j] = data.attribute(j).addStringValue(feat.getAttribute(attT.getMemberName()).toString()); } else { vals[j] = Double.parseDouble(feat.getAttribute(attT.getMemberName()).toString()); } } DenseInstance densInstance = new DenseInstance(1.0, vals); data.add(densInstance); } ArffSaver arffSaver = new ArffSaver(); arffSaver.setInstances(data); arffSaver.setFile(new File(outFilePath)); arffSaver.writeBatch(); }
From source file:fr.loria.synalp.jtrans.phonetiseur.Classifieurs.java
License:Open Source License
private Instances appliquerFiltre(Filter filtre, Instances instances) throws Exception { Instances newInstances; Instance temp;/*from ww w. j ava 2 s. c o m*/ filtre.setInputFormat(instances); for (int i = 0; i < instances.numInstances(); i++) { filtre.input(instances.instance(i)); } filtre.batchFinished(); newInstances = filtre.getOutputFormat(); while ((temp = filtre.output()) != null) { newInstances.add(temp); } return newInstances; }
From source file:gate.plugin.learningframework.data.CorpusRepresentationWeka.java
/** * Create a Weka dataset from Mallet instances. * This creates a Weka dataset from the mallet corpus representation. * NOTE: for now the attributes list will always contain either a numeric or nominal class * (if the pipe has a target alphabet, a nominal class is assumed, otherwise a numeric target). * However, if the mallet instance does not have a target, the corresponding weka instance * will not have the target attribute set in the sparse vector (so a 0 value is used). * TODO: not sure if this has any bad consequences in those situations where we really * want an instance with no target attribute at all, i.e. at classification time. * * @param cr/*from ww w . j a va 2 s . co m*/ * @return */ public static Instances getFromMallet(CorpusRepresentationMallet cr) { Instances wekaInstances = emptyDatasetFromMallet(cr); InstanceList malletInstances = cr.getRepresentationMallet(); for (cc.mallet.types.Instance malletInstance : malletInstances) { weka.core.Instance wekaInstance = wekaInstanceFromMalletInstance(wekaInstances, malletInstance); wekaInstances.add(wekaInstance); } return wekaInstances; }