List of usage examples for weka.core Instances numAttributes
publicint numAttributes()
From source file:fantail.core.Tools.java
License:Open Source License
public static double[] getTargetVector(Instance inst) { Instances targetBag = inst.relationalValue(inst.classIndex()); double[] values = new double[targetBag.numAttributes()]; for (int i = 0; i < values.length; i++) { values[i] = targetBag.instance(0).value(i); }/*from w w w . j ava 2s . co m*/ return values; }
From source file:fantail.core.Tools.java
License:Open Source License
public static String[] getTargetNames(Instance inst) { Instances targetBag = inst.relationalValue(inst.classIndex()); String[] names = new String[targetBag.numAttributes()]; for (int i = 0; i < names.length; i++) { names[i] = targetBag.attribute(i).name(); }//from w w w.ja va 2 s . c o m return names; }
From source file:fantail.core.Tools.java
License:Open Source License
public static ReasonerComponent[] getTargetObjects(Instance inst) { Instances targetBag = inst.relationalValue(inst.classIndex()); String[] names = new String[targetBag.numAttributes()]; ReasonerComponent res[] = new ReasonerComponent[names.length]; for (int i = 0; i < names.length; i++) { ReasonerComponent cl = new ReasonerComponent(i, targetBag.instance(0).value(i)); res[i] = cl;//from www . j av a 2s . co m } return res; }
From source file:fantail.core.Tools.java
License:Open Source License
public static Instances loadFantailARFFInstances(String arffPath) throws Exception { ArffLoader loader = new ArffLoader(); loader.setSource(new File(arffPath)); Instances data = loader.getDataSet(); data.setClassIndex(data.numAttributes() - 1); if (data.classAttribute().isRelationValued() != true) { throw new Exception("The last attribute needs to be 'RelationValued'"); }/*from www . j ava 2s . co m*/ return data; }
From source file:fantail.core.WekaLRHelper.java
License:Open Source License
public static Instances covertArff2Xarff(Instances data) { Instances xarffData = null;// w w w. j a v a 2 s . c o m try { String userDIR = System.getProperty("user.dir"); String randFileName = Long.toString(System.nanoTime()).substring(10) + ".fantail.algorithms.LRT.temp.xarff"; String path_separator = System.getProperty("file.separator"); String xarffPath = userDIR + path_separator + randFileName; //System.out.println(m_xarffPath); int numObjects = Tools.getNumberTargets(data); StringBuilder sb = new StringBuilder(); sb.append("@relation arff2xarff").append(System.getProperty("line.separator")); for (int i = 0; i < data.numAttributes() - 1; i++) { sb.append("@attribute "); sb.append(data.attribute(i).name()); sb.append(" numeric").append(System.getProperty("line.separator")); } sb.append("@attribute L RANKING {"); for (int i = 0; i < numObjects; i++) { String spr = ","; if (i == numObjects - 1) { spr = ""; } String targetName = "T" + (i); sb.append(targetName).append(spr); } sb.append("}").append(System.getProperty("line.separator")); sb.append("@data ").append(System.getProperty("line.separator")); for (int i = 0; i < data.numInstances(); i++) { Instance inst = data.instance(i); for (int j = 0; j < data.numAttributes() - 1; j++) { sb.append(inst.value(j)).append(","); } for (int x = 1; x <= numObjects; x++) { int rank = x; String[] names = Tools.getTargetNames(inst); String algo = getName(rank, Tools.getTargetVector(inst), names); System.out.println("\t algo: " + algo + ". rank: " + rank + ", Tools.getTargetVector(inst):" + Arrays.toString(Tools.getTargetVector(inst)) + ", " + "names:" + Arrays.toString(names)); String sprr = ">"; if (x == names.length) { sprr = ""; } sb.append(algo).append(sprr); } sb.append(System.getProperty("line.separator")); } File file = new File(xarffPath); Writer output = new BufferedWriter(new FileWriter(file)); output.write(sb.toString()); output.close(); System.out.println(file.getAbsoluteFile()); weka.core.converters.XArffLoader xarffLoader = new weka.core.converters.XArffLoader(); xarffLoader.setSource(new File(xarffPath)); xarffData = xarffLoader.getDataSet(); // File tmpxarffFile = new File(xarffPath); if (tmpxarffFile.exists()) { tmpxarffFile.delete(); } } catch (Exception e) { e.printStackTrace(); System.exit(-1); } return xarffData; }
From source file:fantail.core.WekaLRHelper.java
License:Open Source License
public static Instances covertArff2Xarff2(DATA_TYPE data_type, Instances data) { Instances xarffData = null;//from w w w .j a va 2 s. co m try { String userDIR = System.getProperty("user.dir"); //String randFileName = Long.toString(System.nanoTime()).substring(10) + ".LRT.temp.xarff"; String randFileName = UUID.randomUUID().toString() + ".LRT.temp.xarff"; String path_separator = System.getProperty("file.separator"); String xarffPath = userDIR + path_separator + randFileName; //System.out.println(m_xarffPath); int numObjects = Tools.getNumberTargets(data); StringBuilder sb = new StringBuilder(); sb.append("@relation arff2xarff").append(System.getProperty("line.separator")); for (int i = 0; i < data.numAttributes() - 1; i++) { sb.append("@attribute "); sb.append(data.attribute(i).name()); sb.append(" numeric").append(System.getProperty("line.separator")); } sb.append("@attribute L RANKING {"); for (int i = 0; i < numObjects; i++) { String spr = ","; if (i == numObjects - 1) { spr = ""; } String targetName = "T" + (i); sb.append(targetName).append(spr); } sb.append("}").append(System.getProperty("line.separator")); sb.append("@data ").append(System.getProperty("line.separator")); for (int i = 0; i < data.numInstances(); i++) { Instance inst = data.instance(i); // determine a ranking of the class labels String ranking_result = determineRanking(Tools.getTargetObjects(inst)); if (ranking_result == null) continue; //System.out.println("\t ranking_result:" + ranking_result); // looking at a>b>c, if the 'a' part consists of more than two partial relations, we need to split them. List<ArrayList<String>> label_collection = new ArrayList<ArrayList<String>>(); // generate feature string String attr_set_str = ""; for (int j = 0; j < data.numAttributes() - 1; j++) { attr_set_str += (inst.value(j) + ","); } // split label string via ">" String items[] = ranking_result.split(">"); for (int j = 0; j < items.length; j++) { String labels[] = items[j].split("\\|"); // if the first label has more than or equal to 2 partial relations, we split it. ArrayList<String> label_list = new ArrayList<String>(); if (j == 0) { if (labels.length >= 2) { for (int k = 0; k < labels.length; k++) { label_list.add(labels[k]); } } else { label_list.add(items[j]); } label_collection.add(label_list); } else { if (labels.length >= 3) { for (int k = 0; k < labels.length; k++) { label_list.add(labels[k]); } } else { label_list.add(items[j]); } label_collection.add(label_list); } } List<String> prev_items_in_label_collection = new ArrayList<String>(); for (int j = 0; j < label_collection.size(); j++) { List<String> items_in_label_collection = new ArrayList<String>(); if (j == 0) { for (int k = 0; k < label_collection.get(j).size(); k++) { items_in_label_collection.add(label_collection.get(j).get(k)); } } else { for (int k = 0; k < label_collection.get(j).size(); k++) { for (int l = 0; l < prev_items_in_label_collection.size(); l++) { items_in_label_collection.add(prev_items_in_label_collection.get(l) + ">" + label_collection.get(j).get(k)); } } } prev_items_in_label_collection = items_in_label_collection; } for (int j = 0; j < prev_items_in_label_collection.size(); j++) { //System.out.println("\t\t line:" + prev_items_in_label_collection.get(j)); sb.append(attr_set_str + prev_items_in_label_collection.get(j) + "\n"); } InputStream is = new ByteArrayInputStream(sb.toString().getBytes()); weka.core.converters.XArffLoader xarffLoader = new weka.core.converters.XArffLoader(); xarffLoader.setSource(is); xarffData = xarffLoader.getDataSet(); } } catch (Exception e) { e.printStackTrace(); System.exit(-1); } return xarffData; }
From source file:fantail.examples.LabelRankingAlgorithmsMultiRunEvalExample01.java
License:Open Source License
public static void main(String[] args) throws Exception { String arffPath = "/Users/Quan/Dropbox/Ranking_Datasets/LabelRankingSemiSyntheticData/iris_dense.csv.arff"; Instances data = Tools.loadFantailARFFInstances(arffPath); int numRuns = 30; int randSeed = 1; double trainsetRatio = 0.90; System.out.println(arffPath); System.out.println("Num of labels: " + Tools.getNumberTargets(data)); System.out.println("Num of instances: " + data.numInstances()); System.out.println("Num of attributes (incl. target att): " + data.numAttributes()); System.out.println();//from w w w. j av a 2 s . co m System.out.println("Num of runs: " + numRuns); System.out.println("trainsetRatio: " + trainsetRatio); System.out.println(); AbstractRanker ranker; MultiRunEvaluation eval; // String strFormat = "%-30s %-30s %-30s"; System.out.println(String.format(strFormat, "<Algorithms>", "<Kendall's tau>", "<Spearman Correlation>")); // ranker = new AverageRanking(); eval = new MultiRunEvaluation(data); eval.multiRunEvaluate(ranker, numRuns, trainsetRatio, randSeed); printResult(strFormat, ranker, eval); ranker = new RankingWithBinaryPCT(); eval = new MultiRunEvaluation(data); eval.multiRunEvaluate(ranker, numRuns, trainsetRatio, randSeed); printResult(strFormat, ranker, eval); ranker = new RankingByPairwiseComparison(); eval = new MultiRunEvaluation(data); eval.multiRunEvaluate(ranker, numRuns, trainsetRatio, randSeed); printResult(strFormat, ranker, eval); ranker = new RankingWithkNN(); eval = new MultiRunEvaluation(data); eval.multiRunEvaluate(ranker, numRuns, trainsetRatio, randSeed); printResult(strFormat, ranker, eval); ranker = new BinaryART(); ((BinaryART) ranker).setK(9999); ((BinaryART) ranker).setMiniLeaf(1); eval = new MultiRunEvaluation(data); eval.multiRunEvaluate(ranker, numRuns, trainsetRatio, randSeed); printResult(strFormat, ranker, eval); ranker = new ARTForests(); ((ARTForests) ranker).setNumIterations(50); ((ARTForests) ranker).setK(0); ((ARTForests) ranker).setNumMinInstances(1); eval = new MultiRunEvaluation(data); eval.multiRunEvaluate(ranker, numRuns, trainsetRatio, randSeed); printResult(strFormat, ranker, eval); // ranker = new RankingViaRegression(); eval = new MultiRunEvaluation(data); eval.multiRunEvaluate(ranker, numRuns, trainsetRatio, randSeed); printResult(strFormat, ranker, eval); }
From source file:fantail.examples.LabelRankingSingleAlgoExample01.java
License:Open Source License
public static void main(String[] args) throws Exception { String arffPath = "/Users/Quan/Dropbox/Ranking_Datasets/LabelRankingSemiSyntheticData/glass_dense.csv.arff"; Instances data = Tools.loadFantailARFFInstances(arffPath); int numRuns = 30; int randSeed = 1; double trainsetRatio = 0.50; System.out.println(arffPath); System.out.println("Num of labels: " + Tools.getNumberTargets(data)); System.out.println("Num of instances: " + data.numInstances()); System.out.println("Num of attributes (incl. target att): " + data.numAttributes()); System.out.println();//from w w w. j a v a 2s. c o m AbstractRanker ranker; MultiRunEvaluation eval; String strFormat = "%-30s %-30s %-30s"; System.out.println(String.format(strFormat, "<Algorithms>", "<Kendall>", "<SpearmanCC>")); ranker = new ARTForests(); eval = new MultiRunEvaluation(data); eval.multiRunEvaluate(ranker, numRuns, trainsetRatio, randSeed); printResult(strFormat, ranker, eval); }
From source file:farm_ads.MyClassifier.java
public Instances readIntances(String URL) throws Exception { FarmAds fa = new FarmAds(URL); FarmAdsVector fav = new FarmAdsVector(); fav.writeFile("data\\dataVecto.dat", fa); DataSource source = new DataSource("data\\dataVecto.dat"); Instances instances = source.getDataSet(); if (instances.classIndex() == -1) { instances.setClassIndex(instances.numAttributes() - 1); }//www . j a v a 2 s . c o m return instances; }
From source file:farm_ads.MyClassifier.java
public Instances readIntances(String URL, Hashtable att, Hashtable numAtt, String iv) throws Exception { FarmAds fa = new FarmAds(att, numAtt, iv, URL); FarmAdsVector fav = new FarmAdsVector(); fav.writeFile("data\\dataVecto.dat", fa); DataSource source = new DataSource("data\\dataVecto.dat"); Instances instances = source.getDataSet(); if (instances.classIndex() == -1) { instances.setClassIndex(instances.numAttributes() - 1); }/*from www . j a v a 2 s .c o m*/ return instances; }