MatchVariables#
- class feature_engine.preprocessing.MatchVariables(fill_value=nan, missing_values='raise', verbose=True)[source]#
MatchVariables() ensures that the same variables observed in the train set are present in the test set. If the dataset to transform contains variables that were not present in the train set, they are dropped. If the dataset to transform lacks variables that were present in the train set, these variables are added to the dataframe with a value determined by the user (np.nan by default).
train = pd.DataFrame({ "Name": ["tom", "nick", "krish", "jack"], "City": ["London", "Manchester", "Liverpool", "Bristol"], "Age": [20, 21, 19, 18], "Marks": [0.9, 0.8, 0.7, 0.6], }) test = pd.DataFrame({ "Name": ["tom", "sam", "nick"], "Age": [20, 22, 23], "Marks": [0.9, 0.7, 0.6], "Hobbies": ["tennis", "rugby", "football"] }) match_columns = MatchVariables() match_columns.fit(train) df_transformed = match_columns.transform(test)
Note that in the returned dataframe, the variable “Hobbies” was removed and the variable “City” was added with np.nan:
df_transformed Name City Age Marks 0 tom np.nan 20 0.9 1 sam np.nan 22 0.7 2 nick np.nan 23 0.6
The order of the variables in the transformed dataset is also adjusted to match that observed in the train set.
More details in the User Guide.
- Parameters
- fill_value: integer, float or string. Default=np.nan
The values for the variables that will be added to the transformed dataset.
- missing_values: string, default=’ignore’
Indicates if missing values should be ignored or raised. If ‘ignore’, the transformer will ignore missing data when transforming the data. If ‘raise’ the transformer will return an error if the training or the datasets to transform contain missing values.
- verbose: bool, default=True
If True, the transformer will print out the names of the variables that are added and / or removed from the dataset.
- Attributes
- input_features_:
The variables present in the train set, in the order observed during fit.
- n_features_in_:
The number of features in the train set used in fit.
Methods
fit:
Identify the variable names in the train set.
transform:
Add or delete variables to match those observed in the train set.
fit_transform:
Fit to the data. Then transform it.
- fit(X, y=None)[source]#
Learns and stores the names of the variables in the training dataset.
- Parameters
- X: pandas dataframe of shape = [n_samples, n_features]
The input dataframe.
- y: None
y is not needed for this transformer. You can pass y or None.
- fit_transform(X, y=None, **fit_params)[source]#
Fit to data, then transform it.
Fits transformer to
X
andy
with optional parametersfit_params
and returns a transformed version ofX
.- Parameters
- Xarray-like of shape (n_samples, n_features)
Input samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None
Target values (None for unsupervised transformations).
- **fit_paramsdict
Additional fit parameters.
- Returns
- X_newndarray array of shape (n_samples, n_features_new)
Transformed array.
- get_params(deep=True)[source]#
Get parameters for this estimator.
- Parameters
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns
- paramsdict
Parameter names mapped to their values.
- set_params(**params)[source]#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters
- **paramsdict
Estimator parameters.
- Returns
- selfestimator instance
Estimator instance.
- transform(X)[source]#
Drops variables that were not seen in the train set and adds variables that were in the train set but not in the data to transform. In other words, it returns a dataframe with matching columns.
- Parameters
- X: pandas dataframe of shape = [n_samples, n_features]
The data to transform.
- Returns
- X_new: Pandas dataframe, shape = [n_samples, n_features]
The dataframe with variables that match those observed in the train set.
- :rtype:py:class:
~pandas.core.frame.DataFrame