Calculate the mean of values in a loop

Question:

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
Asked By: Z47

||

Answers:

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)))
Answered By: Ignatius Reilly

Initialize a list by doing something like

mylist = []
avgvar = 0
for i in range(5):
    avgvar +=1
    mylist.append(avgvar)

average = sum(mylist)/len(mylist)
Answered By: Dragnipur
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.