Java Code For Nearest Neighbor classifier
Java Code For Nearest Neighbor classifier
import java.io.*;
importjava.util.*;
importjava.math.*;
public class KNN
{
Scanner src=new
Scanner(System.in);
introwcount=0,colcount;
String f[][];
intcolvar=0;
float[] newcol;
float[] requirement;
String[] req;
double[] dist;
intT,pos;
float min=0.0f,max=0.0f;
void display()
{
for(int q=0;q<rowcount;q++)
{
for(int s=0;s<colcount;s++)
System.out.print(f[q][s]+"\t");
System.out.println();
}
}//display-------------------------------------------
void newcolcal()
{
System.out.println("Enter
column name");
String tempstr=src.nextLine();
for(int q=0;q<colcount;q++)
if(f[0][q].equals(tempstr))
colvar=q;
}//newcolcal-----------------------------------------
void normal()
{
int flag=0;
for(int q=1;q<rowcount;q++)
{
newcol[flag]=Float.parseFloat(f[q][colvar]);
flag++;
}
for(int
q=0;q<newcol.length;q++)
if(max<newcol[q])
max=newcol[q];
min=max;
for(int
q=0;q<newcol.length;q++)
if(min>newcol[q])
min=newcol[q];
for(int
q=0;q<newcol.length;q++)
newcol[q]=(newcol[q]-min)/(max-min);
T=(int)Math.sqrt(rowcount-1);
}//normal---------------------------------------------
void display1()
{
for(int
q=0;q<newcol.length;q++)
f[q+1][colvar]=Float.toString(newcol[q]);
display();
}//display1()-----------------------------------------
void distance()
{
dist=new double[rowcount-1];
System.out.println("Enter
requirement:");
requirement=new
float[colcount-2];
req=new String[colcount-2];
for(int q=0;q<req.length;q++)
req[q]=src.nextLine();
for(int q=0;q<req.length;q++)
{
if(req[q].equals("F"))
{req[q]="0";}
if(req[q].equals("M"))
{req[q]="1";}
}
for(int
q=0;q<requirement.length;q++)
requirement[q]=Float.parseFloat(req[q]);
requirement[1]=(requirement[1]-min)/(max-min);
float colsum=0.0f;
for(int q=1;q<rowcount;q++)
{
for(int
s=1;s<colcount-1;s++)
{
float
colminus=requirement[s-1]-Float.parseFloat(f[q][s]);
float
colsqr=colminus*colminus;
colsum=colsum+colsqr;
}
dist[q-1]=Math.sqrt(colsum);
//System.out.println(dist[q-1]);
colsum=0.0f;
}
set();
}//distance()-----------------------------
void set()
{
double[] Tset=new double[T];
int[] rowset=new int[T];
for(int q=0;q<Tset.length;q++)
{
Tset[q]=dist[q];
rowset[q]=q;
}
for(int q=T;q<dist.length;q++)
if(dist[q]<largest(Tset))
{
Tset[pos]=dist[q];
rowset[pos]=q;
}
commonclass(rowset);
}//set()--------------------------------------------
double largest(double[] arr)
{
pos=0;
double largest=0.0d;
for(int q=0;q<arr.length;q++)
if(arr[q]>largest)
{
largest=arr[q];
pos=q;
}
return(largest);
}//largest()------------------------------------------
void commonclass(int[] array)
{
String[] commonclass=new
String[array.length];
String
cl[]={"short","med","tall"};
intclcheck[]=new int[cl.length];
int common=0,clpos=0;
for(int
q=0;q<clcheck.length;q++)
clcheck[q]=0;
for(int
q=0;q<array.length;q++)
{
for(int s=0;s<colcount;s++)
System.out.print(f[array[q]+1][s]+"\t");
System.out.println();
}
for(int
q=0;q<array.length;q++)
commonclass[q]=f[array[q]+1][colcount-1];
for(int
q=0;q<commonclass.length;q++)
for(int s=0;s<cl.length;s++)
if(cl[s].equals(commonclass[q]))
clcheck[s]++;
for(int
q=0;q<clcheck.length;q++)
if(common<clcheck[q])
{
common=clcheck[q];
clpos=q;
}
System.out.println("Most
Common class is "+cl[clpos]);
}//commonclass()------------------------------------------
void read()
{
String
file="person.txt";
String line=null;
try
{
BufferedReaderbr=new
BufferedReader(new FileReader(file));
while((line=br.readLine())!=null)
rowcount++;
br.close();
br=new BufferedReader(new
FileReader(file));
line=br.readLine();
colcount=line.split("\t").length;
br.close();
f=new String[rowcount][colcount];
newcol=new float[rowcount-1];
br=new BufferedReader(new
FileReader(file));
int row=0,col=0,lc=0;
while((line=br.readLine())!=null)
{
line=line.replace('F','0');
line=line.replace('M','1');
String[]
temp=line.split("\t");
for(int
q=0;q<temp.length;q++)
f[row][q]=temp[q];
row++;
}
br.close();
}
catch(FileNotFoundException ex)
{System.out.println("error");}
catch(IOException ex)
{System.out.println("error");}
}
public static void main(String
arg[])
{
KNN knn=new KNN();
knn.read();
knn.newcolcal();
knn.normal();
knn.display1();
knn.distance();
}}
0 comments :