2017-04-05 56 views
0

R平方估計的這個函數的誤差是多少?Rsquared的估計

def R2(X, Y, model): 
    Y_mean = np.mean(Y, axis=0) 
    pred = model.predict(X) 
    res = np.sum(np.square(Y - pred)) 
    tot = np.sum(np.square(Y - Y_mean)) 
    r2 = 1 - res/tot 

    return r2 

回答

1

好消息是,您計算確定係數R2的函數是正確的。您可以通過計算R2來計算Y,並按照預期計算出1.0。

問題是Y和pred的形狀不一樣。 如果您重塑預測以匹配Y的形狀,則數學表現如預期。

def R2(X, Y, model): 
    Y_mean = np.mean(Y, axis=0) 
    pred = model.predict(X) 
    print Y.shape, pred.shape 
    pred = pred.reshape(Y.shape[0]) 
    print Y.shape, pred.shape 
    res = np.sum(np.square(Y - pred)) 
    tot = np.sum(np.square(Y - Y_mean)) 
    r2 = 1 - res/tot 

    return r2 
+0

非常感謝。我的錯。我修改了問題和標籤,以便它們可以被具有相同問題的人發現和使用 – volatile