def impute_and_scale(df, scaling=None):
"""Impute missing values with mean and scale data included in pandas dataframe.
Parameters
----------
df : pandas dataframe
dataframe to impute and scale
scaling : 'maxabs' [-1,1], 'minmax' [0,1], 'std', or None, optional (default 'std')
type of scaling to apply
"""
df = df.dropna(axis=1, how='all')
imputer = Imputer(strategy='mean', axis=0)
mat = imputer.fit_transform(df)
# print(mat.shape)
if scaling is None:
return pd.DataFrame(mat, columns=df.columns)
# Scaling data
if scaling == 'maxabs':
# Normalizing -1 to 1
scaler = MaxAbsScaler()
elif scaling == 'minmax':
# Scaling to [0,1]
scaler = MinMaxScaler()
else:
# Standard normalization
scaler = StandardScaler()
mat = scaler.fit_transform(mat)
# print(mat.shape)
df = pd.DataFrame(mat, columns=df.columns)
return df
p1b3_locally_connected_v2.py 文件源码
python
阅读 24
收藏 0
点赞 0
评论 0
评论列表
文章目录