'Fit estimator using RANSAC algorithm.
Parameters
X : array-like or sparse matrix, shape [n_samples, n_features]
Training data.
y : array-like, shape = [n_samples] or [n_samples, n_targets]
Target values.
sample_weight : array-like, shape = [n_samples]
Individual weights for each sample
raises error if sample_weight is passed and base_estimator
fit method does not support it.
Raises
ValueError
If no valid consensus set could be found. This occurs if
`is_data_valid` and `is_model_valid` return False for all
`max_trials` randomly chosen sub-samples.'

def fit(self, X, y, sample_weight=None):

    X = check_array(X, accept_sparse='csr')
    y = check_array(y, ensure_2d=False)
    check_consistent_length(X, y)
    if (self.base_estimator is not None):
        base_estimator = clone(self.base_estimator)
    else:
        base_estimator = LinearRegression()
    if (self.min_samples is None):
        min_samples = (X.shape[1] + 1)
    elif (0 < self.min_samples < 1):
        min_samples = np.ceil((self.min_samples * X.shape[0]))
    elif (self.min_samples >= 1):
        if ((self.min_samples % 1) != 0):
            raise ValueError('Absolute    number    of    samples    must    be    an    integer    value.')
        min_samples = self.min_samples
    else:
        raise ValueError('Value    for    `min_samples`    must    be    scalar    and    positive.')
    if (min_samples > X.shape[0]):
        raise ValueError('`min_samples`    may    not    be    larger    than    number    of    samples    ``X.shape[0]``.')
    if ((self.stop_probability < 0) or (self.stop_probability > 1)):
        raise ValueError('`stop_probability`    must    be    in    range    [0,    1].')
    if (self.residual_threshold is None):
        residual_threshold = np.median(np.abs((y - np.median(y))))
    else:
        residual_threshold = self.residual_threshold
    if (self.residual_metric is not None):
        warnings.warn("'residual_metric'    was    deprecated    in    version    0.18    and    will    be    removed    in    version    0.20.    Use    'loss'    instead.", DeprecationWarning)
    if (self.loss == 'absolute_loss'):
        if (y.ndim == 1):
            loss_function = (lambda y_true, y_pred: np.abs((y_true - y_pred)))
        else:
            loss_function = (lambda y_true, y_pred: np.sum(np.abs((y_true - y_pred)), axis=1))
    elif (self.loss == 'squared_loss'):
        if (y.ndim == 1):
            loss_function = (lambda y_true, y_pred: ((y_true - y_pred) ** 2))
        else:
            loss_function = (lambda y_true, y_pred: np.sum(((y_true - y_pred) ** 2), axis=1))
    elif callable(self.loss):
        loss_function = self.loss
    else:
        raise ValueError(("loss    should    be    'absolute_loss',    'squared_loss'    or    a    callable.Got    %s.    " % self.loss))
    random_state = check_random_state(self.random_state)
    try:
        base_estimator.set_params(random_state=random_state)
    except ValueError:
        pass
    estimator_fit_has_sample_weight = has_fit_parameter(base_estimator, 'sample_weight')
    estimator_name = type(base_estimator).__name__
    if ((sample_weight is not None) and (not estimator_fit_has_sample_weight)):
        raise ValueError(('%s    does    not    support    sample_weight.    Samples    weights    are    only    used    for    the    calibration    itself.' % estimator_name))
    if (sample_weight is not None):
        sample_weight = np.asarray(sample_weight)
    n_inliers_best = 1
    score_best = (- np.inf)
    inlier_mask_best = None
    X_inlier_best = None
    y_inlier_best = None
    self.n_skips_no_inliers_ = 0
    self.n_skips_invalid_data_ = 0
    self.n_skips_invalid_model_ = 0
    n_samples = X.shape[0]
    sample_idxs = np.arange(n_samples)
    (n_samples, _) = X.shape
    self.n_trials_ = 0
    max_trials = self.max_trials
    while (self.n_trials_ < max_trials):
        self.n_trials_ += 1
        if (((self.n_skips_no_inliers_ + self.n_skips_invalid_data_) + self.n_skips_invalid_model_) > self.max_skips):
            break
        subset_idxs = sample_without_replacement(n_samples, min_samples, random_state=random_state)
        X_subset = X[subset_idxs]
        y_subset = y[subset_idxs]
        if ((self.is_data_valid is not None) and (not self.is_data_valid(X_subset, y_subset))):
            self.n_skips_invalid_data_ += 1
            continue
        if (sample_weight is None):
            base_estimator.fit(X_subset, y_subset)
        else:
            base_estimator.fit(X_subset, y_subset, sample_weight=sample_weight[subset_idxs])
        if ((self.is_model_valid is not None) and (not self.is_model_valid(base_estimator, X_subset, y_subset))):
            self.n_skips_invalid_model_ += 1
            continue
        y_pred = base_estimator.predict(X)
        if (self.residual_metric is not None):
            diff = (y_pred - y)
            if (diff.ndim == 1):
                diff = diff.reshape((-1), 1)
            residuals_subset = self.residual_metric(diff)
        else:
            residuals_subset = loss_function(y, y_pred)
        inlier_mask_subset = (residuals_subset < residual_threshold)
        n_inliers_subset = np.sum(inlier_mask_subset)
        if (n_inliers_subset < n_inliers_best):
            self.n_skips_no_inliers_ += 1
            continue
        inlier_idxs_subset = sample_idxs[inlier_mask_subset]
        X_inlier_subset = X[inlier_idxs_subset]
        y_inlier_subset = y[inlier_idxs_subset]
        score_subset = base_estimator.score(X_inlier_subset, y_inlier_subset)
        if ((n_inliers_subset == n_inliers_best) and (score_subset < score_best)):
            continue
        n_inliers_best = n_inliers_subset
        score_best = score_subset
        inlier_mask_best = inlier_mask_subset
        X_inlier_best = X_inlier_subset
        y_inlier_best = y_inlier_subset
        max_trials = min(max_trials, _dynamic_max_trials(n_inliers_best, n_samples, min_samples, self.stop_probability))
        if ((n_inliers_best >= self.stop_n_inliers) or (score_best >= self.stop_score)):
            break
    if (inlier_mask_best is None):
        if (((self.n_skips_no_inliers_ + self.n_skips_invalid_data_) + self.n_skips_invalid_model_) > self.max_skips):
            raise ValueError('RANSAC    skipped    more    iterations    than    `max_skips`    without    finding    a    valid    consensus    set.    Iterations    were    skipped    because    each    randomly    chosen    sub-sample    failed    the    passing    criteria.    See    estimator    attributes    for    diagnostics    (n_skips*).')
        else:
            raise ValueError('RANSAC    could    not    find    a    valid    consensus    set.    All    `max_trials`    iterations    were    skipped    because    each    randomly    chosen    sub-sample    failed    the    passing    criteria.    See    estimator    attributes    for    diagnostics    (n_skips*).')
    elif (((self.n_skips_no_inliers_ + self.n_skips_invalid_data_) + self.n_skips_invalid_model_) > self.max_skips):
        warnings.warn('RANSAC    found    a    valid    consensus    set    but    exited    early    due    to    skipping    more    iterations    than    `max_skips`.    See    estimator    attributes    for    diagnostics    (n_skips*).', UserWarning)
    base_estimator.fit(X_inlier_best, y_inlier_best)
    self.estimator_ = base_estimator
    self.inlier_mask_ = inlier_mask_best
    return self
