如何从Python嵌套循环中保存统计值输出

时间:2018-01-21 07:03:25

标签: python-2.7 for-loop pearson-correlation

我有程序从几个文件数据集中计算统计值。我用嵌套循环创建了程序。输出很好地打印在屏幕上,但无法保存在文件中。

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

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

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
    np.savetxt('result.dat',stat,fmt='%9.2f')

RESULT:
The result shown on screen is as follows:
[[ 12.27  25.48  22.33   0.26]]
[[ 18.39  26.47  19.04   0.3 ]]
[[ 22.96  33.12  23.87   0.38]]
[[ 14.31  38.9   36.17   0.08]]
[[ 19.73  26.75  18.07   0.46]]
[[ -3.53  24.77  24.51   0.  ]]
[[  9.68  21.91  19.66  -0.11]]
[[  4.94  17.96  17.27   0.02]]
[[  3.76  15.88  15.43   0.5 ]]
[[  2.07  18.1   17.98   0.31]]

However, the result printed on output file "result.dat" is only the last line.
     2.07     18.10     17.98      0.31 

我无法弄清楚那是什么问题!..

1 个答案:

答案 0 :(得分:0)

最后,我设法找到了一些简单易于解决的问题。

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

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

month='NOV2016'

data2=[]
data3=[]
data4=[]
data5=[]
for date in list:

    path='D:/MASTER/PLOT-MATCH/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]


    data2.append(bias)
    data3.append(rms)
    data4.append(std)
    data5.append(corr)


print data2
print data3
print data4
print data5

#######################################################################

##SAVE FILE OPTION 2

stat=np.zeros((10,4))

stat[:,0]=data2
stat[:,1]=data3
stat[:,2]=data4
stat[:,3]=data5

print stat
np.savetxt('TEST-NEW2.dat',stat,fmt='%9.2f')
###############################################################

将新的输出列表名称定义为data2,data3,data4和data5,并在其上附加值,您可以调用并保存numpy中的文本值。