Features.transform

Features.transform(function, features=None, *, useLog=None)

Modify this object by applying a function to each feature.

Perform an inplace modification of the data in this object through the application of the provided function to the features or subset of features in this object.

Parameters:
  • function – Must accept the view of a feature as an argument.

  • features (identifier, list of identifiers) – May be a single feature name or index, an iterable, container of feature names and/or indices. None indicates application to all features.

  • useLog (bool, None) – Local control for whether to send object creation to the logger. If None (default), use the value as specified in the “logger” “enabledByDefault” configuration option. If True, send to the logger regardless of the global option. If False, do NOT send to the logger, regardless of the global option.

Examples

Transform all features; apply to all points.

>>> X = nimble.ones(3, 5)
>>> X.features.transform(lambda ft: ft + 2)
>>> X
<Matrix 3pt x 5ft
     0  1  2  3  4
   ┌──────────────
 0 │ 3  3  3  3  3
 1 │ 3  3  3  3  3
 2 │ 3  3  3  3  3
>

Transform all features; apply to certain points. Note that the function recieves a read-only view of each feature, so we need to make a copy in order to modify any specific data.

>>> def transformMiddlePoint(ft):
...     ftList = ft.copy(to='python list', outputAs1D=True)
...     ftList[1] += 4
...     return ftList
>>> X = nimble.ones(3, 5)
>>> X.features.transform(transformMiddlePoint)
>>> X
<Matrix 3pt x 5ft
     0  1  2  3  4
   ┌──────────────
 0 │ 1  1  1  1  1
 1 │ 5  5  5  5  5
 2 │ 1  1  1  1  1
>

Transform a subset of features.

>>> X = nimble.ones(3, 5)
>>> X.features.transform(lambda ft: ft + 6, features=[1, 3])
>>> X
<Matrix 3pt x 5ft
     0  1  2  3  4
   ┌──────────────
 0 │ 1  7  1  7  1
 1 │ 1  7  1  7  1
 2 │ 1  7  1  7  1
>

Keywords: apply, modify, alter, change, map, compute