数组的列式标准化(缩放)

阿诺德·克莱因

我想[0, 1]在python中标准化(放置在范围内)二维数组,但要针对特定​​的列。

例如,给定:

a = array([[1 2 3],[4,5,6],[7,8,9]])

我需要类似“ norm_column_wise(a,1)”的东西,它采用矩阵“ a”,并且仅对第二列[2,5,8]进行规范化,

结果应该是:

norm_column_wise(a,1) = array([[1,0,3],[4,0.5,6],[7,1.0,9]])

我编写了一个简单的标准化代码:

def norm_column_wise(arr): 
    return (arr-arr.min(0))/(arr.max(0)-arr.min(0))

但是它适用于数组的所有列。如何修改此简单代码以指定特定列?

提前致谢!

身体吸引力

我会用numpy的。

import numpy as np

def normalize_column(A, col):
    A[:,col] = (A[:,col] - np.min(A[:,col])) / (np.max(A[:,col]) - np.min(A[:,col]))

if __name__ == '__main__':
    A = np.matrix([[1,2,3], [4,5,6], [7,8,9]], dtype=float)
    normalize_column(A, 1)
    print (A)

结果

[[ 1.   0.   3. ]
 [ 4.   0.5  6. ]
 [ 7.   1.   9. ]]

按照上述说明,max-min可以替换为:

np.ptp(A,0)[0,col]

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章