I was running a CVS file using KNN(java) to find and calculate the shortest distance with the desired K.
This is the screenshot with 57
columns, goes from f1 to f57.
The following is then my code when you run it you will get NumberFormat error. Why is that happening? What should I change?
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.FileNotFoundException;
class Knn {
static class Sample {
int label;
int [] pixels;
}
private static List<Sample> readFile(String file) throws IOException {
List<Sample> samples = new ArrayList<Sample>();
BufferedReader reader = new BufferedReader(new FileReader(file));
try {
String line = reader.readLine(); // ignore header
while((line = reader.readLine()) != null)
{
String[] tokens = line.split(",");
Sample sample = new Sample();
sample.label = Integer.parseInt(tokens[0]);
sample.pixels = new int[tokens.length - 1];
for(int i = 1; i < tokens.length; i++)
{
sample.pixels[i-1] = Integer.parseInt(tokens[i]);
}
samples.add(sample);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally { reader.close(); }
return samples;
}
private 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); // euclidian distance would be sqrt(sum)...
}
private static int classify(List<Sample> trainingSet, int[] pixels) {
int label = 0, bestDistance = Integer.MAX_VALUE;
for(Sample sample: trainingSet) {
int dist = distance(sample.pixels, pixels);
if(dist < bestDistance) {
bestDistance = dist;
label = sample.label;
}
}
return label;
}
public static void main(String[] argv) throws IOException {
List<Sample> trainingSet = readFile("/Users/gary/Desktop/spam_train.csv");
List<Sample> validationSet = readFile("/Users/gary/Desktop/spam_test.csv");
int numCorrect = 0;
for(Sample sample:validationSet)
{
if(classify(trainingSet, sample.pixels) == sample.label) numCorrect++;
}
System.out.println("Accuracy: " + (double)numCorrect / validationSet.size() * 100 + "%");
}
}
sample.label = Integer.parseInt(tokens[0]);
due to this statement, you get NumberFormatException because suppose you access t10 row which is not convertible to integer using interger.parseint it is not internally treated as a string
I was running a CVS file using KNN(java) to find and calculate the shortest distance with...
Java programming How do i change this program to scan data from file and find the sum of the valid entry and count vaild and invalid entries The provided data file contains entries in the form ABCDE BB That is - a value ABCDE, followed by the base BB. Process this file, convert each to decimal (base 10) and determine the sum of the decimal (base 10) values. Reject any entry that is invalid. Report the sum of the values...
composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main { public static void main(String[] args) { System.out.print("Enter the...
I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...
The names of the two input files as well as the output file are supposed to be provided as arguments. Could you please fix it? Thanks. DPriorityQueue.java: // Import the required classes import java.io.*; import java.util.*; //Create the class public class DPriorityQueue { //Declare the private members variables. private int type1,type2; private String CostInTime[][], SVertex, DVertex; private List<String> listOfTheNodes; private Set<String> List; private List<Root> ListOfVisitedNode; private HashMap<String, Integer> minimalDistance; private HashMap<String, Integer> distOfVertices; private PriorityQueue<City> priorityQueue; // prove the definition...
I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...
Help !! I need help with Depth-First Search using an
undirected graph.
Write a program, IN JAVA, to implement the
depth-first search algorithm using the pseudocode given.
Write a driver program, which reads input file mediumG.txt as an
undirected graph and runs
the depth-first search algorithm to find paths to all the other
vertices considering 0 as the
source. This driver program should display the paths in the
following manner:
0 to ‘v’: list of all the vertices traversed to...
I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...
QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...
I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 { public static...
need help editing or rewriting java code, I have this program
running that creates random numbers and finds min, max, median ect.
from a group of numbers,array. I need to use a data class and a
constructor to run the code instead of how I have it written right
now. this is an example of what i'm being asked
for.
This is my code:
import java.util.Random;
import java.util.Scanner;
public class RandomArray {
// method to find the minimum number in...