Fit the baseline regressor. Parameters ---------- X : array-like of shape (n_samples, n_features) Training data. y : array-like of shape (n_samples,) or (n_samples, n_outputs) Target values. sample_weight : array-like of shape (n_sam
(self, X, y, sample_weight=None)
| 541 | |
| 542 | @_fit_context(prefer_skip_nested_validation=True) |
| 543 | def fit(self, X, y, sample_weight=None): |
| 544 | """Fit the baseline regressor. |
| 545 | |
| 546 | Parameters |
| 547 | ---------- |
| 548 | X : array-like of shape (n_samples, n_features) |
| 549 | Training data. |
| 550 | |
| 551 | y : array-like of shape (n_samples,) or (n_samples, n_outputs) |
| 552 | Target values. |
| 553 | |
| 554 | sample_weight : array-like of shape (n_samples,), default=None |
| 555 | Sample weights. |
| 556 | |
| 557 | Returns |
| 558 | ------- |
| 559 | self : object |
| 560 | Fitted estimator. |
| 561 | """ |
| 562 | validate_data(self, X, skip_check_array=True) |
| 563 | |
| 564 | y = check_array(y, ensure_2d=False, input_name="y") |
| 565 | if len(y) == 0: |
| 566 | raise ValueError("y must not be empty.") |
| 567 | |
| 568 | if y.ndim == 1: |
| 569 | y = np.reshape(y, (-1, 1)) |
| 570 | self.n_outputs_ = y.shape[1] |
| 571 | |
| 572 | check_consistent_length(X, y, sample_weight) |
| 573 | |
| 574 | if sample_weight is not None: |
| 575 | sample_weight = _check_sample_weight(sample_weight, X) |
| 576 | |
| 577 | if self.strategy == "mean": |
| 578 | self.constant_ = np.average(y, axis=0, weights=sample_weight) |
| 579 | |
| 580 | elif self.strategy == "median": |
| 581 | if sample_weight is None: |
| 582 | self.constant_ = np.median(y, axis=0) |
| 583 | else: |
| 584 | self.constant_ = _weighted_percentile( |
| 585 | y, sample_weight, percentile_rank=50.0 |
| 586 | ) |
| 587 | |
| 588 | elif self.strategy == "quantile": |
| 589 | if self.quantile is None: |
| 590 | raise ValueError( |
| 591 | "When using `strategy='quantile', you have to specify the desired " |
| 592 | "quantile in the range [0, 1]." |
| 593 | ) |
| 594 | percentile_rank = self.quantile * 100.0 |
| 595 | if sample_weight is None: |
| 596 | self.constant_ = np.percentile(y, axis=0, q=percentile_rank) |
| 597 | else: |
| 598 | self.constant_ = _weighted_percentile( |
| 599 | y, sample_weight, percentile_rank=percentile_rank |
| 600 | ) |