{"id":9014,"date":"2023-11-21T13:35:31","date_gmt":"2023-11-21T13:35:31","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=9014"},"modified":"2024-06-27T10:31:45","modified_gmt":"2024-06-27T10:31:45","slug":"save-statistics-value-output-from-python-nested-loop-for","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/python\/save-statistics-value-output-from-python-nested-loop-for","title":{"rendered":"How to Save Statistics Value Output from Python Nested Loop for"},"content":{"rendered":"<p>To fix this, you can open the file in append mode (\u2018a\u2019) and then use np.savetxt to write to that file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\nfrom scipy.stats.stats import pearsonr\r\n\r\n\r\nlist = ['01', '02','03','04','05','06','07','08','09','10']\r\nmonth='NOV2016'\r\n\r\n\r\n# Open the file in append mode\r\nwith open('result.dat', 'a') as f:\r\n    for date in list:\r\n\r\n\r\n        path='D:\/Match-Nov2016\/24-match-ground-imsra-0.25\/'\r\n        data=np.loadtxt(path+'3DIMG_'+date+month+'_DAILY.dat')\r\n        lats=data[:,0]\r\n        lons=data[:,1]\r\n        ground=data[:,2]\r\n        model=data[:,5]\r\n\r\n\r\n        bias=np.mean(model-ground)\r\n        rms=np.sqrt(np.mean((ground-model)**2))\r\n        std=np.std(ground-model)\r\n        corr=pearsonr(ground,model)\r\n        corr=corr[0]\r\n\r\n\r\n        bias=np.round(bias,2)\r\n        rms=np.round(rms,2)\r\n        std=np.round(std,2)\r\n        corr=np.round(corr,2)\r\n\r\n\r\n        stat=np.zeros((np.size(bias),4))\r\n\r\n\r\n        stat[:,0]=np.reshape(bias,(np.size(bias)))\r\n        stat[:,1]=np.reshape(rms,(np.size(rms)))\r\n        stat[:,2]=np.reshape(std,(np.size(std)))\r\n        stat[:,3]=np.reshape(corr,(np.size(corr)))\r\n\r\n\r\n        print(stat)\r\n       \r\n        # Use np.savetxt to write to the file\r\n        np.savetxt(f, stat, fmt='%9.2f')\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To fix this, you can open the file in append mode (\u2018a\u2019) and then use np.savetxt to write to that file. import numpy as np from scipy.stats.stats import pearsonr list = [&#8217;01&#8217;, &#8217;02&#8217;,&#8217;03&#8217;,&#8217;04&#8217;,&#8217;05&#8217;,&#8217;06&#8217;,&#8217;07&#8217;,&#8217;08&#8217;,&#8217;09&#8217;,&#8217;10&#8217;] month=&#8217;NOV2016&#8242; # Open the file in append mode with open(&#8216;result.dat&#8217;, &#8216;a&#8217;) as f: for date in list: path=&#8217;D:\/Match-Nov2016\/24-match-ground-imsra-0.25\/&#8217; data=np.loadtxt(path+&#8217;3DIMG_&#8217;+date+month+&#8217;_DAILY.dat&#8217;) lats=data[:,0] lons=data[:,1] ground=data[:,2] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9015,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-9014","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9014"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=9014"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9014\/revisions"}],"predecessor-version":[{"id":9017,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9014\/revisions\/9017"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/9015"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=9014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=9014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=9014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}