PreparingSteps.java Source code

Java tutorial

Introduction

Here is the source code for PreparingSteps.java

Source

import java.util.logging.Level;
import java.util.logging.Logger;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.core.Debug;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils;

/*
 * 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 PreparingSteps {

    public Instances getReadFileData(String path) {
        ConverterUtils.DataSource source;
        Instances data = null;

        try {
            source = new ConverterUtils.DataSource(path);
            data = source.getDataSet();
            data.setClassIndex(data.numAttributes() - 1); // class indexi belirleniyor

        } catch (Exception ex) {
            Logger.getLogger(PreparingSteps.class.getName()).log(Level.SEVERE, null, ex);
        }

        return data;
    }

    public double[][] createFoodSources(int attrNum, double[][] food_sources) {
        food_sources = new double[attrNum - 1][attrNum];

        for (int i = 0; i < food_sources.length; i++)
            food_sources[i][i] = 1;

        return food_sources;
    }

    public double getSourceFitnessValue(int foldnumber, int N, Debug.Random rand, Instances data,
            double[] food_source, Evaluation eval, Classifier classifier) {
        double fitness = 0;
        int girildi = 0;
        Instances data1 = data;

        for (int j = 0; j < N - 1; j++) {
            if (food_source[j] == 0) {
                data1.deleteAttributeAt(j - girildi);
                girildi += 1;
            }

        }

        try {
            eval.crossValidateModel(classifier, data1, foldnumber, rand);
            fitness = eval.weightedFMeasure();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return fitness;
    }

    public void calculateFitnessValueProbabilities() {

    }

    public Instances deleteattributefromData(Instances data, double[] food_source) {
        int girildi = 0;
        int N = data.numAttributes();

        for (int j = 0; j < food_source.length; j++) {
            if (food_source[j] == 0) {
                data.deleteAttributeAt(j - girildi);
                girildi += 1;
            }
        }
        return data;
    }
}