python - pandas dataframe rows scaling with sklearn
问题描述
How can I apply a sklearn scaler to all rows of a pandas dataframe. The question is related to pandas dataframe columns scaling with sklearn. How can I apply a sklearn scaler to all values of a row?
NOTE: I know that for feature scaling it's normal to have features in columns and scaling features column wise like in the refenced other question. However I'd like to use sklearn scalers for preprocessing data for visualization where it's reasonable to scale row wise in my case.
解决方案
Sklearn 适用于 panda 数据帧和 numpy 数组,当数据帧不支持时,numpy 数组允许进行一些基本的矩阵转换。
您可以将数据框转换为 numpy 数组,vectors = df.values
. 然后转置数组,按列缩放转置后的数组,将其转回
scaled_rows = scaler.fit_transform(vectors.T).T
并将其转换为数据框scaled_df = pd.DataFrame(data = scaled_rows, columns = df.columns)
推荐阅读
- numpy - InterpolatedUnivariateSpline 和 ax.fill_between 产生低 Y 值的意外结果(填充错误区域)
- mysql - 对于许多复选框来说,是多列还是单列位字符串更好
- python - 如何在python中的大文件中搜索二元组时降低时间复杂度?
- scala - 同构类型的 Cats 类型类
- django - 如何获取下载pdf文件的链接?(姜戈)
- ios - 如何解决致命错误:Swift IOS 中的索引超出范围
- r - 将多个 ggplot2 图形与一个共同的 x 轴和不同的 y 轴对齐,每个轴都有不同的 y 轴标签
- c# - DataGridView 添加行在底部显示打乱的行
- html - 除了标签如何使网格居中?
- swift - 如何在监控原因(地理围栏)时关闭 gps 时出现错误或警告?