<commit_before># -*- coding: utf-8 -*-
"""
A Theil-Sen Estimator for Multiple Linear Regression Model
"""

# Author: <NAME> <<EMAIL>>
#
# License: BSD 3 clause

from __future__ import division, print_function, absolute_import

import warnings
from itertools import combinations

import numpy as np
from scipy import linalg
from scipy.special import binom
from scipy.linalg.lapack import get_lapack_funcs

from .base import LinearModel
from ..base import RegressorMixin
from ..utils import check_array, check_random_state, ConvergenceWarning
from ..utils import check_consistent_length, _get_n_jobs
from ..externals.joblib import Parallel, delayed
from ..externals.six.moves import xrange as range


def _modified_weiszfeld_step(X, x_old):
    """Modified Weiszfeld step.

    This function defines one iteration step in order to approximate the
    spatial median (L1 median). It is a form of an iteratively re-weighted
    least squares method.

    Parameters
    ----------
    X : array, shape = [n_samples, n_features]
        Training vector, where n_samples is the number of samples and
        n_features is the number of features.

    x_old : array, shape = [n_features]
        Current start vector.

    Returns
    -------
    x_new : array, shape = [n_features]
        New iteration step.

    References
    ----------
    - On Computation of Spatial Median for Robust Data Mining, 2005
      <NAME> and <NAME>
      http://users.jyu.fi/~samiayr/pdf/ayramo_eurogen05.pdf

    Notes
    -----
    On page 4 of the referenced paper a formal mathematical definition of the
    algorithm below is given. For an easier understanding, the variable names
    used in the paper are also used in the algorithm.
    """
    epsilon = np.finfo(np.double).eps
    X = X.T
    diff = X.T - x_old
    normdiff = np.sqrt(np.sum(diff ** 2, axis=1))
    mask = normdiff >= epsilon
    if mask.sum() < X.shape[1]:
        eta = 1.
    else:
        eta = 0.
    diff = diff[mask, :]
    normdiff = normdiff[mask][:, np.newaxis]
    r = linalg.norm(np.sum(diff / normdiff, axis=0))
    if r > epsilon:  # to avoid division by zero
        T = np.sum(X.T[mask, :] / normdiff, axis=0) / \
            np.sum(1 / normdiff, axis=0)
    else:
        T = 1.
        r = 1.
    return max(0., 1. - eta / r) * T + min(1., eta / r) * x_old


def _spatial_median(X, max_iter=300, tol=1.e-3):
    """Spatial median (L1 median).

    The spatial median is member of a class of so-called M-estimators which
    are defined by an optimization problem. Given a number of p points in an
    n-dimensional space, the point x minimizing the sum of all distances to the
    p other points is called spatial median.

    Parameters
    ----------
    X : array, shape = [n_samples, n_features]
        Training vector, where n_samples is the number of samples and
        n_features is the number of features.

    max_iter : int, optional
        Maximum number of iterations.  Default is 300.

    tol : float, optional
        Stop the algorithm if spatial_median has converged. Default is 1.e-3.

    Returns
    -------
    spatial_median : array, shape = [n_features]
        Spatial median.

    n_iter: int
        Number of iterations needed.

    References
    ----------
    - On Computation of Spatial Median for Robust Data Mining, 2005
      <NAME> and <NAME>
      http://users.jyu.fi/~samiayr/pdf/ayramo_eurogen05.pdf
    """
    if X.shape[1] == 1:
        return 1, np.median(X.ravel())
    # We are computing the tol on the squared norm
    tol **= 2
    spatial_median_old = np.mean(X, axis=0)
    for n_iter in range(max_iter):
        spatial_median = _modified_weiszfeld_step(X, spatial_median_old)
        if np.sum((spatial_median_old - spatial_median) ** 2) < tol:
            return n_iter, spatial_median
        else:
            spatial_median_old = spatial_median
    warnings.warn("Maximum number of iterations {max_iter} reached in spatial "
                  "median for TheilSen regressor.".format(max_iter=max_iter),
                  ConvergenceWarning)
    return n_iter, spatial_median
<commit_msg>COSMIT: Break and for .. else in _spatial_median<commit_after># -*- coding: utf-8 -*-
"""
A Theil-Sen Estimator for Multiple Linear Regression Model
"""

# Author: <NAME> <<EMAIL>>
#
# License: BSD 3 clause

from __future__ import division, print_function, absolute_import

import warnings
from itertools import combinations

import numpy as np
from scipy import linalg
from scipy.special import binom
from scipy.linalg.lapack import get_lapack_funcs

from .base import LinearModel
from ..base import RegressorMixin
from ..utils import check_array, check_random_state, ConvergenceWarning
from ..utils import check_consistent_length, _get_n_jobs
from ..externals.joblib import Parallel, delayed
from ..externals.six.moves import xrange as range


def _modified_weiszfeld_step(X, x_old):
    """Modified Weiszfeld step.

    This function defines one iteration step in order to approximate the
    spatial median (L1 median). It is a form of an iteratively re-weighted
    least squares method.

    Parameters
    ----------
    X : array, shape = [n_samples, n_features]
        Training vector, where n_samples is the number of samples and
        n_features is the number of features.

    x_old : array, shape = [n_features]
        Current start vector.

    Returns
    -------
    x_new : array, shape = [n_features]
        New iteration step.

    References
    ----------
    - On Computation of Spatial Median for Robust Data Mining, 2005
      <NAME> and <NAME>
      http://users.jyu.fi/~samiayr/pdf/ayramo_eurogen05.pdf

    Notes
    -----
    On page 4 of the referenced paper a formal mathematical definition of the
    algorithm below is given. For an easier understanding, the variable names
    used in the paper are also used in the algorithm.
    """
    epsilon = np.finfo(np.double).eps
    X = X.T
    diff = X.T - x_old
    normdiff = np.sqrt(np.sum(diff ** 2, axis=1))
    mask = normdiff >= epsilon
    if mask.sum() < X.shape[1]:
        eta = 1.
    else:
        eta = 0.
    diff = diff[mask, :]
    normdiff = normdiff[mask][:, np.newaxis]
    r = linalg.norm(np.sum(diff / normdiff, axis=0))
    if r > epsilon:  # to avoid division by zero
        T = np.sum(X.T[mask, :] / normdiff, axis=0) / \
            np.sum(1 / normdiff, axis=0)
    else:
        T = 1.
        r = 1.
    return max(0., 1. - eta / r) * T + min(1., eta / r) * x_old


def _spatial_median(X, max_iter=300, tol=1.e-3):
    """Spatial median (L1 median).

    The spatial median is member of a class of so-called M-estimators which
    are defined by an optimization problem. Given a number of p points in an
    n-dimensional space, the point x minimizing the sum of all distances to the
    p other points is called spatial median.

    Parameters
    ----------
    X : array, shape = [n_samples, n_features]
        Training vector, where n_samples is the number of samples and
        n_features is the number of features.

    max_iter : int, optional
        Maximum number of iterations.  Default is 300.

    tol : float, optional
        Stop the algorithm if spatial_median has converged. Default is 1.e-3.

    Returns
    -------
    spatial_median : array, shape = [n_features]
        Spatial median.

    n_iter: int
        Number of iterations needed.

    References
    ----------
    - On Computation of Spatial Median for Robust Data Mining, 2005
      <NAME> and <NAME>
      http://users.jyu.fi/~samiayr/pdf/ayramo_eurogen05.pdf
    """
    if X.shape[1] == 1:
        return 1, np.median(X.ravel())
    # We are computing the tol on the squared norm
    tol **= 2
    spatial_median_old = np.mean(X, axis=0)
    for n_iter in range(max_iter):
        spatial_median = _modified_weiszfeld_step(X, spatial_median_old)
        if np.sum((spatial_median_old - spatial_median) ** 2) < tol:
            break
        else:
            spatial_median_old = spatial_median
    else:
        warnings.warn("Maximum number of iterations {max_iter} reached in "
                      "spatial median for TheilSen regressor.".format(
            max_iter=max_iter), ConvergenceWarning)
    return n_iter, spatial_median

{'fraction_non_alphanumeric': 0.0962936046511628, 'fraction_numerical': 0.011385658914728682, 'mean_word_length': 2.4780960404380794, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '3202515', 'n_tokens_mistral': 2695, 'n_tokens_neox': 2510, 'n_words': 880}