如何对从矩阵列中减去其平均值并除以标准差的列进行编码的函数?

时间:2020-04-15 08:52:58

标签: python

帮助编码函数接收矩阵输入,并从列中减去其平均值,然后将列除以标准差。 +您需要检查是否没有被零除。不能使用圆圈。

我接下来要编码:

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而不是使用循环来解决此问题。

0 个答案:

没有答案
相关问题