⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crossvalidation.java

📁 GUI Ant-Miner is a tool for extracting classification rules from data. It is an updated version of a
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				double testAccuracyRate = calculateAccuracyRate(testSet, antsFoundRuleList, defaultClassIndex);
				totalTestAccuracyRate += testAccuracyRate;
				
				accuracyRatesList.add(new Double(testAccuracyRate));
				
				System.out.println("Accuracy rate on the training set: "+trainingAccuracyRate+" %");
				System.out.println("Accuracy rate on the test set:     "+testAccuracyRate+" %");
				
				caller.getJTextArea1().append("\nAccuracy rate on the training set: "+trainingAccuracyRate+" %\n");
				caller.getJTextArea1().append("Accuracy rate on the test set:     "+testAccuracyRate+" %\n\n");
				caller.getJTextArea1().append("Time taken:                        "+((new Date().getTime() - date2.getTime())/1000.0)+" s.\n");
				
				
			}
			
			if(!interrupted){
				DecimalFormat myFormatter = new DecimalFormat("###.##");		
				
				caller.getJTextArea1().append("\n-------------------------------------------------------------------\n");
				caller.getJTextArea1().append("                 "+folds+"-Fold Cross Validation Results\n");
				caller.getJTextArea1().append("-------------------------------------------------------------------\n");
				caller.getJTextArea1().append("Accuracy Rate on Test Set |   Rules Number   | Conditions Number   \n");
				caller.getJTextArea1().append("-------------------------------------------------------------------\n");
				caller.getJTextArea1().append("    "+myFormatter.format(totalTestAccuracyRate/folds)+"%  +/- "+myFormatter.format(calculateVariance(accuracyRatesList,(totalTestAccuracyRate/folds),folds))+"%");		
				
				double total=0.0;
				li = numberOfRulesList.listIterator();
				while(li.hasNext()){
					total += ((Double) li.next()).doubleValue();
				}
				caller.getJTextArea1().append("     |  "+myFormatter.format(total/folds)+"  +/- "+myFormatter.format(calculateVariance(numberOfRulesList,(total/folds),folds)));
				
				total=0.0;
				li = numberOfTermsList.listIterator();
				while(li.hasNext()){
					total += ((Double) li.next()).doubleValue();
				}
				caller.getJTextArea1().append("  |   "+myFormatter.format(total/folds)+"  +/- "+myFormatter.format(calculateVariance(numberOfTermsList,(total/folds),folds)));
				
				caller.getJTextArea1().append("\n\nTotal elapsed time: "+((new Date().getTime() - date.getTime())/1000)+" s.\n");
			}else
				caller.getJTextArea1().append("\nCLASSIFICATION HAS BEEN CANCELED!");
		} catch (Exception e) {}
		
		caller.getJTextArea1().setCaretPosition(caller.getJTextArea1().getText().length());
		caller.getJProgressBar1().setIndeterminate(false);
		caller.setIsClassifying(false);

	}
	
	private double calculateVariance(List valuesList, double average, int folds){
		double calc = 0.0;
		ListIterator li = valuesList.listIterator();
		while(li.hasNext()){
			calc += Math.pow(((Double) li.next()).doubleValue() - average, 2.0);
		}
		calc /= folds - 1;
		calc /= folds;
		calc = Math.sqrt(calc);
		return calc;
	}
	
	private double calculateAccuracyRate(List instancesList, List antsList, int defaultClassIndex){
		int correctlyCovered = 0;
		ListIterator li = instancesList.listIterator();
		Object temp, temp2;
		ListIterator liAnt;
		ListIterator liAnt2;
		boolean covering, classesCompared;
		while(li.hasNext()){
			temp = li.next();
			liAnt = antsList.listIterator();
			classesCompared = false;
			while(liAnt.hasNext() && !classesCompared){
				Object temp3 = liAnt.next();
				liAnt2 = ((Ant) temp3).getRulesList().listIterator();
				covering = true;
				while(liAnt2.hasNext() && covering){
					temp2 = liAnt2.next();
					
					if(((DataInstance) temp).getValues().get(((int[]) temp2)[0]).equals(((Attribute) attributesList.get(((int[]) temp2)[0])).getTypes().get(((int[]) temp2)[1])))
						covering = true;
					else
						covering = false;
					
				}

				//if the rule covered the case, check if the rule consequent matches the class of the case 
				if(covering){
					if(((DataInstance) temp).getValues().get(((DataInstance) temp).getValues().size()-1).equals(((Ant) temp3).getRuleConsequent())){
						correctlyCovered++;
					}
					classesCompared = true;

				//if the case was not covered by any rule so far and there is only the default rule left, ...
				//check if the case class matches the default rule consequent
				}else if(!liAnt.hasNext()){
					if(((DataInstance) temp).getValues().get(((DataInstance) temp).getValues().size()-1).equals(((Attribute) attributesList.get(attributesList.size()-1)).getTypes().get(defaultClassIndex)))
						correctlyCovered++;
					classesCompared = true;
				}
			}
		}
		Double result = new Double(((double)correctlyCovered)/((double)instancesList.size()));		
		if(Double.isNaN(result.doubleValue())){
			result = new Double(0);
		}
		return result.doubleValue()*100;
	}
	
	private void calculateProbabilities(Ant currentAnt, List hList, List pheromoneList, List probabilityList){
		ListIterator li, li2;
		double sum = 0;
		for(int x=0;x<attributesList.size();x++){
			li = ((List) hList.get(x)).listIterator();
			li2 = ((List) pheromoneList.get(x)).listIterator();
			if(currentAnt.getMemory()[x] == 0){ //if attribute hasn't been used...
				while(li.hasNext()){
					sum += (double) ((Double) li.next()).doubleValue() * ((Double) li2.next()).doubleValue();
				}
			}
		}
		li = probabilityList.listIterator();
		ListIterator lih = hList.listIterator();
		ListIterator liPhero = pheromoneList.listIterator(); 
		ListIterator lih2;
		ListIterator liPhero2;
		int x=0;
		while(li.hasNext()){
			li2 = ((List) li.next()).listIterator();
			lih2 = ((List) lih.next()).listIterator();
			liPhero2 = ((List) liPhero.next()).listIterator();
			while(li2.hasNext()){
					li2.next();
					
					Object temp= lih2.next();
					Object temp2 = liPhero2.next();
					
					if(currentAnt.getMemory()[x] == 0){
						Double result = new Double((((Double) temp).doubleValue() * ((Double) temp2).doubleValue()) / sum);
						
						//if division by zero, attribute cannot be used
						if(Double.isNaN(result.doubleValue())){
							result = new Double(0);
							currentAnt.getMemory()[x] = 2;
						}
						li2.set(result);
					}
					else
						li2.set(new Double(0));
				}
			x++;
		}
	}
	
	private void determineRuleConsequent(Ant ant){
		int [] freqClass = new int[numClasses];
		ListIterator li;
		li = ant.getInstancesIndexList().listIterator();
		while(li.hasNext()){
			int trainingSetIndex = ((Integer) li.next()).intValue();
			int classIndex = ((Attribute) attributesList.get(attributesList.size()-1)).getTypes().indexOf( ((DataInstance) trainingSet.get(trainingSetIndex)).getValues().get(((DataInstance) trainingSet.get(trainingSetIndex)).getValues().size()-1) );
			freqClass[classIndex]++;
		}
		int largestFreq = 0, largestFreqIndex = -1;
		for(int x=0;x<numClasses;x++){
			if(largestFreq < freqClass[x]){
				largestFreq = freqClass[x];
				largestFreqIndex = x;
			}
		}
		if(largestFreqIndex != -1)
			ant.setRuleConsequent(((Attribute) attributesList.get(attributesList.size()-1)).getTypes().get(largestFreqIndex).toString());
	}
	
	private void calculateRuleQuality(Ant ant){
		ListIterator li;
		double quality;
		int vp, fp, fn, vn;
		vp=fp=fn=vn=0;
		int dataInstanceIndex=0;
		li = trainingSet.listIterator();
		while(li.hasNext()){
			li.next();
			//if the data instance is covered by the rule...
			if(ant.getInstancesIndexList().contains(new Integer(dataInstanceIndex))){
				if(((DataInstance) trainingSet.get(dataInstanceIndex)).getValues().get(attributesList.size()-1).equals(ant.getRuleConsequent()))
					vp++;
				else
					fp++;
			}else{
				if(((DataInstance) trainingSet.get(dataInstanceIndex)).getValues().get(attributesList.size()-1).equals(ant.getRuleConsequent()))
					fn++;
				else
					vn++;
			}
			dataInstanceIndex++;
		}
		quality = ((double) vp / (vp + fn)) * ((double) vn / (fp + vn));
		if(Double.isNaN(quality))
			quality = 0.0;		
		ant.setRuleQuality(quality);
	}
	
	private boolean ruleConstructor(Ant ant, int i, int j){
		ListIterator liTraining, li3;
		Object temp2;
		
		List indexToRemoveList = new LinkedList();
		liTraining = trainingSet.listIterator();
		int dataInstanceIndex=0;
		int total = 0;
		if(ant.getRulesList().size() == 0){
			while(liTraining.hasNext()){
				if(((DataInstance) liTraining.next()).getValues().get(i).equals(((Attribute) attributesList.get(i)).getTypes().get(j))){
					ant.getInstancesIndexList().add(new Integer(dataInstanceIndex));
					total++;
				}
				dataInstanceIndex++;
			}
			if(total < minCasesRule){
				ant.getRulesList().clear();
				ant.getInstancesIndexList().clear();
			}
		}else{
			ListIterator liInstancesIndex = ant.getInstancesIndexList().listIterator();
			while(liInstancesIndex.hasNext()){
				temp2 = liInstancesIndex.next();
				if(((DataInstance) trainingSet.get(((Integer) temp2).intValue())).getValues().get(i).equals(((Attribute) attributesList.get(i)).getTypes().get(j)))
					total++;
				else
					indexToRemoveList.add(temp2);
			}
		}
		
		int [] termij = new int[2];
		termij[0] = i;
		termij[1] = j;
		if(total >= minCasesRule){
			ant.getRulesList().add(termij);
			ant.getMemory()[i] = 1; //cannot use attribute i to construct rule anymore
			li3 = indexToRemoveList.listIterator();
			while(li3.hasNext()){
				ant.getInstancesIndexList().remove(ant.getInstancesIndexList().indexOf(li3.next()));
			}
			return true;
		}else{			
			return false; //term i was analyzed but could not be used
		}
	}
	
	private void printHeader(){
		ListIterator li;
		if(caller.getJCheckBox1IsSelected())
			caller.getJTextArea1().setText(null);
		caller.getJTextArea1().append("=== Run Information ===\n\n");
		caller.getJTextArea1().append("Relation:   " + caller.getJLabel2().getText() + "\n");
		caller.getJTextArea1().append("Instances:  " + dataInstancesList.size() + "\n");
		caller.getJTextArea1().append("Attributes: " + attributesList.size() + "\n");
		li = attributesList.listIterator();
		while(li.hasNext()){
			caller.getJTextArea1().append("            " + ((Attribute) li.next()).getAttributeName() + "\n");
		}
		caller.getJTextArea1().append("\nUser-defined Parameters\n\n");
		caller.getJTextArea1().append("Folds:                 "+caller.getjTextField2Value()+"\n");
		caller.getJTextArea1().append("Number of Ants:        "+caller.getJTextField1().getText()+"\n");
		caller.getJTextArea1().append("Min. Cases per Rule:   "+caller.getJTextField3().getText()+"\n");
		caller.getJTextArea1().append("Max. uncovered Cases:  "+caller.getJTextField4().getText()+"\n");
		caller.getJTextArea1().append("Rules for Convergence: "+caller.getJTextField5().getText()+"\n");
		caller.getJTextArea1().append("Number of Iterations:  "+caller.getJTextField6().getText()+"\n");
	}
	
	//assign each case a number with a value between 0 and the number of cross-validation folds -1 
	private void group(){
		Random random = new Random();
		Object temp;
		int randomNumber;
		
		loosenGroups();
		
		ListIterator i = dataInstancesList.listIterator();
		while(i.hasNext()){
			temp = i.next();
			while(((DataInstance) temp).getCrossValidationGroup() == -1){
				randomNumber = (random.nextInt() << 1 >>> 1) % folds;
				if(control[randomNumber] >= 0){
					control[randomNumber]--;
					((DataInstance) temp).setCrossValidationGroup(randomNumber);
				}
			}
		}
	}
	
	//undo previously formed groups by applying -1 to the value of each case group 
	private void loosenGroups(){
		ListIterator i = dataInstancesList.listIterator();
		while(i.hasNext()){			
			((DataInstance) i.next()).setCrossValidationGroup(-1);
		}
	}
	
	public double log2(double d) {
		return Math.log(d)/Math.log(2.0);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -