Python for loop within function not returning values

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Python for loop within function not returning values



I wrote this fairly simple Python function but for some reason after the for loop ends, nothing returns or is able to be printed out within the function. I can call the function just fine and I called prints within the for loop to ensure the values were correct and they were. Am I missing anything obvious here? The print statement at the bottom prints nothing.


def evaluate_arima_model(X, arima_order, s_arima_order):
scores =
train_steps = [36, 48, 60, 72, 84]
for i in train_steps:
Train = X[0:i]
Test = X[i:i + 12]
model = SARIMAX(Train, order=arima_order, seasonal_order=s_arima_order)
model_fit = model.fit(trend='nc', disp=0)
yhat = model_fit.forecast(12)
rmse = sqrt(mean_squared_error(numpy.exp(Test), numpy.exp(yhat)))
scores.append(rmse)
print(scores)
return scores



This is how the function is called (by another function with nested loops


def evaluate_models(dataset, p_values, d_values, q_values, sp_values, sd_values, sq_values, s_values):
dataset = dataset.astype('float32')
best_score, best_cfg, best_cfg2 = float("inf"), None, None
for p in p_values:
for d in d_values:
for q in q_values:
order = (p,d,q)
for sp in sp_values:
for sd in sd_values:
for sq in sq_values:
for s in s_values:
sorder = (sp,sd,sq,s)
try:
rmse = evaluate_arima_model(dataset, order, sorder)
if rmse < best_score:
best_score, best_cfg, best_cfg2 = rmse, order, sorder
print('ARIMA%s SARIMA%s RMSE=%.3f' % (order,sorder,rmse))
except:
continue
print('n','Best ARIMA%s SARIMA%s RMSE=%.3f' % (best_cfg, best_cfg2, best_score))


series = read_csv('dataset.csv', header=None, index_col=0, parse_dates=True, squeeze=True)
series = numpy.log(series)

# Evaluate parameters
p_values = range(0, 2)
d_values = range(0, 2)
q_values = range(0, 2)

# Evaluate seasonal parameters
sp_values = range(0, 2)
sd_values = range(0, 2)
sq_values = range(0, 2)

#Set seasonality
s_values = [12]

#Call grid loop
evaluate_models(series, p_values, d_values, q_values, sp_values, sd_values, sq_values, s_values)



Output: Best ARIMANone SARIMANone RMSE=inf



New version still not working:


def evaluate_arima_model(X, arima_order, s_arima_order):
scores =
train_steps = [36, 48, 60, 72, 84]
for i in train_steps:
Train = X[0:i]
Test = X[i:i + 12]
model = SARIMAX(Train, order=arima_order, seasonal_order=s_arima_order)
model_fit = model.fit(trend='nc', disp=0)
yhat = model_fit.forecast(12)
rmse = None
rmse = sqrt(mean_squared_error(numpy.exp(Test), numpy.exp(yhat)))
scores.append(rmse)
print(scores)
print(scores)
return scores





Does it print nothing or ?
– AKX
Aug 6 at 14:30






printing is not returning. You mean you've assigned the result of your function and it's None ?
– Jean-François Fabre
Aug 6 at 14:30


None





So, since we can't reproduce it. What is shown with the different prints you did? i.e. this "I can call the function just fine and I called prints within the for loop to ensure the values were correct and they were."
– Mathieu
Aug 6 at 14:31






nothing prints out at all
– bhat557
Aug 6 at 14:31





Adding a return still doesn't make it return anything sadly. I would expect that print to run even with no return though, yes?
– bhat557
Aug 6 at 14:41




3 Answers
3



Your print() statement must print something. However, because you do not have a return statement, your function does not return anything (well, it returns None). If you want your function to return something, add one last line:


print()


return


None


return scores



Simplify code:


In [1]: def evaluate_arima_model(X, arima_order, s_arima_order):
...: scores =
...: train_steps = [36, 48, 60, 72, 84]
...: for i in train_steps:
...: rmse = None
...: scores.append(rmse)
...: print(scores)
...: return scores
...:
...:

In [2]: evaluate_arima_model(1,1,1)
[None, None, None, None, None]
Out[2]: [None, None, None, None, None]



I do not see a reason for this to not work.





it should at least print the scores, even if there is no return statement, because of print(scores)
– Waleed Iqbal
Aug 6 at 14:35


print(scores)





@WaleedIqbal Yes, it should. And I do not see why it wouldn't print. It has to!
– AGN Gazer
Aug 6 at 14:39





Yeah that's what I'm not getting. I expect that print to run. Adding the return doesn't help anyway
– bhat557
Aug 6 at 14:42





@bhat557 See my edit. There is no way it does not work. Most likely the code that you posted is not the same as the code that you are running.
– AGN Gazer
Aug 6 at 14:46





@bhat557 I have no idea why it was not working before, but I strongly suspect that the example that you posted is not the same as your actual code because there is no logical reason for your example not to work.
– AGN Gazer
Aug 6 at 15:06



You need to write the return statement instead of print.


def evaluate_arima_model(X, arima_order, s_arima_order):
scores =
train_steps = [36, 48, 60, 72, 84]
for i in train_steps:
Train = X[0:i]
Test = X[i:i + 12]
model = SARIMAX(Train, order=arima_order, seasonal_order=s_arima_order)
model_fit = model.fit(trend='nc', disp=0)
yhat = model_fit.forecast(12)
rmse = sqrt(mean_squared_error(numpy.exp(Test), numpy.exp(yhat)))
scores.append(rmse)
return(scores)





there is no println in python.
– Waleed Iqbal
Aug 6 at 14:38


println





@WaleedIqbal edited actually I use more of Scala syntax so sorry for the typo.
– Raman Mishra
Aug 6 at 14:39



Finally sorted this out, the issue was returning a list vs a scalar, which is what I needed. So "return scores[0]" fixed it.


def evaluate_arima_model(X, arima_order, s_arima_order):
scores =
train_steps = [36]
for i in train_steps:
Train = X[0:i]
Test = X[i:i + 12]
model = SARIMAX(Train, order=arima_order, seasonal_order=s_arima_order)
model_fit = model.fit(trend='nc', disp=0)
yhat = model_fit.forecast(12)
rmse = sqrt(mean_squared_error(numpy.exp(Test), numpy.exp(yhat)))
scores.append(rmse)
return scores[0]






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard