I suppose when you said df_scaled is empty, you actually meant all values inside is 0 (not df_scaled = None, which does not make sense).
You can try this:
from sklearn.preprocessing import StandardScaler
std_scaler = StandardScaler()
new = train_data[['Fare']]
df_scaled = std_scaler.fit_transform(new)
df_scaled
There is a difference between train_data[['Fare']]
and train_data['Fare'].values.reshape(1,-1)
.
Let consider n = len(train_data[['Fare']])
, then:
train_data[['Fare']].shape = (n, 1)
train_data['Fare'].values.reshape(1,-1).shape = (1, n)
If you apply scaler to shape (1, n), if will fit transform each value separately, instead of the whole list, which will result in value of 0 as nature of standardization.
If you look at the documentation of the function, it requires input to be an ndarray of shape (n_features,)
. In your case n_features = 1
, so input should have shape (1,) which is equivalent to (n,1)
.
Reference: https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html
Hope that help.