Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculate the mean of values in a loop

I want to calculate the average of the different train scores and Test scores at the end of this for loop. How can I do that?

from sklearn.model_selection import train_test_split

#do five iterations
for i in range(5):

    X_train, X_test, y_train, y_test = train_test_split(X_normalized, y_for_normalized, test_size=0.1)    
    clf=new_model.fit(X_train, y_train)
    print ("Train score:", clf.score(X_train, y_train)) # from documentation .score returns r^2
    print ("Test score:", clf.score(X_test, y_test))   # from documentation .score returns r^2
    
print ("The Mean for Train scores is:",(np.mean(clf.score(X_train, y_train))))
    
print ("The Mean for Test scores is:",(np.mean(clf.score(X_test, y_test))))

    


Train score: 0.9181289941457126
Test score: -0.09888229299588057
Train score: 0.8990976131879111
Test score: 0.1907090731116397
Train score: 0.9251838290754301
Test score: 0.7965430972258479
Train score: 0.8904928040118292
Test score: 0.8192181875721168
Train score: 0.9234597364618801
Test score: 0.9729625064193129
The Mean for Train scores is: 0.9234597364618801
The Mean for Test scores is: 0.9729625064193129

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use a list to capture the scores of each iteration and then calculate the average over that list:

tr_scores = []
test_scores = []

for i in range(5):

    X_train, X_test, y_train, y_test = train_test_split(X_normalized, y_for_normalized, test_size=0.1)
    clf=new_model.fit(X_train, y_train)
    
    tr_sc = clf.score(X_train, y_train)
    ts_sc = clf.score(X_test, y_test)
    print ("Train score:", tr_sc) # from documentation .score returns r^2
    print ("Test score:", ts_sc)   # from documentation .score returns r^2
    tr_scores.append(tr_sc)
    test_scores.append(ts_sc)
    
print ("The Mean for Train scores is:",(np.mean(tr_scores)))
    
print ("The Mean for Test scores is:",(np.mean(test_scores)))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading