ulanBator
ulanBator

Reputation: 45

Trained SVM only ever outputs 1.0 as a result despite 0.0 training error

I am trying to classify a dataset! In this dataset the first column is the ideal outcome and the other 20 columns are the inputs.

The problem that arises here for me is that the SVM trained on the dataset (in this case 80% is used for training) shows a training error of 0.0 but it always predicts 1.0 as outcome.

I have divided the set into two parts one for training (80% of the data) and 20% for classification. The data is a concatenation of two short timeseries of RSI values (one 2 period and one 14 period).

Why does the SVM behave this way? And can I do something to avoid this? I thought 0.0 of training error would mean, that on the training set the SVM makes no more errors. This seems to be false judging from the results.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.encog.Encog;
import org.encog.ml.data.MLData;
import org.encog.ml.data.MLDataPair;
import org.encog.ml.data.MLDataSet;
import org.encog.ml.data.basic.BasicMLDataSet;
import org.encog.ml.svm.SVM;
import org.encog.ml.svm.training.SVMTrain;

public class SVMTest {

    public static void main(String[] args) {
        List<String> lines = readFile("/home/wens/mlDataSet.csv");
        double[][] trainingSetData = getInputData(lines, 0, lines.size()/10*8);
        double[][] trainingIdeal = getIdeal(lines, 0, lines.size()/10*8);
        MLDataSet trainingSet = new BasicMLDataSet(trainingSetData, trainingIdeal);
        double[][] classificationSetData = getInputData(lines, lines.size()/10*8, lines.size());
        double[][] classificationIdeal = getIdeal(lines, lines.size()/10*8, lines.size());
        MLDataSet classificationSet = new BasicMLDataSet(classificationSetData, classificationIdeal);

        SVM svm = new SVM(20,false);
        final SVMTrain train = new SVMTrain(svm, trainingSet);
        train.iteration();
        train.finishTraining();
        System.out.println("training error: " + train.getError());

        System.out.println("SVM Results:");
        for(MLDataPair pair: classificationSet ) {
            final MLData output = svm.compute(pair.getInput());
            System.out.println("actual: " + output.getData(0) + "\tideal=" + pair.getIdeal().getData(0));
        }

        Encog.getInstance().shutdown();
    }

    private static List<String> readFile(String filepath){
        List<String> res = new ArrayList<>();
        try {
            File f = new File(filepath);
            BufferedReader b = new BufferedReader(new FileReader(f));
            String readLine = "";
            while ((readLine = b.readLine()) != null) {
                res.add(readLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    private static double[][] getInputData(List<String> lines, int start, int end){
        double[][] res = new double[end-start][20];
        int cnt = 0;
        for(int i=start; i<end; i++){
            String[] tmp = lines.get(i).split("\t");
            for(int j=1; j<tmp.length; j++){
                res[cnt][j-1] = Double.parseDouble(tmp[j]);
            }
            cnt++;
        }
        return res;
    }

    private static double[][] getIdeal(List<String> lines, int start, int end){
        double[][] res = new double[end-start][1];
        int cnt = 0;
        for(int i=start; i<end; i++){
            String[] tmp = lines.get(i).split("\t");
            res[cnt][0] = Double.parseDouble(tmp[0]);
            cnt++;
        }
        return res;
    }
}

Upvotes: 1

Views: 97

Answers (0)

Related Questions