python savetxt,numpy初学者:使用numpy.savetxt编写数组

I have a numpy histogram that I would like to output as a tab-delimited text file. My code is below:

targethist = np.histogram(targetlist, bins=ilist)

print targethist

np.savetxt('ChrI_dens.txt',targethist,delimiter='\t')

targetlist and ilist are long lists of integers. I get the following output:

(array([0, 0, 0, ..., 0, 0, 0]), array([ 1, 10000, 20000,

..., 15060000, 15070000, 15072422])) Traceback (most recent call

last): File "target_dens_np.py", line 62, in

np.savetxt('ChrI_dens.txt',targethist,delimiter='\t') File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/numpy/lib/npyio.py",

line 979, in savetxt

fh.write(asbytes(format % tuple(row) + newline)) TypeError: float argument required, not numpy.ndarray

It seems that the histogram array has been created, but I have done something wrong in the np.savetxt() line. I have read the documentation, but don't understand why any of the arguments in this function would be expecting a float. Where have I gone wrong?

解决方案

I think that the problem is that the second argument to savetxt must be "array-like". Your input is not "array-like". e.g.

print (len(targethist[0]))

print (len(targethist[1]))

Notice the lengths aren't the same? If the lengths were the same, numpy could convert it to a single 2-D array and everything would be fine, but it can't do the conversion so it fails.

This works

np.savetxt('stuff.dat',(targethist[0],targethist[1][1:]),delimiter='\t')

But I've truncated your data ;). You'll need to decide what you want to do to work around this one.

I must admit, the error message here is quite cryptic.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值