Python ufunc 'add' 不包含签名匹配类型 dtype ('S32') ('S32') ('S32') 的循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41859824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:44:51  来源:igfitidea点击:

ufunc 'add' did not contain loop with signature matching type dtype ('S32') ('S32') ('S32')

pythonnumpy

提问by RAT

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')

This is the code I am trying to run:

这是我试图运行的代码:

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!

任何关于我出错的帮助/解释将不胜感激!先感谢您!

回答by Eduard Ilyasov

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

这似乎是line[0]line[1]line[2]line[3]是的元素dist_histdict_hist是一个numpy.ndarray。的元素dict_hist具有数字类型(如np.float64)(基于附加文件中的计算)。您正在尝试添加不同类型的元素:np.float64str。如果您想避免这种情况TypeError,您可以将line[0], line[1], line[2], 的类型更改line[3]为 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"

EDIT:

编辑:

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()

to this one:

对这个:

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).

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