RANSAC (RANdom SAmple Consensus) algorithm.
RANSAC is an iterative algorithm for the robust estimation of parameters from a subset of inliers from the complete data set.
Read more in the User Guide.
Base estimator object which implements the following methods:
fit(X, y)
: Fit model to given training data and target values.
score(X, y)
: Returns the mean accuracy on the given test data, which is used for the stop criterion defined by stop_score
. Additionally, the score is used to decide which of two equally large consensus sets is chosen as the better one.
predict(X)
: Returns predicted values using the linear model, which is used to compute residual error using loss function.
If estimator
is None, then LinearRegression
is used for target values of dtype float.
Note that the current implementation only supports regression estimators.
Minimum number of samples chosen randomly from original data. Treated as an absolute number of samples for min_samples >= 1
, treated as a relative number ceil(min_samples * X.shape[0])
for min_samples < 1
. This is typically chosen as the minimal number of samples necessary to estimate the given estimator
. By default a LinearRegression
estimator is assumed and min_samples
is chosen as X.shape[1] + 1
. This parameter is highly dependent upon the model, so if a estimator
other than LinearRegression
is used, the user must provide a value.
Maximum residual for a data sample to be classified as an inlier. By default the threshold is chosen as the MAD (median absolute deviation) of the target values y
. Points whose residuals are strictly equal to the threshold are considered as inliers.
This function is called with the randomly selected data before the model is fitted to it: is_data_valid(X, y)
. If its return value is False the current randomly chosen sub-sample is skipped.
This function is called with the estimated model and the randomly selected data: is_model_valid(model, X, y)
. If its return value is False the current randomly chosen sub-sample is skipped. Rejecting samples with this function is computationally costlier than with is_data_valid
. is_model_valid
should therefore only be used if the estimated model is needed for making the rejection decision.
Maximum number of iterations for random sample selection.
Maximum number of iterations that can be skipped due to finding zero inliers or invalid data defined by is_data_valid
or invalid models defined by is_model_valid
.
Added in version 0.19.
Stop iteration if at least this number of inliers are found.
Stop iteration if score is greater equal than this threshold.
RANSAC iteration stops if at least one outlier-free set of the training data is sampled in RANSAC. This requires to generate at least N samples (iterations):
N >= log(1 - probability) / log(1 - e**m)
where the probability (confidence) is typically set to high value such as 0.99 (the default) and e is the current fraction of inliers w.r.t. the total number of samples.
String inputs, ‘absolute_error’ and ‘squared_error’ are supported which find the absolute error and squared error per sample respectively.
If loss
is a callable, then it should be a function that takes two arrays as inputs, the true and predicted value and returns a 1-D array with the i-th value of the array corresponding to the loss on X[i]
.
If the loss on a sample is greater than the residual_threshold
, then this sample is classified as an outlier.
Added in version 0.18.
The generator used to initialize the centers. Pass an int for reproducible output across multiple function calls. See Glossary.
Final model fitted on the inliers predicted by the “best” model found during RANSAC sampling (copy of the estimator
object).
Number of random selection trials until one of the stop criteria is met. It is always <= max_trials
.
Boolean mask of inliers classified as True
.
Number of iterations skipped due to finding zero inliers.
Added in version 0.19.
Number of iterations skipped due to invalid data defined by is_data_valid
.
Added in version 0.19.
Number of iterations skipped due to an invalid model defined by is_model_valid
.
Added in version 0.19.
Number of features seen during fit.
Added in version 0.24.
n_features_in_
,)
Names of features seen during fit. Defined only when X
has feature names that are all strings.
Added in version 1.0.
See also
HuberRegressor
Linear regression model that is robust to outliers.
TheilSenRegressor
Theil-Sen Estimator robust multivariate regression model.
SGDRegressor
Fitted by minimizing a regularized empirical loss with SGD.
References
Examples
>>> from sklearn.linear_model import RANSACRegressor >>> from sklearn.datasets import make_regression >>> X, y = make_regression( ... n_samples=200, n_features=2, noise=4.0, random_state=0) >>> reg = RANSACRegressor(random_state=0).fit(X, y) >>> reg.score(X, y) 0.9885 >>> reg.predict(X[:1,]) array([-31.9417])
For a more detailed example, see Robust linear model estimation using RANSAC
Fit estimator using RANSAC algorithm.
Training data.
Target values.
Individual weights for each sample raises error if sample_weight is passed and estimator fit method does not support it.
Added in version 0.18.
Parameters routed to the fit
method of the sub-estimator via the metadata routing API.
Added in version 1.5: Only available if sklearn.set_config(enable_metadata_routing=True)
is set. See Metadata Routing User Guide for more details.
Fitted RANSACRegressor
estimator.
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.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
Added in version 1.5.
A MetadataRouter
encapsulating routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Predict using the estimated model.
This is a wrapper for estimator_.predict(X)
.
Input data.
Parameters routed to the predict
method of the sub-estimator via the metadata routing API.
Added in version 1.5: Only available if sklearn.set_config(enable_metadata_routing=True)
is set. See Metadata Routing User Guide for more details.
Returns predicted values.
Return the score of the prediction.
This is a wrapper for estimator_.score(X, y)
.
Training data.
Target values.
Parameters routed to the score
method of the sub-estimator via the metadata routing API.
Added in version 1.5: Only available if sklearn.set_config(enable_metadata_routing=True)
is set. See Metadata Routing User Guide for more details.
Score of the prediction.
Configure whether metadata should be requested to be passed to the fit
method.
Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True
(seesklearn.set_config
). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if provided. The request is ignored if metadata is not provided.
False
: metadata is not requested and the meta-estimator will not pass it tofit
.
None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Metadata routing for sample_weight
parameter in fit
.
The updated object.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline
). The latter have parameters of the form <component>__<parameter>
so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4