Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# validate inputs X and y (optional)
X = check_array(X)
self._set_n_classes(y)
self.detector_ = LocalOutlierFactor(n_neighbors=self.n_neighbors,
algorithm=self.algorithm,
leaf_size=self.leaf_size,
metric=self.metric,
p=self.p,
metric_params=self.metric_params,
contamination=self.contamination,
n_jobs=self.n_jobs)
self.detector_.fit(X=X, y=y)
# Invert decision_scores_. Outliers comes with higher outlier scores
self.decision_scores_ = invert_order(
self.detector_.negative_outlier_factor_)
self._process_decision_scores()
return self
# Do not pass behaviour argument when sklearn version is < 0.20 or >0.21
else: # pragma: no cover
self.detector_ = IsolationForest(n_estimators=self.n_estimators,
max_samples=self.max_samples,
contamination=self.contamination,
max_features=self.max_features,
bootstrap=self.bootstrap,
n_jobs=self.n_jobs,
random_state=self.random_state,
verbose=self.verbose)
self.detector_.fit(X=X, y=None, sample_weight=None)
# invert decision_scores_. Outliers comes with higher outlier scores.
self.decision_scores_ = invert_order(
self.detector_.decision_function(X))
self._process_decision_scores()
return self
self.detector_ = OneClassSVM(kernel=self.kernel,
degree=self.degree,
gamma=self.gamma,
coef0=self.coef0,
tol=self.tol,
nu=self.nu,
shrinking=self.shrinking,
cache_size=self.cache_size,
verbose=self.verbose,
max_iter=self.max_iter)
self.detector_.fit(X=X, y=y, sample_weight=sample_weight,
**params)
# invert decision_scores_. Outliers comes with higher outlier scores
self.decision_scores_ = invert_order(
self.detector_.decision_function(X))
self._process_decision_scores()
return self
# build the histograms for all dimensions
for i in range(n_features):
self.hist_[:, i], self.bin_edges_[:, i] = \
np.histogram(X[:, i], bins=self.n_bins, density=True)
# the sum of (width * height) should equal to 1
assert (np.isclose(1, np.sum(
self.hist_[:, i] * np.diff(self.bin_edges_[:, i])), atol=0.1))
# outlier_scores = self._calculate_outlier_scores(X)
outlier_scores = _calculate_outlier_scores(X, self.bin_edges_,
self.hist_,
self.n_bins,
self.alpha, self.tol)
# invert decision_scores_. Outliers comes with higher outlier scores
self.decision_scores_ = invert_order(np.sum(outlier_scores, axis=1))
self._process_decision_scores()
return self
X : numpy array of shape (n_samples, n_features)
The training input samples. Sparse matrices are accepted only
if they are supported by the base estimator.
Returns
-------
anomaly_scores : numpy array of shape (n_samples,)
The anomaly score of the input samples.
"""
check_is_fitted(self, ['decision_scores_', 'threshold_', 'labels_'])
# Invert outlier scores. Outliers comes with higher outlier scores
# noinspection PyProtectedMember
if _get_sklearn_version() > 19:
return invert_order(self.detector_._score_samples(X))
else:
return invert_order(self.detector_._decision_function(X))
if they are supported by the base estimator.
Returns
-------
anomaly_scores : numpy array of shape (n_samples,)
The anomaly score of the input samples.
"""
check_is_fitted(self, ['hist_', 'bin_edges_'])
X = check_array(X)
# outlier_scores = self._calculate_outlier_scores(X)
outlier_scores = _calculate_outlier_scores(X, self.bin_edges_,
self.hist_,
self.n_bins,
self.alpha, self.tol)
return invert_order(np.sum(outlier_scores, axis=1))
larger anomaly scores.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The training input samples. Sparse matrices are accepted only
if they are supported by the base estimator.
Returns
-------
anomaly_scores : numpy array of shape (n_samples,)
The anomaly score of the input samples.
"""
check_is_fitted(self, ['decision_scores_', 'threshold_', 'labels_'])
# invert outlier scores. Outliers comes with higher outlier scores
return invert_order(self.detector_.decision_function(X))
if they are supported by the base estimator.
Returns
-------
anomaly_scores : numpy array of shape (n_samples,)
The anomaly score of the input samples.
"""
check_is_fitted(self, ['decision_scores_', 'threshold_', 'labels_'])
# Invert outlier scores. Outliers comes with higher outlier scores
# noinspection PyProtectedMember
if _get_sklearn_version() > 19:
return invert_order(self.detector_._score_samples(X))
else:
return invert_order(self.detector_._decision_function(X))
larger anomaly scores.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The training input samples. Sparse matrices are accepted only
if they are supported by the base estimator.
Returns
-------
anomaly_scores : numpy array of shape (n_samples,)
The anomaly score of the input samples.
"""
check_is_fitted(self, ['decision_scores_', 'threshold_', 'labels_'])
# Invert outlier scores. Outliers comes with higher outlier scores
return invert_order(self.detector_.decision_function(X))