public ReportDiscrete(IModelDiscrete <DomainType, LabelType> model, int[,] confusion_matrix_instances) { this.Model = model; this.confusion_matrix_instances = ToolsCollection.Copy(confusion_matrix_instances); this.confusion_matrix_rates = new double[confusion_matrix_instances.GetLength(0), confusion_matrix_instances.GetLength(1)]; int instance_count = 0; this.CorrectLabelCount = 0; for (int index_0 = 0; index_0 < confusion_matrix_instances.GetLength(0); index_0++) { for (int index_1 = 0; index_1 < confusion_matrix_instances.GetLength(1); index_1++) { instance_count += confusion_matrix_instances[index_0, index_1]; if (index_0 == index_1) { CorrectLabelCount += confusion_matrix_instances[index_0, index_1]; } } } CorrectLabelRate = CorrectLabelCount / (double)instance_count; for (int index_0 = 0; index_0 < confusion_matrix_instances.GetLength(0); index_0++) { for (int index_1 = 0; index_1 < confusion_matrix_instances.GetLength(1); index_1++) { confusion_matrix_rates[index_0, index_1] = (double)confusion_matrix_instances[index_0, index_1] / (double)instance_count; } } }
public ReportDiscrete <DomainType, LabelType> GenerateAndTestDiscrete(IDataSet <DomainType, LabelType> training_set, IDataSet <DomainType, LabelType> test_set) { //TODO check data contexts (they should match) IModelDiscrete <DomainType, LabelType> model = GenerateModelDiscrete(training_set); int label_value_count = test_set.DataContext.GetLabelDescriptor(0).ValueCount; int[,] confusion_matrix_instances = new int[label_value_count, label_value_count]; for (int instance_index = 0; instance_index < test_set.InstanceCount; instance_index++) { //int true_label_index = test_set.DataContext.GetLabelValueIndex(test_set.GetInstanceLabelData(instance_index)); //int model_label_index = model.GetLabelIndex(test_set.GetInstanceFeatureData(instance_index)); //confusion_matrix_instances[true_label_index, model_label_index]++; //// throw new NotImplementedException(); } return(new ReportDiscrete <DomainType, LabelType>(model, confusion_matrix_instances)); }
public Tuple <IModelDiscrete <DomainType, LabelType>, IList <int> > SelectFeatureSet(ITemplateModelDiscrete <DomainType, LabelType> template, IDataSet <DomainType, LabelType> data_set) { ISet <ISet <int> > black_list = new HashSet <ISet <int> >(); ISet <int> full_set = new HashSet <int>(); for (int feature_index = 0; feature_index < data_set.FeatureCount; feature_index++) { full_set.Add(feature_index); } ISet <int> current_set = new HashSet <int>(); double best_score = feature_set_evaluator.Evaluate(template, data_set, current_set); bool improvement = true; while (improvement) { List <ISet <int> > options = new List <ISet <int> >(); options.AddRange(RemoveFeature(current_set, black_list)); options.AddRange(AddFeature(current_set, black_list, full_set)); improvement = false; foreach (ISet <int> option in options) { double score = feature_set_evaluator.Evaluate(template, data_set, option); //System.out.print("scorein " + score); //CollectionTools.print(option); if (best_score.CompareTo(score) == -1) { //System.out.print("New best scorein " + score); //CollectionTools.print(option); best_score = score; current_set = option; improvement = true; } } } List <int> current_list = new List <int>(current_set); current_list.Sort(); IModelDiscrete <DomainType, LabelType> model = template.GenerateModelDiscrete(data_set.SelectFeatures(current_list)); return(new Tuple <IModelDiscrete <DomainType, LabelType>, IList <int> >(model, current_list)); }
public static void WriteModelDiscrete <DomainType, LabelType>(BinaryWriter writer, IModelDiscrete <DomainType, LabelType> model) { string model_type = model.ModelType; writer.Write(model_type); switch (model_type) { case "ModelDiscreteC45Default": ModelDiscreteC45Default.Write(writer, (ModelDiscreteC45Default)model); break; case "ModelNearestNeighborListDefault": ModelNearestNeighborListDefault.Write(writer, (ModelNearestNeighborListDefault)model); break; case "ModelNearestNeighborKDTreeDefault": ModelNearestNeighborKDTreeDefault.Write(writer, (ModelNearestNeighborKDTreeDefault)model); break; case "ModelLibSVMCSVC": ModelLibSVMCSVC.Write(writer, (ModelLibSVMCSVC)model); break; case "ModelNaiveBayesIntervalDefault": ModelNaiveBayesInterval.Write(writer, (ModelNaiveBayesInterval)model); break; default: throw new Exception("Unknown discrete model type: " + model_type); } }
public ModelDiscreteSelecting(IDataContext data_context, IModelDiscrete <DomainType, LabelType> model, IList <int> selected_feature_indexes) : base(data_context, "ModelDiscreteSelecting") { this.model = model; this.selected_feature_indexes = selected_feature_indexes; }
public static ReportDiscrete <DomainType, LabelType> Read(IModelDiscrete <DomainType, LabelType> model, BinaryReader reader) { return(new ReportDiscrete <DomainType, LabelType>(model, reader.ReadInt32Array2D())); }
public ReportDiscrete(IModelDiscrete <DomainType, LabelType> model, BinaryReader reader) : this(model, reader.ReadInt32Array2D()) { }