dataframe对某一列数据sklearn标准化

出错的代码:
import sklearn.preprocessing as preprocessing
scaler = preprocessing.StandardScaler()
age_scale_param = scaler.fit(df[‘Age’])
df[‘Age_scaled’] = scaler.fit_transform(df[‘Age’], age_scale_param)
fare_scale_param = scaler.fit(df[‘Fare’])
df[‘Fare_scaled’] = scaler.fit_transform(df[‘Fare’], fare_scale_param)
df
出错的原因:
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
改正好的代码:
import sklearn.preprocessing as preprocessing
scaler = preprocessing.StandardScaler()
age_scale_param = scaler.fit(df[‘Age’].values.reshape(-1, 1))
df[‘Age_scaled’] = scaler.fit_transform(df[‘Age’].values.reshape(-1, 1), age_scale_param)
fare_scale_param = scaler.fit(df[‘Fare’].values.reshape(-1, 1))
df[‘Fare_scaled’] = scaler.fit_transform(df[‘Fare’].values.reshape(-1, 1), fare_scale_param)
df

你可能感兴趣的:(数据挖掘)