【问题标题】:Python Numpy mean of square calculation (is this the right way)Python Numpy平方均值计算(这是正确的方法)
【发布时间】:2017-09-28 01:02:05
【问题描述】:

在 python 和 numpy 上不是很好,但在处理机器学习代码的均方误差。

想做一个python子程序,根据test_data返回均方误差,结果如下。

现在答案很好(不要引用,因为 x 和 y 已经是矩阵)

    for (x, y) in test_data:
        predictedMatrix = self.feedforward(x)
        actualMatrix    = y

    results = ((predictedMatrix - actualMatrix) ** 2).mean()

    return(results)

*原来的问题是*

我的代码看起来不像 python 代码,实际上整个练习看起来不像其他人在机器学习上的 numpy 代码。它有效,但没有好处。感谢您提供的建议。

import numpy as np
test_data  =      [(np.array([[ 0.        ], [ 0.        ]]), np.array([[ 0.],[ 1.]])),  # this is index not actually 1
                   (np.array([[ 0.        ], [ 1.        ]]), np.array([[ 1.],[ 0.]])),
                   (np.array([[ 1.        ], [ 0.        ]]), np.array([[ 1.],[ 0.]])),
                   (np.array([[ 1.        ], [ 1.        ]]), np.array([[ 0.],[ 1.]]))]

#import numpy as np

training_data = [ (np.array([[ 0.        ], [ 0.        ]]), np.array([[ 0.],[ 1.]])),
                  (np.array([[ 0.        ], [ 1.        ]]), np.array([[ 1.],[ 0.]])),
                  (np.array([[ 1.        ], [ 0.        ]]), np.array([[ 1.],[ 0.]])),
                  (np.array([[ 1.        ], [ 1.        ]]), np.array([[ 0.],[ 1.]]))]


#import numpy as np
validation_data = [(np.array([[ 0.        ],[ 0.        ]]), np.array([[ 0.],[ 1.]])), # this is index not actually 1
                   (np.array([[ 0.        ],[ 1.        ]]), np.array([[ 1.],[ 0.]])),
                   (np.array([[ 1.        ],[ 0.        ]]), np.array([[ 1.],[ 0.]])),
                   (np.array([[ 1.        ],[ 1.        ]]), np.array([[ 0.],[ 1.]]))]

# We should do self.feedforward(x) but for here just

self_forward_x = np.array([[ 0.],[ 1.]])

test_results = [self_forward_x - y
                for (x, y) in test_data]

print "test_results : {0}".format(test_results)

#test_results : [array([[ 0.],[ 0.]]),
#               array([[-1.],[ 1.]]),
#               array([[-1.],[ 1.]]),
#               array([[ 0.],[ 0.]])]

# how to do sum of mean square error to check the progress of the epochs

# i.e. how to get mse which I think is
# (0**2 + 0**2)/2 + (-1**2 + 1**2)/2 + (-1**2 + 1**2)/2 + (0**2 + 0**2)/2 not / 4 as we have 4 cases ? should I divided by 4 ... confused.

sumarray = 0
i = 0

for arrays in test_results:
    for arrayi in arrays:
        #print "arrayi : {0}".format(arrayi)
        #print "sum(arrayi) : {0}".format(sum(arrayi))
        sumarray = sumarray + np.sum(arrayi**2)
        i = i + 1

return (sumarray / i)
# print "i, sumarray : {0}, {1}".format(i, sumarray)

【问题讨论】:

    标签: python numpy neural-network mse


    【解决方案1】:

    在 numpy 中计算 MSE 非常简单:

    mse = ((predictedMatrix - actualMatrix) ** 2).mean(axis=_axis)
    

    _axis = 0 => 逐行计算得到一个向量。

    _axis = 1 => 逐列计算得到一个向量。

    _axis = None => 逐元素计算得到一个数字。

    【讨论】:

    • 非常感谢。仍然在循环中挣扎,但已排序。你的答案是上帝派来的。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 2017-01-12
    • 2018-03-10
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    相关资源
    最近更新 更多