FeatureSelectionClass.java Source code

Java tutorial

Introduction

Here is the source code for FeatureSelectionClass.java

Source

import weka.attributeSelection.AttributeSelection;
import weka.attributeSelection.ChiSquaredAttributeEval;
import weka.attributeSelection.GainRatioAttributeEval;
import weka.attributeSelection.InfoGainAttributeEval;
import weka.attributeSelection.Ranker;
import weka.core.Instances;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ferhat
 */
public class FeatureSelectionClass {
    public AttributeSelection withGainRatio(String path) throws Exception {
        int N;
        PreparingSteps pr = new PreparingSteps();
        N = pr.getReadFileData(path).numAttributes();
        Instances data = pr.getReadFileData(path);

        AttributeSelection selector = new AttributeSelection();
        InfoGainAttributeEval evaluator = new InfoGainAttributeEval();
        Ranker ranker = new Ranker();
        ranker.setNumToSelect(Math.min(500, N - 1));
        selector.setEvaluator(evaluator);
        selector.setSearch(ranker);
        selector.SelectAttributes(data);
        return selector;

    }

    public AttributeSelection withInfoGain(String path) throws Exception {
        int N;
        PreparingSteps pr = new PreparingSteps();
        N = pr.getReadFileData(path).numAttributes();
        Instances data = pr.getReadFileData(path);

        AttributeSelection selector = new AttributeSelection();
        GainRatioAttributeEval evaluator = new GainRatioAttributeEval();
        Ranker ranker = new Ranker();
        ranker.setNumToSelect(Math.min(500, N - 1));
        selector.setEvaluator(evaluator);
        selector.setSearch(ranker);
        selector.SelectAttributes(data);
        return selector;
    }

    public AttributeSelection withChiSquare(String path) throws Exception {
        int N;
        PreparingSteps pr = new PreparingSteps();
        N = pr.getReadFileData(path).numAttributes();
        Instances data = pr.getReadFileData(path);

        AttributeSelection selector = new AttributeSelection();
        ChiSquaredAttributeEval evaluator = new ChiSquaredAttributeEval();
        Ranker ranker = new Ranker();
        ranker.setNumToSelect(Math.min(500, N - 1));
        selector.setEvaluator(evaluator);
        selector.setSearch(ranker);
        selector.SelectAttributes(data);
        return selector;

    }
}