Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Like this? Share it with your network


saihw1_weka_tutorial.pptx - Machine Discovery and Social Network ...

Uploaded on


  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Weka tutorial
  • 2. What is Weka?
    A collection of machine learningalgorithms for data mining tasks
    Weka contains tools for
    data pre-processing,
    classification, regression,
    association rules, and
    Suggestion: Version 3.5.8
  • 3. What can it help in your hw1?
    Data analysis
    Easy to try different classifiers
    If you want to get better performance, you still have to implement many things ,such as cross validation, parameters selection , and clustering .
    P.S. You are free to use anything to complete the homework.
  • 4. Explorer
  • 5. Classifier
    Black : build in
    Red: supported but need to download by user
    Installation guide for libsvm :
  • 6. Use Weka in your Java code
    The most common components you might want to use, are
    Instances - your data
    Filter - for pre-processing the data
    Classifier/Clusterer - is built on the processed data
    Evaluating - how good is the classifier/clusterer?
    Attribute selection- removing irrelevant attributes from your data
  • 7. Arff format
    @relation KDDCUP
    @attribute Ground-Truth {-1.0,1.0}
    @attribute Image-Finding-ID numeric
    @attribute Study-Finding-ID numeric
    @attribute Image-ID numeric
    @attribute Study-ID numeric
    @attribute LeftBreast {0.0,1.0}
    @attribute MLO {0.0,1.0}
    @attribute X-location numeric
    @attribute Y-location numeric
    @attribute X-nipple-location numeric
    @attribute Y-nipple-location numeric
    @attribute att1 numeric
    @attribute att2 numeric

    @attribute att117 numeric
    @attribute serialNumber numeric
  • 8. Instances
    import weka.core.Instances;
    import java.io.BufferedReader;
    import java.io.FileReader;
    Instances data = new Instances( new BufferedReader( new FileReader("/some/where/data.arff")));
    // setting class attribute
    data.setClassIndex(data.numAttributes() - 1);
    // The class index indicate the target attribute used for classification.
  • 9. filters
    import weka.core.Instances;
    import weka.filters.Filter;
    import weka.filters.unsupervised.attribute.Remove;
    String[] options = new String[2];
    options[0] = "-R"; // "range"
    options[1] = "1"; // first attribute
    Remove remove = new Remove(); // new instance of filter remove.setOptions(options); // set options remove.setInputFormat(data); // inform filter about dataset AFTERsetting options
    Instances newData = Filter.useFilter(data, remove); // apply filter
  • 10. classifier
    import weka.classifiers.functions.LibSVM;
    String[] options = String[] options = weka.core.Utils.splitOptions("-S 0 -K 2 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 40.0 -C 1.0 -E 0.0010 -P 0.1 -B");
    LibSVM classifier = new LibSVM(); // new instance of tree classifier.setOptions(options); // set the options classifier.buildClassifier(data); // build classifier
  • 11. Classifying instances
    Instances unlabeled=…//load from somewhere

    for (inti = 0; i < unlabeled.numInstances(); i++) {
    Instance ins=unlabeled.instance(i);
    clsLabel = classifier.classifyInstance(ins); //get predict label
    double[] prob_array=classifier.distributionForInstance(ins);
    //get probability for each category
  • 12. Example:weka+libsvm+5 folds CV
    public static void main(String[] args) throws Exception {
    PrintWriterpw_score=new PrintWriter( new FileOutputStream ("c:empcore.txt"));
    PrintWriterpw_label=new PrintWriter(new FileOutputStream ("c:empabel.txt"));
    PrintWriterpw_pid=new PrintWriter(new FileOutputStream ("c:empid.txt"));
    Instances data = new Instances(
    new BufferedReader(
    new FileReader("C:emprainSet_sn.arff")));
    Remove remove = new Remove(); // new instance of filter
    remove.setOptions(weka.core.Utils.splitOptions("-R 2-11,129"));// set options
    remove.setInputFormat(data); // inform filter about dataset AFTER setting options
    Int seed = 2; // the seed for randomizing the data
    int folds = 5; // the number of folds to generate, >=2
    data.setClassIndex(0); // first attribute is groundtruth
    Instances randData;
    Random rand = new Random(seed); // create seeded number generator
    randData = new Instances(data); // create copy of original data
    randData.randomize(rand); // randomize data with number generator
  • 13. for(int n=0;n<folds;n++){
    Instances train = randData.trainCV(folds, n);
    Instances test = randData.testCV(folds, n);
    System.out.println("Fold "+n+"train "+train.numInstances()+"test "+test.numInstances());
    String[] options = weka.core.Utils.splitOptions("-S 0 -K 2 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 40.0 -C 1.0 -E 0.0010 -P 0.1 -B");
    LibSVM classifier=new LibSVM();
    FilteredClassifierfc = new FilteredClassifier();
    double[] tmp=(double[])fc.distributionForInstance(test.instance(i));
    //tmp[0] :prob of negtive
    //tmp[1] :prob of positive
    pw_label.println(test.instance(i).attribute(0).value((int)test.instance(i).value(0))); //ground truth
    pw_score.println(tmp[1]); //predict value
    pw_pid.println((int)test.instance(i).value(4)); //study-ID
  • 14. FROC
    Load “predicted score”, “ground truth”, and “patient id”.
    Initialize :
    “Detected_patients= [ ]
    Sorting rows
    ( priority “predicted score” > “ground truth” > “patient id” in descending order).
    For each row,
    If ground truth is negative, x+=1
    Else // get a positive point
    If patient is not in “Detected_patients, //get a new positive patient
    y+=1 and add patient_id to Detected_patients
    else //patient is found before
    do nothing
    x => 0~ average false alarm per image i.e. X is divided by total image numbers
    y => 0~1 i.e. Y is divided by patients numbers
    Calculate the area under the curve
  • 15. FROC tools-JAVA
    java -cp bin mslab.kddcup2008.roc.ROC score.txt label.txt pid.txt
    score.txt : predict label for each point . i.e. probability for being positive
    label.txt : ground truth for each point
    pid.txt : patient ID for each point
  • 16. FROC tools-Matlab
    Matlabmatlab function
    [Pd_patient_wise,FA_per_image,AUC] = get_ROC_KDD(p,Y,PID,fa_low,fa_high)
    The y location of each point on the curve.
    The x location of each point on the curve.
    p – Predicted label
    Y – Ground truth
    PID – Patient ID
  • 17. FROC curve example
  • 18. The result of above example:
    AUC = 0.0782
    Measurements by Points:
    TP = 237
    FN = 386
    FP = 108
    TN = 101563
    precision = 0.6870
    recall = 0.3804
    FScore = 0.4897
  • 19. Reference:
    Use weka in your java code
    Generating cross-validation folds
    Example code
    Java roc code
    matlab roc code