public void testing_saving_and_loading_saved_model() { // Save Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv")). Classifiers.Trees.RandomForest. NumExecutionSlots(4). NumFeatures(5). NumTrees(50). FlushToFile("titanic_randor_forest.model"); // Load IUntypedBaseClassifier <weka.classifiers.Classifier> classifier = BaseClassifier.Read("titanic_randor_forest.model"); TitanicDataRow row = new TitanicDataRow { age = 10, pclass = "1", sex = "male", embarked = "C" }; // Classify double prediction = classifier.ClassifyInstance(Runtime.BuildInstance(0, row)); double proba = classifier.ClassifyInstanceProba(Runtime.BuildInstance(0, row)); Assert.AreEqual(0.0, prediction); Assert.IsTrue(proba < 0.5); }
public List <string> GeneratePredictions( Func <double, int, string> outputline, IUntypedBaseClassifier <Classifier> classifier, string logfile = null, Func <int, double> exception_value = null, bool quiet = false) { if (classifier.Runtime == this) { throw new ArgumentException("It appears that the training set is being used to generate predictions. The Runtime used to train the classifier(s) should the different than the Runtime used when calling GeneratePredictions."); } return(new PredictionsGenerator(this).GeneratePredictionsImpl( outputline, classifier, logfile, exception_value, quiet)); }
public static void FlushToFile(IUntypedBaseClassifier <Classifier> classifier, string file, bool quiet = false) { FlushToFile(classifier.Impl, file, quiet); }
internal List <string> GeneratePredictionsImpl( Func <double, int, string> outputline, IUntypedBaseClassifier <Classifier> classifier, string logfile = null, Func <int, double> exception_value = null, bool quiet = false) { var outlines = new List <string>(); var failures = 0; var results = new Dictionary <double, int>(); for (int i = 0; i < rt.NumInstances; i++) { double classification; try { classification = classifier.ClassifyInstance(rt[i]); } catch (Exception) { if (exception_value == null) { throw; } classification = exception_value(i); failures++; } if (!results.ContainsKey(classification)) { results[classification] = 1; } else { results[classification]++; } if (outputline != null) { outlines.Add(outputline(classification, i)); } } if (failures > 0) { Console.WriteLine("GeneratePredictions had " + failures + " failures."); } if (!quiet) { Console.WriteLine("Predictions:\n\t" + String.Join("\n\t", results. Select(kvp => kvp.Key + ": " + kvp.Value + " - " + (kvp.Value * 100.0 / rt.NumInstances).ToString("N5")))); } if (String.IsNullOrEmpty(logfile)) { logfile = String.Format("{0}_predictions_{1}.csv", classifier.GetType().Name, DateTime.Now.ToString("yyyyMMdd_HHmmss")); } if (outlines.Count > 0) { File.WriteAllLines(logfile, outlines); } return(outlines); }