[code=java]
public class Point
{
private int x;
private int y;
private int Number;
public Point(int X, int Y)
{
this.x =X;
this.y = Y;
this.Number=0;
}
public void assign(int clustNo)
{
this.Number = clustNo;
}
public int getNumber()
{
return this.Number;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public static double distance(Point dp1, Point dp2)
{
double result = 0;
double resultX = dp1.getX() - dp2.getX();
double resultY = dp1.getY() - dp2.getY();
result = Math.sqrt(resul tX*resultX + resultY*resultY );
return result;
}
public String toString()
{
return "("+ this.x + " ," +this.y + ")" + " belongs to g" + this.Number ;
}
}
import java.io.*;
import java.util.*;
public class trial
{
private int k;
private grouping[] group;
private int nIterations;
private Vector inputdata;
private String inputFileName;
public trial (int k, String inputFileName)
{
this.k = k;
this.inputFileN ame = inputFileName;
this.group= new grouping[this.k];
this.nIteration s = 0;
this.inputdata = new Vector();
}
public trial(int k, List inputdata)
{
this.k = k;
this.inputFileN ame = inputFileName;
this.group= new grouping[this.k];
this.nIteration s = 0;
this.inputdata= new Vector(inputdat a);
}
public void readData() throws IOException
{
BufferedReader in = new BufferedReader( new FileReader(this .inputFileName) );
String line = "";
while ((line = in.readLine()) != null )
{
StringTokenizer st = new StringTokenizer (line, " \t\n\r\f,");
if (st.countTokens () == 2)
{
Point dp = new Point(Integer.p arseInt(st.next Token()), Integer.parseIn t(st.nextToken( )));
dp.assign(0);
this.inputdata. add(dp);
}
}
in.close();
}
public void runs()
{
// Select k points as initial means
for (int i=0; i < k; i++)
{
this.clusters[i] = new clustering(i);
this.clusters[i].setMean((Point )(this.inputdat a.get((int)(Mat h.random() * this.inputdata. size()))));
}
do
{
Iterator i = this.inputdata. iterator();
while (i.hasNext())
this.assign((Po int)(i.next())) ;
this.nIteration s++;
}while (this.updateMea ns());
}
private void clusterassign(P oint dp)
{
int currentCluster = dp.getClusterNu mber();
double minDistance = Point.distance( dp, this.clusters[currentCluster].getMean());;
for (int i=0; i <this.k; i++)
if (DataPoint.dist ance(dp, this.clusters[i].getMean()) < minDistance)
{
minDistance = DataPoint.dista nce(dp, this.clusters[i].getMean());
currentCluster = i;
}
dp.clusterassig n(currentCluste r);
}
private boolean updateMeans()
{
boolean reply = false;
int[] x = new int[this.k];
int[] y = new int[this.k];
int[] size = new int[this.k];
Point[] pastMeans = new Point[this.k];
for (int i=0; i<this.k; i++)
{
x[i] = 0;
y[i] = 0;
size[i] = 0;
pastMeans[i] = this.clusters[i].getMean();
}
Iterator i = this.inputdata. iterator();
while (i.hasNext())
{
Point dp = (Point)(i.next( ));
int currentCluster = dp.getClusterNu mber();
x[currentCluster] += dp.getX();
y[currentCluster] += dp.getY();
size[currentCluster]++;
}
for (int j=0; j < this.k; j++ )
if(size[j] != 0) {
x[j] /= size[j];
y[j] /= size[j];
Point temp = new Point(x[j], y[j]);
temp.clusterass ign(j);
this.clusters[j].setMean(temp);
if (Point.distance (pastMeans[j], this.clusters[j].getMean()) !=0 )
reply = true;
}
return reply;
}
public int getK() {
return this.k;
}
public clustering getCluster(int index) {
return this.clusters[index];
}
public String toString()
{
return this.inputdata. toString();
}
public Vector getPoints() {
return this.inputdata ;
}
public static void main(String[] args) {
System.out.prin tln("Please Enter the Number ");
Scanner input1 = new Scanner(System. in);
int n=input1.nextIn t();
System.out.prin tln(" Enter the file name");
Scanner input2 = new Scanner(System. in);
String name = input2.next();
File file = new File(name);
trial km = new trial(n, name);
try {
km.readData();
} catch (Exception e) {
System.err.prin tln(e);
System.exit(-1);
}
km.runs();
System.out.prin tln(" \n" +km);
}
} [/code]
this code works well on the data
345,2
300,2
390,2
400,3
457,3
478,3
200,1
234,1
280,1
and produces the output
C:\Program Files\Java\jdk1 .6.0_03\bin>jav a trialPlease Enter the Number
3
Enter the file name
hi.csv
[(345 ,2) belongs to g1, (300 ,2) belongs to g1, (390 ,2) belongs to
g0, (400 ,3) belongs to g0, (457 ,3) belongs to g0, (478 ,3)
belongs to g0, (200 ,1) belongs to g2, (234 ,1) belongs to g2,
(280 ,1) belongs to g1]
but i need the output in the form in the excel sheet
group0 group1 group2
400,3 345 ,2 200,1
457,3 390 ,2 234,1
478,3 300 ,2 28,1
Please help me . it is urgent . i have to submit my project
public class Point
{
private int x;
private int y;
private int Number;
public Point(int X, int Y)
{
this.x =X;
this.y = Y;
this.Number=0;
}
public void assign(int clustNo)
{
this.Number = clustNo;
}
public int getNumber()
{
return this.Number;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public static double distance(Point dp1, Point dp2)
{
double result = 0;
double resultX = dp1.getX() - dp2.getX();
double resultY = dp1.getY() - dp2.getY();
result = Math.sqrt(resul tX*resultX + resultY*resultY );
return result;
}
public String toString()
{
return "("+ this.x + " ," +this.y + ")" + " belongs to g" + this.Number ;
}
}
import java.io.*;
import java.util.*;
public class trial
{
private int k;
private grouping[] group;
private int nIterations;
private Vector inputdata;
private String inputFileName;
public trial (int k, String inputFileName)
{
this.k = k;
this.inputFileN ame = inputFileName;
this.group= new grouping[this.k];
this.nIteration s = 0;
this.inputdata = new Vector();
}
public trial(int k, List inputdata)
{
this.k = k;
this.inputFileN ame = inputFileName;
this.group= new grouping[this.k];
this.nIteration s = 0;
this.inputdata= new Vector(inputdat a);
}
public void readData() throws IOException
{
BufferedReader in = new BufferedReader( new FileReader(this .inputFileName) );
String line = "";
while ((line = in.readLine()) != null )
{
StringTokenizer st = new StringTokenizer (line, " \t\n\r\f,");
if (st.countTokens () == 2)
{
Point dp = new Point(Integer.p arseInt(st.next Token()), Integer.parseIn t(st.nextToken( )));
dp.assign(0);
this.inputdata. add(dp);
}
}
in.close();
}
public void runs()
{
// Select k points as initial means
for (int i=0; i < k; i++)
{
this.clusters[i] = new clustering(i);
this.clusters[i].setMean((Point )(this.inputdat a.get((int)(Mat h.random() * this.inputdata. size()))));
}
do
{
Iterator i = this.inputdata. iterator();
while (i.hasNext())
this.assign((Po int)(i.next())) ;
this.nIteration s++;
}while (this.updateMea ns());
}
private void clusterassign(P oint dp)
{
int currentCluster = dp.getClusterNu mber();
double minDistance = Point.distance( dp, this.clusters[currentCluster].getMean());;
for (int i=0; i <this.k; i++)
if (DataPoint.dist ance(dp, this.clusters[i].getMean()) < minDistance)
{
minDistance = DataPoint.dista nce(dp, this.clusters[i].getMean());
currentCluster = i;
}
dp.clusterassig n(currentCluste r);
}
private boolean updateMeans()
{
boolean reply = false;
int[] x = new int[this.k];
int[] y = new int[this.k];
int[] size = new int[this.k];
Point[] pastMeans = new Point[this.k];
for (int i=0; i<this.k; i++)
{
x[i] = 0;
y[i] = 0;
size[i] = 0;
pastMeans[i] = this.clusters[i].getMean();
}
Iterator i = this.inputdata. iterator();
while (i.hasNext())
{
Point dp = (Point)(i.next( ));
int currentCluster = dp.getClusterNu mber();
x[currentCluster] += dp.getX();
y[currentCluster] += dp.getY();
size[currentCluster]++;
}
for (int j=0; j < this.k; j++ )
if(size[j] != 0) {
x[j] /= size[j];
y[j] /= size[j];
Point temp = new Point(x[j], y[j]);
temp.clusterass ign(j);
this.clusters[j].setMean(temp);
if (Point.distance (pastMeans[j], this.clusters[j].getMean()) !=0 )
reply = true;
}
return reply;
}
public int getK() {
return this.k;
}
public clustering getCluster(int index) {
return this.clusters[index];
}
public String toString()
{
return this.inputdata. toString();
}
public Vector getPoints() {
return this.inputdata ;
}
public static void main(String[] args) {
System.out.prin tln("Please Enter the Number ");
Scanner input1 = new Scanner(System. in);
int n=input1.nextIn t();
System.out.prin tln(" Enter the file name");
Scanner input2 = new Scanner(System. in);
String name = input2.next();
File file = new File(name);
trial km = new trial(n, name);
try {
km.readData();
} catch (Exception e) {
System.err.prin tln(e);
System.exit(-1);
}
km.runs();
System.out.prin tln(" \n" +km);
}
} [/code]
this code works well on the data
345,2
300,2
390,2
400,3
457,3
478,3
200,1
234,1
280,1
and produces the output
C:\Program Files\Java\jdk1 .6.0_03\bin>jav a trialPlease Enter the Number
3
Enter the file name
hi.csv
[(345 ,2) belongs to g1, (300 ,2) belongs to g1, (390 ,2) belongs to
g0, (400 ,3) belongs to g0, (457 ,3) belongs to g0, (478 ,3)
belongs to g0, (200 ,1) belongs to g2, (234 ,1) belongs to g2,
(280 ,1) belongs to g1]
but i need the output in the form in the excel sheet
group0 group1 group2
400,3 345 ,2 200,1
457,3 390 ,2 234,1
478,3 300 ,2 28,1
Please help me . it is urgent . i have to submit my project
Comment