FILE: docs/api/numpy.md¶
NumPy API Reference¶
Functions for array operations, normalization, safe division, and reproducibility.
normalize(arr, method='minmax', axis=0, feature_range=(0, 1), clip=False)¶
Normalize or standardize an array.
Parameters:
- arr – numpy array or pandas Series
- method – 'minmax', 'zscore', or 'robust'
- axis – 0 for columns, 1 for rows (2D arrays)
- feature_range – output range for minmax method
- clip – clip output to feature_range (minmax only)
Returns: Normalized numpy array
Example:
norm_arr = dk.normalize(arr)
norm_arr = dk.normalize(arr, method='zscore')
norm_arr = dk.normalize(arr, method='minmax', feature_range=(-1, 1))
safe_divide(numerator, denominator, fill=0.0, nan_safe=True)¶
Divide arrays safely, handling zeros and NaNs.
Parameters:
- numerator – array-like or scalar
- denominator – array-like or scalar
- fill – value to use where denominator is 0 or NaN
- nan_safe – treat NaN denominators as 0 → fill
Returns: numpy array of results
Example:
result = dk.safe_divide(prices, quantities, fill=0.0)
result = dk.safe_divide(a, b, fill=np.nan, nan_safe=False)
set_seed(n, torch=True, tf=True)¶
Set all random seeds for reproducibility.
Parameters:
- n – integer seed value
- torch – seed PyTorch if available
- tf – seed TensorFlow if available
Returns: None (prints confirmation)
Example:
dk.set_seed(42)
dk.set_seed(123, torch=False, tf=False)
assert_shape(arr, expected=None, fix=None, name='array')¶
Check array shape with optional auto‑fix.
Parameters:
- arr – numpy array
- expected – expected shape tuple (or None)
- fix – 'flatten', 'squeeze', 'expand', 'reshape_col', 'reshape_row'
- name – name for error messages
Returns: Array (possibly reshaped) or raises ValueError
Example:
arr = dk.assert_shape(arr, expected=(100, 1), fix='expand')
dk.assert_shape(arr, fix='flatten')
print(dk.assert_shape(arr)) # prints shape