楼主: ReneeBK
1343 5

Git Mirror of Weka [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4898份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49640 个
通用积分
55.8137
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2017-2-7 12:01:35 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
Weka (mirror)

NOTE The owner of this repository has no affiliation with official WEKA project. This repo is periodically updated as a kindness to others who have shown interest in it. It can take several hours to checkout the full official WEKA subversion repository and several minutes just to update with any new commits. Therefore, this exists to provide an easy way to access and peruse the WEKA source using git, nothing more.


This is a git mirror of the The University of Waikato machine learning project WEKA.

Weka is a collection of machine learning algorithms for data mining tasks. The algorithms can either be applied directly to a dataset or called from your own Java code. Weka contains tools for data pre-processing, classification, regression, clustering, association rules, and visualization. It is also well-suited for developing new machine learning schemes.

The official WEKA source code is hosted using subversion at the Waikato SVN server.

The current version of WEKA is licensed under the GNU General Public license version 3.0.

Use the weka-trunk branch to follow the official code base under active development at the University of Waikato.




本帖隐藏的内容

https://github.com/bnjmn/weka

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Mirror MIRR WEKA IRR periodically nothing commits University checkout learning

沙发
ReneeBK 发表于 2017-2-7 12:03:26

AttributeSelectionTest.java

  1. /*
  2. *    This program is free software; you can redistribute it and/or modify
  3. *    it under the terms of the GNU General Public License as published by
  4. *    the Free Software Foundation; either version 2 of the License, or
  5. *    (at your option) any later version.
  6. *
  7. *    This program is distributed in the hope that it will be useful,
  8. *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. *    GNU General Public License for more details.
  11. *
  12. *    You should have received a copy of the GNU General Public License
  13. *    along with this program; if not, write to the Free Software
  14. *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */

  16. /*
  17. *    AttributeSelectionTest.java
  18. *    Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
  19. *
  20. */

  21. package wekaexamples.attributeSelection;

  22. import weka.attributeSelection.AttributeSelection;
  23. import weka.attributeSelection.CfsSubsetEval;
  24. import weka.attributeSelection.GreedyStepwise;
  25. import weka.classifiers.Evaluation;
  26. import weka.classifiers.meta.AttributeSelectedClassifier;
  27. import weka.classifiers.trees.J48;
  28. import weka.core.Instances;
  29. import weka.core.Utils;
  30. import weka.core.converters.ConverterUtils.DataSource;
  31. import weka.filters.Filter;

  32. import java.util.Random;

  33. /**
  34. * performs attribute selection using CfsSubsetEval and GreedyStepwise
  35. * (backwards) and trains J48 with that. Needs 3.5.5 or higher to compile.
  36. *
  37. * @author FracPete (fracpete at waikato dot ac dot nz)
  38. * @version $Revision$
  39. */
  40. public class AttributeSelectionTest {

  41.   /**
  42.    * uses the meta-classifier
  43.    */
  44.   protected static void useClassifier(Instances data) throws Exception {
  45.     System.out.println("\n1. Meta-classfier");
  46.     AttributeSelectedClassifier classifier = new AttributeSelectedClassifier();
  47.     CfsSubsetEval eval = new CfsSubsetEval();
  48.     GreedyStepwise search = new GreedyStepwise();
  49.     search.setSearchBackwards(true);
  50.     J48 base = new J48();
  51.     classifier.setClassifier(base);
  52.     classifier.setEvaluator(eval);
  53.     classifier.setSearch(search);
  54.     Evaluation evaluation = new Evaluation(data);
  55.     evaluation.crossValidateModel(classifier, data, 10, new Random(1));
  56.     System.out.println(evaluation.toSummaryString());
  57.   }

  58.   /**
  59.    * uses the filter
  60.    */
  61.   protected static void useFilter(Instances data) throws Exception {
  62.     System.out.println("\n2. Filter");
  63.     weka.filters.supervised.attribute.AttributeSelection filter = new weka.filters.supervised.attribute.AttributeSelection();
  64.     CfsSubsetEval eval = new CfsSubsetEval();
  65.     GreedyStepwise search = new GreedyStepwise();
  66.     search.setSearchBackwards(true);
  67.     filter.setEvaluator(eval);
  68.     filter.setSearch(search);
  69.     filter.setInputFormat(data);
  70.     Instances newData = Filter.useFilter(data, filter);
  71.     System.out.println(newData);
  72.   }

  73.   /**
  74.    * uses the low level approach
  75.    */
  76.   protected static void useLowLevel(Instances data) throws Exception {
  77.     System.out.println("\n3. Low-level");
  78.     AttributeSelection attsel = new AttributeSelection();
  79.     CfsSubsetEval eval = new CfsSubsetEval();
  80.     GreedyStepwise search = new GreedyStepwise();
  81.     search.setSearchBackwards(true);
  82.     attsel.setEvaluator(eval);
  83.     attsel.setSearch(search);
  84.     attsel.SelectAttributes(data);
  85.     int[] indices = attsel.selectedAttributes();
  86.     System.out.println("selected attribute indices (starting with 0):\n" + Utils.arrayToString(indices));
  87.   }

  88.   /**
  89.    * takes a dataset as first argument
  90.    *
  91.    * @param args        the commandline arguments
  92.    * @throws Exception  if something goes wrong
  93.    */
  94.   public static void main(String[] args) throws Exception {
  95.     // load data
  96.     System.out.println("\n0. Loading data");
  97.     DataSource source = new DataSource(args[0]);
  98.     Instances data = source.getDataSet();
  99.     if (data.classIndex() == -1)
  100.       data.setClassIndex(data.numAttributes() - 1);

  101.     // 1. meta-classifier
  102.     useClassifier(data);

  103.     // 2. filter
  104.     useFilter(data);

  105.     // 3. low-level
  106.     useLowLevel(data);
  107.   }
  108. }
复制代码

藤椅
ReneeBK 发表于 2017-2-7 12:04:55

Associations

  1. /*
  2. *    This program is free software; you can redistribute it and/or modify
  3. *    it under the terms of the GNU General Public License as published by
  4. *    the Free Software Foundation; either version 2 of the License, or
  5. *    (at your option) any later version.
  6. *
  7. *    This program is distributed in the hope that it will be useful,
  8. *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. *    GNU General Public License for more details.
  11. *
  12. *    You should have received a copy of the GNU General Public License
  13. *    along with this program; if not, write to the Free Software
  14. *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */

  16. /*
  17. *    AprioriOutput.java
  18. *    Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
  19. *
  20. */

  21. package wekaexamples.associations;

  22. import weka.associations.Apriori;
  23. import weka.core.Instances;
  24. import weka.core.converters.ConverterUtils.DataSource;

  25. /**
  26. * Loads a dataset, builds Apriori on it and outputs Apriori's model.
  27. *
  28. * @author FracPete (fracpete at waikato dot ac dot nz)
  29. * @version $Revision$
  30. */
  31. public class AprioriOutput {

  32.   /**
  33.    * Expects a dataset as first parameter. The last attribute is used
  34.    * as class attribute.
  35.    *
  36.    * @param args        the command-line parameters
  37.    * @throws Exception        if something goes wrong
  38.    */
  39.   public static void main(String[] args) throws Exception {
  40.     // load data
  41.     Instances data = DataSource.read(args[0]);
  42.     data.setClassIndex(data.numAttributes() - 1);

  43.     // build associator
  44.     Apriori apriori = new Apriori();
  45.     apriori.setClassIndex(data.classIndex());
  46.     apriori.buildAssociations(data);

  47.     // output associator
  48.     System.out.println(apriori);
  49.   }
  50. }
复制代码

板凳
ReneeBK 发表于 2017-2-7 12:07:25

IncrementalClassifier.java

  1. /*
  2. *    IncrementalClassifier.java
  3. *    Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
  4. *
  5. */

  6. package wekaexamples.classifiers;

  7. import weka.classifiers.bayes.NaiveBayesUpdateable;
  8. import weka.core.Instance;
  9. import weka.core.Instances;
  10. import weka.core.converters.ArffLoader;

  11. import java.io.File;

  12. /**
  13. * This example trains NaiveBayes incrementally on data obtained
  14. * from the ArffLoader.
  15. *
  16. * @author FracPete (fracpete at waikato dot ac dot nz)
  17. * @version $Revision$
  18. */
  19. public class IncrementalClassifier {

  20.   /**
  21.    * Expects an ARFF file as first argument (class attribute is assumed
  22.    * to be the last attribute).
  23.    *
  24.    * @param args        the commandline arguments
  25.    * @throws Exception  if something goes wrong
  26.    */
  27.   public static void main(String[] args) throws Exception {
  28.     // load data
  29.     ArffLoader loader = new ArffLoader();
  30.     loader.setFile(new File(args[0]));
  31.     Instances structure = loader.getStructure();
  32.     structure.setClassIndex(structure.numAttributes() - 1);

  33.     // train NaiveBayes
  34.     NaiveBayesUpdateable nb = new NaiveBayesUpdateable();
  35.     nb.buildClassifier(structure);
  36.     Instance current;
  37.     while ((current = loader.getNextInstance(structure)) != null)
  38.       nb.updateClassifier(current);

  39.     // output generated model
  40.     System.out.println(nb);
  41.   }
  42. }
复制代码

报纸
ReneeBK 发表于 2017-2-7 12:10:06
  1. /*
  2. *    This program is free software; you can redistribute it and/or modify
  3. *    it under the terms of the GNU General Public License as published by
  4. *    the Free Software Foundation; either version 2 of the License, or
  5. *    (at your option) any later version.
  6. *
  7. *    This program is distributed in the hope that it will be useful,
  8. *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. *    GNU General Public License for more details.
  11. *
  12. *    You should have received a copy of the GNU General Public License
  13. *    along with this program; if not, write to the Free Software
  14. *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */

  16. /*
  17. *    CrossValidationAddPrediction.java
  18. *    Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
  19. *
  20. */

  21. package wekaexamples.classifiers;

  22. import weka.classifiers.AbstractClassifier;
  23. import weka.classifiers.Classifier;
  24. import weka.classifiers.Evaluation;
  25. import weka.core.Instances;
  26. import weka.core.OptionHandler;
  27. import weka.core.Utils;
  28. import weka.core.converters.ConverterUtils.DataSink;
  29. import weka.core.converters.ConverterUtils.DataSource;
  30. import weka.filters.Filter;
  31. import weka.filters.supervised.attribute.AddClassification;

  32. import java.util.Random;

  33. /**
  34. * Performs a single run of cross-validation and adds the prediction on the
  35. * test set to the dataset.
  36. *
  37. * Command-line parameters:
  38. * <ul>
  39. *    <li>-t filename - the dataset to use</li>
  40. *    <li>-o filename - the output file to store dataset with the predictions
  41. *    in</li>
  42. *    <li>-x int - the number of folds to use</li>
  43. *    <li>-s int - the seed for the random number generator</li>
  44. *    <li>-c int - the class index, "first" and "last" are accepted as well;
  45. *    "last" is used by default</li>
  46. *    <li>-W classifier - classname and options, enclosed by double quotes;
  47. *    the classifier to cross-validate</li>
  48. * </ul>
  49. *
  50. * Example command-line:
  51. * <pre>
  52. * java wekaexamples.classifiers.CrossValidationAddPrediction -t anneal.arff -c last -o predictions.arff -x 10 -s 1 -W "weka.classifiers.trees.J48 -C 0.25"
  53. * </pre>
  54. *
  55. * @author FracPete (fracpete at waikato dot ac dot nz)
  56. * @version $Revision$
  57. */
  58. public class CrossValidationAddPrediction {

  59.   /**
  60.    * Performs the cross-validation. See Javadoc of class for information
  61.    * on command-line parameters.
  62.    *
  63.    * @param args        the command-line parameters
  64.    * @throws Exception        if something goes wrong
  65.    */
  66.   public static void main(String[] args) throws Exception {
  67.     // loads data and set class index
  68.     Instances data = DataSource.read(Utils.getOption("t", args));
  69.     String clsIndex = Utils.getOption("c", args);
  70.     if (clsIndex.length() == 0)
  71.       clsIndex = "last";
  72.     if (clsIndex.equals("first"))
  73.       data.setClassIndex(0);
  74.     else if (clsIndex.equals("last"))
  75.       data.setClassIndex(data.numAttributes() - 1);
  76.     else
  77.       data.setClassIndex(Integer.parseInt(clsIndex) - 1);

  78.     // classifier
  79.     String[] tmpOptions;
  80.     String classname;
  81.     tmpOptions     = Utils.splitOptions(Utils.getOption("W", args));
  82.     classname      = tmpOptions[0];
  83.     tmpOptions[0]  = "";
  84.     Classifier cls = (Classifier) Utils.forName(Classifier.class, classname, tmpOptions);

  85.     // other options
  86.     int seed  = Integer.parseInt(Utils.getOption("s", args));
  87.     int folds = Integer.parseInt(Utils.getOption("x", args));

  88.     // randomize data
  89.     Random rand = new Random(seed);
  90.     Instances randData = new Instances(data);
  91.     randData.randomize(rand);
  92.     if (randData.classAttribute().isNominal())
  93.       randData.stratify(folds);

  94.     // perform cross-validation and add predictions
  95.     Instances predictedData = null;
  96.     Evaluation eval = new Evaluation(randData);
  97.     for (int n = 0; n < folds; n++) {
  98.       Instances train = randData.trainCV(folds, n);
  99.       Instances test = randData.testCV(folds, n);
  100.       // the above code is used by the StratifiedRemoveFolds filter, the
  101.       // code below by the Explorer/Experimenter:
  102.       // Instances train = randData.trainCV(folds, n, rand);

  103.       // build and evaluate classifier
  104.       Classifier clsCopy = AbstractClassifier.makeCopy(cls);
  105.       clsCopy.buildClassifier(train);
  106.       eval.evaluateModel(clsCopy, test);

  107.       // add predictions
  108.       AddClassification filter = new AddClassification();
  109.       filter.setClassifier(cls);
  110.       filter.setOutputClassification(true);
  111.       filter.setOutputDistribution(true);
  112.       filter.setOutputErrorFlag(true);
  113.       filter.setInputFormat(train);
  114.       Filter.useFilter(train, filter);  // trains the classifier
  115.       Instances pred = Filter.useFilter(test, filter);  // perform predictions on test set
  116.       if (predictedData == null)
  117.         predictedData = new Instances(pred, 0);
  118.       for (int j = 0; j < pred.numInstances(); j++)
  119.         predictedData.add(pred.instance(j));
  120.     }

  121.     // output evaluation
  122.     System.out.println();
  123.     System.out.println("=== Setup ===");
  124.     if (cls instanceof OptionHandler)
  125.       System.out.println("Classifier: " + cls.getClass().getName() + " " + Utils.joinOptions(((OptionHandler) cls).getOptions()));
  126.     else
  127.       System.out.println("Classifier: " + cls.getClass().getName());
  128.     System.out.println("Dataset: " + data.relationName());
  129.     System.out.println("Folds: " + folds);
  130.     System.out.println("Seed: " + seed);
  131.     System.out.println();
  132.     System.out.println(eval.toSummaryString("=== " + folds + "-fold Cross-validation ===", false));

  133.     // output "enriched" dataset
  134.     DataSink.write(Utils.getOption("o", args), predictedData);
  135.   }
  136. }
复制代码

地板
ReneeBK 发表于 2017-2-7 12:13:17
  1. /*
  2. *    This program is free software; you can redistribute it and/or modify
  3. *    it under the terms of the GNU General Public License as published by
  4. *    the Free Software Foundation; either version 2 of the License, or
  5. *    (at your option) any later version.
  6. *
  7. *    This program is distributed in the hope that it will be useful,
  8. *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. *    GNU General Public License for more details.
  11. *
  12. *    You should have received a copy of the GNU General Public License
  13. *    along with this program; if not, write to the Free Software
  14. *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */

  16. /*
  17. *    CrossValidationAddPrediction.java
  18. *    Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
  19. *
  20. */

  21. package wekaexamples.classifiers;

  22. import weka.classifiers.AbstractClassifier;
  23. import weka.classifiers.Classifier;
  24. import weka.classifiers.Evaluation;
  25. import weka.core.Instances;
  26. import weka.core.OptionHandler;
  27. import weka.core.Utils;
  28. import weka.core.converters.ConverterUtils.DataSink;
  29. import weka.core.converters.ConverterUtils.DataSource;
  30. import weka.filters.Filter;
  31. import weka.filters.supervised.attribute.AddClassification;

  32. import java.util.Random;

  33. /**
  34. * Performs a single run of cross-validation and adds the prediction on the
  35. * test set to the dataset.
  36. *
  37. * Command-line parameters:
  38. * <ul>
  39. *    <li>-t filename - the dataset to use</li>
  40. *    <li>-o filename - the output file to store dataset with the predictions
  41. *    in</li>
  42. *    <li>-x int - the number of folds to use</li>
  43. *    <li>-s int - the seed for the random number generator</li>
  44. *    <li>-c int - the class index, "first" and "last" are accepted as well;
  45. *    "last" is used by default</li>
  46. *    <li>-W classifier - classname and options, enclosed by double quotes;
  47. *    the classifier to cross-validate</li>
  48. * </ul>
  49. *
  50. * Example command-line:
  51. * <pre>
  52. * java wekaexamples.classifiers.CrossValidationAddPrediction -t anneal.arff -c last -o predictions.arff -x 10 -s 1 -W "weka.classifiers.trees.J48 -C 0.25"
  53. * </pre>
  54. *
  55. * @author FracPete (fracpete at waikato dot ac dot nz)
  56. * @version $Revision$
  57. */
  58. public class CrossValidationAddPrediction {

  59.   /**
  60.    * Performs the cross-validation. See Javadoc of class for information
  61.    * on command-line parameters.
  62.    *
  63.    * @param args        the command-line parameters
  64.    * @throws Exception        if something goes wrong
  65.    */
  66.   public static void main(String[] args) throws Exception {
  67.     // loads data and set class index
  68.     Instances data = DataSource.read(Utils.getOption("t", args));
  69.     String clsIndex = Utils.getOption("c", args);
  70.     if (clsIndex.length() == 0)
  71.       clsIndex = "last";
  72.     if (clsIndex.equals("first"))
  73.       data.setClassIndex(0);
  74.     else if (clsIndex.equals("last"))
  75.       data.setClassIndex(data.numAttributes() - 1);
  76.     else
  77.       data.setClassIndex(Integer.parseInt(clsIndex) - 1);

  78.     // classifier
  79.     String[] tmpOptions;
  80.     String classname;
  81.     tmpOptions     = Utils.splitOptions(Utils.getOption("W", args));
  82.     classname      = tmpOptions[0];
  83.     tmpOptions[0]  = "";
  84.     Classifier cls = (Classifier) Utils.forName(Classifier.class, classname, tmpOptions);

  85.     // other options
  86.     int seed  = Integer.parseInt(Utils.getOption("s", args));
  87.     int folds = Integer.parseInt(Utils.getOption("x", args));

  88.     // randomize data
  89.     Random rand = new Random(seed);
  90.     Instances randData = new Instances(data);
  91.     randData.randomize(rand);
  92.     if (randData.classAttribute().isNominal())
  93.       randData.stratify(folds);

  94.     // perform cross-validation and add predictions
  95.     Instances predictedData = null;
  96.     Evaluation eval = new Evaluation(randData);
  97.     for (int n = 0; n < folds; n++) {
  98.       Instances train = randData.trainCV(folds, n);
  99.       Instances test = randData.testCV(folds, n);
  100.       // the above code is used by the StratifiedRemoveFolds filter, the
  101.       // code below by the Explorer/Experimenter:
  102.       // Instances train = randData.trainCV(folds, n, rand);

  103.       // build and evaluate classifier
  104.       Classifier clsCopy = AbstractClassifier.makeCopy(cls);
  105.       clsCopy.buildClassifier(train);
  106.       eval.evaluateModel(clsCopy, test);

  107.       // add predictions
  108.       AddClassification filter = new AddClassification();
  109.       filter.setClassifier(cls);
  110.       filter.setOutputClassification(true);
  111.       filter.setOutputDistribution(true);
  112.       filter.setOutputErrorFlag(true);
  113.       filter.setInputFormat(train);
  114.       Filter.useFilter(train, filter);  // trains the classifier
  115.       Instances pred = Filter.useFilter(test, filter);  // perform predictions on test set
  116.       if (predictedData == null)
  117.         predictedData = new Instances(pred, 0);
  118.       for (int j = 0; j < pred.numInstances(); j++)
  119.         predictedData.add(pred.instance(j));
  120.     }

  121.     // output evaluation
  122.     System.out.println();
  123.     System.out.println("=== Setup ===");
  124.     if (cls instanceof OptionHandler)
  125.       System.out.println("Classifier: " + cls.getClass().getName() + " " + Utils.joinOptions(((OptionHandler) cls).getOptions()));
  126.     else
  127.       System.out.println("Classifier: " + cls.getClass().getName());
  128.     System.out.println("Dataset: " + data.relationName());
  129.     System.out.println("Folds: " + folds);
  130.     System.out.println("Seed: " + seed);
  131.     System.out.println();
  132.     System.out.println(eval.toSummaryString("=== " + folds + "-fold Cross-validation ===", false));

  133.     // output "enriched" dataset
  134.     DataSink.write(Utils.getOption("o", args), predictedData);
  135.   }
  136. }
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-8 01:42