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
Related
I'm building (or learning how to) a sports REST API using Spring Boot, Java, and MySQL. I'm building a method that currently takes each match from a collection of matches and returns an ArrayList of TeamStandings for the full list of matches.
Here is the method:
public List<TeamStanding> createStandingsTable(Match[] matches){
List<TeamStanding> teamStandings = new ArrayList<TeamStanding>();
for(int i = 0;i < matches.length; i++) {
TeamStanding firstTeam = new TeamStanding();
TeamStanding secondTeam = new TeamStanding();
//set team ids
firstTeam.setIdTeam(matches[i].getWcmHome());
secondTeam.setIdTeam(matches[i].getWcmAway());
//first team stats
firstTeam.setTeamPlayed((long) 1);
firstTeam.setTeamGoalsFavor(matches[i].getWcmHomeGoals());
firstTeam.setTeamGoalsAgainst(matches[i].getWcmAwayGoals());
firstTeam.setTeamGoalDif(firstTeam.getTeamGoalsFavor() - firstTeam.getTeamGoalsAgainst());
//second team stats
secondTeam.setTeamPlayed((long) 1);
secondTeam.setTeamGoalsFavor(matches[i].getWcmAwayGoals());
secondTeam.setTeamGoalsAgainst(matches[i].getWcmHomeGoals());
secondTeam.setTeamGoalDif(secondTeam.getTeamGoalsFavor() - secondTeam.getTeamGoalsAgainst());
//combined team stats
if(firstTeam.getTeamGoalsFavor() > secondTeam.getTeamGoalsFavor()) {
firstTeam.setTeamWins((long) 1);
firstTeam.setTeamLoses((long) 0);
firstTeam.setTeamDraws((long) 0);
firstTeam.setTeamPoints((long) 3);
secondTeam.setTeamWins((long) 0);
secondTeam.setTeamLoses((long) 1);
secondTeam.setTeamDraws((long) 0);
secondTeam.setTeamPoints((long) 0);
} else if (firstTeam.getTeamGoalsFavor() == secondTeam.getTeamGoalsFavor()) {
firstTeam.setTeamWins((long) 0);
firstTeam.setTeamLoses((long) 0);
firstTeam.setTeamDraws((long) 1);
firstTeam.setTeamPoints((long) 1);
secondTeam.setTeamWins((long) 0);
secondTeam.setTeamLoses((long) 0);
secondTeam.setTeamDraws((long) 1);
secondTeam.setTeamPoints((long) 1);
} else {
firstTeam.setTeamWins((long) 0);
firstTeam.setTeamLoses((long) 1);
firstTeam.setTeamDraws((long) 0);
firstTeam.setTeamPoints((long) 0);
secondTeam.setTeamWins((long) 1);
secondTeam.setTeamLoses((long) 0);
secondTeam.setTeamDraws((long) 0);
secondTeam.setTeamPoints((long) 3);
}
teamStandings.add(firstTeam);
teamStandings.add(secondTeam);
}
return teamStandings;
}
And the result is something like this:
[
{
"idTeam": 7,
"teamPoints": 3,
"teamPlayed": 1,
"teamWins": 1,
"teamDraws": 0,
"teamLoses": 0,
"teamGoalsFavor": 4,
"teamGoalsAgainst": 1,
"teamGoalDif": 3
},
{
"idTeam": 13,
"teamPoints": 0,
"teamPlayed": 1,
"teamWins": 0,
"teamDraws": 0,
"teamLoses": 1,
"teamGoalsFavor": 1,
"teamGoalsAgainst": 4,
"teamGoalDif": -3
},
{
"idTeam": 4,
"teamPoints": 3,
"teamPlayed": 1,
"teamWins": 1,
"teamDraws": 0,
"teamLoses": 0,
"teamGoalsFavor": 1,
"teamGoalsAgainst": 0,
"teamGoalDif": 1
},
{
"idTeam": 7,
"teamPoints": 0,
"teamPlayed": 1,
"teamWins": 0,
"teamDraws": 0,
"teamLoses": 1,
"teamGoalsFavor": 0,
"teamGoalsAgainst": 1,
"teamGoalDif": -1
}
]
My question is how can I merge these objects based on the idTeam? The result I'm trying to achieve would be to have all the rest of the properties added up while the idTeam remains the same. In the given example the expected one would be:
[
{
"idTeam": 7,
"teamPoints": 3,
"teamPlayed": 2,
"teamWins": 1,
"teamDraws": 0,
"teamLoses": 1,
"teamGoalsFavor": 4,
"teamGoalsAgainst": 2,
"teamGoalDif": 2
},
{
"idTeam": 13,
"teamPoints": 0,
"teamPlayed": 1,
"teamWins": 0,
"teamDraws": 0,
"teamLoses": 1,
"teamGoalsFavor": 1,
"teamGoalsAgainst": 4,
"teamGoalDif": -3
},
{
"idTeam": 4,
"teamPoints": 3,
"teamPlayed": 1,
"teamWins": 1,
"teamDraws": 0,
"teamLoses": 0,
"teamGoalsFavor": 1,
"teamGoalsAgainst": 0,
"teamGoalDif": 1
}
]
Also just a detail, I built the ArrayList of TeamStandings first and now I'm trying to merge them but perhaps I should be stacking them as a loop through the array of Matches, within the same method above but I'm not sure.
Iterate through the list of TeamStanding, mind the team ID and perform the additions. You might want to use the Map to save the pair of team ID as a key and the team itself as a value for easier manipulation. Here is the snipped (I haven't tested it, so you might need to amend it a bit).
List<TeamStanding> list = createStandingsTable(matches);
Map<Integer, TeamStanding> map = new HashMap<>();
for (TeamStanding team: list) {
int id = team.getIdTeam();
if (map.containsKey(id)) {
TeamStanding other = map.get(id);
other.setTeamPoints(team.getTeamPoints());
other.setTeamPlayed(team.getTeamPlayed());
// and so on...
} else {
map.put(id, team);
}
}
List<TeamStanding> merged = new ArrayList<>(map.values());
If you want to create the merged List<TeamStanding> directly from Match[], then you have to use the same idea, however, this might be a bit complicated to combine both of the iterations together. Then I recommend you to stick with these two separate iterations. Brevity, readability and maintainability over performance - moreover, the performance is not really an issue here.
You can use HashMap. Use "idTeam" as key and the object TeamStanding as value. Now you can iterate on the result list and if you find the object in the map, just update its fields and if you don't find then insert the object. After iteration finishes, you can call map.values() and it will give you a collection of objects(TeamStanding) and then you can create a new ArrayList with this collection.
The code will go like as follows:
public List<TeamStanding> mergeTeamStandingList(List<TeamStanding> teamStandingList) {
final Map<Integer, TeamStanding> idTeamVsTeamStandingMap = new HashMap<Integer, TeamStanding>();
teamStandingList.forEach(teamStanding -> {
if(idTeamVsTeamStandingMap.containsKey(teamStanding.getIdTeam())) {
TeamStanding teamStanding1 = idTeamVsTeamStandingMap.get(teamStanding.getIdTeam());
teamStanding1.setTeamDraws(teamStanding1.getTeamDraws() + teamStanding.getTeamDraws());
//so on
} else {
idTeamVsTeamStandingMap.put(teamStanding.getIdTeam(), teamStanding);
}
});
return new ArrayList<>(idTeamVsTeamStandingMap.values());
}
Create a merge method on your Teamstanding object.
public TeamStanding merge(TeamStanding other) {
this.teamPoints += other.getTeamPoints();
this.teamPlayed += other.getTeamPlayed();
this.teamWins += other.getTeamWins();
this.teamDraws += other.getTeamDraws();
this.teamGoalsFavor += other.getTeamGoalsFavor();
this.teamLoses += other.getTeamLoses();
this.teamGoalDif += other.getTeamGoalDif();
return this;
}
Then use Streams to group by teamId and reduce the common items using the merge method.
Map<Integer, Optional<TeamStanding>> mapReduced = teamStandings
.stream()
.collect(groupingBy(TeamStanding::getIdTeam, Collectors.reducing(TeamStanding::merge)));
I'm trying to concatenate many byte[] arrays into one, big array. I've found a solution utilizing ByteArrayOutputStream but I find results weird.
Code
List<SampleChunk> chunkList = new ArrayList<SampleChunk>();
public static byte[] getRaw(List<SampleChunk> list) throws FileNotFoundException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
for (int i = 0; i < list.size(); i++) {
try {
os.write(list.get(i).getSamples());
} catch (IOException e) {
e.printStackTrace();
}
}
PrintWriter pr = new PrintWriter(new File("all.txt"));
pr.write(Arrays.toString(os.toByteArray()));
pr.close();
return os.toByteArray();
}
public void init(){
XuggleAudio xa = new XuggleAudio(new File("in.flv"));
PrintWriter pr = new PrintWriter(new File("chunks.txt"));
int count = 0;
SampleChunk sc = null;
while ((sc = xa.nextSampleChunk()) != null) {
pr.write(count++ + " " + Arrays.toString(sc.getSamples()) + "\n");
chunkList.add(sc);
}
getRaw(chunkList);
}
Problem is, that theese two output doesn't match. PrintWriter in init() method prints something like this
0 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2 [45, -1, -78, -4, 48, -1, -63, -4, 86,
3 [-38, -1, -55, -1, -62, -1, 85
4 ...
but output form getRaw() method contains only values from {-1,1,0,2}. Both output have the same size (os.toByteArray().length == list.size * (...)getSamples.lenght). What did I wrong? How to merge theese arrays right?
I have three classes and am trying to make a game whereby users move along a grid depending on what is rolled by a die.
I have my main BoardGame class, containing the GUI and the counters which currently are Jlabel images (i'm open to suggestions as to what I could use instead of a JLabel - i wasnt so sure myself). I have a Grid class which I have arranged into a 2D array and called an instance of in the BoardGame class, and I have a die class which rolls a random number from 1-6.
I am trying to get me counters to start at the first square on the grid, and then advance in a left-to-right-right-to-left fashion. I am unsure however of how to make the counters move through the grid in the first place. Hopefully, if I can figure this out, I believe I can then implement them moving a specific amount via the die.
Thanks for the help in advance
GameBoard class:
public class GameBoard extends javax.swing.JFrame {
private JLabel Board;
private JLabel GreenDot;
private JLabel redDot;
private JButton startButton;
private Grid grid;
private Die die;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Grid grid = new Grid();
GameBoard inst = new GameBoard(grid);
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public GameBoard(Grid grid) {
super();
this.grid = grid;
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
redDot = new JLabel();
getContentPane().add(redDot);
redDot.setText("jLabel1");
redDot.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/download.png")));
redDot.setBounds(220, 434, 20, 12);
redDot.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
}
{
GreenDot = new JLabel();
getContentPane().add(GreenDot);
GreenDot.setText("jLabel1");
GreenDot.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/3d-green-ball-th.png")));
GreenDot.setBounds(222, 453, 21, 13);
GreenDot.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
}
{
startButton = new JButton();
getContentPane().add(startButton);
startButton.setText("Start Game");
startButton.setBounds(64, 443, 83, 23);
}
{
Board = new JLabel();
getContentPane().add(Board);
Board.setLayout(null);
Board.setText("jLabel1");
Board.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/board.jpg")));
Board.setBounds(204, -1, 742, 484);
}
pack();
this.setSize(963, 523);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
}
Grid class:
public class Grid {
int[][] multi = {
{ 0, 0,-1, 0, 0,-1, 0,-1, 0, 0},
{ 0, 0, 0, 0, 0, 0,-1, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 0,-1, 0,-1, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0,-1, 0, 0, 1},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 1, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{ 0, 0, 0,-1, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 1, 0, 0, 0, 0, 1, 0}
};
}
Die class:
public class Die {
public Die() {
}
public void dieRoll() {
int SIDES = 6;
int roll = (int) (Math.random() * SIDES) + 1;
System.out.println(roll);
}
}
A simple way of doing this would be to change the die class so it has
{
private int sides;
public Die(int numSides){
sides = numSides;
}
public int roll(){
return (int) (Math.random() * SIDES) + 1
}
}
Then you can roll a six sided die like so
//this creates the die
Die sixSides = new Die(6);
//this rolls the die and prints the roll
System.out.print("That roll got you a " + sixSides.roll());
to move along a 2D array, all you need to do is use the modulus and you need to create a point object class
public position move(int num, int x, int y){//input is dice roll int then current position
for(int i = 0; i < num;i++){
if(i%10==0){
y++;
x = 0;
}else{
x++;
}
}
return new point(x,y);
}
To access the x and y positions of the point object you would need to write get and set methods for the object.
something like this
point player1 = new point(0,0);
int xposition = player1.getX
I'm stitching a bitmap from a layout array, that puts the a larger bitmap together as a guide. Using multiple bitmaps into one bitmap. What I rotate the whole bitmap, my solution is to restitch it but have the array account for this rotation. The making of the bitmap is determined by the order of the array. The stitching assumes that the array starts from zero and that is the first index or the left top most corner and then goes to the right to the end and goes to beginning of the next row. I would like to have a 90, 180, 270, and 360 functions to call on. I'm thinking 360 is easy I iterate backwards. I'm using 11 by 11 which is constant.
For example
0, 1, 3, 4
5, 6, 7, 8,
9, 10, 11, 12
13, 14, 15, 16
when I rotate 90
4, 8, 12, 16
3, 7, 11, 15
1, 6, 10, 14
0, 5, 9, 13
Try this. This may have performance impact but its a simple solution.
import java.util.Arrays;
public class RotateMatrix {
public static void main(String[] args) {
int original[][] = { { 0, 1, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
printArray(original);
System.out.println("After 90 Degree");
printArray(rotate(original, 90));
System.out.println("After 180 Degree");
printArray(rotate(original, 180));
System.out.println("After 270 Degree");
printArray(rotate(original, 270));
System.out.println("After 360 Degree");
printArray(rotate(original, 360));
}
private static int[][] rotate(int[][] original, int angle) {
int[][] output = original;
int times = 4 - angle/90;
for(int i=0; i<times; i++){
output = rotate(output);
}
return output;
}
private static int[][] rotate(int[][] original) {
int n = original.length;
int m = original[0].length;
int[][] output = new int[m][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
output[j][n - 1 - i] = original[i][j];
return output;
}
private static void printArray(int[][] array) {
for (int i = 0; i < array.length; i++) {
System.out.println(Arrays.toString(array[i]));
}
}
}
This rotates the array anti-clockwise direction just as in your example. However if you want to change this to clockwise just change int times = 4 - angle/90; to int times = angle/90; in rotate(int[][] original, int angle) method.
I am making a small class for a little project. It's a text based RPG game and I am trying to create a drop class for when NPC's die. I have create a couple Math.random methods (which are exactly the same, just differently named for my convenience.) to drop random amounts for an item id (name) and to get the rarity of the item dropped. It all works fine, but it only randomizes one time (on startup or run) and it won't randomize the amounts after that. I am also randomizing it between 2 numbers, for example, 25 and 50, the random not going lower then 25 or higher then 50.
My question is: How can I randomize a integer in a 2D Array or a general array after each time a NPC dies, so the random number that is first obtained changes and doesn't stay the same. Because right now, it stays at the number choose. if the number is 25, then the next npc I kill, the amount would still be 25.. and 25.. and 25.. and so on. I need it to randomize or change.
Please help, thank you.
public class DropConfig {
private static final int
ALWAYS = 0,
VERY_COMMON = rate(1, 9),
COMMON = rate(10, 20),
UNCOMMON = rate(30, 40),
RARE = rate(50, 60),
VERY_RARE = rate(70, 80),
SUPER_RARE = rate(90, 100);
public static final int[][] NPC_DROPS = {
// Normal NPC's
{1, 526, 1, ALWAYS},
{2, 526, 1, ALWAYS},
{3, 526, 1, ALWAYS},
{1, 995, drop(1, 50), ALWAYS},
{2, 995, drop(1, 50), ALWAYS},
{3, 995, drop(1, 50), ALWAYS},
// Moderate NPC's
{9, 526, 1, ALWAYS},
{9, 995, drop(250, 500), UNCOMMON},
{9, 555, drop(2, 7), VERY_COMMON},
{9, 995, drop(5, 50), VERY_COMMON},
{9, 1050, 1, RARE},
};
public static int rate(int min, int max) {
return 1 + (int)(Math.random() * ((max - min) + 1));
}
//Same as rate, different name for looks.
public static int drop(int min, int max) {
return 1 + (int)(Math.random() * ((max - min) + 1));
}
Heres where I call the drops method
public void npcDeath() {
int npc = 0;
if (npc == null)
return;
for(npc = 0; npc < DropConfig.NPC_DROPS.length; npc++) {
if(npc == DropConfig.NPC_DROPS[npc][0]) {
if(Misc.random(DropConfig.NPC_DROPS[npc][3]) == 0) { //Drops ALWAYS item
Item(DropConfig.NPC_DROPS[npc][1], DropConfig.NPC_DROPS[npc][2]);
}
}
}
}
If I understand correctly, you would like the elements of the NPC_DROPS array initialized with a call to drop() to be reinitialized each time this NPC_DROPS array is used.
Well, NPC_DROPS is a constant, so it can't change. Generate it each time it's accessed, using a method:
public static int[][] generateNpcDrops(){
return new int[][] {
// Normal NPC's
{1, 526, 1, ALWAYS},
{2, 526, 1, ALWAYS},
{3, 526, 1, ALWAYS},
{1, 995, drop(1, 50), ALWAYS},
{2, 995, drop(1, 50), ALWAYS},
{3, 995, drop(1, 50), ALWAYS},
// Moderate NPC's
{9, 526, 1, ALWAYS},
{9, 995, drop(250, 500), UNCOMMON},
{9, 555, drop(2, 7), VERY_COMMON},
{9, 995, drop(5, 50), VERY_COMMON},
{9, 1050, 1, RARE},
}
}
...
public void npcDeath() {
int npc = 0;
if (npc == null)
return;
int[][] npcDrops = DropConfig.generateNpcDrops();
for(npc = 0; npc < npcDrops.length; npc++) {
if(npc == npcDrops[npc][0]) {
if(Misc.random(npcDrops[npc][3]) == 0) { //Drops ALWAYS item
Item(c, npcDrops[npc][1], npcDrops[npc][2]);
}
}
}
}
You want to place functions as constants. You can do this in a language like Scala naturally, but in Java you have to work a little harder.
In every case, you need to call a function to get the actual value.
You can use enum and anonymous methods, but the simplest way/hack is to encode your ranges.
public static int rate(int min, int max) { // same for drop.
int range = max - min;
return (range << 16) | (min & 0xFFFF);
}
public static int eval(int minMax) {
int min = (short) minMax;
int range = (short) (minMax >> 16);
if (range == 0)
return min; // plain number.
else
return min + (int) (Math.random() * (range + 1));
}
You need to call eval() to turn your encoded range into a random number.
Creating an instance of Random with the same seed you will get the same 'random' number. Have you considered using SecureRandom? The difference between SecureRandom and Random is that SecureRandom produces non-deterministic output on each call.