ageron/handson-mlp

[bug] Chapter 2 Exercise 6

Open

#35 aperta il 24 mar 2026

Vedi su GitHub
 (0 commenti) (0 reazioni) (1 assegnatario)Jupyter Notebook (513 fork)github user discovery
bughelp wanted

Metriche repository

Star
 (1561 star)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

Enter the chapter number

Chapter 2 Exercise 6

Enter the page number

No response

What is the cell's number in the notebook

164

Enter the environment you are using to run the notebook

None

Describe your issue

class StandardScalerClone(TransformerMixin, BaseEstimator): def init(self, with_mean=True): # no *args or **kwargs! self.with_mean = with_mean

**def fit(self, X, y=None):
    X = validate_data(self, X, ensure_2d=True)
    self.n_features_in_ = X.shape[1]
    if self.with_mean:
        self.mean_ = np.mean(X, axis=0)
    self.scale_ = np.std(X, axis=0, ddof=0)
    self.scale_[self.scale_ == 0] = 1  # Avoid division by zero
    return self**

def transform(self, X):
    check_is_fitted(self)
    X = validate_data(self, X, ensure_2d=True, reset=False)
    if self.with_mean:
        X = X - self.mean_
    return X / self.scale_

def inverse_transform(self, X):
    check_is_fitted(self)
    X = validate_data(self, X, ensure_2d=True, reset=False)
    return X * self.scale_ + self.mean_

def get_feature_names_out(self, input_features=None):
    if input_features is None:
        return getattr(self, "feature_names_in_",
                       [f"x{i}" for i in range(self.n_features_in_)])
    else:
        if len(input_features) != self.n_features_in_:
            raise ValueError("Invalid number of features")
        if hasattr(self, "feature_names_in_") and not np.all(
            self.feature_names_in_ == input_features
        ):
            raise ValueError("input_features ≠ feature_names_in_")
        return input_features

I think we need to assign feature_names_in in the fit function as per the exercise?

Enter what you expected to happen

No response

If you found a workaround, describe it here

No response

Guida contributor