Parameters of sklearn class methods

Viewed 124

I have 2 small questions. So, after browsing a lot of the documentation of sklearn, I have noticed that many sklearn preprocessing classes such as standard scaler have a .transform method which takes the X values and scales them. However , to ensure that this can be performed in a pipeline, the method also ensures that the y values are taken as input too (albeit ignored if need be)

So, question 1: the documentation of these methods looks like this:

fit_transform(self, X[, y]).

Why is the y in brackets like that with a preceding comma. Is it to signify that this value is not necessary as an input?

Question 2: If my thoughts on question one are correct, the fit_transform documentation of SelectKBest looks like this:

fit_transform(self, X[, y])

In this case, how can y be an optional input, if there is no way to select K features without the y values as targets?

1 Answers

If you check the explanation of the docs:

def fit_transform(self, X, y=None, **fit_params):
        """
        Fit to data, then transform it.
        Fits transformer to X and y with optional parameters fit_params
        and returns a transformed version of X.
        Parameters
        ----------
        X : {array-like, sparse matrix, dataframe} of shape \
                (n_samples, n_features)
        y : ndarray of shape (n_samples,), default=None
            Target values.
        **fit_params : dict
            Additional fit parameters.
        Returns
        -------
        X_new : ndarray array of shape (n_samples, n_features_new)
            Transformed array.
        """
        # non-optimized default implementation; override when a better
        # method is possible for a given clustering algorithm
        if y is None:
            # fit method of arity 1 (unsupervised transformation)
            return self.fit(X, **fit_params).transform(X)
        else:
            # fit method of arity 2 (supervised transformation)
            return self.fit(X, y, **fit_params).transform(X)

The answer for 1 and 2 is in the last lines: if y is none , so its basically for unsupervised transformation, for example if you run `k-means' where you don't have a target value y.

Related