帮助编码函数接收矩阵输入,并从列中减去其平均值,然后将列除以标准差。 +您需要检查是否没有被零除。不能使用圆圈。
我接下来要编码:
def scale(X):
res = X - np.mean(X, axis = 0)
return res / np.std(res, axis = 0)[np.std(res, axis = 0) != 0]
但是,如果矩阵在一列中具有相同的值,则会生成错误,例如:
np.array([3, 6, 6, 8, 1, 3, 1, 3, 8, 4, 3, 6, 7, 7, 4]).reshape((3, 5))
error: operands could not be broadcast together with shapes (3.5) (4,)
告诉我们如何使用numpy而不是使用循环来解决此问题。