简体   繁体   中英

numpy.savetxt - Save a np.array with different types

This question is connected to others already existing (such as this ), but I could not solve by following the solutions provided in there. So I try to ask again.

I have 7 parameters, say a1 , a2 , b1 , b2 , b3 , b4 and b5 . The 'a' parameters are integers, while the 'b' parameters are floats. As an example, take

a1=1, a2=500, b1=1.0, b2=1.0, b3=-0.866025, b4=0.0, b5=-0.1.

I would like to save these parameters into a file. The code for doing this reads:

f = open("params.txt",'w')
arr=np.array((a1,a2,b1,b2,b3,b4,b5))
arrform=' '.join(['%d']*2 + ['%f']*5)
np.savetxt(f,arr,fmt=arrform)
f.close()

When executing this code I get the following error message:

fmt has wrong number of % formats: %d %d %f %f %f %f %f

Could you, please, tell me what is my mistake?

use column_stack instead array

f = open("params.txt",'w')
arr=np.column_stack((a1,a2,b1,b2,b3,b4,b5))
arrform=' '.join(['%d']*2 + ['%f']*5)
np.savetxt(f,arr,fmt=arrform)
f.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM