MCPcopy
hub / github.com/scikit-learn/scikit-learn / fit

Method fit

sklearn/multiclass.py:331–395  ·  view source on GitHub ↗

Fit underlying estimators. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features) Data. y : {array-like, sparse matrix} of shape (n_samples,) or (n_samples, n_classes) Multi-class targets. An indicator matrix t

(self, X, y, **fit_params)

Source from the content-addressed store, hash-verified

329 prefer_skip_nested_validation=False
330 )
331 def fit(self, X, y, **fit_params):
332 """Fit underlying estimators.
333
334 Parameters
335 ----------
336 X : {array-like, sparse matrix} of shape (n_samples, n_features)
337 Data.
338
339 y : {array-like, sparse matrix} of shape (n_samples,) or (n_samples, n_classes)
340 Multi-class targets. An indicator matrix turns on multilabel
341 classification.
342
343 **fit_params : dict
344 Parameters passed to the ``estimator.fit`` method of each
345 sub-estimator.
346
347 .. versionadded:: 1.4
348 Only available if `enable_metadata_routing=True`. See
349 :ref:`Metadata Routing User Guide <metadata_routing>` for more
350 details.
351
352 Returns
353 -------
354 self : object
355 Instance of fitted estimator.
356 """
357 _raise_for_params(fit_params, self, "fit")
358
359 routed_params = process_routing(
360 self,
361 "fit",
362 **fit_params,
363 )
364 # A sparse LabelBinarizer, with sparse_output=True, has been shown to
365 # outperform or match a dense label binarizer in all cases and has also
366 # resulted in less or equal memory consumption in the fit_ovr function
367 # overall.
368 self.label_binarizer_ = LabelBinarizer(sparse_output=True)
369 Y = self.label_binarizer_.fit_transform(y)
370 Y = Y.tocsc()
371 self.classes_ = self.label_binarizer_.classes_
372 columns = (col.toarray().ravel() for col in Y.T)
373 # In cases where individual estimators are very fast to train setting
374 # n_jobs > 1 in can results in slower performance due to the overhead
375 # of spawning threads. See joblib issue #112.
376 self.estimators_ = Parallel(n_jobs=self.n_jobs, verbose=self.verbose)(
377 delayed(_fit_binary)(
378 self.estimator,
379 X,
380 column,
381 fit_params=routed_params.estimator.fit,
382 classes=[
383 "not %s" % self.label_binarizer_.classes_[i],
384 self.label_binarizer_.classes_[i],
385 ],
386 )
387 for i, column in enumerate(columns)
388 )

Calls 6

LabelBinarizerClass · 0.90
ParallelClass · 0.90
delayedFunction · 0.90
_raise_for_paramsFunction · 0.85
process_routingFunction · 0.85
fit_transformMethod · 0.45