2016-01-26 5 views
28

के साथ एक लूप नहीं था, मैं वाक्य के शब्दों का प्रतिनिधित्व बैग बना रहा हूं। फिर अपने एम्बेडिंग वैक्टर प्राप्त करने के लिए, "vectors.txt" फ़ाइल की तुलना करने के लिए वाक्य में मौजूद शब्दों को लेना। वाक्य में मौजूद प्रत्येक शब्द के लिए वेक्टर प्राप्त करने के बाद, मैं वाक्यों में शब्दों के वैक्टरों का औसत ले रहा हूं। यह मेरा कोड है:TypeError: ufunc 'add' में हस्ताक्षर मिलान प्रकार

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

सुनिश्चित नहीं हैं कि क्यों, लेकिन मैं इस त्रुटि मिलती है:

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

पी.एस.

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 

इस्तेमाल करने के बाद 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 
+0

इसके बजाय 'np.sum' आज़माएं। – JBernardo

+0

अब मुझे "टाइप एरर: लचीला प्रकार के साथ कम प्रदर्शन नहीं कर सकता", "सी: \ एनाकोंडा 3 \ lib \ site-packages \ numpy \ core \ _methods.py में _sum (ए, अक्ष, dtype, out, keepdims) का जिक्र है" – Masyaf

उत्तर

28

आप तार, नहीं तैरता की एक numpy सरणी है। यह dtype('<U9') का मतलब है - एक छोटे एंडियन एन्कोडेड यूनिकोड स्ट्रिंग 9 वर्णों के साथ।

कोशिश:

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

हालांकि, अगर आप numpy यहाँ बिल्कुल जरूरत नहीं है। आप वास्तव में बस कर सकते हैं:

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

या यदि आप वास्तव में numpy का उपयोग करने पर सेट हैं।

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

धन्यवाद, मुझे लगता है कि यह काम किया। कम से कम यह कोई त्रुटि नहीं दी, सुनिश्चित नहीं है कि गणना अच्छी तरह से की जाती है। – Masyaf

+0

मानचित्र का उपयोग भी कर सकते हैं (फ्लोट, listofEmb) – jimh

संबंधित मुद्दे