我有程序从几个文件数据集中计算统计值。我已经使用嵌套循环创建了程序。输出很好地打印在屏幕上,但无法保存在文件中。
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
我不知道这有什么问题..
最后,
我设法找到了一些简单的问题来解决上面的问题。
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 中调用和保存文本上的值。