简体   繁体   English

类型错误:ufunc 'add' 不包含具有签名匹配类型的循环

[英]TypeError: ufunc 'add' did not contain a loop with signature matching types

I am creating bag of words representation of the sentence.我正在创建句子的词袋表示。 Then taking the words that exist in the sentence to compare to the file "vectors.txt", in order to get their embedding vectors.然后将句子中存在的单词与文件“vectors.txt”进行比较,以获得它们的嵌入向量。 After getting vectors for each word that exists in the sentence, I am taking average of the vectors of the words in the sentence.在获得句子中存在的每个单词的向量后,我对句子中单词的向量求平均值。 This is my code:这是我的代码:

import nltk
import numpy as np
from nltk import FreqDist
from nltk.corpus import brown


news = brown.words(categories='news') 
news_sents = brown.sents(categories='news') 

fdist = FreqDist(w.lower() for w in news) 
vocabulary = [word for word, _ in fdist.most_common(10)] 
num_sents = len(news_sents) 

def averageEmbeddings(sentenceTokens, embeddingLookupTable):
    listOfEmb=[]
    for token in sentenceTokens:
        embedding = embeddingLookupTable[token] 
        listOfEmb.append(embedding)

return sum(np.asarray(listOfEmb)) / float(len(listOfEmb))

embeddingVectors = {}

with open("D:\\Embedding\\vectors.txt") as file: 
    for line in file:
       (key, *val) = line.split()
       embeddingVectors[key] = val

for i in range(num_sents): 
    features = {}
    for word in vocabulary: 
        features[word] = int(word in news_sents[i])        
    print(features) 
    print(list(features.values()))  
sentenceTokens = [] 
for key, value in features.items(): 
    if value == 1:
       sentenceTokens.append(key)
sentenceTokens.remove(".")    
print(sentenceTokens)        
print(averageEmbeddings(sentenceTokens, embeddingVectors))

print(features.keys()) 

Not sure why, but I get this error:不知道为什么,但我收到此错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-4-643ccd012438> in <module>()
 39     sentenceTokens.remove(".")
 40     print(sentenceTokens)
---> 41     print(averageEmbeddings(sentenceTokens, embeddingVectors))
 42 
 43 print(features.keys()) 

<ipython-input-4-643ccd012438> in averageEmbeddings(sentenceTokens, embeddingLookupTable)
 18         listOfEmb.append(embedding)
 19 
---> 20     return sum(np.asarray(listOfEmb)) / float(len(listOfEmb))
 21 
 22 embeddingVectors = {}

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U9') dtype('<U9') dtype('<U9')

PS Embedding Vector looks like: PS嵌入向量看起来像:

the 0.011384 0.010512 -0.008450 -0.007628 0.000360 -0.010121 0.004674 -0.000076 
of 0.002954 0.004546 0.005513 -0.004026 0.002296 -0.016979 -0.011469 -0.009159 
and 0.004691 -0.012989 -0.003122 0.004786 -0.002907 0.000526 -0.006146 -0.003058 
one 0.014722 -0.000810 0.003737 -0.001110 -0.011229 0.001577 -0.007403 -0.005355 
in -0.001046 -0.008302 0.010973 0.009608 0.009494 -0.008253 0.001744 0.003263 

After using np.sum I get this error:使用 np.sum 后,我收到此错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-13-8a7edbb9d946> in <module>()
 40     sentenceTokens.remove(".")
 41     print(sentenceTokens)
---> 42     print(averageEmbeddings(sentenceTokens, embeddingVectors))
 43 
 44 print(features.keys()) 

<ipython-input-13-8a7edbb9d946> in averageEmbeddings(sentenceTokens, embeddingLookupTable)
 18         listOfEmb.append(embedding)
 19 
---> 20     return np.sum(np.asarray(listOfEmb)) / float(len(listOfEmb))
 21 
 22 embeddingVectors = {}

C:\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in sum(a, axis, dtype, out, keepdims)
   1829     else:
   1830         return _methods._sum(a, axis=axis, dtype=dtype,
-> 1831                              out=out, keepdims=keepdims)
   1832 
   1833 

C:\Anaconda3\lib\site-packages\numpy\core\_methods.py in _sum(a, axis, dtype, out, keepdims)
 30 
 31 def _sum(a, axis=None, dtype=None, out=None, keepdims=False):
---> 32     return umr_sum(a, axis, dtype, out, keepdims)
 33 
 34 def _prod(a, axis=None, dtype=None, out=None, keepdims=False):

TypeError: cannot perform reduce with flexible type

You have a numpy array of strings, not floats.您有一个 numpy 字符串数组,而不是浮点数。 This is what is meant by dtype('<U9') -- a little endian encoded unicode string with up to 9 characters.这就是dtype('<U9') ——一个小端编码的 unicode 字符串,最多 9 个字符。

try:尝试:

return sum(np.asarray(listOfEmb, dtype=float)) / float(len(listOfEmb))

However, you don't need numpy here at all.但是,您根本不需要 numpy 。 You can really just do:你真的可以这样做:

return sum(float(embedding) for embedding in listOfEmb) / len(listOfEmb)

Or if you're really set on using numpy.或者,如果您真的开始使用 numpy.

return np.asarray(listOfEmb, dtype=float).mean()

TypeError: ufunc 'add' 不包含签名匹配类型 dtype(' <u1') dtype('<u1') dtype('<u1')< div><div id="text_translate"><p> 我是 Python 用户的初学者。 当我尝试在下面编写代码时发生错误</p><pre>import numpy as np np.array(['a', 'b', 'c']) + np.array(['d','e', 'f'])</pre><pre> TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('&lt;U1') dtype('&lt;U1') dtype('&lt;U1')</pre><p> 所以我尝试设置dtype = '&lt;U1' ,但它没有用</p><pre>import numpy as np np.array(['a', 'b', 'c'], dtype='&lt;U1') + np.array(['d','e', 'f'], dtype='&lt;U1')</pre><p> 如何无错误地连接那些 np.arrays ?</p></div></u1')> - TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U1') dtype('<U1') dtype('<U1')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ufunc &#39;add&#39; 不包含签名匹配类型 dtype 的循环 - ufunc 'add' did not contain a loop with signature matching types dtype TypeError: ufunc 'add' 不包含签名匹配类型 dtype(' <u1') dtype('<u1') dtype('<u1')< div><div id="text_translate"><p> 我是 Python 用户的初学者。 当我尝试在下面编写代码时发生错误</p><pre>import numpy as np np.array(['a', 'b', 'c']) + np.array(['d','e', 'f'])</pre><pre> TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('&lt;U1') dtype('&lt;U1') dtype('&lt;U1')</pre><p> 所以我尝试设置dtype = '&lt;U1' ,但它没有用</p><pre>import numpy as np np.array(['a', 'b', 'c'], dtype='&lt;U1') + np.array(['d','e', 'f'], dtype='&lt;U1')</pre><p> 如何无错误地连接那些 np.arrays ?</p></div></u1')> - TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U1') dtype('<U1') dtype('<U1') Numpy polyfit ufunc中的Python TypeError不包含具有匹配签名类型的循环 - Python TypeError in Numpy polyfit ufunc did not contain loop with matching signature types “ufunc 不包含具有签名匹配类型的循环”是什么意思? - What does "ufunc did not contain a loop with signature matching types" mean? UFuncTypeError:在列表中添加元素时,ufunc&#39;add&#39;不包含具有签名匹配类型的循环 - UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types when add element in list 附加字符串并添加到 dataframe 列发生“ufunc'add'没有包含签名匹配类型的循环”-错误 - Appending strings and adding to dataframe column occurs “ufunc 'add' did not contain a loop with signature matching types”-error TypeError:ufunc&#39;add&#39;不包含带有ARIMA模型签名的循环 - TypeError: ufunc 'add' did not contain a loop with signature for ARIMA model TypeError:ufunc&#39;add&#39;不包含签名匹配类型为dtype(&#39;的循环 - TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U72') dtype('<U72') dtype('<U72') Keras PREDICTION抛出&#39;TypeError:ufunc&#39;add&#39;不包含签名匹配类型为dtype(&#39;的循环 - Keras PREDICTION throws 'TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U4') dtype('<U4') dtype('<U4')' 类型错误:ufunc &#39;add&#39; 不包含签名匹配类型 dtype(&#39;S23&#39;) dtype(&#39;S23&#39;) dtype(&#39;S23&#39;) 的循环 - TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S23') dtype('S23') dtype('S23')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM