Machine learning on a 8 x 8 matrix - Java

Viewed 130

I am trying to implement a machine learning algorithm (k-nn for example).As it stands my Main class, which essentially builds 8×8-pixel matrices into an array to be manipulated later. (See the data description and sample dataset.) As it stands my arrays are printing as a like so:

, Class Code: 7 DataSet:[0, 0, 3, 9, 15, 8, 0, 0, 0, 1, 15, 16, 16, 7, 0, 0, 0, 0, 5, 16, 16, 10, 4, 0, 0, 0, 3, 16, 16, 16, 9, 0, 0, 0, 0, 15, 14, 4, 0, 0, 0, 0, 0, 13, 5, 0, 0, 0, 0, 0, 1, 15, 3, 0, 0, 0, 0, 0, 4, 13, 0, 0, 0, 0, 7]

Now for my starting point I'm looking to try to implement a very basic kNN algorithm as something to build from but I am having trouble manipulating the datasets that are being outputted. I have been reading up on Foundations of Machine Learning by M. Mohri but it hasn't been of any help. My Main class for building my data:

import java.util.*;
import java.io.*;

public class Main {

    static class Data {

        int[] dataSet;
        int classCode;

        public Data(int[] dataSet, int label) {
            this.dataSet = dataSet;
            this.classCode = label;
        }

        @Override
        public String toString() {
            return "Class Code: " + classCode + " DataSet:" + Arrays.toString(dataSet) + "\n";
        }

    }

    ArrayList<Data> dataSetList;
    int[][] dataArray = new int[2810][65];

    private void readFile(String csvFile) {
        int instances = 0;
        dataSetList = new ArrayList<>();
        try {
            Scanner scan = new Scanner(new BufferedReader(new FileReader(csvFile)));
            while (scan.hasNext()) {
                String line = scan.next();
                String[] extractedDataFromFile = line.split(",");
                for (int i = 0; i < extractedDataFromFile.length; i++) {
                    dataArray[instances][i] = Integer.parseInt(extractedDataFromFile[i]);
                }
                dataSetList.add(new Data(dataArray[instances], dataArray[instances][extractedDataFromFile.length - 1]));
                instances++;
            }
            System.out.println(dataSetList.toString());
        } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.readFile("dataset1.csv");
    }

}

This is my first time experimenting with machine learning so any feedback or approach to this would be hugely appreciated.

EDIT//
I'm looking at a basic kNN implementation as a starting point whether someone can redirect me to material on implementing on a similar data set or an example using my current provided code. I apologize if my initial post was a little vague

0 Answers
Related