/// <summary>
        /// Safely gets all class labels.
        /// </summary>
        /// <typeparam name="TInstanceSource">The type of a source of instances.</typeparam>
        /// <typeparam name="TInstance">The type of an instance.</typeparam>
        /// <typeparam name="TLabelSource">The type of a source of labels.</typeparam>
        /// <typeparam name="TLabel">The type of a label.</typeparam>
        /// <typeparam name="TFeatures">The type of the features.</typeparam>
        /// <param name="mapping">The mapping.</param>
        /// <param name="instanceSource">An optional instance source.</param>
        /// <param name="labelSource">An optional label source.</param>
        /// <returns>All possible values of a label.</returns>
        /// <exception cref="MappingException">Thrown if the class labels are null, empty, identical or not unique.</exception>
        public static IReadOnlyList <TLabel> GetClassLabelsSafe <TInstanceSource, TInstance, TLabelSource, TLabel, TFeatures>(
            this IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, TFeatures> mapping,
            TInstanceSource instanceSource = default(TInstanceSource),
            TLabelSource labelSource       = default(TLabelSource))
        {
            IEnumerable <TLabel> classLabels = mapping.GetClassLabels(instanceSource, labelSource);

            if (classLabels == null)
            {
                throw new MappingException("The class labels must not be null.");
            }

            IReadOnlyList <TLabel> classLabelList = classLabels.OrderBy(classLabel => classLabel).ToList();

            if (classLabelList.Any(label => label == null))
            {
                throw new MappingException("A class label must not be null.");
            }

            var classLabelSet         = new HashSet <TLabel>(classLabelList);
            int uniqueClassLabelCount = classLabelSet.Count;

            if (uniqueClassLabelCount != classLabelList.Count)
            {
                throw new MappingException("All class labels must be unique.");
            }

            if (uniqueClassLabelCount < 2)
            {
                throw new MappingException("There must be at least two distinct class labels.");
            }

            return(classLabelList);
        }
Esempio n. 2
0
        /// <summary>
        /// Safely gets all class labels.
        /// </summary>
        /// <typeparam name="TInstanceSource">The type of a source of instances.</typeparam>
        /// <typeparam name="TInstance">The type of an instance.</typeparam>
        /// <typeparam name="TLabelSource">The type of a source of labels.</typeparam>
        /// <typeparam name="TLabel">The type of a label.</typeparam>
        /// <typeparam name="TFeatureValues">The type of the feature values.</typeparam>
        /// <param name="mapping">The mapping.</param>
        /// <param name="instanceSource">An optional instance source.</param>
        /// <param name="labelSource">An optional label source.</param>
        /// <returns>All possible values of a label.</returns>
        /// <exception cref="MappingException">Thrown if the class labels are null or not unique.</exception>
        public static IEnumerable <TLabel> GetClassLabelsSafe <TInstanceSource, TInstance, TLabelSource, TLabel, TFeatureValues>(
            this IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, TFeatureValues> mapping,
            TInstanceSource instanceSource = default(TInstanceSource),
            TLabelSource labelSource       = default(TLabelSource))
        {
            IEnumerable <TLabel> classLabels = mapping.GetClassLabels(instanceSource, labelSource);

            if (classLabels == null)
            {
                throw new MappingException("The class labels must not be null.");
            }

            IList <TLabel> classLabelList = classLabels.ToList();

            if (classLabelList.Any(label => label == null))
            {
                throw new MappingException("A class label must not be null.");
            }

            var classLabelSet = new HashSet <TLabel>(classLabelList);

            if (classLabelSet.Count != classLabelList.Count)
            {
                throw new MappingException("All class labels must be unique.");
            }

            return(classLabelList);
        }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NativeClassifierMapping{TInstanceSource,TInstance,TLabelSource,TStandardLabel,TNativeLabel}"/> class.
 /// </summary>
 /// <param name="standardMapping">The mapping for accessing data in standard format.</param>
 protected NativeClassifierMapping(IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TStandardLabel, Vector> standardMapping)
 {
     this.StandardMapping = standardMapping;
     this.featuresCached  = false;
     this.labelsCached    = false;
     this.batchCount      = 1;
 }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BinaryNativeClassifierMapping{TInstanceSource,TInstance,TLabelSource,TLabel}"/>
        /// class from a reader of a binary stream.
        /// </summary>
        /// <param name="reader">The binary reader to read the mapping from.</param>
        /// <param name="standardMapping">The mapping for accessing data in standard format.</param>
        public BinaryNativeClassifierMapping(IReader reader, IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, Vector> standardMapping)
            : base(reader, standardMapping)
        {
            int deserializedVersion = reader.ReadSerializationVersion(CustomSerializationVersion);

            if (LabelIsBoolean && deserializedVersion >= 2)
            {
                SetClassLabels(default);
        CreateMulticlassClassifier <TInstanceSource, TInstance, TLabelSource, TLabel>(
            IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, Vector> mapping)
        {
            if (mapping == null)
            {
                throw new ArgumentNullException("mapping");
            }

            return(new CompoundMulticlassStandardDataFormatBayesPointMachineClassifier <TInstanceSource, TInstance, TLabelSource, TLabel>(mapping));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MulticlassNativeClassifierMapping{TInstanceSource,TInstance,TLabelSource,TLabel}"/>
        /// class from a reader of a binary stream.
        /// </summary>
        /// <param name="reader">The binary reader to read the mapping from.</param>
        /// <param name="standardMapping">The mapping for accessing data in standard format.</param>
        public MulticlassNativeClassifierMapping(BinaryReader reader, IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, Vector> standardMapping)
            : base(reader, standardMapping)
        {
            int deserializedVersion = reader.ReadSerializationVersion(CustomSerializationVersion);

            if (deserializedVersion == CustomSerializationVersion)
            {
                this.classLabelSet = new IndexedSet <TLabel>(reader);
            }
        }
        CreateGaussianPriorBinaryClassifier <TInstanceSource, TInstance, TLabelSource, TLabel>(
            IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, Vector> mapping)
        {
            if (mapping == null)
            {
                throw new ArgumentNullException("mapping");
            }

            return(new GaussianBinaryStandardDataFormatBayesPointMachineClassifier <TInstanceSource, TInstance, TLabelSource, TLabel>(mapping));
        }
Esempio n. 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ClassifierEvaluatorMapping{TInstanceSource,TInstance,TLabelSource,TLabel,TFeatureValues}"/> class.
        /// </summary>
        /// <param name="mapping">A classifier mapping.</param>
        public ClassifierEvaluatorMapping(IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, TFeatureValues> mapping)
        {
            if (mapping == null)
            {
                throw new ArgumentNullException("mapping");
            }

            this.classifierMapping = mapping;

            this.cachedLabels = new Dictionary <TInstance, TLabel>();
        }
Esempio n. 9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NativeClassifierMapping{TInstanceSource,TInstance,TLabelSource,TStandardLabel,TNativeLabel}"/>
        /// class from a reader of a binary stream.
        /// </summary>
        /// <param name="reader">The binary reader to read the mapping from.</param>
        /// <param name="standardMapping">The mapping for accessing data in standard format.</param>
        protected NativeClassifierMapping(BinaryReader reader, IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TStandardLabel, Vector> standardMapping)
        {
            this.StandardMapping = standardMapping;

            int deserializedVersion = reader.ReadSerializationVersion(CustomSerializationVersion);

            if (deserializedVersion == CustomSerializationVersion)
            {
                this.isSparseFeatureRepresentation = reader.ReadBoolean();
                this.featureCount    = reader.ReadInt32();
                this.batchCount      = reader.ReadInt32();
                this.lastBatchCount  = reader.ReadInt32();
                this.lastBatchNumber = reader.ReadInt32();
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BinaryNativeClassifierMapping{TInstanceSource,TInstance,TLabelSource,TLabel}"/>
        /// class from a reader of a binary stream.
        /// </summary>
        /// <param name="reader">The binary reader to read the mapping from.</param>
        /// <param name="standardMapping">The mapping for accessing data in standard format.</param>
        public BinaryNativeClassifierMapping(BinaryReader reader, IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, Vector> standardMapping)
            : base(reader, standardMapping)
        {
            int deserializedVersion = reader.ReadSerializationVersion(CustomSerializationVersion);

            if (deserializedVersion == CustomSerializationVersion)
            {
                this.areClassLabelsSet = reader.ReadBoolean();

                if (this.areClassLabelsSet)
                {
                    this.positiveClassLabel = (TLabel)reader.ReadObject();
                    this.negativeClassLabel = (TLabel)reader.ReadObject();
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MulticlassNativeClassifierMapping{TInstanceSource,TInstance,TLabelSource,TLabel}"/>
        /// class from a reader of a binary stream.
        /// </summary>
        /// <param name="reader">The binary reader to read the mapping from.</param>
        /// <param name="standardMapping">The mapping for accessing data in standard format.</param>
        public MulticlassNativeClassifierMapping(IReader reader, IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, Vector> standardMapping)
            : base(reader, standardMapping)
        {
            int deserializedVersion = reader.ReadSerializationVersion(CustomSerializationVersion);

            if (deserializedVersion >= 2)
            {
                int count = reader.ReadInt32();
                this.classLabelSet = new IndexedSet <TLabel>();
                for (int i = 0; i < count; i++)
                {
                    this.classLabelSet.Add(standardMapping.ParseLabel(reader.ReadString()));
                }
            }
            else
            {
                this.classLabelSet = new IndexedSet <TLabel>(reader);
            }
        }
Esempio n. 12
0
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="CompoundMulticlassStandardDataFormatBayesPointMachineClassifier{TInstanceSource,TInstance,TLabelSource,TLabel}"/> class.
 /// </summary>
 /// <param name="standardMapping">The mapping used for accessing data in the standard format.</param>
 public CompoundMulticlassStandardDataFormatBayesPointMachineClassifier(IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, Vector> standardMapping)
     : base(standardMapping)
 {
     this.Classifier = new CompoundMulticlassNativeDataFormatBayesPointMachineClassifier <TInstanceSource, TInstance, TLabelSource>(this.NativeMapping);
     this.Settings   = new MulticlassBayesPointMachineClassifierSettings <TLabel>(() => this.Classifier.IsTrained);
 }
        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="BinaryStandardDataFormatBayesPointMachineClassifier{TInstanceSource,TInstance,TLabelSource,TLabel,TTrainingSettings}"/> class.
        /// </summary>
        /// <param name="standardMapping">The mapping used for accessing data in the standard format.</param>
        protected BinaryStandardDataFormatBayesPointMachineClassifier(IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, Vector> standardMapping)
        {
            Debug.Assert(standardMapping != null, "The mapping must not be null.");

            this.NativeMapping = new BinaryNativeClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel>(standardMapping);
        }
        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="BinaryStandardDataFormatBayesPointMachineClassifier{TInstanceSource,TInstance,TLabelSource,TLabel,TTrainingSettings}"/>
        /// class from a reader of a binary stream.
        /// </summary>
        /// <param name="reader">The binary reader to read the state of the binary Bayes point machine classifier from.</param>
        /// <param name="standardMapping">The mapping used for accessing data in the standard format.</param>
        protected BinaryStandardDataFormatBayesPointMachineClassifier(IReader reader, IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, Vector> standardMapping)
            : base(reader)
        {
            Debug.Assert(standardMapping != null, "The mapping must not be null.");

            int deserializedVersion = reader.ReadSerializationVersion(CustomSerializationVersion);

            if (deserializedVersion == CustomSerializationVersion)
            {
                this.NativeMapping = new BinaryNativeClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel>(reader, standardMapping);
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="GaussianBinaryStandardDataFormatBayesPointMachineClassifier{TInstanceSource,TInstance,TLabelSource,TLabel}"/>
        /// class from a reader of a binary stream.
        /// </summary>
        /// <param name="reader">The binary reader to read the state of the binary Bayes point machine classifier from.</param>
        /// <param name="standardMapping">The mapping used for accessing data in the standard format.</param>
        public GaussianBinaryStandardDataFormatBayesPointMachineClassifier(IReader reader, IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, Vector> standardMapping)
            : base(reader, standardMapping)
        {
            int deserializedVersion = reader.ReadSerializationVersion(CustomSerializationVersion);

            if (deserializedVersion == CustomSerializationVersion)
            {
                // Deserialization of classifier and settings
                this.Classifier = new GaussianBinaryNativeDataFormatBayesPointMachineClassifier <TInstanceSource, TInstance, TLabelSource>(reader, this.NativeMapping);
                this.Settings   = new GaussianBinaryBayesPointMachineClassifierSettings <TLabel>(reader, () => this.Classifier.IsTrained);
            }
        }
Esempio n. 16
0
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="GaussianBinaryStandardDataFormatBayesPointMachineClassifier{TInstanceSource,TInstance,TLabelSource,TLabel}"/> class.
 /// </summary>
 /// <param name="standardMapping">The mapping used for accessing data in the standard format.</param>
 public GaussianBinaryStandardDataFormatBayesPointMachineClassifier(IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, Vector> standardMapping)
     : base(standardMapping)
 {
     this.Classifier = new GaussianBinaryNativeDataFormatBayesPointMachineClassifier <TInstanceSource, TInstance, TLabelSource>(this.NativeMapping);
     this.Settings   = new GaussianBinaryBayesPointMachineClassifierSettings <TLabel>(() => this.Classifier.IsTrained);
 }
Esempio n. 17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BinaryNativeClassifierMapping{TInstanceSource,TInstance,TLabelSource,TLabel}"/> class.
 /// </summary>
 /// <param name="standardMapping">The mapping for accessing data in standard format.</param>
 public BinaryNativeClassifierMapping(IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, Vector> standardMapping)
     : base(standardMapping)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MulticlassNativeClassifierMapping{TInstanceSource,TInstance,TLabelSource,TLabel}"/> class.
 /// </summary>
 /// <param name="standardMapping">The mapping for accessing data in standard format.</param>
 public MulticlassNativeClassifierMapping(IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, Vector> standardMapping)
     : base(standardMapping)
 {
     this.classLabelSet = new IndexedSet <TLabel>();
 }
Esempio n. 19
0
 ForEvaluation <TInstanceSource, TInstance, TLabelSource, TLabel, TFeatures>(
     this IClassifierMapping <TInstanceSource, TInstance, TLabelSource, TLabel, TFeatures> mapping)
 {
     return(new ClassifierEvaluatorMapping <TInstanceSource, TInstance, TLabelSource, TLabel, TFeatures>(mapping));
 }