66# License: BSD 2 clause
77
88
9+ import inspect
910import warnings
1011
1112import numpy as np
@@ -92,10 +93,14 @@ class CBLOF(BaseDetector):
9293 RandomState instance used by `np.random`.
9394
9495 n_jobs : int, optional (default=1)
95- Accepted for API compatibility but currently unused: the value is
96- neither stored on the estimator nor forwarded to the clustering
97- step, so ``get_params()`` reports ``None`` for it and ``clone()``
98- does not carry it over. See issue #713.
96+ Number of parallel jobs for the default KMeans backend.
97+ Only forwarded to ``KMeans`` when set to a value other than 1
98+ *and* the installed scikit-learn version supports it (the
99+ parameter was deprecated in 0.23 and removed in 0.25).
100+ On sklearn >= 0.25, or when left at the default of 1, this value
101+ is stored for ``get_params()`` / ``clone()`` compatibility only.
102+ Has no effect when a custom ``clustering_estimator`` is provided;
103+ set ``n_jobs`` on that estimator directly.
99104
100105 Attributes
101106 ----------
@@ -149,6 +154,7 @@ def __init__(self, n_clusters=8, contamination=0.1,
149154 self .use_weights = use_weights
150155 self .check_estimator = check_estimator
151156 self .random_state = random_state
157+ self .n_jobs = n_jobs
152158
153159 # noinspection PyIncorrectDocstring
154160 def fit (self , X , y = None ):
@@ -175,9 +181,17 @@ def fit(self, X, y=None):
175181
176182 # check parameters
177183 # number of clusters are default to 8
178- self ._validate_estimator (default = KMeans (
179- n_clusters = self .n_clusters ,
180- random_state = self .random_state ))
184+ _kmeans_kwargs = dict (
185+ n_clusters = self .n_clusters , random_state = self .random_state )
186+ # n_jobs was removed from KMeans in sklearn 0.25; on 0.23/0.24 it is
187+ # deprecated and emits FutureWarning for any concrete value, including
188+ # the default 1. Only forward when the user explicitly requested more
189+ # than one job so that plain CBLOF().fit(X) stays warning-free.
190+ if (self .clustering_estimator is None
191+ and self .n_jobs != 1
192+ and "n_jobs" in inspect .signature (KMeans .__init__ ).parameters ):
193+ _kmeans_kwargs ["n_jobs" ] = self .n_jobs
194+ self ._validate_estimator (default = KMeans (** _kmeans_kwargs ))
181195
182196 self .clustering_estimator_ .fit (X = X , y = y )
183197 # Get the labels of the clustering results
0 commit comments