Solves linear One-Class SVM using Stochastic Gradient Descent.
This implementation is meant to be used with a kernel approximation technique (e.g. sklearn.kernel_approximation.Nystroem
) to obtain results similar to sklearn.svm.OneClassSVM
which uses a Gaussian kernel by default.
Read more in the User Guide.
Added in version 1.0.
The nu parameter of the One Class SVM: an upper bound on the fraction of training errors and a lower bound of the fraction of support vectors. Should be in the interval (0, 1]. By default 0.5 will be taken.
Whether the intercept should be estimated or not. Defaults to True.
The maximum number of passes over the training data (aka epochs). It only impacts the behavior in the fit
method, and not the partial_fit
. Defaults to 1000. Values must be in the range [1, inf)
.
The stopping criterion. If it is not None, the iterations will stop when (loss > previous_loss - tol). Defaults to 1e-3. Values must be in the range [0.0, inf)
.
Whether or not the training data should be shuffled after each epoch. Defaults to True.
The verbosity level.
The seed of the pseudo random number generator to use when shuffling the data. If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random
.
The learning rate schedule to use with fit
. (If using partial_fit
, learning rate must be controlled directly).
‘constant’: eta = eta0
‘optimal’: eta = 1.0 / (alpha * (t + t0))
where t0 is chosen by a heuristic proposed by Leon Bottou.
‘invscaling’: eta = eta0 / pow(t, power_t)
‘adaptive’: eta = eta0, as long as the training keeps decreasing. Each time n_iter_no_change consecutive epochs fail to decrease the training loss by tol or fail to increase validation score by tol if early_stopping is True, the current learning rate is divided by 5.
The initial learning rate for the ‘constant’, ‘invscaling’ or ‘adaptive’ schedules. The default value is 0.0 as eta0 is not used by the default schedule ‘optimal’. Values must be in the range [0.0, inf)
.
The exponent for inverse scaling learning rate. Values must be in the range [0.0, inf)
.
Deprecated since version 1.8: Negative values for power_t
are deprecated in version 1.8 and will raise an error in 1.10. Use values in the range [0.0, inf) instead.
When set to True, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. See the Glossary.
Repeatedly calling fit or partial_fit when warm_start is True can result in a different solution than when calling fit a single time because of the way the data is shuffled. If a dynamic learning rate is used, the learning rate is adapted depending on the number of samples already seen. Calling fit
resets this counter, while partial_fit
will result in increasing the existing counter.
When set to True, computes the averaged SGD weights and stores the result in the coef_
attribute. If set to an int greater than 1, averaging will begin once the total number of samples seen reaches average. So average=10
will begin averaging after seeing 10 samples.
Weights assigned to the features.
Offset used to define the decision function from the raw scores. We have the relation: decision_function = score_samples - offset.
The actual number of iterations to reach the stopping criterion.
Number of weight updates performed during training. Same as (n_iter_ * n_samples + 1)
.
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.
Notes
This estimator has a linear complexity in the number of training samples and is thus better suited than the sklearn.svm.OneClassSVM
implementation for datasets with a large number of training samples (say > 10,000).
Examples
>>> import numpy as np >>> from sklearn import linear_model >>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) >>> clf = linear_model.SGDOneClassSVM(random_state=42) >>> clf.fit(X) SGDOneClassSVM(random_state=42)
>>> print(clf.predict([[4, 4]])) [1]
Signed distance to the separating hyperplane.
Signed distance is positive for an inlier and negative for an outlier.
Testing data.
Decision function values of the samples.
Convert coefficient matrix to dense array format.
Converts the coef_
member (back) to a numpy.ndarray. This is the default format of coef_
and is required for fitting, so calling this method is only required on models that have previously been sparsified; otherwise, it is a no-op.
Fitted estimator.
Fit linear One-Class SVM with Stochastic Gradient Descent.
This solves an equivalent optimization problem of the One-Class SVM primal optimization problem and returns a weight vector w and an offset rho such that the decision function is given by <w, x> - rho.
Training data.
Not used, present for API consistency by convention.
The initial coefficients to warm-start the optimization.
The initial offset to warm-start the optimization.
Weights applied to individual samples. If not provided, uniform weights are assumed. These weights will be multiplied with class_weight (passed through the constructor) if class_weight is specified.
Returns a fitted instance of self.
Perform fit on X and returns labels for X.
Returns -1 for outliers and 1 for inliers.
The input samples.
Not used, present for API consistency by convention.
Arguments to be passed to fit
.
Added in version 1.4.
1 for inliers, -1 for outliers.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest
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.
Fit linear One-Class SVM with Stochastic Gradient Descent.
Subset of the training data.
Not used, present for API consistency by convention.
Weights applied to individual samples. If not provided, uniform weights are assumed.
Returns a fitted instance of self.
Return labels (1 inlier, -1 outlier) of the samples.
Testing data.
Labels of the samples.
Raw scoring function of the samples.
Testing data.
Unshiffted scoring function values of the samples.
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
(see sklearn.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 to fit
if provided. The request is ignored if metadata is not provided.
False
: metadata is not requested and the meta-estimator will not pass it to fit
.
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 coef_init
parameter in fit
.
Metadata routing for offset_init
parameter in fit
.
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.
Configure whether metadata should be requested to be passed to the partial_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
(see sklearn.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 to partial_fit
if provided. The request is ignored if metadata is not provided.
False
: metadata is not requested and the meta-estimator will not pass it to partial_fit
.
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 partial_fit
.
The updated object.
Convert coefficient matrix to sparse format.
Converts the coef_
member to a scipy.sparse matrix, which for L1-regularized models can be much more memory- and storage-efficient than the usual numpy.ndarray representation.
The intercept_
member is not converted.
Fitted estimator.
Notes
For non-sparse models, i.e. when there are not many zeros in coef_
, this may actually increase memory usage, so use this method with care. A rule of thumb is that the number of zero elements, which can be computed with (coef_ == 0).sum()
, must be more than 50% for this to provide significant benefits.
After calling this method, further fitting with the partial_fit method (if any) will not work until you call densify.
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