noisecurvecrossvalidationresultproducer.java

来自「wekaUT是 university texas austin 开发的基于wek」· Java 代码 · 共 1,346 行 · 第 1/3 页

JAVA
1,346
字号
    	return m_SplitEvaluator;    }      /**     * Set the SplitEvaluator.     *     * @@param newSplitEvaluator new SplitEvaluator to use.     */    public void setSplitEvaluator(SplitEvaluator newSplitEvaluator) {    	m_SplitEvaluator = newSplitEvaluator;	m_SplitEvaluator.setAdditionalMeasures(m_AdditionalMeasures);    }    /**     * Returns an enumeration describing the available options..     *     * @@return an enumeration of all the available options.     */    public Enumeration listOptions() {	Vector newVector = new Vector(10);	newVector.addElement(new Option(					"\tThe number of folds to use for the cross-validation.\n"					+"\t(default 10)", 					"X", 1, 					"-X <number of folds>"));	newVector.addElement(new Option(					"\tA list of specific points to plot as a string of numbers\n"+					"separated by commas or spaces.\n"+					"Whole numbers indicate a specific number of examples,\n", 					"P", 1, 	 				"-P <point list>"));	newVector.addElement(new Option(					"Save raw split evaluator output.",					"D",0,"-D"));	newVector.addElement(new Option(					"Noise add to Class in Training.",					"N",0,"-N"));	newVector.addElement(new Option(					"Noise add to Feature in Training.",					"F",0,"-F"));	newVector.addElement(new Option(					"Set Features Missing in Training.",					"M",0,"-M"));	newVector.addElement(new Option(					"Noise add to Class in Testing (Overridden if Option -N not selected).",					"n",0,"-n"));	newVector.addElement(new Option(					"Noise add to Feature in Testing (Overridden if Option -F not selected).",					"f",0,"-f"));	newVector.addElement(new Option(					"Set Features Missing in Testing (Overridden if Option -M not selected).",					"m",0,"-m"));	newVector.addElement(new Option(					"\tThe filename where raw output will be stored.\n"					+"\tIf a directory name is specified then then individual\n"					+"\toutputs will be gzipped, otherwise all output will be\n"					+"\tzipped to the named file. Use in conjuction with -D."					+"\t(default splitEvalutorOut.zip)", 					"O", 1, 					"-O <file/directory name/path>"));	newVector.addElement(new Option(					"\tThe full class name of a SplitEvaluator.\n"					+"\teg: weka.experiment.ClassifierSplitEvaluator", 					"W", 1, 					"-W <class name>"));	if ((m_SplitEvaluator != null) &&	    (m_SplitEvaluator instanceof OptionHandler)) {	    newVector.addElement(new Option(					    "",					    "", 0, "\nOptions specific to split evaluator "					    + m_SplitEvaluator.getClass().getName() + ":"));	    Enumeration enum = ((OptionHandler)m_SplitEvaluator).listOptions();	    while (enum.hasMoreElements()) {		newVector.addElement(enum.nextElement());	    }	}	return newVector.elements();    }    /**     * Parses a given list of options. Valid options are:<p>     *     * -X num_folds <br>     * The number of folds to use for the cross-validation. <p>     *     * -D <br>     * Specify that raw split evaluator output is to be saved. <p>     *     * -O file/directory name <br>     * Specify the file or directory to which raw split evaluator output     * is to be saved. If a directory is specified, then each output string     * is saved as an individual gzip file. If a file is specified, then     * each output string is saved as an entry in a zip file. <p>     *     * -W classname <br>     * Specify the full class name of the split evaluator. <p>     *     * -N Add Noise to Class in Training     * -n Add Noise to Class in Testing     * -F Add Noise to Features in Training     * -f Add Noise to Features in Testing     * -M Set Features Missing in Training     * -m Set Features Missing in Testing     *     * All option after -- will be passed to the split evaluator.     *     * @@param options the list of options as an array of strings     * @@exception Exception if an option is not supported     */    public void setOptions(String[] options) throws Exception {    	setRawOutput(Utils.getFlag('D', options));	//First we set the Test parameters, if the Train parameters are false, they will in the later set turn these parameters off to override	setclassNoiseTest(Utils.getFlag('n', options));	setfeatureNoiseTest(Utils.getFlag('f', options));	setfeatureMissTest(Utils.getFlag('m', options));	setclassNoise(Utils.getFlag('N', options));	setfeatureNoise(Utils.getFlag('F', options));	setfeatureMiss(Utils.getFlag('M', options));	String fName = Utils.getOption('O', options);	if (fName.length() != 0) {	    setOutputFile(new File(fName));	}	String numFolds = Utils.getOption('X', options);	if (numFolds.length() != 0) {	    setNumFolds(Integer.parseInt(numFolds));	} else {	    setNumFolds(10);	}	String plotPoints = Utils.getOption('P', options);	if (plotPoints.length() != 0) {	    setPlotPoints(plotPoints);	} else {	    setPlotPoints("");	}		String seName = Utils.getOption('W', options);	if (seName.length() == 0) {	    throw new Exception("A SplitEvaluator must be specified with"				+ " the -W option.");	}	// Do it first without options, so if an exception is thrown during	// the option setting, listOptions will contain options for the actual	// SE.	setSplitEvaluator((SplitEvaluator)Utils.forName(							SplitEvaluator.class,							seName,							null));	if (getSplitEvaluator() instanceof OptionHandler) {	    ((OptionHandler) getSplitEvaluator())		.setOptions(Utils.partitionOptions(options));	}    }    /**     * Gets the current settings of the result producer.     *     * @@return an array of strings suitable for passing to setOptions     */    public String [] getOptions() {	String [] seOptions = new String [0];	if ((m_SplitEvaluator != null) && 	    (m_SplitEvaluator instanceof OptionHandler)) {	    seOptions = ((OptionHandler)m_SplitEvaluator).getOptions();	}	//CHECK VALUE OF seOptions.length + 16 + 4 - 6 + 8	String [] options = new String [seOptions.length + 20 + 8];	int current = 0;	options[current++] = "-X"; options[current++] = "" + getNumFolds();	if (getRawOutput()) {	    options[current++] = "-D";	}	if (getclassNoise()) {	    options[current++] = "-N";	}	if (getfeatureNoise()) {	    options[current++] = "-F";	}	if (getfeatureMiss()) {	    options[current++] = "-M";	}	if (getclassNoiseTest()) {	    options[current++] = "-n";	}	if (getfeatureNoiseTest()) {	    options[current++] = "-f";	}	if (getfeatureMissTest()) {	    options[current++] = "-m";	}	options[current++] = "-O"; 	options[current++] = getOutputFile().getName();	options[current++] = "-P";	options[current++] = getPlotPoints();	if (getSplitEvaluator() != null) {	    options[current++] = "-W";	    options[current++] = getSplitEvaluator().getClass().getName();	}	options[current++] = "--";	System.arraycopy(seOptions, 0, options, current, 			 seOptions.length);	current += seOptions.length;	while (current < options.length) {	    options[current++] = "";	}	return options;    }    /**     * Gets a text descrption of the result producer.     *     * @@return a text description of the result producer.     */    public String toString() {	String result = "NoiseCurveCrossValidationResultProducer: ";	result += getCompatibilityState();	if (m_Instances == null) {	    result += ": <null Instances>";	} else {	    result += ": " +  Utils.backQuoteChars(m_Instances.relationName());	}	return result;    }    public void addClassNoise(Instances train, Instances test, int noiseLevel) throws Exception {	Instance curr;	double noisePercent = m_PlotPoints[noiseLevel];	//The experiment will be repeatable for a particular run	//Adding to Training	//Method 1 - Did not use this	// For each instance, toss a coin, determine whether to change the class	// If decided to change, randomly select from the range of class calues	//Method 2 - Used this	// Find number (N) of instances to add noise to	// Loop for N	// Randomly choose an instance	// Randomly choose class label	// Assign new class label	double N1 = (noisePercent / 100.0);	double N2 = train.numInstances() * N1;	int N = (int) N2;	for (int i = 0; i < N; i++){	    //Pick an instance randomly	    int instanceNumber = (int)(m_Random.nextDouble() * train.numInstances());	    //Instance pointed by current curr	    curr = train.instance(instanceNumber);	    //Toss a coin and change if Class Label is Nominal	    if (train.classAttribute().isNominal()){		int oldClass = (int)train.instance(instanceNumber).classValue();		int newClass = (int)(m_Random.nextDouble() * train.numClasses());		while (newClass == oldClass)		    {			newClass = (int)(m_Random.nextDouble() * train.numClasses());		    }		curr.setClassValue(newClass);	    }	    else{		System.out.println("CLASS NOT NOMINAL");	    }	}//end of adding noise to train class	    	//If true m_classNoiseTest Adding to Testing		if (m_classNoiseTest){	    double N3 = (noisePercent / 100.0);	    double N4 = test.numInstances() * N3;	    int NN = (int) N4;	    for (int i = 0; i < NN; i++){		//Pick an instance randomly		int instanceNumber = (int)(m_Random.nextDouble() * test.numInstances());		//Instance pointed by current curr		curr = test.instance(instanceNumber);		//Toss a coin and change if Class Label is Nominal		if (test.classAttribute().isNominal()){		    curr.setClassValue((int)(m_Random.nextDouble() * test.numClasses()));		}		else{		    System.out.println(" CLASS NOT NOMINAL");		}	    }	}//end of adding noise to test class	    }//end of classnoise    public void addFeatureNoise(Instances train, Instances test, int noiseLevel) throws Exception {	//Add noise to Training Set	//Find number of Features (N) to be added noise to	Instance curr;	double noisePercent = m_PlotPoints[noiseLevel];	double N1 = (noisePercent / 100.0);	double N2 = train.numInstances() * (train.numAttributes()-1) * N1;	int N = (int) N2;	//Loop for N	for (int i = 0; i < N; i++)	    {				//Pick a random instance		int instanceNumber = (int)(m_Random.nextDouble() * train.numInstances());		curr = train.instance(instanceNumber);		//Pick this Instance's feature randomly		int attIndex = (int)(m_Random.nextDouble() * curr.numAttributes());		double attValue = 0.0;		if (attIndex == curr.classIndex())		    {			while (attIndex == curr.classIndex()){			    attIndex = (int)(m_Random.nextDouble() * curr.numAttributes());			}					    }		//Check if Nominal add noise randomly		if (curr.attribute(attIndex).isNominal())		    {			attValue = (double)(int)(m_Random.nextDouble() * curr.attribute(attIndex).numValues());			curr.setValue(attIndex, (double)attValue);		    }		//If Numeric find mean, standard Deviation, find Gaussian value		if (curr.attribute(attIndex).isNumeric())		    {			double []stats = (double [])m_AttributeStats.get(attIndex);			attValue = (double)((m_Random.nextGaussian() * stats[1]) + stats[0]);			attValue = Utils.roundDouble((double)attValue, 2);			curr.setValue(attIndex, (double)attValue);			// For Checking Trace::: System.out.println("Feature, Train, Numeric, Total Noisey" + N + "Instance Num: " + instanceNumber + "AttIndex: " + attIndex + "Value : " + attValue + "Hence: " + curr.toString());		    }	    }	//If true m_featureNoiseTest, Add noise to Test Set	if (m_featureNoiseTest == true)	    {				//Find number of Features (NN) to be added noise to		double N3 = (noisePercent / 100.0);		double N4 = test.numInstances() * (test.numAttributes()-1) * N3;		int NN = (int) N4;		//Loop for NN		for (int i = 0; i < NN; i++)		    {					//Pick a random instance			int instanceNumber = (int)(m_Random.nextDouble() * test.numInstances());			curr = test.instance(instanceNumber);			//Pick this Instance's feature randomly			int attIndex = (int)(m_Random.nextDouble() * curr.numAttributes());			double attValue = 0.0;			if (attIndex == curr.classIndex())			    {				while (attIndex == curr.classIndex()){				    attIndex = (int)(m_Random.nextDouble() * curr.numAttributes());				}			    }			//Check if Nominal add noise randomly			if (curr.attribute(attIndex).isNominal())			    {				attValue = (double)(int)(m_Random.nextDouble() * curr.attribute(attIndex).numValues());				curr.setValue(attIndex, (double)attValue);				//For Checking Trace::: System.out.println("Feature, Nominal, Testing, Total Noisey: " + NN + ": Instance Num: " + instanceNumber + "AttIndex: " + attIndex + "Value : " + attValue + ": " + curr.toString());			    }			//If Numeric find mean, standard Deviation, find Gaussian value			if (curr.attribute(attIndex).isNumeric())			    {				double []stats = (double [])m_AttributeStats.get(attIndex);				attValue = (double)((m_Random.nextGaussian() * stats[1]) + stats[0]);				attValue = Utils.roundDouble((double)attValue, 2);				curr.setValue(attIndex, (double)attValue);			    }		    }	    }//end of feature noise test    }//End of Feature Noise        public void addFeatureMiss(Instances train, Instances test, int noiseLevel) throws Exception {	//IF WE WANT TO INCLUDE MISSING IN CLASS THEN REMOVE -1 from N2 calculation and remove while loop	//for Training set	//find number of feature to make missing	Instance curr;	double noisePercent = m_PlotPoints[noiseLevel];	double N1 = (noisePercent / 100.0);	double N2 = train.numInstances() * (train.numAttributes()-1) * N1;	int N = (int) N2;	//Loop for number of feature to make missing	for (int i = 0; i < N; i++)	    {				//Pick a random instance   		int instanceNumber = (int)(m_Random.nextDouble() * train.numInstances());		curr = train.instance(instanceNumber);		//Pick this Instance's feature, randomly choose an attribute		int attIndex = (int)(m_Random.nextDouble() * curr.numAttributes());		if (attIndex == curr.classIndex())		    {			while (attIndex == curr.classIndex()){			    attIndex = (int)(m_Random.nextDouble() * curr.numAttributes());			}					    }		//Set it missing		//For Checking Trace::: System.out.println("Train, Total Missing " + N + " Setting instance" + instanceNumber + "Feature " + attIndex + "Missing");		curr.setMissing(attIndex);	    }      	//If true, m_featureMissTest,  for test set	if (m_featureMissTest == true)	    {		double N3 = (noisePercent / 100.0);		double N4 = test.numInstances() * (test.numAttributes()-1) * N3;		int NN = (int) N4;		//Loop for number of feature to make missing		for (int i = 0; i < NN; i++)		    {					//Pick a random instance   			int instanceNumber = (int)(m_Random.nextDouble() * test.numInstances());			curr = test.instance(instanceNumber);			//Pick this Instance's feature randomly	//randomly choose an attribute			int attIndex = (int)(m_Random.nextDouble() * curr.numAttributes());			if (attIndex == curr.classIndex())			    {				while (attIndex == curr.classIndex()){				    attIndex = (int)(m_Random.nextDouble() * curr.numAttributes());				}						    }			//Set it missing			//For Checking Trace::: System.out.println("Testing, Total Missing " + NN + "Setting instance" + instanceNumber + "Feature " + attIndex + "Missing");			curr.setMissing(attIndex);		    }	    }    }//End of Feature Miss    // Quick test     public static void main(String [] args) {	NoiseCurveCrossValidationResultProducer rp = new NoiseCurveCrossValidationResultProducer();	rp.setPlotPoints(args[0]);	System.out.println(rp.getPlotPoints());	if (rp.m_PlotPoints != null) System.out.println(isInteger(rp.m_PlotPoints[0]));    }} // NoiseCurveCrossValidationResultProducer

⌨️ 快捷键说明

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