/// <summary>
        ///   Creates a set of decision variables from a <see cref="Codification"/> codebook.
        /// </summary>
        ///
        /// <param name="codebook">The codebook containing information about the variables.</param>
        /// <param name="columns">The columns to consider as decision variables.</param>
        ///
        /// <returns>An array of <see cref="DecisionVariable"/> objects
        /// initialized with the values from the codebook.</returns>
        ///
        public static DecisionVariable[] FromCodebook(Codification <string> codebook, params string[] columns)
        {
            if (columns.Length == 0)
            {
                throw new ArgumentException("List of columns is empty.");
            }

            var variables = new DecisionVariable[columns.Length];

            for (int i = 0; i < variables.Length; i++)
            {
                string name = columns[i];

                Codification.Options col;

                if (codebook.Columns.TryGetValue(name, out col))
                {
                    variables[i] = new DecisionVariable(name, col.NumberOfSymbols);
                }
                else
                {
                    variables[i] = new DecisionVariable(name, DecisionVariableKind.Continuous);
                }
            }

            return(variables);
        }
 public static DecisionTree Create(int[][] x, int[] y, IList <DecisionVariable> attributes)
 {
     if (attributes == null || attributes.Count == 0)
     {
         attributes = DecisionVariable.FromData(x);
     }
     return(Create(y, attributes));
 }
Beispiel #3
0
        /// <summary>
        ///   Creates a set of decision variables from input data.
        /// </summary>
        ///
        /// <param name="inputs">The input data.</param>
        ///
        /// <returns>An array of <see cref="DecisionVariable"/> objects
        /// initialized with the values from the codebook.</returns>
        ///
        public static DecisionVariable[] FromData(int[][] inputs)
        {
            int cols      = inputs.Columns();
            var variables = new DecisionVariable[cols];

            for (int i = 0; i < variables.Length; i++)
            {
                variables[i] = new DecisionVariable(i.ToString(), DecisionVariableKind.Discrete);
            }
            return(variables);
        }
        /// <summary>
        ///   Creates a set of decision variables from input data.
        /// </summary>
        ///
        /// <param name="inputs">The input data.</param>
        ///
        /// <returns>An array of <see cref="DecisionVariable"/> objects
        /// initialized with the values from the codebook.</returns>
        ///
        public static DecisionVariable[] FromData(int[][] inputs)
        {
            int cols      = inputs.Columns();
            var variables = new DecisionVariable[cols];

            for (int i = 0; i < variables.Length; i++)
            {
                variables[i] = new DecisionVariable(i.ToString(), inputs.GetColumn(i).GetRange());
            }
            return(variables);
        }
        /// <summary>
        ///   Creates a set of decision variables from input data.
        /// </summary>
        ///
        /// <param name="inputs">The input data.</param>
        ///
        /// <returns>An array of <see cref="DecisionVariable"/> objects
        /// initialized with the values from the codebook.</returns>
        ///
        public static DecisionVariable[] FromData(int?[][] inputs)
        {
            int cols      = inputs.Columns();
            var variables = new DecisionVariable[cols];

            for (int i = 0; i < variables.Length; i++)
            {
                variables[i] = new DecisionVariable(i.ToString(), inputs.GetColumn(i)
                                                    .Where(x => x.HasValue).Select(x => x.Value).ToArray().GetRange());
            }
            return(variables);
        }
Beispiel #6
0
        /// <summary>
        ///   Creates a set of decision variables from a <see cref="Codification"/> codebook.
        /// </summary>
        ///
        /// <param name="codebook">The codebook containing information about the variables.</param>
        /// <param name="columns">The columns to consider as decision variables.</param>
        ///
        /// <returns>An array of <see cref="DecisionVariable"/> objects
        /// initialized with the values from the codebook.</returns>
        ///
        public static DecisionVariable[] FromCodebook(Codification codebook, params string[] columns)
        {
            DecisionVariable[] variables = new DecisionVariable[columns.Length];

            for (int i = 0; i < variables.Length; i++)
            {
                string name = columns[i];
                var    col  = codebook.Columns[name];
                variables[i] = new DecisionVariable(name, col.Symbols);
            }

            return(variables);
        }
        /// <summary>
        ///   Learns a new Random Forest with the given data.
        /// </summary>
        ///
        /// <param name="inputs">The input points.</param>
        /// <param name="output">The class label for each point.</param>
        ///
        /// <returns>A <see cref="RandomForest"/> object that learned
        ///   how to assign class labels to input points.</returns>
        ///
        public RandomForest Learn(double[][] inputs, int[] output)
        {
            if (forest == null)
            {
                int classes = output.Max() + 1;
                this.forest = new RandomForest(NumberOfTrees, classes);
                var variables = DecisionVariable.FromData(inputs);
                for (int i = 0; i < forest.Trees.Length; i++)
                {
                    forest.Trees[i] = new DecisionTree(variables, classes);
                }
            }

            run(inputs, output);
            return(this.forest);
        }
        /// <summary>
        ///   Creates a set of decision variables from a <see cref="OrderedDictionary{TKey, TValue}"/> codebook.
        /// </summary>
        ///
        /// <param name="columns">The ordered dictionary containing information about the variables.</param>
        ///
        /// <returns>An array of <see cref="DecisionVariable"/> objects
        ///   initialized with the values from the codebook.</returns>
        ///
        public static DecisionVariable[] FromDictionary(OrderedDictionary <string, string[]> columns)
        {
            if (columns.Count == 0)
            {
                throw new ArgumentException("List of columns is empty.");
            }

            var variables = new DecisionVariable[columns.Count];

            for (int i = 0; i < variables.Length; i++)
            {
                string name = columns.GetKeyByIndex(i);
                variables[i] = new DecisionVariable(name, columns[name].Length);
            }

            return(variables);
        }
        /// <summary>
        ///   Learns a model that can map the given inputs to the given outputs.
        /// </summary>
        ///
        /// <param name="x">The model inputs.</param>
        /// <param name="y">The desired outputs associated with each <paramref name="x">inputs</paramref>.</param>
        /// <param name="weights">The weight of importance for each input-output pair (if supported by the learning algorithm).</param>
        ///
        /// <returns>A model that has learned how to produce <paramref name="y"/> given <paramref name="x"/>.</returns>
        ///
        public RandomForest Learn(double[][] x, int[] y, double[] weights = null)
        {
            if (weights != null)
            {
                throw new ArgumentException(Accord.Properties.Resources.NotSupportedWeights, "weights");
            }

            if (forest == null)
            {
                if (this.attributes == null)
                {
                    this.attributes = DecisionVariable.FromData(x);
                }
                this.forest = CreateTree(y);
            }

            run(x, y);
            return(this.forest);
        }
        /// <summary>
        ///   Learns a model that can map the given inputs to the given outputs.
        /// </summary>
        ///
        /// <param name="x">The model inputs.</param>
        /// <param name="y">The desired outputs associated with each <paramref name="x">inputs</paramref>.</param>
        /// <param name="weights">The weight of importance for each input-output pair.</param>
        ///
        /// <returns>A model that has learned how to produce <paramref name="y"/> given <paramref name="x"/>.</returns>
        ///
        public RandomForest Learn(double[][] x, int[] y, double[] weights = null)
        {
            if (forest == null)
            {
                int classes = y.Max() + 1;
                this.forest = new RandomForest(NumberOfTrees, classes);
                if (this.attributes == null)
                {
                    this.attributes = DecisionVariable.FromData(x);
                }
                for (int i = 0; i < forest.Trees.Length; i++)
                {
                    forest.Trees[i] = new DecisionTree(attributes, classes);
                }
            }

            run(x, y);
            return(this.forest);
        }
Beispiel #11
0
        /// <summary>
        ///   Creates a set of decision variables from a <see cref="Codification"/> codebook.
        /// </summary>
        ///
        /// <param name="codebook">The codebook containing information about the variables.</param>
        /// <param name="columns">The columns to consider as decision variables.</param>
        ///
        /// <returns>An array of <see cref="DecisionVariable"/> objects
        /// initialized with the values from the codebook.</returns>
        ///
        public static DecisionVariable[] FromCodebook(Codification codebook, params string[] columns)
        {
            DecisionVariable[] variables = new DecisionVariable[columns.Length];

            for (int i = 0; i < variables.Length; i++)
            {
                string name = columns[i];

                Codification.Options col;

                if (codebook.Columns.TryGetValue(name, out col))
                {
                    variables[i] = new DecisionVariable(name, col.Symbols);
                }
                else
                {
                    variables[i] = new DecisionVariable(name, DecisionVariableKind.Continuous);
                }
            }

            return(variables);
        }
        /// <summary>
        ///   Learns a model that can map the given inputs to the given outputs.
        /// </summary>
        ///
        /// <param name="x">The model inputs.</param>
        /// <param name="y">The desired outputs associated with each <paramref name="x">inputs</paramref>.</param>
        /// <param name="weights">The weight of importance for each input-output pair (if supported by the learning algorithm).</param>
        ///
        /// <returns>A model that has learned how to produce <paramref name="y"/> given <paramref name="x"/>.</returns>
        ///
        public RandomForest Learn(double[][] x, int[] y, double[] weights = null)
        {
            if (weights != null)
            {
                throw new ArgumentException(Accord.Properties.Resources.NotSupportedWeights, "weights");
            }

            if (forest == null)
            {
                int classes = y.Max() + 1;
                this.forest = new RandomForest(NumberOfTrees, classes);
                if (this.attributes == null)
                {
                    this.attributes = DecisionVariable.FromData(x);
                }
                for (int i = 0; i < forest.Trees.Length; i++)
                {
                    forest.Trees[i] = new DecisionTree(attributes, classes);
                }
            }

            run(x, y);
            return(this.forest);
        }
 /// <summary>
 ///   Creates a new decision forest learning algorithm.
 /// </summary>
 ///
 /// <param name="attributes">The attributes to be processed by the induced tree.</param>
 ///
 public RandomForestLearning(OrderedDictionary <string, string[]> attributes)
     : this()
 {
     this.attributes = DecisionVariable.FromDictionary(attributes);
 }