Вы находитесь на странице: 1из 33

import import import import import import import import

java.io.*; java.util.*; java.awt.*; java.awt.event.*; javax.swing.*; java.sql.*; java.lang.Integer; java.lang.Double;

class Apriori extends JFrame implements ActionListener { private BufferedReader fileInput; private JTextArea textArea; private JMenuBar greenMenuBar; JMenu jmTrain,jmTest,jmPerformance; JMenuItem setomega,setnum,setmin,setap,setsr,setti,setace; protected class TtreeNode { protected int support = 0; protected TtreeNode[] childRef = null; protected TtreeNode() {} private TtreeNode(int sup) { support = sup; } } private TtreeNode[] startTtreeRef; private short[][] dataArray = null; private static final double MIN_SUPPORT = 0.0; private static final double MAX_SUPPORT = 100.0; private boolean inputFormatOkFlag = true; private boolean haveDataFlag = false; private boolean hasSupportFlag = false; private boolean nextLevelExists = true ; private File fileName; private int numRows = 0; private int numCols = 0; private double support = 20.0; private double minSupportRows = 1.0; String name; //For selected features public int [] t=new int[150]; public int nofeat; public int []finall =new int[500]; public int rang; public omega [] m= new omega [150]; public float []b=new float[150]; public int []sp={0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, 21,22,23,24,25,26,27}; //constructor Apriori-GUI public Apriori() {

JFrame frame=new JFrame("ASSOCIATION RULE MINING FOR MAMMOGRAM DIAGNOSIS "); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(1030,740); frame.setVisible(true); Container container =frame.getContentPane(); container.setBackground(Color.blue); container.setLayout(new BorderLayout(10,10)); JMenuBar greenMenuBar = new JMenuBar(); greenMenuBar.setOpaque(true); greenMenuBar.setBackground(new Color(154, 165, 127)); greenMenuBar.setPreferredSize(new Dimension(200, 20)); greenMenuBar.setEnabled(true); JMenu jmFile=new JMenu("File "); JMenuItem exit=new JMenuItem("Exit"); greenMenuBar.add(jmFile); jmFile.add(exit); exit.addActionListener(this); JMenu jmTrain=new JMenu("Training "); JMenuItem setomega=new JMenuItem("Set & Run Omega"); JMenuItem setnum=new JMenuItem("Open Num File For Apriori"); JMenuItem setmin=new JMenuItem("Add Min.Support For Apriori"); JMenuItem setap=new JMenuItem("Run Apriori"); JMenuItem setsr=new JMenuItem("Sensitive Rules"); greenMenuBar.add(jmTrain); jmTrain.add(setomega); jmTrain.add(setnum); jmTrain.add(setmin); jmTrain.add(setap); jmTrain.add(setsr); setomega.addActionListener(this); setnum.addActionListener(this); setmin.addActionListener(this); setap.addActionListener(this); setsr.addActionListener(this); frame.setJMenuBar(greenMenuBar); textArea = new JTextArea(40, 15); textArea.setEditable(false); container.add(new JScrollPane(textArea),BorderLayout.CENTER); frame.pack(); } //actionPerformed event of Apriori public void actionPerformed(ActionEvent event) { try { omega oo=new omega(); if (event.getActionCommand().equals("Set & Run Omega")) oo.runomeg a(); if (event.getActionCommand().equals("Open Num File For Apriori

")) getFileName(); if (event.getActionCommand().equals("Add Min.Support For A priori")) addSupport(); if (event.getActionCommand().equals("Run Apriori")) ap rioriT(); if (event.getActionCommand().equals("Sensitive Rules") ) display(); if (event.getActionCommand().equals("Exit"))System.exi t(0); if (event.getActionCommand().equals("Set & ACE")) runA ce(); if (event.getActionCommand().equals("Check Performance ")) check();

} catch(Exception e) { } } public int noc; public class omega { public float[][] a =new float[2][150]; public float[][] original =new float[2][150]; public int [] rankk= new int[200]; public int row,col,zero,ones,maj,diff; public float ir; float globalin; public int noc; public float [][] range=new float[120][120]; public int rang; String [] s=new String[150]; omega() { col=0; diff=0; rang=0; } //get data function of omega public int getdata(int feat) { int numberOfrows=0; int numberOfColumns=0; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection( "j dbc:odbc:project" );

Statement st = con.createStatement(); ResultSet rs = st.executeQuery( "Select * from [ Sheet5$]" ); ResultSetMetaData rsmd = rs.getMetaData(); numberOfColumns = rsmd.getColumnCount(); col=0; int i=0; while (rs.next()) { s[i] = rs.getString(1); float feature = rs.getFloat(feat); a[0][i]=feature; original[0][i]=feature; int clas = rs.getInt(30); a[1][i]=clas; original[1][i]=clas; i++; col++; } numberOfrows=i; rs.close(); st.close(); } catch(Exception ex) {} return numberOfrows; } public void sort() { int n=col; for(int i=0;i<n-1;i++) { for(int j=n-1;i<j;j--) { if(a[0][j]<a[0][j-1]) { float t=a[0][j]; a[0][j]=a[0][j-1]; a[0][j-1]=t; t=a[1][j]; a[1][j]=a[1][j-1]; a[1][j-1]=t; } }

} } //cut function of omega public float cut() { omega [] step=new omega [150]; omega [] step2=new omega [150]; omega [] step3=new omega [150]; int int int int n=col; j=0; c=0; c1=0;

for(int i=0;i<n;i++) { j=j+1; if(a[1][i]!=a[1][i+1] i==n-1) { c=c+1; step[c]=new omega(); step[c].col=j; for(int k=j-1;k<j;k--,i--) { step[c].a[0][k]=a[0][i]; step[c].a[1][k]=a[1][i]; if(k==0) break; } step[c].ones=0; step[c].zero=0; for(int q=0;q<step[c].col;q++) { if(step[c].a[1][q]==3) { step[c].zero++; } if(step[c].a[1][q]==4) { step[c].ones++; } } if(step[c].ones>step[c].zero) { step[c].maj=step[c].ones; } else { step[c].maj=step[c].zero; } i=i+j-1; j=0;

} } //CONDITION 2 if(c>1) { int g,v,l,x,k; int z=0; for( v=1;v<=c;v++) { z=z+1; step2[z]=new omega(); if( step[v].maj<2 && v!=c) { for( g=0;g<step[v].col;g++) { step2[z].a[0][g]=step[v] .a[0][g]; step2[z].a[1][g]=step[v] .a[1][g]; } for( l=g,k=0;k<step[v+1].col;l++ ,k++) { step2[z].a[0][l]=step[v+ 1].a[0][k]; step2[z].a[1][l]=step[v+ 1].a[1][k]; } step2[z].col=l; v=v+1; } else { for( x=0;x<step[v].col;x++) { step2[z].a[0][x]=step[v] .a[0][x]; step2[z].a[1][x]=step[v] .a[1][x]; } step2[z].col=step[v].col; } step2[z].ones=0; step2[z].zero=0; for(int q=0;q<step2[z].col;q++) { if(step2[z].a[1][q]==3) { step2[z].zero++; } if(step2[z].a[1][q]==4) { step2[z].ones++; } } if(step2[z].ones>step2[z].zero)

{ step2[z].maj=step2[z].ones; } else { step2[z].maj=step2[z].zero; } step2[z].ir=0; step2[z].ir=(step2[z].col-step2[z].maj)/ (float)step2[z].col; } c1=z; } //CONDITION 3 if(c1>1) { int g,l,x,k,v; int z=0; for( v=1;v<=c1;v++) { z=z+1; step3[z]=new omega(); if(v==c1) { for( x=0;x<step2[v].col;x++) { step3[z].a[0][x]=step2[v ].a[0][x]; step3[z].a[1][x]=step2[v ].a[1][x]; } step3[z].col=step2[v].col; } else { if( step2[v].maj==step2[v+1].maj && step2[v].ir >= 0 && step2[v].ir <= 1 && step2[v+1].ir>=0 && step2[v+1].ir<=1 )//&& v!=c1 ) { for( g=0;g<step2[v].col; g++) { step3[z].a[0][g] =step2[v].a[0][g]; step3[z].a[1][g] =step2[v].a[1][g]; } for( l=g,k=0;k<step2[v+1 ].col;l++,k++) { step3[z].a[0][l] =step2[v+1].a[0][k]; step3[z].a[1][l] =step2[v+1].a[1][k];

} step3[z].col=l; v=v+1; } else { for( x=0;x<step2[v].col; x++) { step3[z].a[0][x] =step2[v].a[0][x]; step3[z].a[1][x] =step2[v].a[1][x]; } step3[z].col=step2[v].co l; } } step3[z].ones=0; step3[z].zero=0; for(int q=0;q<step3[z].col;q++) { if(step3[z].a[1][q]==3) { step3[z].zero++; } if(step[c].a[1][q]==4) { step3[z].ones++; } } if(step3[z].ones>step3[z].zero) { step3[z].maj=step3[z].ones; } if(step3[z].zero>step3[z].ones) { step3[z].maj=step3[z].zero; } range[rang][0]=step3[z].a[0][0]; range[rang][1]=step3[z].a[0][step3[z].co l-1]; rang=rang+1; } noc=z; int sumofdiff=0; for(int p=1;p<=z;p++) { step3[p].diff=step3[p].col- step3[p].maj ; sumofdiff=sumofdiff+step3[p].diff; } int sumofcol=0; for(int p=1;p<=z;p++) {

sumofcol=sumofcol+step3[p].col; } globalin=sumofdiff/(float)sumofcol; } return globalin; } //runomega function of omega public void runomega() { textArea.append("\n Feature Extraction And Selection Pro cess\n************************************************\n"); int i,w,k; int noofimages=0; for(i=2;i<30;i++) { m[i]=new omega(); noofimages=m[i].getdata(i); m[i].sort(); b[i]=m[i].cut(); } String [] s= {"","","Var4-0","Ent1-0","Ent5-0","Ent1-90" ,"Ent3-90","Ent4-90","Ent5-90","Ent1-135","Enrgy1-45","Enrgy5-135","Hom1-0","Hom 2-0","Hom3-0","Hom4-0","Hom1-45","Hom2-45","Hom3-45","Hom1-90","Hom2-90","Hom3-9 0","Hom4-90","Hom5-90","Hom1-135","Hom2-135","Hom3-135","Hom4-135","Hom5-135","M om3-90"};

int rank=2; for(nofeat=0,i=2;i<30;i++) { textArea.append("\n "+s[i]); for(w=0;w<m[i].rang;w++) { rank=rank+1; m[i].rankk[w]=rank; textArea.append("\n range = "+m[ i].range[w][0]+" to "+m[i].range[w][1]+" rank = "+m[i].rankk[w]); } t[nofeat]=i; nofeat++; } FileOutputStream out2; PrintStream pss; try {

out2 = new FileOutputStream("Discrete.num"); pss = new PrintStream( out2 ); for(int p=0;p<noofimages;p++) { if(m[2].original[1][p]==3) pss.print("1"); else pss.print("2");

l:for(int q=0;q<nofeat;q++) { for(int j=0;j<m[t[q]].rang;j++) { if((m[t[q]].original[0][ p]>=m[t[q]].range[j][0]) && (m[t[q]].original[0][p]<=m[t[q]].range[j][1])) { textArea.append( " "+m[t[q]].rankk[j]); pss.print(" "+m[ t[q]].rankk[j]); continue l; } } } textArea.append("\n"); pss.println(""); } pss.close(); } catch (Exception e) { } } } //getFilename function of Apriori private void getFileName() { fileName = new File("Discrete.num"); if (checkFileName()) { readFile(); } if (inputFormatOkFlag) { if (checkOrdering()) { if (haveDataFlag && hasSupportFlag) setap.setEnabled(true); outputDataArray(); textArea.append("Number of records = " + numRows + "\n"); countNumCols();

textArea.append("Number of columns = " + numCols + "\n"); } else { haveDataFlag = false; inputFormatOkFlag = true; textArea.append("Error reading file: " + fileName + "\n\ n"); setap.setEnabled(false); } } } //addSupport function of Apriori private void addSupport() { try { while (true) { String stNum1 = JOptionPane.showInputDialog("Input minimum " +" s upport value between " + MIN_SUPPORT + " and " +MAX_SUPPORT); if (stNum1.indexOf('.') > 0) support = Double.parseDouble(stNum1); else support = Integer.parseInt(stNum1); if (support>=MIN_SUPPORT && support<=MAX_SUPPORT) break; JOptionPane.showMessageDialog(null,"MINIMUM SUPPORT VALUE INPUT E RROR:\n" +"input = " + support + "\nminimum suppo rt input must be a floating point\n" +"number between " + MIN_SUPPORT + " and " +MAX_SUPPORT); } //support=2; textArea.append("Minimum support = " + support + "%\n"); hasSupportFlag=true; } catch(NumberFormatException e) { hasSupportFlag=false; setap.setEnabled(false); } if (haveDataFlag && hasSupportFlag) setap.setEnabled(true); } //apriori function of Apriori private void aprioriT() { minSupportRows = numRows*support/100.0; createTtreeTopLevel(); generateLevel2(); createTtreeLevelN(); textArea.append("\n"); outputFrequentSets(); } // createTtreeTopLevel function of Apriori

protected void createTtreeTopLevel() { startTtreeRef = new TtreeNode[numCols+1]; for (int index=1;index<=numCols;index++) startTtreeRef[index] = new TtreeNode(); createTtreeTopLevel2(); pruneLevelN(startTtreeRef,1); } // createTtreeTopLevel2 function of Apriori protected void createTtreeTopLevel2() { for (int index1=0;index1<dataArray.length;index1++) { if (dataArray[index1] != null) { for (int index2=0;index2<dataArray[index1].length;index2 ++) { startTtreeRef[dataArray[index1][index2]] .support++; } } } } // createTtreeTopLevel2 function of Apriori protected void createTtreeLevelN() { int nextLevel=2; while (nextLevelExists) { //textArea.append("Generating K=" + nextLevel + " large itemsets \n"); addSupportToTtreeLevelN(nextLevel); pruneLevelN(startTtreeRef,nextLevel); nextLevelExists=false; generateLevelN(startTtreeRef,1,nextLevel,null); nextLevel++; } } // addSupportToTtreeLevelN function of Apriori protected void addSupportToTtreeLevelN(int level) { for (int index=0;index<dataArray.length;index++) { if (dataArray[index] != null) { addSupportToTtreeFindLevel(startTtreeRef,level,dataArray [index].length,dataArray[index]); } } } // addSupportToTtreeFindLevel function of Apriori

private void addSupportToTtreeFindLevel(TtreeNode[] linkRef, int level,int endIndex, short[] itemSet) { if (level == 1) { for (int index1=0;index1 < endIndex;index1++) { if (linkRef[itemSet[index1]] != null) { linkRef[itemSet[index1]].support++; } } } else { for (int index=0;index<endIndex;index++) { if (linkRef[itemSet[index]] != null) { if (linkRef[itemSet[index]].childRef != null) addSupportToTtreeFindLevel(linkRef[itemSet[index ]].childRef,level-1,index,itemSet); } } } } // pruneLevelN function of Apriori protected void pruneLevelN(TtreeNode [] linkRef, int level) { int size = linkRef.length; if (level == 1) { for (int index1=1;index1 < size;index1++) { if (linkRef[index1] != null) { if (linkRef[index1].support < minSupportRows) linkRef[index1] = null; } } } else { for (int index1=1;index1 < size;index1++) { if (linkRef[index1] != null) { if (linkRef[index1].childRef != null) pruneLevelN(linkRef[index1].childRef,lev el-1); } } } } // generateLevel2 function of Apriori

protected void generateLevel2() { nextLevelExists=false; for (int index=2;index<startTtreeRef.length;index++) { if (startTtreeRef[index] != null) generateNextLevel(startTtreeRe f,index,realloc2(null,(short) index)); } } // generateLevelN function of Apriori protected void generateLevelN(TtreeNode[] linkRef, int level,int requiredLev el, short[] itemSet) { int index1; int localSize = linkRef.length; if (level == requiredLevel) { for (index1=2;index1<localSize;index1++) { if (linkRef[index1] != null) generateNextLevel(linkRef,i ndex1,realloc2(itemSet,(short) index1)); } } else { for (index1=2;index1<localSize;index1++) { if (linkRef[index1] != null) { generateLevelN(linkRef[index1].childRef,level+1, requiredLevel,realloc2(itemSet,(short) index1)); } } } } // generateNextLevel function of Apriori protected void generateNextLevel(TtreeNode[] parentRef, int endIndex,short[] itemSet) { parentRef[endIndex].childRef = new TtreeNode[endIndex]; short[] newItemSet; TtreeNode currentNode = parentRef[endIndex]; for (int index=1;index<endIndex;index++) { if (parentRef[index] != null) { newItemSet = realloc2(itemSet,(short) index); if (testCombinations(newItemSet)) { currentNode.childRef[index] = new TtreeNode(); nextLevelExists=true; } else currentNode.childRef[index] = null; } } }

// testCombinations function of Apriori protected boolean testCombinations(short[] currentItemSet) { if (currentItemSet.length < 3) return(true); short[] itemSet1 = new short[2]; itemSet1[0] = currentItemSet[1]; itemSet1[1] = currentItemSet[0]; int size = currentItemSet.length-2; short[] itemSet2 = removeFirstNelements(currentItemSet,2); return(combinations(null,0,2,itemSet1,itemSet2)); } // combinations function of Apriori private boolean combinations(short[] sofarSet, int startIndex,int endIndex, short[] itemSet1, short[] itemSet2) { if (endIndex > itemSet2.length) { short[] testSet = append(sofarSet,itemSet1); return(findItemSetInTtree(testSet)); } else { short[] tempSet; for (int index=startIndex;index<endIndex;index++) { tempSet = realloc2(sofarSet,itemSet2[index]); if (!combinations(tempSet,index+1,endIndex+1,itemSet1,it emSet2)) return(false); } } return(true); } // findItemSetInTtree function of Apriori private boolean findItemSetInTtree(short[] itemSet) { if (startTtreeRef[itemSet[0]] != null) { int lastIndex = itemSet.length-1; if (lastIndex == 0) return(true); else return(findItemSetInTtree2(itemSet,1,lastIndex,startTtreeRe f[itemSet[0]].childRef)); } else return(false); } // findItemSetInTtree2 function of Apriori private boolean findItemSetInTtree2(short[] itemSet, int index,int lastIndex , TtreeNode[] linkRef) { if (linkRef[itemSet[index]] != null) { if (index == lastIndex) return(true); else return(findItemSetInTtree2(itemSet,index+1,lastIndex,linkRe

f[itemSet[index]].childRef)); } else return(false); } // checkFileName function of Apriori private boolean checkFileName() { if (fileName.exists()) { if (fileName.canRead()) { if (fileName.isFile()) return(true); else JOptionPane.showMessageDialog(null,"FILE ER ROR: File is a directory"); } else JOptionPane.showMessageDialog(null,"FILE ERROR: Access deni ed"); } else JOptionPane.showMessageDialog(null,"FILE ERROR: No such fil e!"); return(false); } // readFile function of Apriori private void readFile() { try { inputFormatOkFlag=true; getNumberOfLines(); if (inputFormatOkFlag) { dataArray = new short[numRows][]; inputDataSet(); haveDataFlag = true; } else { haveDataFlag = false; textArea.append("Error reading file: " + fileNam e + "\n\n"); setap.setEnabled(false); } } catch(IOException ioException) { JOptionPane.showMessageDialog(this,"Error reading File","Error 5 : ",JOptionPane.ERROR_MESSAGE); closeFile(); System.exit(1); } } // getNumberOfLines function of Apriori private void getNumberOfLines() throws IOException {

int counter = 0; openFile(); String line = fileInput.readLine(); while (line != null) { checkLine(counter+1,line); StringTokenizer dataLine = new StringTokenizer(line); int numberOfTokens = dataLine.countTokens(); if (numberOfTokens == 0) break; counter++; line = fileInput.readLine(); } numRows = counter; closeFile(); } // checkLine function of Apriori private void checkLine(int counter, String str) { for (int index=0;index <str.length();index++) { if (!Character.isDigit(str.charAt(index)) &&!Character.isWhitespace( str.charAt(index))) { JOptionPane.showMessageDialog(null,"FILE INPUT ERROR:\ncharcater " + "on line " + counter + " is not a digit or white space"); inputFormatOkFlag = false; break; } } } // inputDataSet function of Apriori public void inputDataSet() throws IOException { int rowIndex=0; textArea.append("Reading input file\n" + fileName + "\n"); openFile(); String line = fileInput.readLine(); while (line != null) { StringTokenizer dataLine = new StringTokenizer(line); int numberOfTokens = dataLine.countTokens(); if (numberOfTokens == 0) break; short[] code = binConversion(dataLine,numberOfTokens); if (code != null) { int codeLength = code.length; dataArray[rowIndex] = new short[codeLength]; for (int colIndex=0;colIndex<codeLength;colIndex ++) dataArray[rowIndex][colIndex] = code[colIndex]; } else dataArray[rowIndex]= null; rowIndex++; line = fileInput.readLine(); } closeFile();

} // binConversion function of Apriori private short[] binConversion(StringTokenizer dataLine,int numberOfTokens) { short number; short[] newItemSet = null; for (int tokenCounter=0;tokenCounter < numberOfTokens;tokenCount er++) { number = new Short(dataLine.nextToken()).shortValue(); newItemSet = realloc1(newItemSet,number); } return(newItemSet); } // checkOrdering function of Apriori private boolean checkOrdering() { boolean result = true; for(int index=0;index<dataArray.length;index++) { if (!checkLineOrdering(index+1,dataArray[index])) result=false; } return(result); } // checkLineOrdering function of Apriori private boolean checkLineOrdering(int lineNum, short[] itemSet) { for (int index=0;index<itemSet.length-1;index++) { if (itemSet[index] >= itemSet[index+1]) { JOptionPane.showMessageDialog(null,"FILE FORMAT ERROR:\n " +"Attribute data in line " + lineNum +" not in numeric order"); return(false); } } return(true); } // countNumCols function of Apriori private void countNumCols() { int maxAttribute=0; for(int index=0;index<dataArray.length;index++) { int lastIndex = dataArray[index].length-1; if (dataArray[index][lastIndex] > maxAttribute) maxAttribute = dataArray[index][lastIndex]; } numCols = maxAttribute; } // outputDataArray function of Apriori

public void outputDataArray() { for(int index=0;index<dataArray.length;index++) { outputItemSet(dataArray[index]); //textArea.append("\n"); } } // outputItemSet function of Apriori protected void outputItemSet(short[] itemSet) { String itemSetStr = " {"; int counter = 0; for (int index=0;index<itemSet.length;index++) { if (counter != 0) itemSetStr = itemSetStr + ","; counter++; itemSetStr = itemSetStr + itemSet[index]; } //textArea.append(itemSetStr + "}"); } String [] s=new String[500]; // outputFrequentSets function of Apriori public void outputFrequentSets() { int number = 1; //textArea.append("FREQUENT (LARGE) ITEM SETS (with support coun ts)\n" +"------------------------------------------------\n"); short[] itemSetSofar = new short[1]; for (int index=1; index <= numCols; index++) { if (startTtreeRef[index] !=null) { if (startTtreeRef[index].support >= minSupportRows) { //textArea.append("[" + number + "] {" + index + "} = " +startTtreeRef[index].support + "\n"); itemSetSofar[0] = (short) index; number = outputFrequentSets(number+1,itemSetSofa r,index,startTtreeRef[index].childRef); } } } textArea.append("\n"); } // openFile function of Apriori private void openFile() { try { FileReader file = new FileReader(fileName);

fileInput = new BufferedReader(file); } catch(IOException ioException) { JOptionPane.showMessageDialog(this,"Error Opening File","Error 4 : ",JOptionPane.ERROR_MESSAGE); } } // closeFile function of Apriori private void closeFile() { if (fileInput != null) { try { fileInput.close(); } catch (IOException ioException) { JOptionPane.showMessageDialog(this,"Error Opening File", "Error 4: ",JOptionPane.ERROR_MESSAGE); } } } // realloc1 function of Apriori protected short[] realloc1(short[] oldItemSet, short newElement) { if (oldItemSet == null) { short[] newItemSet = {newElement}; return(newItemSet); } int oldItemSetLength = oldItemSet.length; short[] newItemSet = new short[oldItemSetLength+1]; int index; for (index=0;index < oldItemSetLength;index++) newItemSet[index] = oldItemSet[index]; newItemSet[index] = newElement; return(newItemSet); } // append function of Apriori protected short[] append(short[] itemSet1, short[] itemSet2) { if (itemSet1 == null) return(copyItemSet(itemSet2)); else if (itemSet2 == null) return(copyItemSet(itemSet1)); short[] newItemSet = new short[itemSet1.length+itemSet2.length]; int index1; for(index1=0;index1<itemSet1.length;index1++) { newItemSet[index1]=itemSet1[index1]; } for(int index2=0;index2<itemSet2.length;index2++) { newItemSet[index1+index2]=itemSet2[index2];

} return(newItemSet); } // realloc2 function of Apriori protected short[] realloc2(short[] oldItemSet, short newElement) { if (oldItemSet == null) { short[] newItemSet = {newElement}; return(newItemSet); } int oldItemSetLength = oldItemSet.length; short[] newItemSet = new short[oldItemSetLength+1]; newItemSet[0] = newElement; for (int index=0;index < oldItemSetLength;index++) newItemSet[index+1] = oldItemSet[index]; return(newItemSet); } // removeFirstNelements function of Apriori protected short[] removeFirstNelements(short[] oldItemSet, int n) { if (oldItemSet.length == n) return(null); else { short[] newItemSet = new short[oldItemSet.length-n]; for (int index=0;index<newItemSet.length;index++) { newItemSet[index] = oldItemSet[index+n]; } return(newItemSet); } } // copyItemSet function of Apriori protected short[] copyItemSet(short[] itemSet) { if (itemSet == null) return(null); short[] newItemSet = new short[itemSet.length]; for(int index=0;index<itemSet.length;index++) { newItemSet[index] = itemSet[index]; } return(newItemSet); } //class rules public class rules { public int r []=new int[500]; public float supp,conf; public int num,total; //public int []sp=new int[150]; rules()

{ super(); supp=0; conf=0; num=0; } } rules [] Brule = new rules[9000]; rules [] Mrule = new rules[4000]; rules Boriginal=new rules(); rules Moriginal=new rules(); public int Bcount=1; public int Mcount=1; // outputFrequentSets function of Apriori private int outputFrequentSets(int number, short[] itemSetSofar, int siz e,TtreeNode[] linkRef) { try { if (linkRef == null) return(number); for (int index=1; index < size; index++) { if (linkRef[index] != null) { if (linkRef[index].support >= minSupportRows) { short[] newItemSetSofar = realloc2(itemSetSofar, (short) index); outputItemSet(newItemSetSofar); int temp =startTtreeRef[index].support ; float sup=((linkRef[index].support)/(float)numRo ws)*100; float con=((linkRef[index].support)/(float)temp) *100; for(int k=1;k<newItemSetSofar.length;k++) { textArea.append(newItemSetSofar[k]+" "); } textArea.append("-> "+newItemSetSofar[0]+" "+con );

int numb1=0; if (newItemSetSofar[0]==1) { Brule[Bcount]=new rules(); Brule[Bcount].r[0]=newItemSetSof ar[0]; for(int k=1;k<newItemSetSofar.le ngth;k++) {

Brule[Bcount].r[k]=newItemSetSof ar[k]; numb1=k; } Brule[Bcount].num=numb1; Brule[Bcount].supp=sup; Brule[Bcount].conf=con; Bcount++; Boriginal.total=Bcount-1; } int numb2=0; if (newItemSetSofar[0]==2) { Mrule[Mcount]=new rules(); Mrule[Mcount].r[0]=newItemSetSof ar[0]; for(int k=1;k<newItemSetSofar.le ngth;k++) { Mrule[Mcount].r[k]=newItemSetSof ar[k]; numb2=k; } Mrule[Mcount].num=numb2; Mrule[Mcount].supp=sup; Mrule[Mcount].conf=con; Mcount++; Moriginal.total=Mcount-1; } textArea.append("\n"); //p1.println(); number = outputFrequentSets(number + 1,newItemSetSofar,i ndex,linkRef[index].childRef); } } } } catch(Exception e){ } return(number); } // display function of Apriori public void display() { FileOutputStream out1,out2; PrintStream ps,pss; try { out1 = new FileOutputStream("Rules.num"); ps = new PrintStream( out1 ); textArea.append("Sensitive Rules \n -------------------\ n\n\n"); textArea.append("Benign Rules \n -------------------\n\n \n"); for(int i=1;i<=Boriginal.total;i++) { textArea.append("["+i+"] "); ps.print("["+i+"] ");

for(int j=1;j<=Brule[i].num;j++) { textArea.append(Brule[i].r[j]+" "); ps.print(Brule[i].r[j]+" "); } textArea.append("->"+Brule[i].r[0]+" ( "+Bru le[i].supp+" , "+Brule[i].conf+" ) \n\n"); ps.print("->"+Brule[i].r[0]+" ( "+Brule[i].s upp+" , "+Brule[i].conf+" ) "); ps.println(" "); ps.println(" "); } textArea.append("Malignant Rules \n -------------------\ n\n\n"); for(int i=1;i<=Moriginal.total;i++) { textArea.append("["+i+"] "); ps.print("["+i+"] "); for(int j=1;j<=Mrule[i].num;j++) { textArea.append(Mrule[i].r[j]+" "); ps.print(Mrule[i].r[j]+" "); } textArea.append("->"+Mrule[i].r[0]+" ( "+Mru le[i].supp+" , "+Mrule[i].conf+" ) \n\n"); ps.print("->"+Mrule[i].r[0]+" ( "+Mrule[i].s upp+" , "+Mrule[i].conf+" ) "); ps.println(" "); ps.println(" "); } ps.close(); out2 = new FileOutputStream("SensitiveRules.num"); pss = new PrintStream( out2 ); textArea.append("Sensitive Rules specified with ranges \ n ---------------------------------------------------\n"); textArea.append("Benign Rules \n --------------------------------------------------\n"); for(int i=1;i<=Boriginal.total;i++) { textArea.append("["+i+"] "); pss.print("["+i+"] "); for(int j=1;j<=Brule[i].num;j++) { for(int p=2;p<30;p++) { for(int w=0;w<m[p].rang; w++) { if(Brule[i].r[j] ==m[p].rankk[w]) { textArea .append(sp[p]+" [ "+m[p].range[w][0]+" to "+m[p].range[w][1]+" ] " ); pss.prin t(sp[p]+" [ "+m[p].range[w][0]+" to "+m[p].range[w][1]+" ] "); } }

} } textArea.append("-> Benign ( "+Brule[i].supp +" , "+Brule[i].conf+" ) \n\n"); pss.print("-> Benign ( "+Brule[i].supp+" , " +Brule[i].conf+" ) "); pss.println(" "); pss.println(" "); } textArea.append("Malignant Rules \n --------------------------------------------------\n"); for(int i=1;i<=Moriginal.total;i++) { textArea.append("["+i+"] "); pss.print("["+i+"] "); for(int j=1;j<=Mrule[i].num;j++) { for(int p=2;p<30;p++) { for(int w=0;w<m[p].rang; w++) { if(Mrule[i].r[j] ==m[p].rankk[w]) { textArea .append(sp[p]+" [ "+m[p].range[w][0]+" to "+m[p].range[w][1]+" ] " ); pss.prin t(sp[p]+" [ "+m[p].range[w][0]+" to "+m[p].range[w][1]+" ] "); } } } } textArea.append("-> Malignant ( "+Mrule[i].s upp+" , "+Mrule[i].conf+" ) \n\n"); pss.print("-> Malignant ( "+Mrule[i].supp+" , "+Mrule[i].conf+" ) "); pss.println(" "); pss.println(" "); } pss.close(); } catch (Exception e) { textArea.append("Error writing to file"); } } public String [] ss= {"","","Var4-0","Ent1-0","Ent5-0","Ent1-90","Ent3-9 0","Ent4-90","Ent5-90","Ent1-135","Enrgy1-45","Enrgy5-135","Hom1-0","Hom2-0","Ho m3-0","Hom4-0","Hom1-45","Hom2-45","Hom3-45","Hom1-90","Hom2-90","Hom3-90","Hom4 -90","Hom5-90","Hom1-135","Hom2-135","Hom3-135","Hom4-135","Hom5-135","Mom3-90"} ; Double val[]=new Double[150]; //runace function of Apriori public void runAce()throws Exception

{ try { DataInputStream dis=new DataInputStream(new File InputStream("C:\\Users\\deeps\\workspace\\phase-2\\val.txt")); textArea.append("\n Extracted Values From Test I mage \n----------------------------------------------------\n"); int in=2; String temp=dis.readLine(); while(temp!=null) { val[in]=Double.parseDouble(temp); in++; temp=dis.readLine(); } for(int q=2;q<30;q++) { textArea.append(""+ss[q]+" = "+val[q]+"\ n"); } dis.close(); } catch(Exception e) { } textArea.append("\n \n Rank of selected features for tes t image \n ------------------------------------------------------\n "); //checking for rank for(int q=0;q<nofeat;q++) { for(int j=0;j<m[t[q]].rang;j++) { if((val[t[q]]>=m[t[q]].range[j][0]) && ( val[t[q]]<=m[t[q]].range[j][1])) { textArea.append("\n"+ss[t[q]]+" = "+m[t[q]].rankk[j]); finall[q]=m[t[q]].rankk[j]; } } } //checking for rule int match=0,notmatch=0,partialmatch=0; int line=0; int match1=0,notmatch1=0,partialmatch1=0; int line1=0; if (finall[nofeat-1]==0) { textArea.append("\n\n No Rank is matched for the test image"); textArea.append("\n\n ******************NORMAL** ****************"); } else { textArea.append("\n\n Checking Beningn Rules:\n-

--------------------------------------------------\n"); for(int i=1;i<=Boriginal.total;i++) { for(int j=1;j<=Brule[i].num;j++) { for(int k=0;k<nofeat;k++ ) { if (finall[k]==Brule[i]. r[j]) { line=line+1; textArea.append( "Rank match = "+line); } } } if (line==Brule[i].num) { match++; } else if(line==0) { notmatch++; } else { partialmatch++; } line=0; textArea.append("\n \n Rule ["+i+"] matc h = "+match+" nomatch = "+notmatch+" pmatch = "+partialmatch+"\n"); } float w=((3*match)+partialmatch)/(float)((3*matc h)+partialmatch+notmatch); textArea.append("\n w= "+w); int a=0; if (a==1) { textArea.append("\n\n ****************** THIS BENIGN?????????????*******************"); } else { match1=0; notmatch1=0; partialmatch1=0; textArea.append("\n Checking Malignant R ules:\n---------------------------------------------------\n"); for(int i=1;i<=Moriginal.total;i++) { for(int j=1;j<=Mrule[i].num;j++) { for(int k=0;k<nofeat;k++ ) { if (finall[k]==M rule[i].r[j]) { line1=li ne1+1;

textArea .append("Rank match = "+line1); } } } if (line1==Brule[i].num) { match1++; } else if(line1==0) { notmatch1++; } else { partialmatch1++; } line1=0; textArea.append("\n \n Rule ["+i +"] match = "+match1+" nomatch = "+notmatch1+" pmatch = "+partialmatch1+"\n"); } float w1=((3*match1)+partialmatch1)/(flo at)((3*match1)+partialmatch1+notmatch1); textArea.append("\n w1= "+w1); if (w>=w1) { textArea.append("\n\n ********** ***************BENIGN************************"); } //if(match1<=match && w1<=w) else { textArea.append("\n\n ********** ***************MALIGNANT************************"); } } } }

//check function of Apriori Double testvalues[]=new Double[30]; public void check() { int TP=0,TN=0,FP=0,FN=0; float Accuracy,Specificity,Sensitivity; int clas=2; int l=0; int empty=0; //Connection connection = null; int numberOfimages=0; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection( "jdbc:odbc :project" ); Statement st = con.createStatement(); ResultSet rs = st.executeQuery( "Select * from [Sheet6$] " ); ResultSetMetaData rsmd = rs.getMetaData(); int tot=0; while(rs.next()) { testvalues[2]=rs.getDouble(1); testvalues[3]=rs.getDouble(2); testvalues[4]=rs.getDouble(3); testvalues[5]=rs.getDouble(4); testvalues[6]=rs.getDouble(5); testvalues[7]=rs.getDouble(6); testvalues[8]=rs.getDouble(7); testvalues[9]=rs.getDouble(8); testvalues[10]=rs.getDouble(9); testvalues[11]=rs.getDouble(10); testvalues[12]=rs.getDouble(11); testvalues[13]=rs.getDouble(12); testvalues[14]=rs.getDouble(13); testvalues[15]=rs.getDouble(14); testvalues[16]=rs.getDouble(15); testvalues[17]=rs.getDouble(16); testvalues[18]=rs.getDouble(17); testvalues[19]=rs.getDouble(18); testvalues[20]=rs.getDouble(19); testvalues[21]=rs.getDouble(20); testvalues[22]=rs.getDouble(21); testvalues[23]=rs.getDouble(22); testvalues[24]=rs.getDouble(23); testvalues[25]=rs.getDouble(24); testvalues[26]=rs.getDouble(25); testvalues[27]=rs.getDouble(26); testvalues[28]=rs.getDouble(27); testvalues[29]=rs.getDouble(28); clas=rs.getInt(29); textArea.append("\n***************************** ***********"); textArea.append("\n Performance Analysis"); /*for (int k=2;k<30;k++) { textArea.append(" "+testvalues[k]+"\n"); } textArea.append("class="+clas); textArea.append("No features="+nofeat); */ for(int q=0;q<nofeat;q++) { for(int j=0;j<m[t[q]].rang;j++) { if((testvalues[t[q]]>=m[t[q]].ra nge[j][0]) && (testvalues[t[q]]<=m[t[q]].range[j][1])) { // textArea.append("\n"+ss[ t[q]]+" = "+m[t[q]].rankk[j]);

finall[q]=m[t[q]].rankk[ j]; } } } for(int q=0;q<nofeat;q++) { //textArea.append("final values"+ if(finall[q]==0) { empty=empty+1; } } //checking for rule int match=0,notmatch=0,partialmatch=0; int line=0; int match1=0,notmatch1=0,partialmatch1=0; int line1=0; if (empty==nofeat) { textArea.append("\n\n No Rank is matched for the test image"); textArea.append("\n\n ******************NORMAL** ****************"); l=2; } else { //textArea.append("\n Checking Beningn Rules:\n--------------------------------------------------\n"); for(int i=1;i<=Boriginal.total;i++) { for(int j=1;j<=Brule[i].num;j++) { for(int k=0;k<nofeat;k++ ) { if (finall[k]==Brule[i]. r[j]) { line=line+1; } } } if (line==Brule[i].num) { match++; } else if(line==0) { notmatch++; }

finall[q]);

else { partialmatch++; } line=0; //textArea.append("\n \n Rule ["+i+"] ma tch = "+match+" nomatch = "+notmatch+" pmatch = "+partialmatch+"\n"); } float w=((3*match)+partialmatch)/(float)((3*matc h)+partialmatch+notmatch); textArea.append("\n w= "+w); int a=0; if (a==1) { textArea.append("\n\n ****************** THIS BENIGN?????????????*******************"); } else { match1=0; notmatch1=0; partialmatch1=0; //textArea.append("\n Checking Malignant Rules:\n---------------------------------------------------\n"); for(int i=1;i<=Moriginal.total;i++) { for(int j=1;j<=Mrule[i].num;j++) { for(int k=0;k<nofeat;k++ ) { if (finall[k]==M rule[i].r[j]) { line1=li ne1+1; //textAr ea.append("Rank match = "+line); } } } if (line1==Mrule[i].num) { match1++; } else if(line1==0) { notmatch1++; } else { partialmatch1++; } line1=0; //textArea.append("\n \n Rule [" +i+"] match = "+match1+" nomatch = "+notmatch1+" pmatch = "+partialmatch1+"\n" ); }

float w1=((3*match1)+partialmatch1)/(flo at)((3*match1)+partialmatch1+notmatch1); textArea.append("\n w1= "+w1); if (w>=w1) { l=0; textArea.append("\nDiagnosis Res ult: BENIGN"); } else if(w1>=w) { l=1; textArea.append("\nDiagnosis Res ult: MALIGNANT"); } else { l=2; textArea.append("\n\n ********** ********NORMAL******************"); } } } for(int q=0;q<nofeat;q++) { finall[q]=0; } if ((l==0&&clas==3) (l==1&&clas==4)) { TP=TP+1; } else if ((l==1&&clas==3)) { FP=FP+1; } else if ((l==0&&clas==4)) { FN=FN+1; } else if((l==2&& clas==2)) { TN=TN+1; } tot++; } numberOfimages=tot; textArea.append("\n\n\nTotal Number Of images for Testing :"+tot ); textArea.append("\nTP textArea.append("\nFP textArea.append("\nFN textArea.append("\nTN ="+TP); ="+FP); ="+FN); ="+TN);

Accuracy=((TP+TN)/(float)(TP+TN+FP+FN))*100; int a=(int)(Accuracy); Specificity=((TN)/(float)(TN+FP))*100; int speci=(int)(Specificity); Sensitivity=((TP)/(float)(TP+FN))*100; int sen=(int)(Sensitivity); textArea.append("\nAccuracy ="+a+" % "); textArea.append("\nSpecificity ="+speci+" % "); textArea.append("\nSensitivity ="+sen+" % "); rs.close(); st.close(); } //try catch(Exception ex) { textArea.append("Exception: "); textArea.append(ex.getMessage()); } } }//apriori

// MAIN METHOD public class gui { public static void main(String[] args) throws IOException { Apriori newFile = new Apriori(); } }

Вам также может понравиться