standardize truncates fractional values for integer arrays

Author: Afloat16Created Sep 29, 2026Updated Sep 29, 2026

Problem and proposed change

standardize aliases the input array. Assigning standardized floating values back into an integer array truncates them and also changes caller-owned input. Allocate a floating copy before writing the standardized columns.

Reproduction

python
import numpy as np
from mlfromscratch.utils.data_manipulation import standardize
x = np.array([[1, 2], [2, 4], [3, 6]])
print(standardize(x))
# Expected each column: [-1.22474487, 0, 1.22474487]; input x should be unchanged.

Validation

Source: mlfromscratch/utils/data_manipulation.py, Git blob c733567033446e2c69a1fa4ec6df94991b09ffd3; rechecked against current upstream. Focused before/after tests were rerun for this submission.

Complete standalone source module loaded with real dependencies; selected utility behaviors tested. Not the full project suite.

Before: 3 failed, 3 passed in 0.08s

After: 6 passed in 0.09s

Candidate patch

diff
--- a/mlfromscratch/utils/data_manipulation.py
+++ b/mlfromscratch/utils/data_manipulation.py
@@ -91,7 +91,7 @@
 
 def standardize(X):
     """ Standardize the dataset X """
-    X_std = X
+    X_std = X.astype(float, copy=True)
     mean = X.mean(axis=0)
     std = X.std(axis=0)
     for col in range(np.shape(X)[1]):

Source: eriklindernoren/ML-From-Scratch