sklearn StandardScaler returns all zeros
When you're trying to apply fit_transform
method of StandardScaler
object to array of size (1, n) you obviously get all zeros, because for each number of array you subtract from it mean of this number, which equal to number and divide to std of this number. If you want to get correct scaling of your array, you should convert it to array with size (n, 1). You can do it this way:
import numpy as np
X = np.array([1, -4, 5, 6, -8, 5]) # here should be your X in np.array format
X_transformed = scaler.fit_transform(X[:, np.newaxis])
In this case you get Standard scaling for one object by its features, that's not you're looking for.
If you want to get scaling by one feature of 3 objects, you should pass to fit_transform
method array of size (3, 1) with values of certain feature corresponding to each object.
X = np.array([0.00000000e+00, 9.49627142e-04, 3.19029839e-04])
X_transformed = scaler.fit_transform(X[:, np.newaxis]) # you should get
# array([[-1.07174217], [1.33494964], [-0.26320747]]) you're looking for
And if you want to work with already fitted StandardScaler object, you shouldn't use fit_transform
method, beacuse it refit object with new data. StandardScaler
has transform
method, which work with single observation:
X = np.array([1, -4, 5, 6, -8, 5]) # here should be your X in np.array format
X_transformed = scaler.transform(X.reshape(1, -1))
I had the same problem. Another (simpler) solution to the problem of array with size (1, n) is to transpose the matrix and it will be size (n, 1).
X = np.array([0.00000000e+00, 9.49627142e-04, 3.19029839e-04])
X_transformed = scaler.transform(X.T)