Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Derived features: using PCA on a subset of columns

The modelling tools in modelmatrix allow for construction of transformers applied to features.

Index(['Sales', 'CompPrice', 'Income', 'Advertising', 'Population', 'Price', 'ShelveLoc', 'Age', 'Education', 'Urban', 'US'], dtype='str')

Let’s create a ModelSpec that is aware of all of the relevant columns.

Suppose we want to make a Feature representing the first 3 principal components of the features ['CompPrice', 'Income', 'Advertising', 'Population', 'Price'].

We first make a Feature that represents these five features columns, then pca can be used to compute a new Feature that returns the first three principal components.

We can now fit sklearn_pca and create our new feature.

Helper function

The function pca encompasses these steps into a single function for convenience.

Index(['pca(grouped, n_components=3, whiten=True)[0]', 'pca(grouped, n_components=3, whiten=True)[1]', 'pca(grouped, n_components=3, whiten=True)[2]'], dtype='str')

Direct comparison

(np.float64(0.0), np.float64(0.0))