Python Forum
Simple Variable Saving in Loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Variable Saving in Loop
#1
Hello Python community,
I am relatively new the language, and have most of my experience in MATLAB.
I've tried to already search for the answer to my question, however unsuccessfully. Perhaps I am missing the key words...

In any case, I am sure someone could help me rather easily, as I currently feel so: Wall

I have a loop where I set the desired variables first:
feature_ics = {'All': features_all,
            'Brain': features_brain,
            'Artifact': features_artifact}

class_acc_mean = {'All': [], 'Brain': [], 'Artifact': []}
class_acc_std = {'All': [], 'Brain': [], 'Artifact': []}
Then, at the end of my code, I try to get all of the data I want [the data from 5 iterations of different variables 'j']
            logreg_acc_mean[sub_idx] = acc_test_mean
            logreg_acc_std[sub_idx] = acc_test_std
            #print("    Accuracy Subject %i: %.2f %%" %(sub_idx, acc_test_mean*100))


        # Add results to dictionary
        print(logreg_acc_mean)
        print(name)
        print(j)
        class_acc_mean[name,j]= logreg_acc_mean
        class_acc_std[name,j] = logreg_acc_std
An example output looks like this (Exactly what I want to save!):
Output:
------------------------------------------------------------------ Classification Features All ------------------------------------------------------------------ [0.46107143 0.26333333 0.74691176 0.64466667 0.50747253 0.47942857 0.612 0.74142857 0.34580952 0.58695238 0.281 0.46009524 0.29142857] All 0 ------------------------------------------------------------------ [0.45535714 0.22166667 0.59544118 0.60733333 0.32538462 0.50314286 0.48980952 0.64371429 0.40704762 0.60057143 0.183 0.31057143 0.24857143] All 1 ------------------------------------------------------------------ [0.25535714 0.29166667 0.37654412 0.48761905 0.30659341 0.35609524 0.28895238 0.45428571 0.31371429 0.35257143 0.18 0.20666667 0.31428571] All 2 ------------------------------------------------------------------ [0.34464286 0.19666667 0.47654412 0.49152381 0.32098901 0.5107619 0.39695238 0.35152381 0.44238095 0.37857143 0.424 0.3787619 0.39428571] All 3 ------------------------------------------------------------------ [0.47178571 0.225 0.49176471 0.46619048 0.43989011 0.514 0.53657143 0.35304762 0.45904762 0.40542857 0.346 0.3347619 0.33142857] All 4 . . .
Yet, when I then call my variable 'class_acc_mean', I get:
Output:
. . . ('All', 0): array([0.47285714, 0.21166667, 0.50441176, 0.45733333, 0.44494505, 0.52638095, 0.51742857, 0.37219048, 0.45666667, 0.40466667, 0.362 , 0.31085714, 0.32571429]), ('All', 1): array([0.47285714, 0.21166667, 0.50441176, 0.45733333, 0.44494505, 0.52638095, 0.51742857, 0.37219048, 0.45666667, 0.40466667, 0.362 , 0.31085714, 0.32571429]), ('All', 2): array([0.47285714, 0.21166667, 0.50441176, 0.45733333, 0.44494505, 0.52638095, 0.51742857, 0.37219048, 0.45666667, 0.40466667, 0.362 , 0.31085714, 0.32571429]), ('All', 3): array([0.47285714, 0.21166667, 0.50441176, 0.45733333, 0.44494505, 0.52638095, 0.51742857, 0.37219048, 0.45666667, 0.40466667, 0.362 , 0.31085714, 0.32571429]), ('All', 4): array([0.47285714, 0.21166667, 0.50441176, 0.45733333, 0.44494505, 0.52638095, 0.51742857, 0.37219048, 0.45666667, 0.40466667, 0.362 , 0.31085714, 0.32571429]), . . .
Why does 'j' seem to update in my code, but then not save in the variable? How can I save the relevant data for each loop, as in the output during the code run itself? This is seemingly so simple but it's been driving me a bit mad.

Thank you!
Reply
#2
You don't show, but I guess that your loop is for j in...?
Perhaps it would be easier for yourself to keep track of things by using the [inline]dictionary.update()[/inline] method rather than "="?  

[python]
your_dictionary.update({ new_key : new_value})
Reply
#3
Hi Michael,

Thanks for the reply.

I suppose what I am trying to figure out is actually just this simple question:
1. I have three larger conditions [all, brain, artifact]. I want to create a variable to save classification accuracies for each of these.
2. I have five frequency bands "j" in which I've preprocessed my data [0,1,2,3,4].
3. I also have many subjects [n...nx].
4. I want to loop through the subjects for each band, for each condition and basically save a variable at the end which gives me back [conditions x freq][subjects]. At the moment, I have no problem when using a single freq band to get [conditions][subjects], but I can't figure out how to inlay the frequency bands to expand the variable in Python. This must of course be easy! I'm just a bit rusty Shifty
Reply
#4
Hi,

Well first of all it would be nice, if you could share the logic where you loop over your dictionarys/arrays.

Hard to figure out what the problem is w/o the actual code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 430 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,679 Nov-07-2023, 09:49 AM
Last Post: buran
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,566 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  loop (create variable where name is dependent on another variable) brianhclo 1 1,133 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Multiple Loop Statements in a Variable Dexty 1 1,198 May-23-2022, 08:53 AM
Last Post: bowlofred
Big Grin Variable flag vs code outside of for loop?(Disregard) cubangt 2 1,166 Mar-16-2022, 08:54 PM
Last Post: cubangt
  How to save specific variable in for loop in to the database? ilknurg 1 1,142 Mar-09-2022, 10:32 PM
Last Post: cubangt
  How to add for loop values in variable paulo79 1 1,439 Mar-09-2022, 07:20 PM
Last Post: deanhystad
  Using Excel Cell As A Variable In A Loop knight2000 7 4,087 Aug-25-2021, 12:43 PM
Last Post: snippsat
  saving each iteration of a loop sgcgrif3 3 6,708 Jul-27-2021, 01:02 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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