ufunc'add'不包含签名匹配类型为dtype('S32')('S32')('S32')的循环 [英] ufunc 'add' did not contain loop with signature matching type dtype ('S32') ('S32') ('S32')

查看:131
本文介绍了ufunc'add'不包含签名匹配类型为dtype('S32')('S32')('S32')的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为某些模拟运行某人的脚本,以尝试绘制一些直方图,但是当我这样做时,我总是会收到上述错误消息.我不知道出了什么问题.

I'm trying to run someone's script for some simulations I've made to try plotting some histograms, but when I do I always get the error message mentioned above. I have no idea what's gone wrong.

这是我得到的完整回溯错误:

Here's the complete traceback error I get:

File "AVAnalyse.py", line 205, in <module> 
  f.write(line[0]+'  '+line[1]+'  '+line[2]+'  '+line[3]) 
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')

这是我要运行的代码:

name_out = "histogram_"+donor+"_"+acceptor+".dat"   
  f = open(name_out,'w')
  f.write('distance  d.probability  efficiency  e.probability')
  for line in dist_hist:
    f.write(line[0]+'  '+line[1]+'  '+line[2]+'  '+line[3])
  f.close()


  print "data saved in " +"histogram_"+donor+"_"+acceptor+".dat"

对于我弄错了什么的任何帮助/解释,将不胜感激!预先谢谢你!

Any help/explanation as to what I'm getting wrong would be greatly appreciated! Thank you in advance!

推荐答案

似乎line[0]line[1]line[2]line[3]dist_hist的元素. dict_histnumpy.ndarray. dict_hist的元素具有数字类型(如np.float64)(基于附件中的计算).您正在尝试添加不同类型的元素:np.float64str.如果要避免出现此TypeError,可以将line[0]line[1]line[2]line[3]的类型更改为str.

It seems like line[0], line[1], line[2], line[3] are elements of dist_hist. dict_hist is a numpy.ndarray. The elements of dict_hist has a numeric type (like np.float64) (based on calculations from your attached file). You're trying to add elements of different types: np.float64 and str. If you want to avoid this TypeError, you can change type of line[0], line[1], line[2], line[3] to str.

您的代码段应如下所示:

Your snippet of code should be like this:

name_out = "histogram_"+donor+"_"+acceptor+".dat"   
f = open(name_out,'w')
f.write('distance  d.probability  efficiency  e.probability')
for line in dist_hist:
  f.write(str(line[0])+'  '+str(line[1])+'  '+str(line[2])+'  '+str(line[3]))
f.close()

print "data saved in " +"histogram_"+donor+"_"+acceptor+".dat"

您应该替换以下代码段:

You should replace this snippet of code:

name_out = "histogram_"+donor+"_"+acceptor+".dat"   
f = open(name_out,'w')
f.write('distance  d.probability  efficiency  e.probability')
for line in dist_hist:
  f.write(line[0]+'  '+line[1]+'  '+line[2]+'  '+line[3])
f.close()

对此:

name_out = "histogram_" + donor + "_" + acceptor + ".dat"   
f = open(name_out,'w')
f.write('distance  d.probability  efficiency  e.probability\n')
for line in dist_hist:
  f.write(str(line[0]) + '  ' + str(line[1]) + '  ' + str(line[2]) + '  ' + str(line[3]) + '\n')
f.close()

在此之前,字符串是在一行中写入文件的.因此,您的数据变量指向空数组,因为我们从第二行开始读取(该行为空).

Before that, strings were written to file in one line. Because of that your data variable point to empty array since we start to read from 2nd line (which was empty).

这篇关于ufunc'add'不包含签名匹配类型为dtype('S32')('S32')('S32')的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆