java programming & finding the mode of an array - java

i have a task where i need to find the mode of an array. which means i am looking for the int which is most frequent. i have kinda finished that, but the task also says if there are two modes which is the same, i should return the smallest int e.g {1,1,1,2,2,2} should give 1 (like in my file which i use that array and it gives 2)
public class theMode
{
public theMode()
{
int[] testingArray = new int[] {1,1,1,2,2,2,4};
int mode=findMode(testingArray);
System.out.println(mode);
}
public int findMode(int[] testingArray)
{
int modeWeAreLookingFor = 0;
int frequencyOfMode = 0;
for (int i = 0; i < testingArray.length; i++)
{
int currentIndexOfArray = testingArray[i];
int frequencyOfEachInArray = howMany(testingArray,currentIndexOfArray);
if (frequencyOfEachInArray > frequencyOfMode)
{
modeWeAreLookingFor = currentIndexOfArray;
frequencyOfMode = modeWeAreLookingFor;
}
}
return modeWeAreLookingFor;
}
public int howMany(int[] testingArray, int c)
{
int howManyOfThisInt=0;
for(int i=0; i < testingArray.length;i++)
{
if(testingArray[i]==c){
howManyOfThisInt++;
}
}
return howManyOfThisInt;
}
public static void main(String[] args)
{
new theMode();
}
}
as you see my algorithm returns the last found mode or how i should explain it.

I'd approach it differently. Using a map you could use each unique number as the key and then the count as the value. step through the array and for each number found, check the map to see if there is a key with that value. If one is found increment its value by 1, otherwise create a new entry with the value of 1.
Then you can check the value of each map entry to see which has the highest count. If the current key has a higher count than the previous key, then it is the "current" answer. But you have the possibility of keys with similar counts so you need to store each 'winnning' answer.
One way to approach this is to check each map each entry and remove each entry that is less than the current highest count. What you will be left with is a map of all "highest counts". If you map has only one entry, then it's key is the answer, otherwise you will need to compare the set of keys to determine the lowest.

Hint: You're updating ModeWeAreLookingFor when you find a integer with a strictly higher frequency. What if you find an integer that has the same frequency as ModeWeAreLookingFor ?
Extra exercice: In the first iteration of the main loop execution, you compute the frequency of '1'. On the second iteration (and the third, and the fourth), you re-compute this value. You may save some time if you store the result of the first computation. Could be done with a Map.
Java code convention states that method names and variable name should start with a lower case character. You would have a better syntax coloring and code easier to read if you follow this convention.

this might work with a little modification.
http://www.toves.org/books/java/ch19-array/index.html#fig2
if ((count > maxCount) || (count == maxCount && nums[i] < maxValue)) {
maxValue = nums[i];
maxCount = count;
}

since it seems there are no other way, i did a hashmap after all. i am stuck once again in the logics when it comes to comparing frequencys and and the same time picking lowest integer if equal frequencys.
public void theMode()
{
for (Integer number: intAndFrequencyMap.keySet())
{
int key = number;
int value = intAndFrequencyMap.get(number);
System.out.println("the integer: " +key + " exists " + value + " time(s).");
int lowestIntegerOfArray = 0;
int highestFrequencyOfArray = 0;
int theInteger = 0;
int theModeWanted = 0;
if (value > highestFrequencyOfArray)
{
highestFrequencyOfArray = value;
theInteger = number;
}
else if (value == highestFrequencyOfArray)
{
if (number < theInteger)
{
number = theInteger;
}
else if (number > theInteger)
{
}
else if (number == theInteger)
{
number = theInteger;
}
}
}
}

Completed:
import java.util.Arrays;
public class TheMode
{
//Probably not the most effective solution, but works without hashmap
//or any sorting algorithms
public TheMode()
{
int[] testingArray = new int[] {2,3,5,4,2,3,3,3};
int mode = findMode(testingArray);
System.out.println(Arrays.toString(testingArray));
System.out.println("The lowest mode is: " + mode);
int[] test2 = new int[] {3,3,2,2,1};
int mode2=findMode(test2);
System.out.println(Arrays.toString(test2));
System.out.println("The lowest mode is: " +mode2);
int[] test3 = new int[] {4,4,5,5,1};
int mode3 = findMode(test3);
System.out.println(Arrays.toString(test3));
System.out.println(The lowest mode is: " +mode3);
}
public int findMode(int[] testingArray)
{
int modeWeAreLookingFor = 0;
int frequencyOfMode = 0;
for (int i = 0; i < testingArray.length; i++)
{
int currentIndexOfArray = testingArray[i];
int countIntegerInArray = howMany(testingArray, currentIndexOfArray);
if (countIntegerInArray == frequencyOfMode)
{
if (modeWeAreLookingFor > currentIndexOfArray)
{
modeWeAreLookingFor = currentIndexOfArray;
}
}
else if (countIntegerInArray > frequencyOfMode)
{
modeWeAreLookingFor = currentIndexOfArray;
frequencyOfMode = countIntegerInArray;
}
}
return modeWeAreLookingFor;
}
public int howMany(int[] testingArray, int c)
{
int howManyOfThisInt=0;
for(int i=0; i < testingArray.length;i++)
{
if(testingArray[i]==c){
howManyOfThisInt++;
}
}
return howManyOfThisInt;
}
public static void main(String[] args)
{
new TheMode();
}
}

Glad you managed to solve it. As you will now see, there is more than one way to approach a problem. Here's what I meant by using a map
package util;
import java.util.HashMap;
import java.util.Map;
public class MathUtil {
public static void main(String[] args) {
MathUtil app = new MathUtil();
int[] numbers = {1, 1, 1, 2, 2, 2, 3, 4};
System.out.println(app.getMode(numbers));
}
public int getMode(int[] numbers) {
int mode = 0;
Map<Integer, Integer> numberMap = getFrequencyMap(numbers);
int highestCount = 0;
for (int number : numberMap.keySet()) {
int currentCount = numberMap.get(number);
if (currentCount > highestCount) {
highestCount = currentCount;
mode = number;
} else if (currentCount == highestCount && number < mode) {
mode = number;
}
}
return mode;
}
private Map<Integer,Integer> getFrequencyMap(int[] numbers){
Map<Integer, Integer> numberMap = new HashMap<Integer, Integer>();
for (int number : numbers) {
if (numberMap.containsKey(number)) {
int count = numberMap.get(number);
count++;
numberMap.put(number, count);
} else {
numberMap.put(number, 1);
}
}
return numberMap;
}
}

Related

Coin change recursive implementation memoizing wrong values. Could someone help debug?

Could someone help me figure out why this version of the memoized coin change doesn't work?
This is to determine the minimum number of coins to make change for a target amount.
I realize that the cache is putting in the wrong values and without using the memo cache this gives the right answer. I was also able to get a memoized version to work by not passing in the currNumCoins as an argument to the recursive calls. I'm just stumped to why this version doesn't work.
I'm initializing memo as Map<Integer, Integer> memo = new HashMap<>();
Example input: coins = [1,2,5], targetAmount = 11
Expected Answer : 3
Actual Answer: 7
class Solution {
Map<Integer, Integer> memo = new HashMap<>();
public int coinChange(int[] coins, int amount) {
return coinChangeRecHelper(coins, amount, amount, 0);
}
public int coinChangeRecHelper(int[] coins, int amount, int currAmount, int currNumCoins) {
if (currAmount < 0) {
return -1;
}
if (currAmount == 0) {
//return 0;
return currNumCoins;
}
if (memo.containsKey(currAmount)) {
return memo.get(currAmount);
}
int minCoins = Integer.MAX_VALUE;
for (int i = 0; i < coins.length; i++) {
int currCoin = coins[i];
int numCoinsTmp = coinChangeRecHelper(coins, amount, currAmount - currCoin, currNumCoins + 1);
if (numCoinsTmp != -1) {
minCoins = Math.min(minCoins, numCoinsTmp);
}
}
if (minCoins == Integer.MAX_VALUE) {
minCoins = -1;
}
memo.put(currAmount, minCoins);
return minCoins;
}
}
You need a separate memo for each recursion so one does not change the other.
For example memorizing which coins where used can be achieved like so:
import java.util.HashMap;
import java.util.Map;
public class Solution {
private Map<Integer, Integer> memo;
public int coinChange(int[] coins, int amount) {
memo = new HashMap<>();
return coinChangeRecHelper(coins, amount, amount, 0, new HashMap<Integer,Integer>());
}
public int coinChangeRecHelper(int[] coins, int amount, int currAmount, int currNumCoins, Map<Integer, Integer> coinQty ) {
if (currAmount < 0) return -1;
if (currAmount == 0) {
memo = coinQty;
return currNumCoins;
}
int minCoins = Integer.MAX_VALUE;
for (int currCoin : coins) {
Map<Integer, Integer> coinQtyCopy = new HashMap<>(coinQty);
coinQtyCopy.putIfAbsent(currCoin, 0);
coinQtyCopy.put(currCoin, coinQtyCopy.get(currCoin)+1);
int numCoinsTmp = coinChangeRecHelper(coins, amount, currAmount - currCoin, currNumCoins + 1, coinQtyCopy);
if (numCoinsTmp != -1) {
minCoins = Math.min(minCoins, numCoinsTmp);
}
}
if (minCoins == Integer.MAX_VALUE) {
minCoins = -1;
}
return minCoins;
}
public Map<Integer, Integer> getMemo() {
return memo;
}
public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.coinChange(new int[]{1,2,5}, 11) + " coins used: ");
for(int coin : s.getMemo().keySet()) {
System.out.println( s.getMemo().get(coin)+ " of " + coin);
}
}
}
The return-value of coinChangeRecHelper depends on three of its arguments (coins, currAmount, and currNumCoins), but the memo cache is keyed by only one of those values (currAmount), which inherently means that it can't accurately cache the return-value. In other words, the code implicitly assumes that coinChangeRecHelper(coins1, amount1, currAmount, currNumCoins1) == coinChangeRecHelper(coins2, amount2, currAmount, currNumCoins2), but that's a bad assumption.
I was also able to get a memoized version to work by not passing in the currNumCoins as an argument to the recursive calls.
Yes, that approach would mostly fix this issue, by eliminating a mismatched parameter that the caching doesn't account for.
The only remaining issue is coins; if your coinChange method is called twice with different sets of coins, it will erroneously retain the old cache even though it's not applicable to the new call. To fix that, I'd recommend having coinChange create the cache and pass it as an argument to coinChangeRecHelper, rather than using an instance variable.

How to find the position of the highest and second highest value in an array in java

Let's assume that we have an array with integer numbers. For Example
perimeter={180,50,65,78,90,5,150,2,35} and i want to find the highest value and its position in the array and the second value and its position in the array.
public class FindTopTwo {
public static void main(String[] args) {
double perimeter[]= {180,50,65,78,90,5,150,2,35};
double megisto, megisto2;
int i;
megisto=perimeter[0];
megisto2=perimeter[0];
for (i=0;i<perimeter.length;i++) {
if (perimeter[i]>megisto) {
megisto2=megisto;
megisto=perimeter[i];}
else if (perimeter[i]>megisto2) {
megisto2=perimeter[i];
}
}
System.out.println("the first is:"+megisto+"the second highest value is "+megisto2);
}
}
Just keep track of their indexes(i) as their values change:
public static void main(String[] args) {
double perimeter[]= {180,50,65,78,90,5,150,2,35};
double megisto, megisto2;
megisto=perimeter[0];
int megistoIndex = 0;
megisto2=perimeter[0];
int megisto2Index = 0;
for (int i=0;i<perimeter.length;i++) {
if (perimeter[i]>megisto) {
megistoIndex = i;
megisto = perimeter[i];
} else if (perimeter[i] < megisto && perimeter[i] > megisto2) {
megisto2Index = i;
megisto2 = perimeter[i];
}
}
System.out.println(String.format("the first is: %d at index %d. The second highest value is %d at index %d", megisto, megistoIndex, megisto2, megisto2Index));
}
set highestIndex = -1;
set nextHighestIndex = -1;
for every element of perimiter:
if highestIndex = -1:
set highestIndex = i;
else if perimiter[i] > perimiter[highestIndex]
set nextHighestIndex = highestIndex;
set highestIndex = i;
else if perimiter[i] > perimieter[nextHighestIndex]
set nextHighestIndex = i;
If the array is not empty, you will find the highest value in
perimiter[highestIndex]
If the array has at least two distinct values, you will find the second highest value in
perimeter[nextHighestIndex]

How do i compare the values of objects in an object array?

My code outputs this:
Computer 1 bets 5
Computer 2 bets 8
Computer 3 bets 4
Computer 4 bets 3
Computer 5 bets 8
I want to make a method "displayWinners()" that compares the bet value of all the objects in this array and returns all of the object id's with the highest bet value, in this case it would be Computer 2 and 5 with a bet of 8. How would i do that?
public class Computer {
Computer[] c;
private int id;
private int bet;
public void create(int numComps) {
int i;
c = new Computer[numComps];
for (i = 0; i < numComps; i++) {
c[i] = new Computer();
c[i].id = i+1;
c[i].bet = bet();
c[i].display();
}
displayWinners();
}
public int bet() {
return (int) (Math.random() * 10) + 1;
}
public void display() {
String name = "Computer " + id;
System.out.println(name + " bets " + bet);
}
public void displayWinners() {
System.out.println();
}
public static void main(String[] args) {
Computer c = new Computer();
c.create(5);
}
}
why don't you allocate a variable for an index of the maximum value and a value itself, and keep checking & rewriting the variable as function bet() is executed.
the code below is not properly verified but just have a look over.
public class Computer {
Computer[] c;
private int id;
private int bet;
private List<Integer> maxId;
private int maxBet;
public void create(int numComps) {
int i;
c = new Computer[numComps];
maxId = new ArrayList<Integer>();
maxBet = 0;
for (i = 0; i < numComps; i++) {
c[i] = new Computer();
c[i].id = i+1;
c[i].bet = bet();
c[i].display();
if(c[i].bet > maxBet) {
maxId = new ArrayList<Integer>();
maxId.add(c[i].id);
maxBet = c[i].bet;
}
else if(c[i].bet == maxBet) {
maxId.add(c[i].id);
}
}
displayWinners();
}
public int bet() {
return (int) (Math.random() * 10) + 1;
}
public void display() {
String name = "Computer " + id;
System.out.println(name + " bets " + bet);
}
public void displayWinners() {
System.out.format("Computer %d", maxId.get(0));
if(maxId.size() > 1) {
for(int i=1; i<maxId.size(); i++) {
System.out.format(" and %d", maxId.get(i));
}
}
System.out.format(" with a bet of %d\n", maxBet);
}
public static void main(String[] args) {
Computer c = new Computer();
c.create(5);
}
}
Ok, just for the fun to provide this with a small trick.
You just iterate your array.
Then, I will compare the current bet with the maximum value known using Integer.compare and base on the result, I will either:
r > 0 - clear the list and add the instance to the list
r == 0 - Add the instance to the list
r < 0 - Do nothing
Good to know, the actual value return by the method are simpler [-1, 0, 1], it will matter shortly.
Now, we can see some redondance in adding into the list, to reduce that, we are able to use a switch instead of ifs.
List<Computer> winners = new ArrayList<Computer>();
for ( Computer c : computers ){
int r = Integer.compare(c.getBet(), maxBet);
switch(r){
case 1: //Current bet is higher
maxIds.clear();
maxBet = c.getBet();
case 0: //Current bet is equals
winners.add(c);
case -1: //Current bet is lower (optional line, just for the explanation)
}
}
Since I don't use break, after we clear the list, we will add into it.
Note: A fail-safe should be added in case of Integer.compare implementation changes. The documentation state that it will return any value instead of -1, 0 or 1. But the current implementation is simpler :
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
First of all, I recommend you to split storage and business logic. Move all methods for show Computer class out.
So to find the winners you have to do following steps:
Create a list of computers (you can create an array or a list);
Iterate over the collection once to find the highest bet;
Iterate over the collection second time to filter out all computers with the highest bet.
If you ask such question, then I suppose, that no need of you right now to offer different sorting algorithms or special data structures (they all do the same, but much faster for big input data).
This is one of the possible solutions (I tried to keep it simple):
public class Foo {
public static void main(String[] args) throws IOException {
Computer[] computers = createComputer(5);
int highestBet = getHighestBet(computers);
List<Integer> ids = getIdsWithBet(computers, highestBet);
System.out.println(ids.stream().map(String::valueOf).collect(Collectors.joining(",")));
}
private static int getHighestBet(Computer... computers) {
int max = 0;
for (Computer computer : computers)
max = Math.max(max, computer.getBet());
return max;
}
private static List<Integer> getIdsWithBet(Computer[] computers, int bet) {
List<Integer> ids = new ArrayList<>(computers.length);
for (Computer computer : computers)
if (computer.getBet() == bet)
ids.add(computer.getId());
return ids;
}
// you van use List<Computer>
private static Computer[] createComputer(int total) {
Random random = new Random();
Computer[] computers = new Computer[total];
for (int i = 0; i < computers.length; i++) {
computers[i] = new Computer(i + 1, random.nextInt(10) + 1);
System.out.println(computers[i]);
}
return computers;
}
}
// It's better to use unmodifiable objects if you can
final class Computer {
private final int id;
private final int bet;
public Computer(int id, int bet) {
this.id = id;
this.bet = bet;
}
public int getId() {
return id;
}
public int getBet() {
return bet;
}
#Override
public String toString() {
return "Computer " + id + " bets " + bet;
}
}
import java.util.ArrayList at top of file
```
int maxBet = 0;
ArrayList<Integer> maxIds = new ArrayList<Integer>();
// This is a for each loop, it is saying, for each computer in the list of computers
for ( Computer computer : computers ){
/* if that computer has a higher bet than what we previously thought
was the highest, empty our list and set maxBet to that, and add this
computer to the list of computers that have that bet number */
if (computer.getBet() > maxBet){
maxIds.clear();
maxBet = computer.getBet();
maxIds.add(computer.getId());
// another computer has the same max bet value
} else if ( computer.getBet() == maxBet){
maxIds.add(computer.getId());
}
}
System.out.println("Max Bet: " + maxBet);
System.out.print("Computers with max bet: ");
// for each id in our list of computers that have the max bet
for ( int id : maxIds ){
System.out.print(id + " ");
}

Java: Create unique List from an existing List keeping index numbers constant

I have two ArrayLists.
List of dates
List of respective data.
Both are synchronized. I sometimes have more than one data on a same date. I need to create two lists: unique dates and the data (averaged) respectively. So far, I have tried the following methods
int i = 1;
for(int it =0; it < predatetime.size() - 1; it++){
//Compare each element with the next one
if(predatetime.get(it+1) == predatetime.get(it)){
i++;
weight = preweight.get(it+1) + weight;
//If equal, add weights and increment a divisor for averaging
}
else { //if not equal, add to the new lists
if(it == predatetime.size() - 2){ //if the last element is not equal to its previous one, just add it to the list
newDateTime.add(predatetime.get(it+1));
newWeight.add(preweight.get(it+1));
break;
}
weight = weight / i;
newDateTime.add(predatetime.get(it));
newWeight.add(weight);
weight = preweight.get(it+1); //re-initialize variables
i = 1;
}
if(it == predatetime.size() - 2){
weight = weight / i;
newDateTime.add(predatetime.get(it));
newWeight.add(weight);
}
}
There are a lot of problems with this code.
If the list has only one element, it fails. (I know I can write 2 more lines to care of this). Is there a better way to do this?
I know there are similar questions on this website, but still I'm unable to resolve the problem.
This is the full solution
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
public class CustomList {
public static void main(String[] args) {
ArrayList<String> date = new ArrayList<>();
date.add("1");
date.add("2");
date.add("2");
date.add("3");
System.out.println(date);
ArrayList<Integer> value = new ArrayList<>();
value.add(1);
value.add(2);
value.add(4);
value.add(3);
System.out.println(value);
new MyCls().createList(date, value);
}
}
class MyCls {
ArrayList uniqueDate = new ArrayList<String>();
ArrayList averageValue = new ArrayList<Integer>();
LinkedHashMap store = new LinkedHashMap<String, CountEntry>();
class CountEntry {
int value;
int count;
CountEntry() {
}
CountEntry(int v, int c) {
value = v;
count = c;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
public void createList(ArrayList<String> date, ArrayList<Integer> value) {
for (int i = 0; i < date.size(); i++) {
CountEntry tmp = (CountEntry) store.get(date.get(i));
if (tmp == null) {
store.put(date.get(i), new CountEntry(value.get(i), 1));
} else {
int tmpVal = tmp.getValue();
int tmpCount = tmp.getCount();
store.put(date.get(i), new CountEntry(value.get(i) + tmpVal, ++tmpCount));
}
}
ArrayList<String> uniqueDate = new ArrayList<String>(store.keySet());
ArrayList<CountEntry> tempAvgList = new ArrayList<CountEntry>(store.values());
for (CountEntry ce : tempAvgList) {
averageValue.add(ce.getValue() / ce.getCount());
}
System.out.println("Output");
System.out.println(uniqueDate);
System.out.println(averageValue);
}
}
/*
OUTPUT Snap:
[1, 2, 2, 3]
[1, 2, 4, 3]
Output
[1, 2, 3]
[1, 3, 3]
*/
If you try to make your list elements unique why you not try to convert the list to set collection
Set<Foo> foo = new HashSet<Foo>(myList);
Why not create a Map instead with the dates as the key and have the value as a list. This will allow you to keep the dates unique, at the same allow you to have your data as a list.
Map<String, ArrayList<myData>> myMap = new HashMap<String, ArrayList<myData>>();
Then you can just find if your key exists, if it does add it to the array list by using the key to identify the correct list. If it doesnt exist it, add it to the map
Thanks to #Rambler and #JulianGurung, I created a HashMap and it works
HashMap<Integer, Float> hm = new HashMap<Integer,Float>();
int occurance = 0;
float weight = 0;
hm.put(predatetime.get(0), 0f); //initialize with the first value
for(Map.Entry m : hm.entrySet()){
for( int it = 0; it < predatetime.size(); it++){
if(m.getKey() == predatetime.get(it)){
weight = (Float) m.getValue() + preweight.get(it); //Sum all the same data in order to avg later
hm.put(predatetime.get(it), weight);
occurance++;
}
else{ //if it is not equal, add the new element to the map
hm.put(predatetime.get(it), preweight.get(it));
}
}
weight = weight / occurance;
hm.put((Integer) m.getKey(), weight);
weight = 0;
occurance = 0;
}

Altering the value of k in kNN algorithm - Java

I have applied the KNN algorithm for classifying handwritten digits. the digits are in vector format initially 8*8, and stretched to form a vector 1*64..
As it stands my code applies the kNN algorithm but only using k = 1. I'm not entirely sure how to alter the value k after attempting a couple of things I kept getting thrown errors. If anyone could help push me in the right direction it would be really appreciated. The training dataset can be found here and the validation set here.
ImageMatrix.java
import java.util.*;
public class ImageMatrix {
private int[] data;
private int classCode;
private int curData;
public ImageMatrix(int[] data, int classCode) {
assert data.length == 64; //maximum array length of 64
this.data = data;
this.classCode = classCode;
}
public String toString() {
return "Class Code: " + classCode + " Data :" + Arrays.toString(data) + "\n"; //outputs readable
}
public int[] getData() {
return data;
}
public int getClassCode() {
return classCode;
}
public int getCurData() {
return curData;
}
}
ImageMatrixDB.java
import java.util.*;
import java.io.*;
import java.util.ArrayList;
public class ImageMatrixDB implements Iterable<ImageMatrix> {
private List<ImageMatrix> list = new ArrayList<ImageMatrix>();
public ImageMatrixDB load(String f) throws IOException {
try (
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr)) {
String line = null;
while((line = br.readLine()) != null) {
int lastComma = line.lastIndexOf(',');
int classCode = Integer.parseInt(line.substring(1 + lastComma));
int[] data = Arrays.stream(line.substring(0, lastComma).split(","))
.mapToInt(Integer::parseInt)
.toArray();
ImageMatrix matrix = new ImageMatrix(data, classCode); // Classcode->100% when 0 -> 0% when 1 - 9..
list.add(matrix);
}
}
return this;
}
public void printResults(){ //output results
for(ImageMatrix matrix: list){
System.out.println(matrix);
}
}
public Iterator<ImageMatrix> iterator() {
return this.list.iterator();
}
/// kNN implementation ///
public static int distance(int[] a, int[] b) {
int sum = 0;
for(int i = 0; i < a.length; i++) {
sum += (a[i] - b[i]) * (a[i] - b[i]);
}
return (int)Math.sqrt(sum);
}
public static int classify(ImageMatrixDB trainingSet, int[] curData) {
int label = 0, bestDistance = Integer.MAX_VALUE;
for(ImageMatrix matrix: trainingSet) {
int dist = distance(matrix.getData(), curData);
if(dist < bestDistance) {
bestDistance = dist;
label = matrix.getClassCode();
}
}
return label;
}
public int size() {
return list.size(); //returns size of the list
}
public static void main(String[] argv) throws IOException {
ImageMatrixDB trainingSet = new ImageMatrixDB();
ImageMatrixDB validationSet = new ImageMatrixDB();
trainingSet.load("cw2DataSet1.csv");
validationSet.load("cw2DataSet2.csv");
int numCorrect = 0;
for(ImageMatrix matrix:validationSet) {
if(classify(trainingSet, matrix.getData()) == matrix.getClassCode()) numCorrect++;
} //285 correct
System.out.println("Accuracy: " + (double)numCorrect / validationSet.size() * 100 + "%");
System.out.println();
}
In the for loop of classify you are trying to find the training example that is closest to a test point. You need to switch that with a code that finds K of the training points that is the closest to the test data. Then you should call getClassCode for each of those K points and find the majority(i.e. the most frequent) of the class codes among them. classify will then return the major class code you found.
You may break the ties (i.e. having 2+ most frequent class codes assigned to equal number of training data) in any way that suits your need.
I am really inexperienced in Java, but just by looking around the language reference, I came up with the implementation below.
public static int classify(ImageMatrixDB trainingSet, int[] curData, int k) {
int label = 0, bestDistance = Integer.MAX_VALUE;
int[][] distances = new int[trainingSet.size()][2];
int i=0;
// Place distances in an array to be sorted
for(ImageMatrix matrix: trainingSet) {
distances[i][0] = distance(matrix.getData(), curData);
distances[i][1] = matrix.getClassCode();
i++;
}
Arrays.sort(distances, (int[] lhs, int[] rhs) -> lhs[0]-rhs[0]);
// Find frequencies of each class code
i = 0;
Map<Integer,Integer> majorityMap;
majorityMap = new HashMap<Integer,Integer>();
while(i < k) {
if( majorityMap.containsKey( distances[i][1] ) ) {
int currentValue = majorityMap.get(distances[i][1]);
majorityMap.put(distances[i][1], currentValue + 1);
}
else {
majorityMap.put(distances[i][1], 1);
}
++i;
}
// Find the class code with the highest frequency
int maxVal = -1;
for (Entry<Integer, Integer> entry: majorityMap.entrySet()) {
int entryVal = entry.getValue();
if(entryVal > maxVal) {
maxVal = entryVal;
label = entry.getKey();
}
}
return label;
}
All you need to do is adding K as a parameter. Keep in mind, however, that the code above does not handle ties in a particular way.

Categories