Skip to content

FeatureGenerator


class atom.feature_engineering.FeatureGenerator(strategy="dfs", n_features=None, operators=None, n_jobs=1, verbose=0, logger=None, random_state=None, **kwargs)[source]
Generate new features.

Create new combinations of existing features to capture the non-linear relations between the original features.

This class can be accessed from atom through the feature_generation method. Read more in the user guide.

Warning

  • Using the div, log or sqrt operators can return new features with inf or NaN values. Check the warnings that may pop up or use atom's nans attribute.
  • When using dfs with n_jobs>1, make sure to protect your code with if __name__ == "__main__". Featuretools uses dask, which uses python multiprocessing for parallelization. The spawn method on multiprocessing starts a new python process, which requires it to import the __main__ module before it can do its task.
  • gfg can be slow for very large populations.

Tip

dfs can create many new features and not all of them will be useful. Use the FeatureSelector class to reduce the number of features.

Parametersstrategy: str, default="dfs"
Strategy to crate new features. Choose from:

  • "dfs": Deep Feature Synthesis.
  • "gfg": Genetic Feature Generation.

n_features: int or None, default=None
Maximum number of newly generated features to add to the dataset. If None, select all created features.

operators: str, sequence or None, default=None
Mathematical operators to apply on the features. None to use all. Choose from: add, sub, mul, div, abs, sqrt, log, inv, sin, cos, tan.

n_jobs: int, default=1
Number of cores to use for parallel processing.

  • If >0: Number of cores to use.
  • If -1: Use all available cores.
  • If <-1: Use number of cores - 1 + n_jobs.

verbose: int, default=0
Verbosity level of the class. Choose from:

  • 0 to not print anything.
  • 1 to print basic information.
  • 2 to print detailed information.

logger: str, Logger or None, default=None

  • If None: Doesn't save a logging file.
  • If str: Name of the log file. Use "auto" for automatic naming.
  • Else: Python logging.Logger instance.

random_state: int or None, default=None
Seed used by the random number generator. If None, the random number generator is the RandomState used by np.random.

**kwargs
Additional keyword arguments for the SymbolicTransformer instance. Only for the gfg strategy.

Attributesgfg: SymbolicTransformer
Object used to calculate the genetic features. Only for the gfg strategy.

genetic_features: pd.DataFrame
Information on the newly created non-linear features. Only for the gfg strategy. Columns include:

  • name: Name of the feature (generated automatically).
  • description: Operators used to create this feature.
  • fitness: Fitness score.

feature_names_in_: np.array
Names of features seen during fit.

n_features_in_: int
Number of features seen during fit.


See Also

FeatureExtractor

Extract features from datetime columns.

FeatureGrouper

Extract statistics from similar features.

FeatureSelector

Reduce the number of features in the data.


Example

>>> from atom import ATOMClassifier
>>> from sklearn.datasets import load_breast_cancer

>>> X, y = load_breast_cancer(return_X_y=True, as_frame=True)

>>> atom = ATOMClassifier(X, y)
>>> atom.feature_generation(strategy="dfs", n_features=5, verbose=2)

Fitting FeatureGenerator...
Generating new features...
 --> 5 new features were added.

>>> # Note the texture error / worst symmetry column
>>> print(atom.dataset)

     mean radius  mean texture  ...  texture error / worst symmetry  target
0          15.75         19.22  ...                        3.118963       0
1          12.10         17.72  ...                        5.418170       1
2          20.16         19.66  ...                        2.246481       0
3          12.88         18.22  ...                        4.527498       1
4          13.03         18.42  ...                       11.786613       1
..           ...           ...  ...                             ...     ...
564        21.75         20.99  ...                        4.772326       0
565        13.64         16.34  ...                        3.936061       1
566        10.08         15.11  ...                        4.323219       1
567        12.91         16.33  ...                        3.004630       1
568        11.60         18.36  ...                        2.385047       1

[569 rows x 36 columns]
>>> from atom.feature_engineering import FeatureGenerator
>>> from sklearn.datasets import load_breast_cancer

>>> X, y = load_breast_cancer(return_X_y=True, as_frame=True)

>>> fg = FeatureGenerator(strategy="dfs", n_features=5, verbose=2)
>>> X = fg.fit_transform(X, y)

Fitting FeatureGenerator...
Generating new features...
 --> 5 new features were added.

>>> # Note the radius error * worst smoothness column
>>> print(X)

     mean radius  ...  radius error * worst smoothness
0          17.99  ...                         0.177609
1          20.57  ...                         0.067285
2          19.69  ...                         0.107665
3          11.42  ...                         0.103977
4          20.29  ...                         0.104039
..           ...  ...                              ...
564        21.56  ...                         0.165816
565        20.13  ...                         0.089257
566        16.60  ...                         0.051984
567        20.60  ...                         0.119790
568         7.76  ...                         0.034698

[569 rows x 35 columns]


Methods

fitFit to data.
fit_transformFit to data, then transform it.
get_paramsGet parameters for this estimator.
inverse_transformDoes nothing.
logPrint message and save to log file.
saveSave the instance to a pickle file.
set_paramsSet the parameters of this estimator.
transformGenerate new features.


method fit(X, y=None)[source]
Fit to data.

ParametersX: dataframe-like
Feature set with shape=(n_samples, n_features).

y: int, str, sequence, dataframe-like or None, default=None
Target column corresponding to X.

  • If None: y is ignored.
  • If int: Position of the target column in X.
  • If str: Name of the target column in X.
  • If sequence: Target column with shape=(n_samples,) or sequence of column names or positions for multioutput tasks.
  • If dataframe-like: Target columns with shape=(n_samples, n_targets) for multioutput tasks.

Returnsself
Estimator instance.



method fit_transform(X=None, y=None, **fit_params)[source]
Fit to data, then transform it.

ParametersX: dataframe-like or None, default=None
Feature set with shape=(n_samples, n_features). If None, X is ignored.

y: int, str, sequence, dataframe-like or None, default=None
Target column corresponding to X.

  • If None: y is ignored.
  • If int: Position of the target column in X.
  • If str: Name of the target column in X.
  • If sequence: Target column with shape=(n_samples,) or sequence of column names or positions for multioutput tasks.
  • If dataframe-like: Target columns with shape=(n_samples, n_targets) for multioutput tasks.

**fit_params
Additional keyword arguments for the fit method.

Returnsdataframe
Transformed feature set. Only returned if provided.

series
Transformed target column. Only returned if provided.



method get_params(deep=True)[source]
Get parameters for this estimator.

Parametersdeep : bool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returnsparams : dict
Parameter names mapped to their values.



method inverse_transform(X=None, y=None)[source]
Does nothing.

ParametersX: dataframe-like or None, default=None
Feature set with shape=(n_samples, n_features). If None, X is ignored.

y: int, str, sequence, dataframe-like or None, default=None
Target column corresponding to X.

  • If None: y is ignored.
  • If int: Position of the target column in X.
  • If str: Name of the target column in X.
  • If sequence: Target column with shape=(n_samples,) or sequence of column names or positions for multioutput tasks.
  • If dataframe-like: Target columns with shape=(n_samples, n_targets) for multioutput tasks.

Returnsdataframe
Transformed feature set. Only returned if provided.

series
Transformed target column. Only returned if provided.



method log(msg, level=0, severity="info")[source]
Print message and save to log file.

Parametersmsg: int, float or str
Message to save to the logger and print to stdout.

level: int, default=0
Minimum verbosity level to print the message.

severity: str, default="info"
Severity level of the message. Choose from: debug, info, warning, error, critical.



method save(filename="auto", save_data=True)[source]
Save the instance to a pickle file.

Parametersfilename: str, default="auto"
Name of the file. Use "auto" for automatic naming.

save_data: bool, default=True
Whether to save the dataset with the instance. This parameter is ignored if the method is not called from atom. If False, add the data to the load method.



method set_params(**params)[source]
Set the parameters of this estimator.

Parameters**params : dict
Estimator parameters.

Returnsself : estimator instance
Estimator instance.



method transform(X, y=None)[source]
Generate new features.

ParametersX: dataframe-like
Feature set with shape=(n_samples, n_features).

y: int, str, sequence, dataframe-like or None, default=None
Does nothing. Implemented for continuity of the API.

Returnsdataframe
Transformed feature set.