public int ClassifyInstance(double[] attributes, out double[] percentages)
            {
                double classificationResult = 1.0;

                testInstance.setDataset(dataSet);
                testInstance.setClassMissing();
                dataSet.add(testInstance);
                for (int i = 0; i < attributes.Length; i++)
                {
                    testInstance.setValue(i, attributes[i]);
                }
                classificationResult = m_cl.classifyInstance(testInstance);
                dataSet.delete(0);
                percentages = m_cl.distributionForInstance(testInstance);
                return((int)classificationResult);
            }
Ejemplo n.º 2
0
		/// <summary> Method for testing filters ability to process multiple batches.
		/// 
		/// </summary>
		/// <param name="argv">should contain the following arguments:<br>
		/// -i (first) input file <br>
		/// -o (first) output file <br>
		/// -r (second) input file <br>
		/// -s (second) output file <br>
		/// -c class_index <br>
		/// or -h for help on options
		/// </param>
		/// <exception cref="Exception">if something goes wrong or the user requests help on
		/// command options
		/// </exception>
		public static void  batchFilterFile(Filter filter, System.String[] options)
		{
			
			Instances firstData = null;
			Instances secondData = null;
			//UPGRADE_ISSUE: Class hierarchy differences between 'java.io.Reader' and 'System.IO.StreamReader' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1186'"
			System.IO.StreamReader firstInput = null;
			//UPGRADE_ISSUE: Class hierarchy differences between 'java.io.Reader' and 'System.IO.StreamReader' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1186'"
			System.IO.StreamReader secondInput = null;
			System.IO.StreamWriter firstOutput = null;
			System.IO.StreamWriter secondOutput = null;
			bool helpRequest;
			try
			{
				helpRequest = Utils.getFlag('h', options);
				
				System.String fileName = Utils.getOption('i', options);
				if (fileName.Length != 0)
				{
					//UPGRADE_TODO: The differences in the expected value  of parameters for constructor 'java.io.BufferedReader.BufferedReader'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
					//UPGRADE_WARNING: At least one expression was used more than once in the target code. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1181'"
					//UPGRADE_TODO: Constructor 'java.io.FileReader.FileReader' was converted to 'System.IO.StreamReader' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
					firstInput = new System.IO.StreamReader(new System.IO.StreamReader(fileName, System.Text.Encoding.Default).BaseStream, new System.IO.StreamReader(fileName, System.Text.Encoding.Default).CurrentEncoding);
				}
				else
				{
					throw new System.Exception("No first input file given.\n");
				}
				
				fileName = Utils.getOption('r', options);
				if (fileName.Length != 0)
				{
					//UPGRADE_TODO: The differences in the expected value  of parameters for constructor 'java.io.BufferedReader.BufferedReader'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
					//UPGRADE_WARNING: At least one expression was used more than once in the target code. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1181'"
					//UPGRADE_TODO: Constructor 'java.io.FileReader.FileReader' was converted to 'System.IO.StreamReader' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
					secondInput = new System.IO.StreamReader(new System.IO.StreamReader(fileName, System.Text.Encoding.Default).BaseStream, new System.IO.StreamReader(fileName, System.Text.Encoding.Default).CurrentEncoding);
				}
				else
				{
					throw new System.Exception("No second input file given.\n");
				}
				
				fileName = Utils.getOption('o', options);
				if (fileName.Length != 0)
				{
					//UPGRADE_TODO: Constructor 'java.io.FileOutputStream.FileOutputStream' was converted to 'System.IO.FileStream.FileStream' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioFileOutputStreamFileOutputStream_javalangString'"
					firstOutput = new System.IO.StreamWriter(new System.IO.FileStream(fileName, System.IO.FileMode.Create), System.Text.Encoding.Default);
				}
				else
				{
					firstOutput = new System.IO.StreamWriter(System.Console.OpenStandardOutput(), System.Text.Encoding.Default);
				}
				
				fileName = Utils.getOption('s', options);
				if (fileName.Length != 0)
				{
					//UPGRADE_TODO: Constructor 'java.io.FileOutputStream.FileOutputStream' was converted to 'System.IO.FileStream.FileStream' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioFileOutputStreamFileOutputStream_javalangString'"
					secondOutput = new System.IO.StreamWriter(new System.IO.FileStream(fileName, System.IO.FileMode.Create), System.Text.Encoding.Default);
				}
				else
				{
					secondOutput = new System.IO.StreamWriter(System.Console.OpenStandardOutput(), System.Text.Encoding.Default);
				}
				System.String classIndex = Utils.getOption('c', options);
				
				//			if (filter instanceof OptionHandler) 
				//			{
				//				((OptionHandler)filter).setOptions(options);
				//			}
				Utils.checkForRemainingOptions(options);
				
				if (helpRequest)
				{
					throw new System.Exception("Help requested.\n");
				}
				firstData = new Instances(firstInput, 1);
				secondData = new Instances(secondInput, 1);
				if (!secondData.equalHeaders(firstData))
				{
					throw new System.Exception("Input file formats differ.\n");
				}
				if (classIndex.Length != 0)
				{
					if (classIndex.Equals("first"))
					{
						firstData.ClassIndex = 0;
						secondData.ClassIndex = 0;
					}
					else if (classIndex.Equals("last"))
					{
						firstData.ClassIndex = firstData.numAttributes() - 1;
						secondData.ClassIndex = secondData.numAttributes() - 1;
					}
					else
					{
						firstData.ClassIndex = System.Int32.Parse(classIndex) - 1;
						secondData.ClassIndex = System.Int32.Parse(classIndex) - 1;
					}
				}
			}
			catch (System.Exception ex)
			{
				System.String filterOptions = "";
				// Output the error and also the valid options
				//			if (filter instanceof OptionHandler) 
				//			{
				//				filterOptions += "\nFilter options:\n\n";
				//				Enumeration enu = ((OptionHandler)filter).listOptions();
				//				while (enu.hasMoreElements()) 
				//				{
				//					Option option = (Option) enu.nextElement();
				//					filterOptions += option.synopsis() + '\n'
				//						+ option.description() + "\n";
				//				}
				//			}
				
				System.String genericOptions = "\nGeneral options:\n\n" + "-h\n" + "\tGet help on available options.\n" + "-i <filename>\n" + "\tThe file containing first input instances.\n" + "-o <filename>\n" + "\tThe file first output instances will be written to.\n" + "-r <filename>\n" + "\tThe file containing second input instances.\n" + "-s <filename>\n" + "\tThe file second output instances will be written to.\n" + "-c <class index>\n" + "\tThe number of the attribute to use as the class.\n" + "\t\"first\" and \"last\" are also valid entries.\n" + "\tIf not supplied then no class is assigned.\n";
				
				//UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
				throw new System.Exception('\n' + ex.Message + filterOptions + genericOptions);
			}
			bool printedHeader = false;
			if (filter.setInputFormat(firstData))
			{
				//UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'"
				firstOutput.WriteLine(filter.getOutputFormat().ToString());
				printedHeader = true;
			}
			
			// Pass all the instances to the filter
			while (firstData.readInstance(firstInput))
			{
				if (filter.input(firstData.instance(0)))
				{
					if (!printedHeader)
					{
						throw new System.ApplicationException("Filter didn't return true from setInputFormat() " + "earlier!");
					}
					//UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'"
					firstOutput.WriteLine(filter.output().ToString());
				}
				firstData.delete(0);
			}
			
			// Say that input has finished, and print any pending output instances
			if (filter.batchFinished())
			{
				if (!printedHeader)
				{
					//UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'"
					firstOutput.WriteLine(filter.getOutputFormat().ToString());
				}
				while (filter.numPendingOutput() > 0)
				{
					//UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'"
					firstOutput.WriteLine(filter.output().ToString());
				}
			}
			
			if (firstOutput != null)
			{
				//UPGRADE_NOTE: Exceptions thrown by the equivalent in .NET of method 'java.io.PrintWriter.close' may be different. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1099'"
				firstOutput.Close();
			}
			printedHeader = false;
			if (filter.OutputFormatDefined)
			{
				//UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'"
				secondOutput.WriteLine(filter.getOutputFormat().ToString());
				printedHeader = true;
			}
			// Pass all the second instances to the filter
			while (secondData.readInstance(secondInput))
			{
				if (filter.input(secondData.instance(0)))
				{
					if (!printedHeader)
					{
						throw new System.ApplicationException("Filter didn't return true from" + " isOutputFormatDefined() earlier!");
					}
					//UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'"
					secondOutput.WriteLine(filter.output().ToString());
				}
				secondData.delete(0);
			}
			
			// Say that input has finished, and print any pending output instances
			if (filter.batchFinished())
			{
				if (!printedHeader)
				{
					//UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'"
					secondOutput.WriteLine(filter.getOutputFormat().ToString());
				}
				while (filter.numPendingOutput() > 0)
				{
					//UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'"
					secondOutput.WriteLine(filter.output().ToString());
				}
			}
			if (secondOutput != null)
			{
				//UPGRADE_NOTE: Exceptions thrown by the equivalent in .NET of method 'java.io.PrintWriter.close' may be different. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1099'"
				secondOutput.Close();
			}
		}
Ejemplo n.º 3
0
    // Test the classification result of each map that a user played,
    // with the data available as if they were playing through it
    public static void classifyTest(String dataString, String playerID)
    {
        try {
            java.io.StringReader   stringReader = new java.io.StringReader(dataString);
            java.io.BufferedReader buffReader   = new java.io.BufferedReader(stringReader);

            /* NOTE THAT FOR NAIVE BAYES ALL WEIGHTS CAN BE = 1*/
            //weka.core.converters.ConverterUtils.DataSource source = new weka.core.converters.ConverterUtils.DataSource("iris.arff");
            weka.core.Instances thisData = new weka.core.Instances(buffReader);             //source.getDataSet();
            if (thisData.classIndex() == -1)
            {
                thisData.setClassIndex(thisData.numAttributes() - 1);
            }

            weka.core.Instances thisUniqueData = new weka.core.Instances(thisData);
            if (thisUniqueData.classIndex() == -1)
            {
                thisUniqueData.setClassIndex(thisUniqueData.numAttributes() - 1);
            }
            thisUniqueData.delete();

            if (allUniqueData == null)
            {
                allUniqueData = new weka.core.Instances(thisData);
                if (allUniqueData.classIndex() == -1)
                {
                    allUniqueData.setClassIndex(allUniqueData.numAttributes() - 1);
                }
                allUniqueData.delete();
            }

            weka.core.InstanceComparator com = new weka.core.InstanceComparator(false);

            for (int i = 0; i < thisData.numInstances(); i++)
            {
                bool dup = false;
                for (int j = 0; j < allUniqueData.numInstances(); j++)
                {
                    if (com.compare(thisData.instance(i), allUniqueData.instance(j)) == 0)
                    {
                        Debug.Log("Duplicate found!");
                        dup = true;
                        break;
                    }
                }
                if (!dup)
                {
                    allUniqueData.add(thisData.instance(i));
                }
                else
                {
                    dupInstances++;
                }
            }

            for (int i = 0; i < thisData.numInstances(); i++)
            {
                bool dup = false;
                for (int j = 0; j < thisUniqueData.numInstances(); j++)
                {
                    if (com.compare(thisData.instance(i), thisUniqueData.instance(j)) == 0)
                    {
                        Debug.Log("Duplicate found!");
                        dup = true;
                        break;
                    }
                }
                if (!dup)
                {
                    thisUniqueData.add(thisData.instance(i));
                }
                else
                {
                    dupInstancesSamePlayer++;
                }
            }


            //Debug.Log("All Data Instance Count = " + thisData.numInstances());
            //Debug.Log("Unique Data Instance Count = " + thisUniqueData.numInstances());
            //Debug.Log("Done!");
        } catch (java.lang.Exception ex)
        {
            Debug.LogError(ex.getMessage());
        }
    }