How can I store many values in 1 variable, in python?

时间:2016-08-31 17:04:24

标签: python numpy scikit-learn

I am doing a 10 x 10 stratified shuffle split cross validation. As you can see in my code, at the end I get 10 results. I want the mean of these 10 results. So I added a variable: xSSSmean. But this one changes in every loop. So at the end it would have just stored the las value. So, how can I make it store the 10 values and just print me the mean of these?

############10x10 SSS##################################
from sklearn.cross_validation import StratifiedShuffleSplit
for i in range(10):
    sss = StratifiedShuffleSplit(y, 10, test_size=0.1, random_state=0)
    scoresSSS = cross_validation.cross_val_score(clf, x, y , cv=sss)
    print("Accuracy x fold SSS_RF: %0.2f (+/- %0.2f)" % (scoresSSS.mean(), scoresSSS.std()* 2))
    xSSSmean = "%0.2f" % scoresSSS.mean()

print (xSSSmean.mean)



Accuracy x fold SSS_RF: 0.95 (+/- 0.10)
Accuracy x fold SSS_RF: 0.93 (+/- 0.15)
Accuracy x fold SSS_RF: 0.96 (+/- 0.09)
Accuracy x fold SSS_RF: 0.93 (+/- 0.12)
Accuracy x fold SSS_RF: 0.94 (+/- 0.11)
Accuracy x fold SSS_RF: 0.94 (+/- 0.14)
Accuracy x fold SSS_RF: 0.93 (+/- 0.15)
Accuracy x fold SSS_RF: 0.94 (+/- 0.13)
Accuracy x fold SSS_RF: 0.94 (+/- 0.09)
Accuracy x fold SSS_RF: 0.95 (+/- 0.10)
0.95

1 个答案:

答案 0 :(得分:2)

xSSSmean = []   #I create an empty list
for i in range(10):
    sss = StratifiedShuffleSplit(y, 10, test_size=0.1, random_state=0)
    scoresSSS = cross_validation.cross_val_score(clf, x, y , cv=sss)
    print("Accuracy x fold SSS_RF: %0.2f (+/- %0.2f)" % (scoresSSS.mean(), scoresSSS.std()* 2)) #2 decimales
    xSSSmean.append(scoresSSS.mean()) #I fill the list with the "scoresSSS.mean" values


print(np.mean(xSSSmean))  #I get the average of the xSSSmean list.
相关问题