Features.calculate

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

Apply a calculation to each feature.

Return a new object that calculates the results of the given function on the specified features in this object, with output values collected into a new object that is returned upon completion.

Parameters:
  • function (function) – Accepts a view of a feature as an argument and returns the new values in that feature.

  • features (feature, list of features) – The subset of features to limit the calculation to. If None, the calculation will apply 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.

Returns:

nimble Base object

Examples

Apply calculation to all features; apply to all points.

>>> X = nimble.ones(3, 5)
>>> addTwo = X.features.calculate(lambda ft: ft + 2)
>>> addTwo
<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
>

Apply calculation to all features; function modifies a specific point. Note that the function recieves a read-only view of each feature, so a copy is necessary to modify any specific data.

>>> def changeMiddlePoint(ft):
...     ftList = ft.copy(to='python list', outputAs1D=True)
...     ftList[1] += 4
...     return ftList
>>> X = nimble.ones(3, 5)
>>> middleChange = X.features.calculate(changeMiddlePoint)
>>> middleChange
<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
>

Apply calculation to a subset of features.

>>> ftNames = ['f1', 'f2', 'f3']
>>> X = nimble.identity(3, featureNames=ftNames)
>>> calc = X.features.calculate(lambda ft: ft + 6,
...                             features=[2, 0])
>>> calc
<Matrix 3pt x 2ft
     f3  f1
   ┌───────
 0 │ 6   7
 1 │ 6   6
 2 │ 7   6
>

Keywords: apply, modify, alter, statistics, stats, compute