2014-02-21 157 views
6

我有問題試圖使用NumPy計算IPython中的均方根誤差。我敢肯定的功能是正確的,但是當我嘗試和輸入值,它給了我下面的類型錯誤消息:如何使用IPython/NumPy來計算RMSE?

TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple' 

這裏是我的代碼:

import numpy as np 

def rmse(predictions, targets): 
    return np.sqrt(((predictions - targets) ** 2).mean()) 

print rmse((2,2,3),(0,2,6)) 

顯然,什麼是錯的我投入。在我將其放入rmse():行之前,是否需要建立陣列?

+1

這是更好的solutionss:http://stackoverflow.com/questions/17197492/root-mean-square-error-in-python – mrgloom

+0

的可能的複製[在python中的均方根誤差](https://stackoverflow.com/questions/17197492/root-mean-square-error-in-python) – phunehehe

回答

6

它說減法沒有爲元組定義。

嘗試

print rmse(np.array([2,2,3]), np.array([0,2,6])) 

代替。

4

在RMSE功能,請嘗試:

return np.sqrt(np.mean((predictions-targets)**2)) 
相關問題