<commit_before>from .base import LinearBase
from ..maths import dot_product, mean_squared_error, mean_absolute_error, least_squares
from ..utils import set_seed
from pyml.maths import gradient_descent


class LinearRegression(LinearBase):
    def __init__(self, seed=None, bias=True, solver='OLS', learning_rate=0.01,
                 epsilon=0.01, max_iterations=10000):

        LinearBase.__init__(self)

        self._seed = set_seed(seed)
        self.bias = bias
        self.epsilon = epsilon
        self.max_iterations = max_iterations
        self._learning_rate = learning_rate
        if solver in ['OLS', 'gradient_descent']:
            self._solver = solver

    def _train(self, X, y=None):
        self.X = X
        self.y = y

        self._n_features = len(X[0])
        self._initiate_weights(bias=self.bias)

        if self._solver == 'gradient_descent':
            self._coefficients, self._cost, self._iterations = gradient_descent.gradient_descent(self.X,
                                                                                                 self.coefficients,
                                                                                                 self.y,
                                                                                                 self.max_iterations,
                                                                                                 self.epsilon,
                                                                                                 self._learning_rate)
        else:
            self._cost = 'NaN'
            self._iterations = 'NaN'
            self._coefficients = least_squares(self.X, self.y)

    def _predict(self, X):

        if self.bias and len(X[0]) == self._n_features + 1:
            return dot_product(X, self.coefficients)
        elif self.bias and len(X[0]) == self._n_features:
            return dot_product([[1] + row for row in X], self._coefficients)
        elif not self.bias:
            return dot_product(X, self.coefficients)
        else:
            raise ValueError("Something went wrong.")

    def _score(self, X, y_true, scorer='mean_squared_error'):
        if scorer == 'mean_squared_error':
            return mean_squared_error(self.predict(X), y_true)
        elif scorer == 'mean_absolute_error':
            return mean_absolute_error(self.predict(X), y_true)

    @property
    def seed(self):
        return self._seed

    @property
    def coefficients(self):
        return self._coefficients

    @property
    def cost(self):<commit_msg>small optimisation by skipping initialisation of weights for OLS<commit_after>from .base import LinearBase
from ..maths import dot_product, mean_squared_error, mean_absolute_error, least_squares
from ..utils import set_seed
from pyml.maths import gradient_descent


class LinearRegression(LinearBase):
    def __init__(self, seed=None, bias=True, solver='OLS', learning_rate=0.01,
                 epsilon=0.01, max_iterations=10000):
        """

        :param seed:
        :param bias:
        :param solver:
        :param learning_rate:
        :param epsilon:
        :param max_iterations:
        """

        LinearBase.__init__(self)

        self._seed = set_seed(seed)
        self.bias = bias
        self.epsilon = epsilon
        self.max_iterations = max_iterations
        self._learning_rate = learning_rate
        if solver in ['OLS', 'gradient_descent']:
            self._solver = solver

    def _train(self, X, y=None):
        self.X = X
        self.y = y

        self._n_features = len(X[0])

        if self._solver == 'gradient_descent':
            self._initiate_weights(bias=self.bias)
            self._coefficients, self._cost, self._iterations = gradient_descent.gradient_descent(self.X,
                                                                                                 self.coefficients,
                                                                                                 self.y,
                                                                                                 self.max_iterations,
                                                                                                 self.epsilon,
                                                                                                 self._learning_rate)
        else:
            if self.bias:
                self.X = [[1] + row for row in self.X]
            self._cost = 'NaN'
            self._iterations = 'NaN'
            self._coefficients = least_squares(self.X, self.y)

    def _predict(self, X):

        if self.bias and len(X[0]) == self._n_features + 1:
            return dot_product(X, self.coefficients)
        elif self.bias and len(X[0]) == self._n_features:
            return dot_product([[1] + row for row in X], self._coefficients)
        elif not self.bias:
            return dot_product(X, self.coefficients)
        else:
            raise ValueError("Something went wrong.")

    def _score(self, X, y_true, scorer='mean_squared_error'):
        if scorer == 'mean_squared_error':
            return mean_squared_error(self.predict(X), y_true)
        elif scorer == 'mean_absolute_error':
            return mean_absolute_error(self.predict(X), y_true)

    @property
    def seed(self):
        return self._seed

    @property
    def coefficients(self):
        return self._coefficients

    @property
    def cost(self):
{'fraction_non_alphanumeric': 0.0835613939062215, 'fraction_numerical': 0.0060207991242474, 'mean_word_length': 1.4171075837742504, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2219926', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1344, 'n_words': 316}