Calculate the mean of values in a loop

Z47

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
Ignatius Reilly

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)))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calculate mean of calculated values

From Dev

Calculate mean difference values

From Dev

Calculate the mean values if the numbers are same

From Dev

for loop in R to calculate mean from list

From Dev

for loop to calculate mean by group (also ignore NA)

From Dev

Pandas: calculate mean by specific value in column with a for loop

From Dev

Using a for loop to calculate the mean of a list of tuples in Python

From Dev

Running a for loop in r over columns to calculate mean

From Dev

R Loop to calculate mean for each of two variables

From Dev

php calculate values from a loop

From Dev

How to calculate mean of every three values of a list

From Dev

Calculate mean for each row containing lists of values

From Dev

Calculate mean of HashMap values after insertion

From Dev

Calculate mean change in values one day later

From Dev

Calculate mean cell values over different files

From Dev

Calculate max, min and mean values of element in an array

From Dev

Calculate mean on values in python collections.Counter

From Dev

Calculate mean with a filter on a column's values

From Dev

Calculate mean for only three values per row

From Dev

Pythonic way to calculate the mean and variance of values in Counters

From Dev

Sum tuples values to calculate mean - RDD

From Dev

JavaScript; calculate a mean, excluding certain values

From Dev

How to calculate mean of values per unique class

From Dev

R calculate how many values used to calculate mean in aggregate function

From Dev

Loop over a sequence of values and return the mean

From Dev

Integrated for loop to calculate values over a grid/mesh

From Dev

PHP - How to calculate the values ​of the second foreach loop?

From Python

Calculate mean of df, BUT if =>1 of the values differs >20% from this mean, the mean is set to NaN

From Dev

Calculate mean from subset of rows in pandas data frame: groupby or for loop?

Related Related

  1. 1

    Calculate mean of calculated values

  2. 2

    Calculate mean difference values

  3. 3

    Calculate the mean values if the numbers are same

  4. 4

    for loop in R to calculate mean from list

  5. 5

    for loop to calculate mean by group (also ignore NA)

  6. 6

    Pandas: calculate mean by specific value in column with a for loop

  7. 7

    Using a for loop to calculate the mean of a list of tuples in Python

  8. 8

    Running a for loop in r over columns to calculate mean

  9. 9

    R Loop to calculate mean for each of two variables

  10. 10

    php calculate values from a loop

  11. 11

    How to calculate mean of every three values of a list

  12. 12

    Calculate mean for each row containing lists of values

  13. 13

    Calculate mean of HashMap values after insertion

  14. 14

    Calculate mean change in values one day later

  15. 15

    Calculate mean cell values over different files

  16. 16

    Calculate max, min and mean values of element in an array

  17. 17

    Calculate mean on values in python collections.Counter

  18. 18

    Calculate mean with a filter on a column's values

  19. 19

    Calculate mean for only three values per row

  20. 20

    Pythonic way to calculate the mean and variance of values in Counters

  21. 21

    Sum tuples values to calculate mean - RDD

  22. 22

    JavaScript; calculate a mean, excluding certain values

  23. 23

    How to calculate mean of values per unique class

  24. 24

    R calculate how many values used to calculate mean in aggregate function

  25. 25

    Loop over a sequence of values and return the mean

  26. 26

    Integrated for loop to calculate values over a grid/mesh

  27. 27

    PHP - How to calculate the values ​of the second foreach loop?

  28. 28

    Calculate mean of df, BUT if =>1 of the values differs >20% from this mean, the mean is set to NaN

  29. 29

    Calculate mean from subset of rows in pandas data frame: groupby or for loop?

HotTag

Archive