当前位置: 首页>>代码示例>>C#>>正文


C# Instances.readInstance方法代码示例

本文整理汇总了C#中weka.core.Instances.readInstance方法的典型用法代码示例。如果您正苦于以下问题:C# Instances.readInstance方法的具体用法?C# Instances.readInstance怎么用?C# Instances.readInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在weka.core.Instances的用法示例。


在下文中一共展示了Instances.readInstance方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: toPrintClassifications

		/// <summary> Prints the predictions for the given dataset into a String variable.
		/// 
		/// </summary>
		/// <param name="classifier		the">classifier to use
		/// </param>
		/// <param name="train		the">training data
		/// </param>
		/// <param name="testFileName	the">name of the test file
		/// </param>
		/// <param name="classIndex		the">class index
		/// </param>
		/// <param name="attributesToOutput	the">indices of the attributes to output
		/// </param>
		/// <returns>			the generated predictions for the attribute range
		/// </returns>
		/// <throws>  Exception 		if test file cannot be opened </throws>
		protected internal static System.String toPrintClassifications(Classifier classifier, Instances train, System.String testFileName, int classIndex, Range attributesToOutput)
		{
			
			System.Text.StringBuilder text = new System.Text.StringBuilder();
			if (testFileName.Length != 0)
			{
				System.IO.StreamReader testReader = null;
				try
				{
					//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'"
					testReader = new System.IO.StreamReader(new System.IO.StreamReader(testFileName, System.Text.Encoding.Default).BaseStream, new System.IO.StreamReader(testFileName, System.Text.Encoding.Default).CurrentEncoding);
				}
				catch (System.Exception e)
				{
					//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("Can't open file " + e.Message + '.');
				}
				Instances test = new Instances(testReader, 1);
				if (classIndex != - 1)
				{
					test.ClassIndex = classIndex - 1;
				}
				else
				{
					test.ClassIndex = test.numAttributes() - 1;
				}
				int i = 0;
				while (test.readInstance(testReader))
				{
					Instance instance = test.instance(0);
					Instance withMissing = (Instance) instance.copy();
					withMissing.Dataset = test;
					double predValue = ((Classifier) classifier).classifyInstance(withMissing);
					if (test.classAttribute().Numeric)
					{
						if (Instance.isMissingValue(predValue))
						{
							text.Append(i + " missing ");
						}
						else
						{
							text.Append(i + " " + predValue + " ");
						}
						if (instance.classIsMissing())
						{
							text.Append("missing");
						}
						else
						{
							text.Append(instance.classValue());
						}
						text.Append(" " + attributeValuesString(withMissing, attributesToOutput) + "\n");
					}
					else
					{
						if (Instance.isMissingValue(predValue))
						{
							text.Append(i + " missing ");
						}
						else
						{
							//UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
							text.Append(i + " " + test.classAttribute().value_Renamed((int) predValue) + " ");
						}
						if (Instance.isMissingValue(predValue))
						{
							text.Append("missing ");
						}
						else
						{
							//UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
							text.Append(classifier.distributionForInstance(withMissing)[(int) predValue] + " ");
						}
						text.Append(instance.toString(instance.classIndex()) + " " + attributeValuesString(withMissing, attributesToOutput) + "\n");
					}
					test.delete(0);
					i++;
				}
				testReader.Close();
			}
			return text.ToString();
		}
开发者ID:intille,项目名称:mitessoftware,代码行数:100,代码来源:Evaluation.cs

示例2: evaluateModel


//.........这里部分代码省略.........
                   // addresses = (Hashtable)formatter.Deserialize(fs);
                    classifier = (Classifier)formatter.Deserialize(objectStream);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
                    throw;
                }
                finally
                {
                    objectStream.Close();
                    //fs.Close();
                }


				objectInputStream.Close();
			}
			
			// backup of fully setup classifier for cross-validation
			classifierBackup = Classifier.makeCopy(classifier);
			
			// Build the classifier if no object file provided
			if ((classifier is UpdateableClassifier) && (testFileName.Length != 0) && (costMatrix == null) && (trainFileName.Length != 0))
			{
				
				// Build classifier incrementally
				trainingEvaluation.Priors = train;
				testingEvaluation.Priors = train;
				trainTimeStart = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;
				if (objectInputFileName.Length == 0)
				{
					classifier.buildClassifier(train);
				}
				while (train.readInstance(trainReader))
				{
					
					trainingEvaluation.updatePriors(train.instance(0));
					testingEvaluation.updatePriors(train.instance(0));
					((UpdateableClassifier) classifier).updateClassifier(train.instance(0));
					train.delete(0);
				}
				trainTimeElapsed = (System.DateTime.Now.Ticks - 621355968000000000) / 10000 - trainTimeStart;
				trainReader.Close();
			}
			else if (objectInputFileName.Length == 0)
			{
				
				// Build classifier in one go
				tempTrain = new Instances(train);
				trainingEvaluation.Priors = tempTrain;
				testingEvaluation.Priors = tempTrain;
				trainTimeStart = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;
				classifier.buildClassifier(tempTrain);
				trainTimeElapsed = (System.DateTime.Now.Ticks - 621355968000000000) / 10000 - trainTimeStart;
			}
			
			// Save the classifier if an object output file is provided
			if (objectOutputFileName.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'"
				System.IO.Stream os = new System.IO.FileStream(objectOutputFileName, System.IO.FileMode.Create);
				if (objectOutputFileName.EndsWith(".gz"))
				{
					//UPGRADE_ISSUE: Constructor 'java.util.zip.GZIPOutputStream.GZIPOutputStream' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javautilzipGZIPOutputStream'"
					os = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(os);
				}
开发者ID:intille,项目名称:mitessoftware,代码行数:67,代码来源:Evaluation.cs

示例3: test

		/// <summary> Method for testing this class.
		/// 
		/// </summary>
		/// <param name="argv">should contain one element: the name of an ARFF file
		/// </param>
		//@ requires argv != null;
		//@ requires argv.length == 1;
		//@ requires argv[0] != null;
		public static void  test(System.String[] argv)
		{
			
			Instances instances, secondInstances, train, test, empty;
			//Instance instance;
			//UPGRADE_TODO: The differences in the expected value  of parameters for constructor 'java.util.Random.Random'  may cause compilation errors.  "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'"
			System.Random random = new System.Random((System.Int32) 2);
			//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 reader;
			int start, num;
			//double newWeight;
			FastVector testAtts, testVals;
			int i, j;
			
			try
			{
				if (argv.Length > 1)
				{
					throw (new System.Exception("Usage: Instances [<filename>]"));
				}
				
				// Creating set of instances from scratch
				testVals = new FastVector(2);
				testVals.addElement("first_value");
				testVals.addElement("second_value");
				testAtts = new FastVector(2);
				testAtts.addElement(new Attribute("nominal_attribute", testVals));
				testAtts.addElement(new Attribute("numeric_attribute"));
				instances = new Instances("test_set", testAtts, 10);
				instances.add(new Instance(instances.numAttributes()));
				instances.add(new Instance(instances.numAttributes()));
				instances.add(new Instance(instances.numAttributes()));
				instances.ClassIndex = 0;
				System.Console.Out.WriteLine("\nSet of instances created from scratch:\n");
				//UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Out.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'"
				System.Console.Out.WriteLine(instances);
				
				if (argv.Length == 1)
				{
					System.String filename = argv[0];
					//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'"
					reader = new System.IO.StreamReader(filename, System.Text.Encoding.Default);
					
					// Read first five instances and print them
					System.Console.Out.WriteLine("\nFirst five instances from file:\n");
					instances = new Instances(reader, 1);
					instances.ClassIndex = instances.numAttributes() - 1;
					i = 0;
					while ((i < 5) && (instances.readInstance(reader)))
					{
						i++;
					}
					//UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Out.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'"
					System.Console.Out.WriteLine(instances);
					
					// Read all the instances in the file
					//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'"
					reader = new System.IO.StreamReader(filename, System.Text.Encoding.Default);
					instances = new Instances(reader);
					
					// Make the last attribute be the class 
					instances.ClassIndex = instances.numAttributes() - 1;
					
					// Print header and instances.
					System.Console.Out.WriteLine("\nDataset:\n");
					//UPGRADE_TODO: Method 'java.io.PrintStream.println' was converted to 'System.Console.Out.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintStreamprintln_javalangObject'"
					System.Console.Out.WriteLine(instances);
					System.Console.Out.WriteLine("\nClass index: " + instances.classIndex());
				}
				
				// Test basic methods based on class index.
				System.Console.Out.WriteLine("\nClass name: " + instances.classAttribute().name());
				System.Console.Out.WriteLine("\nClass index: " + instances.classIndex());
				System.Console.Out.WriteLine("\nClass is nominal: " + instances.classAttribute().Nominal);
				System.Console.Out.WriteLine("\nClass is numeric: " + instances.classAttribute().Numeric);
				System.Console.Out.WriteLine("\nClasses:\n");
				for (i = 0; i < instances.numClasses(); i++)
				{
					System.Console.Out.WriteLine(instances.classAttribute().value_Renamed(i));
				}
				System.Console.Out.WriteLine("\nClass values and labels of instances:\n");
				for (i = 0; i < instances.numInstances(); i++)
				{
					Instance inst = instances.instance(i);
					System.Console.Out.Write(inst.classValue() + "\t");
					System.Console.Out.Write(inst.toString(inst.classIndex()));
					if (instances.instance(i).classIsMissing())
					{
						System.Console.Out.WriteLine("\tis missing");
					}
					else
					{
//.........这里部分代码省略.........
开发者ID:intille,项目名称:mitessoftware,代码行数:101,代码来源:Instances.cs


注:本文中的weka.core.Instances.readInstance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。