public void test_GetAttrDoubles() { Runtime rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow>("titanic_train.csv", 0, 5); System.Collections.Generic.IEnumerable <double> vals = rt.Impl.GetAttrDoubles(1); CollectionAssert.AreEqual(new[] { 2.0, 0.0, 2.0, 0.0, 2.0 }, vals); }
[Test] public void test_ToEnumerable() { var rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow>("titanic_train.csv", 0, 5); var instances = rt.Impl.ToEnumerable(); Assert.AreEqual(5, instances.Count()); }
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); }
[Test] public void test_GetAttrStrings() { var rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow>("titanic_train.csv", 0, 5); var vals = rt.Impl.GetAttrStrings(1); CollectionAssert.AreEqual(new [] { "3", "1", "3", "1", "3" }, vals); }
public void test_GetAttrStrings() { Runtime rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow>("titanic_train.csv", 0, 5); System.Collections.Generic.IEnumerable <string> vals = rt.Impl.GetAttrStrings(1); CollectionAssert.AreEqual(new[] { "3", "1", "3", "1", "3" }, vals); }
public void test_ToEnumerable() { Runtime rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow>("titanic_train.csv", 0, 5); System.Collections.Generic.IEnumerable <PmlInstance> instances = rt.Impl.ToEnumerable(); Assert.AreEqual(5, instances.Count()); }
public void test_attribute_to_enumerable_gives_nothing_on_numerics() { Runtime rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow2>("titanic_train.csv", 0, 10); weka.core.Attribute age = rt.Impl.attribute(4); System.Collections.Generic.IEnumerable <string> values = age.ToEnumerable(); Assert.AreEqual(new string[0], values); }
public void test_attribute_to_enumerable_gives_all_distinct_nominal_vals() { Runtime rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow>("titanic_train.csv", 0, 10); weka.core.Attribute pcclass = rt.Impl.attribute(1); System.Collections.Generic.IEnumerable <string> values = pcclass.ToEnumerable(); Assert.AreEqual(new[] { "1", "2", "3" }, values); }
public void test_ToStrings_gets_the_string_representation_of_the_instance() { Runtime rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow2>("titanic_train.csv", 0, 3); weka.core.Instance instance = rt[0].Impl; System.Collections.Generic.IEnumerable <string> strs = instance.ToStrings(); Assert.AreEqual(new[] { "0", "3", "Braund, Mr. Owen Harris", "male", "22", "1", "0", "A/5 21171", "7.25", "?", "S" }, strs); }
public void test_ToEnumerableAttributes() { Runtime rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow>("titanic_train.csv", 0, 5); System.Collections.Generic.IEnumerable <PmlAttribute> atts = rt.Impl.ToEnumerableAttributes(); string[] names = atts.Select(a => a.Name).ToArray(); CollectionAssert.AreEqual(new[] { "survived", "pclass", "sex", "embarked" }, names); }
[Test] public void test_ToStrings_gets_the_string_representation_of_the_instance() { var rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow2>("titanic_train.csv", 0, 3); var instance = rt[0].Impl; var strs = instance.ToStrings(); Assert.AreEqual(new [] { "0", "3", "Braund, Mr. Owen Harris", "male", "22", "1", "0", "A/5 21171", "7.25", "?", "S" }, strs); }
[Test] public void test_ToEnumerableAttributes() { var rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow>("titanic_train.csv", 0, 3); var atts = rt[0].Impl.ToEnumerableAttributes(); var names = atts.Select(a => a.Name).ToArray(); Assert.AreEqual(new [] { "survived", "pclass", "sex", "embarked" }, names); }
[Test] public void test_attribute_to_enumerable_gives_nothing_on_numerics() { var rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow2>("titanic_train.csv", 0, 10); var age = rt.Impl.attribute(4); var values = age.ToEnumerable(); Assert.AreEqual(new string [0], values); }
[Test] public void test_attribute_to_enumerable_gives_all_distinct_nominal_vals() { var rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow>("titanic_train.csv", 0, 10); var pcclass = rt.Impl.attribute(1); var values = pcclass.ToEnumerable(); Assert.AreEqual(new [] { "1", "2", "3" }, values); }
public void test_basic_unsupervised_attribute_filtering() { Runtime rt = Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv")); Runtime newrt = rt.Filters.UnsupervisedAttribute.RemoveByName. Expression("sex"). RunFilter(); Assert.AreEqual(rt.NumAttributes, newrt.NumAttributes + 1); }
public void test_basic_unsupervised_instance_filtering() { Runtime rt = Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv")); Runtime newrt = rt.Filters.UnsupervisedInstance.RemoveRange. InstancesIndices("1-100"). RunFilter(); Assert.AreEqual(rt.NumInstances, newrt.NumInstances + 100); }
public void test_basic_supervised_attribute_filtering() { Runtime rt = Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv")); Runtime newrt = rt.Filters.SupervisedAttribute.NominalToBinary. AttributeIndices("2"). // pcclass (1,2,3) RunFilter(); // Add 3 new binary attributes and remove orignial Assert.AreEqual(rt.NumAttributes + 3 - 1, newrt.NumAttributes); }
public void test_simple_evaluation() { Runtime rt = Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv")); rt.Classifiers.Trees.RandomForest. NumExecutionSlots(4). NumFeatures(5). NumTrees(50). EvaluateWithCrossValidation(); }
public void test_keep_attributes_by_names() { Runtime rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow>("titanic_train.csv", 0, 3); AttributesRemover ar = new AttributesRemover(rt); string[] initial = rt.EnumerateAttributes.Select(a => a.Name).ToArray(); ar.KeepAttributes("survived", "embarked", "sex"); string[] after = rt.EnumerateAttributes.Select(a => a.Name).ToArray(); Assert.AreEqual(new[] { "survived", "pclass", "sex", "embarked" }, initial); Assert.AreEqual(new[] { "survived", "sex", "embarked" }, after); }
public void test_basic_supervised_instance_filtering() { Runtime rt = Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv")); Runtime newrt = rt.Filters.SupervisedInstance.SpreadSubsample. DistributionSpread(1.0). RunFilter(); Assert.AreEqual(rt.NumInstances, 891); Assert.AreEqual(newrt.NumInstances, 684); Assert.AreEqual(newrt.Count(i => i.ClassValue == 0.0), 342); Assert.AreEqual(newrt.Count(i => i.ClassValue == 1.0), 342); }
[Test] public void test_removing_attributes_by_index() { var rt = TestingHelpers.LoadSmallRuntime <TitanicDataRow>("titanic_train.csv", 0, 3); var ar = new AttributesRemover(rt); var initial = rt.EnumerateAttributes.Select(a => a.Name).ToArray(); ar.RemoveAttributes(2, 3); var after = rt.EnumerateAttributes.Select(a => a.Name).ToArray(); Assert.AreEqual(new [] { "survived", "pclass", "sex", "embarked" }, initial); Assert.AreEqual(new [] { "survived", "pclass" }, after); }
public void simple_attribute_selection_tests_with_indexes() { Runtime rt = Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv")); BestFirst alg = rt.AttributeSelections.Algorithms.BestFirst. Direction(BestFirst.EDirection.Bi_directional). LookupCacheSize(10); PicNetML.AttrSel.Evals.CfsSubset eval = rt.AttributeSelections.Evaluators.CfsSubset. LocallyPredictive(true). MissingSeparate(true); int[] indexes = alg.SearchIndexes(eval); Assert.AreEqual(new[] { 2, 0 }, indexes); }
public void test_building_predictions_lines() { Runtime rt = Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv")); RandomForest cls = rt.Classifiers.Trees.RandomForest. NumExecutionSlots(4). NumFeatures(5). NumTrees(50); Runtime testset = Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_test.csv"), preprocessor: TestLinePreproc); int count = testset.NumInstances; System.Collections.Generic.List <string> lines = testset.GeneratePredictions(GeneratePredictionLine, cls); Assert.AreEqual(count, lines.Count); }
[Test] public void simple_attribute_selection_tests_with_new_runtime() { var rt = Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv")); var alg = rt.AttributeSelections.Algorithms.BestFirst. Direction(BestFirst.EDirection.Bi_directional). LookupCacheSize(10); var eval = rt.AttributeSelections.Evaluators.CfsSubset. LocallyPredictive(true). MissingSeparate(true); var newrt = alg.Search(eval); var names = newrt.EnumerateAttributes.Select(a => a.Name).ToArray(); Assert.AreEqual(new[] { "sex", "survived" }, names); }
public void test_basic_clustering() { Runtime rt = Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv")); // Remove the classifier (which upsets clusterers) rt = rt.Filters.UnsupervisedAttribute.Remove.AttributeIndices("1").RunFilter(); PicNetML.Clstr.IBaseClusterer <weka.clusterers.SimpleKMeans> clusterer = rt.Clusterers.SimpleKMeans. NumClusters(10). Build(); Assert.AreEqual(1, clusterer.ClusterInstance(rt[0])); Assert.AreEqual(0, clusterer.ClusterInstance(rt[2])); Assert.AreEqual(0, clusterer.ClusterInstance(rt[3])); Assert.AreEqual(1, clusterer.ClusterInstance(rt[4])); }
[Test] public void test_making_single_predictions_from_trained_model() { var rt = Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv")); var classifier = rt.Classifiers.Trees.RandomForest. NumExecutionSlots(4). NumFeatures(5). NumTrees(50); var row = new TitanicDataRow { age = 10, pclass = "1", sex = "male", embarked = "C" }; var prediction = classifier.ClassifyRow(row); var proba = classifier.ClassifyRowProba(row); Assert.AreEqual(0, prediction); Assert.IsTrue(proba < 0.5); }