【发布时间】:2018-05-03 16:14:14
【问题描述】:
这就是我缩放单个向量的方式:
vector = np.array([-4, -3, -2, -1, 0])
# pass the vector, current range of values, the desired range, and it returns the scaled vector
scaledVector = np.interp(vector, (vector.min(), vector.max()), (-1, +1)) # results in [-1. -0.5 0. 0.5 1. ]
如何将上述方法应用于给定matrix 的每一列?
matrix = np.array(
[[-4, -4, 0, 0, 0],
[-3, -3, 1, -15, 0],
[-2, -2, 8, -1, 0],
[-1, -1, 11, 12, 0],
[0, 0, 50, 69, 80]])
scaledMatrix = [insert code that scales each column of the matrix]
请注意,scaledMatrix 的前两列应等于第一个示例中的 scaledVector。对于上面的matrix,正确计算的scaledMatrix是:
[[-1. -1. -1. -0.64285714 -1. ]
[-0.5 -0.5 -0.96 -1. -1. ]
[ 0. 0. -0.68 -0.66666667 -1. ]
[ 0.5 0.5 -0.56 -0.35714286 -1. ]
[ 1. 1. 1. 1. 1. ]]
我目前的做法(错误):
np.interp(matrix, (np.min(matrix), np.max(matrix)), (-1, +1))
【问题讨论】:
-
看起来 scaledMatrix 的第 4 列有错误。 P. Camilleri 有正确的价值观。
-
你对常量列的期望是什么?
标签: python arrays numpy matrix