2015-10-06 416 views
3

此代碼生成錯誤:如何解決IndexError:無效的指數標量

IndexError: invalid index to scalar variable. 

在該行:results.append(RMSPE(np.expm1(y_train[testcv]), [y[1] for y in y_test]))

如何解決呢?

import pandas as pd 
import numpy as np 
from sklearn import ensemble 
from sklearn import cross_validation 

def ToWeight(y): 
    w = np.zeros(y.shape, dtype=float) 
    ind = y != 0 
    w[ind] = 1./(y[ind]**2) 
    return w 

def RMSPE(y, yhat): 
    w = ToWeight(y) 
    rmspe = np.sqrt(np.mean(w * (y - yhat)**2)) 
    return rmspe 

forest = ensemble.RandomForestRegressor(n_estimators=10, min_samples_split=2, n_jobs=-1) 

print ("Cross validations") 
cv = cross_validation.KFold(len(train), n_folds=5) 

results = [] 
for traincv, testcv in cv: 
    y_test = np.expm1(forest.fit(X_train[traincv], y_train[traincv]).predict(X_train[testcv])) 
    results.append(RMSPE(np.expm1(y_train[testcv]), [y[1] for y in y_test])) 

testcv是:

[False False False ..., True True True] 
+1

y [1]在最後一行的期望值是多少?您正在迭代y_test值,那麼示例值是什麼? – Monkpit

+0

http://stackoverflow.com/questions/13594507/what-does-it-mean-to-have-an-index-to-scalar-variable-error-python –

+0

@Monkey:y_test values:[6175.36384809 6267.20711569 5783.46657446。 ..,4323.34539658 4332.18318557 3481.93371173] –

回答

6

您試圖索引標量(非迭代)值:

[y[1] for y in y_test] 
#^this is the problem 

當你調用[y for y in test]你迭代值已經,所以你在y得到一個單一的值。

你的代碼是一樣的努力做到以下幾點:

y_test = [1, 2, 3] 
y = y_test[0] # y = 1 
print(y[0]) # this line will fail 

我不知道你想進入你的結果數組的東西,但你需要擺脫[y[1] for y in y_test]

如果你想每個Y追加在y_test到結果,你需要進行進一步的擴展您的列表中理解到這樣的事情:

[results.append(..., y) for y in y_test] 

或者只是使用一個循環:

for y in y_test: 
    results.append(..., y) 
相關問題