. Advertisement .
..3..
. Advertisement .
..4..
I’m trying to run a new project. I do a couple of things like this:
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]))
[False False False ..., True True True]
but in my program, I am getting the warning :
IndexError: invalid index to scalar variable.
Can someone explain to me why the issue of the “ indexerror: invalid index to scalar variable.” happened? Where have I gone wrong? Thank you!
The cause: You have an iteration, then for each element of that loop which could be a scalar, has no index. If each element is an empty array, single variable, or scalar and is not a list or array you can’t use indices.
Solution:
You want to index the value into a non-iterable scalar.
You call
[y for y in test]
to iterate over the values, so you only get one value iny
. Your code will work the same way as if you tried to do the following:Although I don’t know what you are trying to do with your results array, you should get rid
[y[1] for y in y_test]
. To append each y to y_test, you will need to increase your list comprehension to something like the following:You can also use a for loop
YOLO Object Detection
layer_names = net.getLayerNames() output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
You don’t have to index i in layer_names[i[0]- 1] . It’s easy to remove it and do layer_names[i-1]
layer_names = net.getLayerNames() output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
It Works For Me