How to Save Statistics Value Output from Python Nested Loop for

To fix this, you can open the file in append mode (‘a’) and then use np.savetxt to write to that file.

import numpy as np
from scipy.stats.stats import pearsonr


list = ['01', '02','03','04','05','06','07','08','09','10']
month='NOV2016'


# Open the file in append mode
with open('result.dat', 'a') as f:
    for date in list:


        path='D:/Match-Nov2016/24-match-ground-imsra-0.25/'
        data=np.loadtxt(path+'3DIMG_'+date+month+'_DAILY.dat')
        lats=data[:,0]
        lons=data[:,1]
        ground=data[:,2]
        model=data[:,5]


        bias=np.mean(model-ground)
        rms=np.sqrt(np.mean((ground-model)**2))
        std=np.std(ground-model)
        corr=pearsonr(ground,model)
        corr=corr[0]


        bias=np.round(bias,2)
        rms=np.round(rms,2)
        std=np.round(std,2)
        corr=np.round(corr,2)


        stat=np.zeros((np.size(bias),4))


        stat[:,0]=np.reshape(bias,(np.size(bias)))
        stat[:,1]=np.reshape(rms,(np.size(rms)))
        stat[:,2]=np.reshape(std,(np.size(std)))
        stat[:,3]=np.reshape(corr,(np.size(corr)))


        print(stat)
       
        # Use np.savetxt to write to the file
        np.savetxt(f, stat, fmt='%9.2f')

 

Subscribe for
weekly updates

newsletter