List of usage examples for weka.core Instance stringValue
public String stringValue(Attribute att);
From source file:probcog.bayesnets.learning.DomainLearner.java
License:Open Source License
/** * learns all the examples in the result set. Each row in the result set * represents one example. All the random variables (nodes) that have been * scheduled for learning in the constructor need to be found in each result * row as columns that are named accordingly, i.e. for each random variable * for which the domain is to be learnt, there must be a column with a * matching name in the result set.//from www. j a v a 2 s . c o m * * @param rs * the result set * @throws Exception * if the result set is empty * @throws SQLException * particularly if there is no matching column for one of the * node names */ public void learn(Instances instances) throws Exception, SQLException { // if it's an empty result set, throw exception if (instances.numInstances() == 0) throw new Exception("empty result set!"); // gather domain data int numDirectDomains = directDomains != null ? directDomains.length : 0; int numClusteredDomains = clusteredDomains != null ? clusteredDomains.length : 0; @SuppressWarnings("unchecked") Enumeration<Instance> instanceEnum = instances.enumerateInstances(); while (instanceEnum.hasMoreElements()) { Instance instance = instanceEnum.nextElement(); // for direct learning, add outcomes to the set of outcomes for (int i = 0; i < numDirectDomains; i++) { directDomainData.get(i).add(instance.stringValue(instances.attribute(directDomains[i].getName()))); } // for clustering, gather all instances for (int i = 0; i < numClusteredDomains; i++) { Instance inst = new Instance(1); inst.setValue(attrValue, instance.value(instances.attribute(clusteredDomains[i].nodeName))); clusterData[i].add(inst); } } }
From source file:put.semantic.fcanew.ml.WekaClassifier.java
public static Instance convert(Instance input, Instances src, Instances dst) { Instance result = new Instance(dst.numAttributes()); result.setDataset(dst);/*from w w w. j av a 2 s .com*/ for (int i = 0; i < dst.numAttributes(); ++i) { Attribute srcAttr = src.attribute(dst.attribute(i).name()); if (srcAttr.isNumeric()) { double val = input.value(srcAttr); result.setValue(i, val); } else { String val = input.stringValue(srcAttr); result.setValue(i, val); } } return result; }
From source file:reactivetechnologies.sentigrade.dto.VectorRequestData.java
License:Apache License
public void setTextInstances(Instances texts, String domain) { int c = texts.classIndex(); int t = c == 0 ? 1 : 0; for (Instance text : Collections.list(texts.enumerateInstances())) { getDataSet().add(new Tuple(text.stringValue(t), text.stringValue(c))); }//from w w w . j ava 2 s. c o m getClasses().addAll(classAttrNominals(texts)); setDomain(domain); }
From source file:semana07.IrisKnn.java
public static void main(String[] args) throws FileNotFoundException, IOException, Exception { // DEFININDO CONJUNTO DE TREINAMENTO // - Definindo o leitor do arquivo arff FileReader baseIris = new FileReader("iris.arff"); // - Definindo o grupo de instancias a partir do arquivo "simpsons.arff" Instances iris = new Instances(baseIris); // - Definindo o indice do atributo classe iris.setClassIndex(4);// ww w . j a v a 2s.c om iris = iris.resample(new Debug.Random()); Instances irisTreino = iris.trainCV(3, 0); Instances irisTeste = iris.testCV(3, 0); // DEFININDO EXEMPLO DESCONHECIDO //5.9,3.0,5.1,1.8,Iris-virginica Instance irisInst = new DenseInstance(iris.numAttributes()); irisInst.setDataset(iris); irisInst.setValue(0, 5.9); irisInst.setValue(1, 3.0); irisInst.setValue(2, 5.1); irisInst.setValue(3, 1.8); // DEFININDO ALGORITMO DE CLASSIFICAO //NN IBk vizinhoIris = new IBk(); //kNN IBk knnIris = new IBk(3); // MONTANDO CLASSIFICADOR //NN vizinhoIris.buildClassifier(irisTreino); //kNN knnIris.buildClassifier(irisTreino); // Definindo arquivo a ser escrito FileWriter writer = new FileWriter("iris.csv"); // Escrevendo o cabealho do arquivo writer.append("Classe Real;Resultado NN;Resultado kNN"); writer.append(System.lineSeparator()); // Sada CLI / Console System.out.println("Classe Real;Resultado NN;Resultado kNN"); //Cabealho for (int i = 0; i <= irisTeste.numInstances() - 1; i++) { Instance testeIris = irisTeste.instance(i); // Sada CLI / Console do valor original System.out.print(testeIris.stringValue(4) + ";"); // Escrevendo o valor original no arquivo writer.append(testeIris.stringValue(4) + ";"); // Definindo o atributo classe como indefinido testeIris.setClassMissing(); // CLASSIFICANDO A INSTANCIA // NN double respostaVizinho = vizinhoIris.classifyInstance(testeIris); testeIris.setValue(4, respostaVizinho); String stringVizinho = testeIris.stringValue(4); //kNN double respostaKnn = knnIris.classifyInstance(testeIris); // Atribuindo respota ao valor do atributo do index 4(classe) testeIris.setValue(4, respostaKnn); String stringKnn = testeIris.stringValue(4); // Adicionando resultado ao grupo de instancia iris iris.add(irisInst); //Escrevendo os resultados no arquivo iris.csv writer.append(stringVizinho + ";"); writer.append(stringKnn + ";"); writer.append(System.lineSeparator()); // Exibindo via CLI / Console o resultado System.out.print(respostaVizinho + ";"); System.out.print(respostaKnn + ";"); System.out.println(testeIris.stringValue(4)); } writer.flush(); writer.close(); }
From source file:sg.edu.nus.comp.nlp.ims.classifiers.CMultiClassesSVM.java
License:Open Source License
@Override public void buildClassifier(Instances p_Instances) throws Exception { Instances newInsts = null;/*from ww w . ja v a2 s . c o m*/ if (this.m_Classifier == null) { throw new IllegalStateException("No base classifier has been set!"); } this.m_ZeroR = new ZeroR(); this.m_ZeroR.buildClassifier(p_Instances); this.m_ClassAttribute = p_Instances.classAttribute(); this.getOutputFormat(p_Instances); int numClassifiers = p_Instances.numClasses(); switch (numClassifiers) { case 1: this.m_Classifiers = null; break; case 2: this.m_Classifiers = Classifier.makeCopies(this.m_Classifier, 1); newInsts = new Instances(this.m_OutputFormat, 0); for (int i = 0; i < p_Instances.numInstances(); i++) { Instance inst = this.filterInstance(p_Instances.instance(i)); inst.setDataset(newInsts); newInsts.add(inst); } this.m_Classifiers[0].buildClassifier(newInsts); break; default: this.m_Classifiers = Classifier.makeCopies(this.m_Classifier, numClassifiers); Hashtable<String, ArrayList<Double>> id2Classes = null; if (this.m_IndexOfID >= 0) { id2Classes = new Hashtable<String, ArrayList<Double>>(); for (int i = 0; i < p_Instances.numInstances(); i++) { Instance inst = p_Instances.instance(i); String id = inst.stringValue(this.m_IndexOfID); if (!id2Classes.containsKey(id)) { id2Classes.put(id, new ArrayList<Double>()); } id2Classes.get(id).add(inst.classValue()); } } for (int classIdx = 0; classIdx < this.m_Classifiers.length; classIdx++) { newInsts = this.genInstances(p_Instances, classIdx, id2Classes); this.m_Classifiers[classIdx].buildClassifier(newInsts); } } }
From source file:sg.edu.nus.comp.nlp.ims.classifiers.CMultiClassesSVM.java
License:Open Source License
/** * generate instances for classifier classIdx * * @param p_Instances/*from w w w.j av a 2 s .c om*/ * input instances * @param p_ClassIndex * class index * @param p_ID2Classes * instance ids * @return new instances */ protected Instances genInstances(Instances p_Instances, double p_ClassIndex, Hashtable<String, ArrayList<Double>> p_ID2Classes) { Instances newInsts = new Instances(this.m_OutputFormat, 0); for (int i = 0; i < p_Instances.numInstances(); i++) { Instance inst = p_Instances.instance(i); Instance newInst = null; if (SparseInstance.class.isInstance(inst)) { newInst = new SparseInstance(inst); } else { newInst = new Instance(inst); } if (newInst.value(p_Instances.classIndex()) == p_ClassIndex) { newInst.setValue(inst.classIndex(), 1); } else { if (p_ID2Classes == null || !p_ID2Classes.get(inst.stringValue(this.m_IndexOfID)) .contains(new Double(p_ClassIndex))) { newInst.setValue(inst.classIndex(), 0); } else { continue; } } newInst.deleteAttributeAt(this.m_IndexOfID); newInst.setDataset(newInsts); newInsts.add(newInst); } return newInsts; }
From source file:smo2.SMO.java
License:Open Source License
public void mygenstat(Instances m_tinst) { System.out.println("remove text till here..."); String[] m_cnames = this.classAttributeNames(); try {/*from w w w . ja va2s. c o m*/ for (int m_k = 0; m_k < m_tinst.numInstances(); m_k++) { Instance mc_inst = m_tinst.instance(m_k); String m_classname = mc_inst.stringValue(mc_inst.classAttribute()); // Instance mc_wmiss = (Instance)mc_inst.copy(); // mc_wmiss.setData(m_tinst); // double mpval = // ((Classifier)classifier).classifyInstance(mc_wmiss); /* * int m_rid=0; if(m_classname.equals(m_cnames[0])) { m_rid =0; * }else if(m_classname.equals(m_cnames[1])){ m_rid=1; }else{ * System.out.println("unknown class: m_classname " + * m_classname + ", m_cname " + m_cnames[0] + ", " + * m_cnames[1]); System.exit(1); } */ // System.out.println("classvalue " + mc_inst.classValue() + // " , classname " + m_classname); double m_output = obtainVotes(mc_inst); double[][] m_bias = bias(); double m_myscore = myScore(m_output, m_bias[0][1]); // if(mc_inst.classAttribute().isNumeric()) // { // System.out.println(m_k + "]class=" + m_classname + ",output=" // + m_output + ",bias=" + m_bias[0][1] + ",score=" + // m_myscore+",pred="+mpval); // }else{ // String m_pclass = mc_inst.classAttribute().value((int)mpval); // double // mprob=classifier.distributionForInstance(withMissing)[(int)mpval]; // System.out.println(m_k + "]class=" + m_classname + ",output=" // + m_output + ",bias=" + m_bias[0][1] + ",score=" + // m_myscore+",pred="+m_pclass+",prob="+m_prob); // } System.out.println(m_k + "]class=" + m_classname + ",output=" + m_output + ",bias=" + m_bias[0][1] + ",score=" + m_myscore); } } catch (Exception e) { e.printStackTrace(); System.err.println(e.getMessage()); } }
From source file:test20November.ArffFileLoader.java
public ArffFileLoader(String filename) throws FileNotFoundException, IOException { BufferedReader reader = new BufferedReader(new FileReader(filename)); ArffLoader.ArffReader arff = new ArffLoader.ArffReader(reader); Instances data = arff.getData();//from w ww .j av a 2 s .c om data.setClassIndex(data.numAttributes() - 1); attributes = new String[data.numInstances()][data.numAttributes() - 1]; labels = new String[data.numInstances()]; for (int i = 0; i < data.numInstances(); i++) { Instance instance = data.instance(i); for (int j = 0; j < instance.numAttributes() - 1; j++) { attributes[i][j] = instance.stringValue(j); } labels[i] = instance.stringValue(instance.numAttributes() - 1); } attributesLegalValues = new String[data.numAttributes() - 1][]; for (int i = 0; i < data.numAttributes() - 1; i++) { attributesLegalValues[i] = (String[]) Collections.list(data.attribute(i).enumerateValues()) .toArray(new String[data.attribute(i).numValues()]); } labelsLegalValues = (String[]) Collections.list(data.attribute(data.numAttributes() - 1).enumerateValues()) .toArray(new String[data.attribute(data.numAttributes() - 1).numValues()]); }
From source file:themeextractor.filters.MauiFilter.java
License:Open Source License
private void selectCandidates() throws Exception { if (debugMode) { System.err.println("--- Computing candidates..."); }// w w w . j ava 2 s . c om allCandidates = new HashMap<Instance, HashMap<String, Candidate>>(); // Convert pending input instances into data for classifier int totalDocuments = getInputFormat().numInstances(); for (int i = 0; i < totalDocuments; i++) { Instance current = getInputFormat().instance(i); String fileName = current.stringValue(fileNameAtt); int j = i + 1; if (debugMode) { System.err.println( "---- Processing document " + fileName + ", " + j + " out of " + totalDocuments + "..."); } // Get the phrases for the document String documentText = current.stringValue(documentAtt); HashMap<String, Candidate> candidateList = getCandidates(documentText); if (debugMode) { System.err.println("---- " + candidateList.size() + " candidates"); } allCandidates.put(current, candidateList); } }
From source file:themeextractor.filters.MauiFilter.java
License:Open Source License
/** * Builds the classifier.//from w w w . ja v a 2s . com */ private void buildClassifier() throws Exception { // Generate input format for classifier FastVector atts = new FastVector(); for (int i = 0; i < getInputFormat().numAttributes(); i++) { if (i == documentAtt) { atts.addElement(new Attribute("Term_frequency")); // 2 atts.addElement(new Attribute("IDF")); // atts.addElement(new Attribute("TFxIDF")); // atts.addElement(new Attribute("First_occurrence")); // atts.addElement(new Attribute("Last_occurrence")); // atts.addElement(new Attribute("Spread")); // atts.addElement(new Attribute("Domain_keyphraseness")); // atts.addElement(new Attribute("Length")); // atts.addElement(new Attribute("Generality")); // atts.addElement(new Attribute("Node_degree")); // atts.addElement(new Attribute("Semantic_relatedness")); // atts.addElement(new Attribute("Wikipedia_keyphraseness")); // atts.addElement(new Attribute("Inverse_Wikip_frequency")); // atts.addElement(new Attribute("Total_Wikip_keyphraseness")); // 13 } else if (i == keyphrasesAtt) { if (nominalClassValue) { FastVector vals = new FastVector(2); vals.addElement("False"); vals.addElement("True"); atts.addElement(new Attribute("Keyphrase?", vals)); } else { atts.addElement(new Attribute("Keyphrase?")); } } } classifierData = new Instances("ClassifierData", atts, 0); classifierData.setClassIndex(numFeatures); if (debugMode) { System.err.println("--- Converting instances for classifier"); } int totalDocuments = getInputFormat().numInstances(); // Convert pending input instances into data for classifier for (int i = 0; i < totalDocuments; i++) { Instance current = getInputFormat().instance(i); // Get the key phrases for the document String keyphrases = current.stringValue(keyphrasesAtt); HashMap<String, Counter> hashKeyphrases = getGivenKeyphrases(keyphrases); // Get the phrases for the document HashMap<String, Candidate> candidateList = allCandidates.get(current); // Compute the feature values for each phrase and // add the instance to the data for the classifier int countPos = 0; int countNeg = 0; if (debugMode) { System.err .println("--- Computing features for document " + i + " out of " + totalDocuments + "..."); } for (Candidate candidate : candidateList.values()) { // ignore all candidates that appear less than a threshold if (candidate.getFrequency() < minOccurFrequency) { continue; } // compute feature values double[] vals = computeFeatureValues(candidate, true, hashKeyphrases, candidateList); if (vals[vals.length - 1] == 0) { countNeg++; } else { countPos++; } Instance inst = new Instance(current.weight(), vals); // System.out.println(candidate + "\t" + inst); classifierData.add(inst); } if (debugMode) { System.err.println(countPos + " positive; " + countNeg + " negative instances"); } } if (debugMode) { System.err.println("--- Building classifier"); } if (classifier == null) { // Build classifier if (nominalClassValue) { // FilteredClassifier fclass = new FilteredClassifier(); // fclass.setClassifier(new NaiveBayesSimple()); // fclass.setFilter(new Discretize()); // classifier = fclass; classifier = new Bagging(); // try also // classifier.setOptions( Utils.splitOptions("-P 10 -S 1 -I 10 -W weka.classifiers.trees.J48 -- -U -M 2")); } else { classifier = new Bagging(); // try also // classifier.setOptions(Utils.splitOptions("-P 10 -S 1 -I 10 -W // weka.classifiers.trees.J48 -- -U -M 2")) ; String optionsString = "-P 100 -S 1 -I 10 -W weka.classifiers.trees.M5P -- -U -M 7.0"; String[] options = Utils.splitOptions(optionsString); classifier.setOptions(options); } } classifier.buildClassifier(classifierData); if (debugMode) { System.err.println(classifier); } // Save space classifierData = new Instances(classifierData, 0); }