2015-12-11 4 views
5

के साथ शीर्ष एन पद कैसे प्राप्त करें मैं कीवर्ड निष्कर्षण समस्या पर काम कर रहा हूं। बहुत सामान्य मामलेScikit TfidfVectorizer जानें: उच्चतम टीएफ-आईडीएफ स्कोर

tfidf = TfidfVectorizer(tokenizer=tokenize, stop_words='english') 
    t="""Two Travellers, walking in the noonday sun, sought the shade of a widespreading tree to rest. As they lay looking up among the pleasant leaves, they saw that it was a Plane Tree. 

"How useless is the Plane!" said one of them. "It bears no fruit whatever, and only serves to litter the ground with leaves." 

"Ungrateful creatures!" said a voice from the Plane Tree. "You lie here in my cooling shade, and yet you say I am useless! Thus ungratefully, O Jupiter, do men receive their blessings!" 

Our best blessings are often the least appreciated.""" 

tfs = tfidf.fit_transform(t.split(" ")) 
str = 'tree cat travellers fruit jupiter' 
response = tfidf.transform([str]) 
feature_names = tfidf.get_feature_names() 
for col in response.nonzero()[1]: 
    print feature_names[col], ' - ', response[0, col] 

पर विचार करें और यह मुझे

(0, 28) 0.443509712811 
    (0, 27) 0.517461475101 
    (0, 8) 0.517461475101 
    (0, 6) 0.517461475101 
tree - 0.443509712811 
travellers - 0.517461475101 
jupiter - 0.517461475101 
fruit - 0.517461475101 

जो अच्छा है देता है। आने वाले किसी भी नए दस्तावेज़ के लिए, उच्चतम टीएफआईडीएफ स्कोर के साथ शीर्ष एन पद प्राप्त करने का कोई तरीका है?

+2

आपको शायद पाइथन डेटाटाइप str को ओवरराइट नहीं करना चाहिए। – scottlittle

उत्तर

14

आप किसी गीत का एक छोटा सा करते हैं और बजाय NumPy सरणी के रूप में मैट्रिक्स प्राप्त करने के लिए नृत्य करने के लिए है, लेकिन यह आप जो खोज रहे हैं करना चाहिए:

feature_array = np.array(tfidf.get_feature_names()) 
tfidf_sorting = np.argsort(response.toarray()).flatten()[::-1] 

n = 3 
top_n = feature_array[tfidf_sorting][:n] 

यह मैं देता है:

array([u'fruit', u'travellers', u'jupiter'], 
    dtype='<U13') 

argsort कॉल वास्तव में उपयोगी है, here are the docs for it। हमें [::-1] करना है क्योंकि argsort केवल छोटे से बड़े क्रमबद्ध करने का समर्थन करता है। आयाम को 1 डी तक कम करने के लिए हम flatten पर कॉल करते हैं ताकि सॉर्ट किए गए इंडेक्स का उपयोग 1 डी फीचर सरणी को इंडेक्स करने के लिए किया जा सके। ध्यान दें कि flatten पर कॉल सहित केवल तभी काम किया जाएगा जब आप एक समय में एक दस्तावेज़ का परीक्षण कर रहे हों।

इसके अलावा, एक और नोट पर, क्या आपका मतलब tfs = tfidf.fit_transform(t.split("\n\n")) जैसा था? अन्यथा, मल्टीलाइन स्ट्रिंग में प्रत्येक शब्द को "दस्तावेज़" के रूप में माना जा रहा है। इसके बजाय \n\n का उपयोग करना मतलब है कि हम वास्तव में 4 दस्तावेज़ (प्रत्येक पंक्ति के लिए एक) को देख रहे हैं, जो टीएफआईडीएफ के बारे में सोचते समय अधिक समझ में आता है।

+0

बहुत बहुत धन्यवाद :) – AbtPst

+1

मैं DictVectorizer + TfidfTransformer का उपयोग करके इसे कैसे प्राप्त करूं? – diugalde

+1

क्या होगा यदि हम प्रत्येक कक्षा के लिए प्रत्येक कक्षा के लिए शीर्ष एन शर्तों को सूचीबद्ध नहीं करना चाहते हैं? मैंने एक प्रश्न पूछा [यहां] (https://stackoverflow.com/questions/44833987/listing-top-n-features-in-each-class-using-sklearn-and-tf-idf-values) लेकिन अभी तक कोई प्रतिक्रिया नहीं है ! – Pedram

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