# Creating a hierarchical divisive clustering algorithm

• 03-17-2013, 12:42 PM
MustSeeMelons
Creating a hierarchical divisive clustering algorithm
It works( or should work) like this. The main "Divisive" class reads a file using the Read class and sets it as a linked list Table (Reading works, no problem there). Then one cluster is created, passing the whole table and adding the newly created cluster to list for management. The cluster creates it's distance matrix from the passed table. The cluster has to be divided, so the Split() method is called, finds the MAX distance between to elements in all of the clusters, afterwards goes through all the elements and adds it to the closest cluster. Everything seems fine one paper, but the algorithm works only if i call Split() once. Could it be a problem with how java passes variables? After the second call to Split() I'm getting empty tables and matrix's.. Thank you.

Main "Divisive" class:

Code:

package clusters;

public class Divisive
{

public void Split()
{
MatrixElement max=new MatrixElement(0,0,0);
int pos=0;
for(int k=0;k<clusterList.size();k++)
{
MatrixElement temp=clusterList.get(k).MaxDistance();
if(temp.value>max.value)
{
max=temp;
}
}

System.out.println(pos+". cluster: "+max.rowId+" "+max.columnId+" "+max.value);

if(max.value>1.9)
{

for(int i=0;i<table.size();i++)
{
if(!iD.contains(table.get(i).id))
{
}
}

for(int i=0;i<iD.size();i++) //goes through the clusters record id table
{
for(int j=0;j<clusterList.get(pos).table.size();j++) //goes through the clusters records
{
if(table.get(j).id==iD.get(i)) //finds the location of the first id
{
for(int k=0;k<clusterList.get(pos).matrix.size();k++) //goes through the matrix
{
if(clusterList.get(pos).matrix.get(k).id==iD.get(i)) //finds the id in the matrix
{
if(clusterList.get(pos).matrix.get(k).rowValues.get(max.columnId).value > clusterList.get(pos).matrix.get(k).rowValues.get(max.rowId).value)
{
table.remove(j);
System.out.println("i got here!");
}
else
{
table.remove(j);
System.out.println("i got here!");
}
}
}
}
}
}

clusterList.remove(pos);
}
else
{

}
}

Divisive()
{
Split();
Split();
for(int i=0;i<clusterList.size();i++)
{
clusterList.get(i).WriteFile(i);
System.out.println("Matrix size: "+clusterList.get(i).matrix.size());
System.out.println("Table size: "+clusterList.get(i).table.size());
}
}

public static void main(String[] args)
{
new Divisive();
}

}

The cluster class:

Code:

package clusters;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;

public class Cluster
{
Divisive parent;

public MatrixElement MaxDistance()
{
MatrixElement max=new MatrixElement(0,0,0);
for(int i=0;i<matrix.size();i++)
{
for(int j=0;j<matrix.size();j++)
{
if(matrix.get(i).rowValues.get(j).value>max.value && i!=j)
{
max=matrix.get(i).rowValues.get(j);
}
}
}
return max;
}

class RecordID
{
int i,j,id;
RecordID(int i,int id)
{
this.i=i;
this.id=id;
}
}

public void CreateMatrix()
{

for(int i=0;i<table.size();i++)
{

}

for(int i=0;i<table.size();i++)
{
MatrixRow matrixRow=new MatrixRow(ids.get(i).id);

for(int j=0;j<table.size();j++)
{
}
}

}

{
this.parent=parent;
this.table=table;
CreateMatrix();
}

public double Euclede(Record one, Record two)
{
double sum=0;
for(int i=0;i<one.values.size();i++)
{
sum+=Math.pow(one.values.get(i)-two.values.get(i), 2);
}
return (double) Math.sqrt(sum);
}

public void WriteFile(int sheet)
{
Workbook workbook=new XSSFWorkbook();
Sheet targetSheet=workbook.createSheet();

for(int i=0;i<matrix.size();i++)
{
Row target=targetSheet.createRow(i);
for(int j=0;j<matrix.size();j++)
{
Cell targetCell=target.createCell(j);
targetCell.setCellValue(matrix.get(i).rowValues.get(j).value);
}
}
try{
FileOutputStream output=new FileOutputStream("test"+sheet+".xlsx");
workbook.write(output);
output.close();
}catch(Exception e){

}
}
}

Matrix Row class:

Code:

package clusters;

public class MatrixRow
{
int id;

MatrixRow(int id)
{
this.id=id;
}
}

Code:

package clusters;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

int skip=0;
String filename;

public void run()
{
try{
OPCPackage excel = OPCPackage.open(new File(filename));
XSSFWorkbook workBook = new XSSFWorkbook(excel);
Sheet sheet=workBook.getSheetAt(0);

for(int i=skip;i<sheet.getLastRowNum()+1;i++) //atsija pirmo rindu
{
Row temp=sheet.getRow(i);
Record rec=new Record();

for(int j=0;j<temp.getLastCellNum();j++)
{
Cell cell=temp.getCell(j);
if(j==temp.getLastCellNum()-1)
{
rec.realType=cell.getStringCellValue();
}
else
{
switch(cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
break;
case Cell.CELL_TYPE_STRING:
break;
}

}
}
}
Average();
}
catch(Exception e)
{
e.printStackTrace();
}

//parent.setTable(table);
}

public void Average()
{
for(int i=0;i<table.get(0).values.size();i++)
{
double avg=0;
for(int k=0;k<table.size();k++)
{
avg+=table.get(k).values.get(i);
}
avg=avg/(table.size());
for(int k=0;k<table.size();k++)
{
if(table.get(k).values.get(i).doubleValue()==0)
{
table.get(k).values.set(i, avg);
}
}
}
}

{
this.filename=filename;
this.parent=parent;
if(skip==true)
{
this.skip=1;
}
run();
}

{
this.filename=filename;
if(skip==true)
{
this.skip=1;
}
run();
}
}

Tables line:

Code:

package clusters;

public class Record {
int id;

Record()
{
}
}

MatrixElement:

Code:

package clusters;

public class MatrixElement
{
int rowId,columnId;
double value;

MatrixElement(int rowId, int columnId, double value)
{
this.rowId=rowId;
this.columnId=columnId;
this.value=value;
}
}

• 03-19-2013, 10:59 AM
tamilarasi
Re: Creating a hierarchical divisive clustering algorithm
Quote:

Originally Posted by MustSeeMelons
how java passes variables? After the second call to Split() I'm getting empty tables and matrix's..

I think you will create and call Split method with parameters that like..

you create method like
Code: