Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print
#1
Hello, I'm trying to find a way to excute the command print below to get the same load the prediction. But it doesn't load it, it just print the print.
n=2005;
while(n!=2008) :
    prog='print("predictions_",n,","," parameters_",n," ","= get_predictions_each_year",(n,12), sep="")';
    exec(prog)
    n=n+1;
Output:
predictions_2005, parameters_2005 = get_predictions_each_year(2005, 12) predictions_2006, parameters_2006 = get_predictions_each_year(2006, 12) predictions_2007, parameters_2007 = get_predictions_each_year(2007, 12)
# Get Predictions alongside information on SARIMA paramters used for 2005
predictions_2005, parameters_2005 = get_predictions_each_year(2005,12)
Output:
100% 14/14 [00:06<00:00, 2.10it/s]
Could you help me with this ?
Thank you
Reply
#2
what are you doing here?
If I understand your goal correctly:
for year in range(2005, 2008):
    print(f'predictions_{year}, parameters_{year}, {get_predictions(year, 12)}')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Output:
File "<ipython-input-83-b54c4c9c810b>", line 2 print(f'{predictions_{year}}, {parameters_{year}}, {get_predictions(year, 12)}' ^ SyntaxError: invalid syntax
Hello Thank you for your answer.I've got a error message.
I wanna write a loop basically for different year and execute this at the end of the loop :
predictions_2005, parameters_2005 = get_predictions_each_year(2005, 12)
So if i take more years for example from 2000 to 2008, I don't need to write it few times

Like this : https://cdn1.imggmi.com/uploads/2019/10/...3-full.png
Reply
#4
sorry, the extra curly braces were added by mistake - I already edited my answer when I saw them.
But anyway I misunderstood your goal ( I thought you want to print string), so just ignore it
It looks like you try to create variables dynamically, e.g. you want to assign the result of get_predictions(2005, 12) to predictions_2005 and parameters_2005
You should use use proper data structure instead
predictions = {}
parameters = {}

for year in range(2005, 2008):
    prediction, param = get_predictions(year, 12)
    predictions[year] = prediction
    parameters[year] = param
This way you will get a dict with predictions and dict with params.
predictions dict would look like {2005:<some value>, 2006:<some value>, 2007:<some value>}

Of course you can have single dict of dicts with both predictions and params in the neseted dicts
e.g. results dict would look like {2005:{'predictions':<some value>, 'params:<some value>}, 2006:{'predictions':<some value>, 'params:<some value>}, 2007:{'predictions':<some value>, 'params:<some value>}}
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thank you for your answer ! it's helpful but i still get this output :
TypeError Traceback (most recent call last)
<ipython-input-84-a6b18bca794a> in <module>()
3
4 for year in range(2005, 2008):
----> 5 predictions, param = get_predictions(year, 12)
6 predictions[year] = prediction
7 parameters[year] = param

TypeError: get_predictions() missing 2 required positional arguments: 'months_to_predict' and 'bu'
Reply
#6
get_predictions() is in fact your get_predictions_each_year()
Do you have another function called just get_predictions()?
can you show the signature for get_predictions_each_year()?

in your code you have 2 params that look like year and 12 (months?) so I have these 2 as you can see year and 12

Note that you have an extra s on line 5 you have predictions, it should be single - prediction otherwise you will override the dict
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Wonderful It's working i changed get_predictions() to get_predictions_each_year() Thanks a lot !!
Reply
#8
Note the last edit I made to my previous post
on line 5:
prediction, param = , not predictions, param =
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020