如何修正IndexError:无效索引到标量变量?

时间:2022-02-26 22:30:14

This code generates error:

这段代码生成错误:

IndexError: invalid index to scalar variable.

at the line: results.append(RMSPE(np.expm1(y_train[testcv]), [y[1] for y in y_test]))

在行:result .追加(RMSPE(np.expm1(y_train[testcv]), [y[1] for y in y_test]))

How to fix it?

如何修复它吗?

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 is:

testcv是:

[False False False ...,  True  True  True]

1 个解决方案

#1


6  

You are trying to index into a scalar (non-iterable) value:

您正在尝试索引一个标量(不可迭代的)值:

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

When you call [y for y in test] you are iterating over the values already, so you get a single value in y.

当你在测试中调用y (y)时,你已经在遍历这些值,因此在y中得到一个单值。

Your code is the same as trying to do the following:

您的代码与尝试执行以下操作相同:

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

I'm not sure what you're trying to get into your results array, but you need to get rid of [y[1] for y in y_test].

我不确定你想要得到的结果数组是什么,但是你需要在y_test中去掉[y[1] = y]。

If you want to append each y in y_test to results, you'll need to expand your list comprehension out further to something like this:

如果您想要将y_test中的每个y附加到结果中,您需要将您的列表理解扩展到以下内容:

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

Or just use a for loop:

或者只使用for循环:

for y in y_test:
    results.append(..., y)

#1


6  

You are trying to index into a scalar (non-iterable) value:

您正在尝试索引一个标量(不可迭代的)值:

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

When you call [y for y in test] you are iterating over the values already, so you get a single value in y.

当你在测试中调用y (y)时,你已经在遍历这些值,因此在y中得到一个单值。

Your code is the same as trying to do the following:

您的代码与尝试执行以下操作相同:

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

I'm not sure what you're trying to get into your results array, but you need to get rid of [y[1] for y in y_test].

我不确定你想要得到的结果数组是什么,但是你需要在y_test中去掉[y[1] = y]。

If you want to append each y in y_test to results, you'll need to expand your list comprehension out further to something like this:

如果您想要将y_test中的每个y附加到结果中,您需要将您的列表理解扩展到以下内容:

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

Or just use a for loop:

或者只使用for循环:

for y in y_test:
    results.append(..., y)