<commit_before>﻿// file:	Supervised\Regression\LinearRegression.cs
//
// summary:	Implements the linear regression class
using System;
using System.Linq;
using System.Collections.Generic;

using numl.Math.LinearAlgebra;
using numl.PreProcessing;

namespace numl.Supervised.Regression
{
    /// <summary>A linear regression generator.</summary>
    public class LinearRegressionGenerator : Generator
    {
        /// <summary>
        /// If the feature cutoff count is reached, the generator will use gradient descent 
        /// for initialisation instead of normal equations. This reduces processor complexity.
        /// <para>The default value is false.</para>
        /// </summary>
        public bool UseGradientDescent { get; set; }

        /// <summary>Gets or sets the learning rate used with gradient descent.</summary>
        /// <para>The default value is 0.01</para>
        /// <value>The learning rate.</value>
        public double LearningRate { get; set; }

        /// <summary>Gets or sets the maximum iterations used with gradient descent.</summary>
        /// <para>The default is 500</para>
        /// <value>The maximum iterations.</value>
        public int MaxIterations { get; set; }

        /// <summary>
        /// Initialise a new LinearRegressionGenerator
        /// </summary>
        public LinearRegressionGenerator()
        {
            this.UseGradientDescent = false;
            this.MaxIterations = 500;
            this.LearningRate = 0.01;
        }

        /// <summary>Generate model based on a set of examples.</summary>
        /// <exception cref="NotImplementedException">Thrown when the requested operation is unimplemented.</exception>
        /// <param name="x">The Matrix to process.</param>
        /// <param name="y">The Vector to process.</param>
        /// <returns>Model.</returns>
        public override IModel Generate(Matrix x, Vector y)
        {
            // create initial theta
            int width = x.Cols;

            Vector theta = Vector.Zeros(3);
            Matrix copy = x.Copy();

            // normalise features
            for (int i = 0; i < copy.Cols; i++)
            {
                var j = FeatureNormalizer.FeatureScale(copy[i, VectorType.Col]);
                for (int k = 0; k < copy.Rows; k++)
                {
                    copy[k, i] = j[k];
                }
            }

            // add intercept term
            copy = copy.Insert(Vector.Ones(copy.Rows), 0, VectorType.Col);

            if (!this.UseGradientDescent)
            {
                theta = ((((copy.Transpose() * copy).Inverse()) * copy.Transpose()) * y).ToVector();
            }
            else
            {
                int m = copy.Rows;
                
                // run gradient descent on theta
                for (int i = 0; i < this.MaxIterations; i++)
                {
                    theta = theta - (this.LearningRate / m * (((copy * theta).ToVector()) - y) * copy).ToVector();
                }
            }

            // once converged create model and apply theta

            LinearRegressionModel model = new LinearRegressionModel(x.Mean(VectorType.Row), x.StdDev(VectorType.Row)) { 
                Theta = theta
            };

            return model;
        }
    }
}
<commit_msg>CK - 20150104  - Removed misleading exception message in summary of LinearRegressionGenerator<commit_after>﻿// file:	Supervised\Regression\LinearRegression.cs
//
// summary:	Implements the linear regression class
using System;
using System.Linq;
using System.Collections.Generic;

using numl.Math.LinearAlgebra;
using numl.PreProcessing;

namespace numl.Supervised.Regression
{
    /// <summary>A linear regression generator.</summary>
    public class LinearRegressionGenerator : Generator
    {
        /// <summary>
        /// If the feature cutoff count is reached, the generator will use gradient descent 
        /// for initialisation instead of normal equations. This reduces processor complexity.
        /// <para>The default value is false.</para>
        /// </summary>
        public bool UseGradientDescent { get; set; }

        /// <summary>Gets or sets the learning rate used with gradient descent.</summary>
        /// <para>The default value is 0.01</para>
        /// <value>The learning rate.</value>
        public double LearningRate { get; set; }

        /// <summary>Gets or sets the maximum iterations used with gradient descent.</summary>
        /// <para>The default is 500</para>
        /// <value>The maximum iterations.</value>
        public int MaxIterations { get; set; }

        /// <summary>
        /// Initialise a new LinearRegressionGenerator
        /// </summary>
        public LinearRegressionGenerator()
        {
            this.UseGradientDescent = false;
            this.MaxIterations = 500;
            this.LearningRate = 0.01;
        }

        /// <summary>Generate model based on a set of examples.</summary>
        /// <param name="x">The Matrix to process.</param>
        /// <param name="y">The Vector to process.</param>
        /// <returns>Model.</returns>
        public override IModel Generate(Matrix x, Vector y)
        {
            // create initial theta
            int width = x.Cols;

            Vector theta = Vector.Zeros(3);
            Matrix copy = x.Copy();

            // normalise features
            for (int i = 0; i < copy.Cols; i++)
            {
                var j = FeatureNormalizer.FeatureScale(copy[i, VectorType.Col]);
                for (int k = 0; k < copy.Rows; k++)
                {
                    copy[k, i] = j[k];
                }
            }

            // add intercept term
            copy = copy.Insert(Vector.Ones(copy.Rows), 0, VectorType.Col);

            if (!this.UseGradientDescent)
            {
                theta = ((((copy.Transpose() * copy).Inverse()) * copy.Transpose()) * y).ToVector();
            }
            else
            {
                int m = copy.Rows;
                
                // run gradient descent on theta
                for (int i = 0; i < this.MaxIterations; i++)
                {
                    theta = theta - (this.LearningRate / m * (((copy * theta).ToVector()) - y) * copy).ToVector();
                }
            }

            // once converged create model and apply theta

            LinearRegressionModel model = new LinearRegressionModel(x.Mean(VectorType.Row), x.StdDev(VectorType.Row)) { 
                Theta = theta
            };

            return model;
        }
    }
}

{'fraction_non_alphanumeric': 0.11464968152866242, 'fraction_numerical': 0.006369426751592357, 'mean_word_length': 1.6646464646464647, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5103769', 'n_tokens_mistral': 1794, 'n_tokens_neox': 1648, 'n_words': 493}