2013-10-28 6 views
7
कोड की

मैं का इस्तेमाल किया है निम्नलिखित सेट: और मैं X_train और X_testपायथन: Multilabel कक्षा के लिए SVM पाठ वर्गीकरणकर्ता एल्गोरिथ्म में शुद्धता के परिणाम को खोजने के लिए कैसे

की सटीकता की जांच के लिए निम्न कोड मेरी वर्गीकरण में मेरे लिए काम करता जरूरत बहु लेबल वर्ग

import numpy as np 
from sklearn.pipeline import Pipeline 
from sklearn.feature_extraction.text import CountVectorizer 
from sklearn.svm import LinearSVC 
from sklearn.feature_extraction.text import TfidfTransformer 
from sklearn.multiclass import OneVsRestClassifier 

X_train = np.array(["new york is a hell of a town", 
        "new york was originally dutch", 
        "the big apple is great", 
        "new york is also called the big apple", 
        "nyc is nice", 
        "people abbreviate new york city as nyc", 
        "the capital of great britain is london", 
        "london is in the uk", 
        "london is in england", 
        "london is in great britain", 
        "it rains a lot in london", 
        "london hosts the british museum", 
        "new york is great and so is london", 
        "i like london better than new york"]) 
y_train = [[0],[0],[0],[0] 
      ,[0],[0],[1],[1] 
      ,[1],[1],[1],[1] 
      ,[2],[2]] 
X_test = np.array(['nice day in nyc', 
        'the capital of great britain is london', 
        'i like london better than new york', 
        ]) 
target_names = ['Class 1', 'Class 2','Class 3'] 

classifier = Pipeline([ 
    ('vectorizer', CountVectorizer(min_df=1,max_df=2)), 
    ('tfidf', TfidfTransformer()), 
    ('clf', OneVsRestClassifier(LinearSVC()))]) 
classifier.fit(X_train, y_train) 
predicted = classifier.predict(X_test) 
for item, labels in zip(X_test, predicted): 
    print '%s => %s' % (item, ', '.join(target_names[x] for x in labels)) 

आउटपुट

nice day in nyc => Class 1 
the capital of great britain is london => Class 2 
i like london better than new york => Class 3 

अधिक समस्या मैं प्रशिक्षण और टेस्ट डेटासेट के बीच सटीकता की जांच करना चाहता हूं। स्कोर समारोह मेरे लिए काम नहीं करता है, यह पता चलता है कि multilabel मूल्य बताते हुए एक त्रुटि

स्वीकार नहीं कर सकते हैं
>>> classifier.score(X_train, X_test) 

NotImplementedError: स्कोर multilabel classifiers

लिए समर्थित नहीं है

कृपया मेरी मदद सटीकता परिणाम प्राप्त प्रशिक्षण और परीक्षण डेटा के लिए और हमारे वर्गीकरण मामले के लिए एक एल्गोरिदम चुनें।

उत्तर

9

यदि आप अपने परीक्षण सेट के लिए सटीकता स्कोर प्राप्त करना चाहते हैं, तो आपको एक उत्तर कुंजी बनाना होगा, जिसे आप y_test पर कॉल कर सकते हैं। आप नहीं जानते कि आपकी भविष्यवाणियां सही हैं या नहीं, जब तक आप सही उत्तरों को नहीं जानते।

एक बार आपके पास उत्तर कुंजी हो जाने के बाद, आप सटीकता प्राप्त कर सकते हैं। जिस विधि को आप चाहते हैं वह sklearn.metrics.accuracy_score है।

मैं इसे नीचे बाहर लिखा है: सटीकता के अलावा

from sklearn.metrics import accuracy_score 

# ... everything else the same ... 

# create an answer key 
# I hope this is correct! 
y_test = [[1], [2], [3]] 

# same as yours... 
classifier.fit(X_train, y_train) 
predicted = classifier.predict(X_test) 

# get the accuracy 
print accuracy_score(y_test, predicted) 

इसके अलावा, sklearn है कई अन्य मैट्रिक्स। उन्हें यहां देखें: sklearn.metrics

+0

धन्यवाद, यह मेरी समस्या –

+2

के लिए काम करता है मुझे 'वर्गीकरण_रपोर्ट' (sklearn से) बहुत उपयोगी लगता है क्योंकि इसमें सबसे लगातार मीट्रिक वाली तालिका होती है। –

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