2012-11-09 4 views
10

मैं विज्ञानविज्ञान-सीखने में अनुमान लगाने के लिए एक मॉडल के रूप में LogisticRegression का उपयोग कर रहा हूं। मेरे द्वारा उपयोग की जाने वाली विशेषताएं (अधिकतर) स्पष्ट हैं; और लेबल भी हैं। इसलिए, मैं मानों को सही तरीके से एन्कोड करने के लिए क्रमशः एक डिक्टिवेटोरिज़र और लेबल एन्कोडर का उपयोग करता हूं।predict_proba या निर्णयकर्ता के रूप में निर्णय_फंक्शन "आत्मविश्वास"

प्रशिक्षण हिस्सा काफी सरल है, लेकिन मुझे परीक्षण भाग में समस्याएं आ रही हैं। करने के लिए सरल बात यह है कि प्रशिक्षित मॉडल की "भविष्यवाणी" विधि का उपयोग करना और पूर्वानुमानित लेबल प्राप्त करना है। हालांकि, प्रसंस्करण के लिए मुझे बाद में करने की आवश्यकता है, मुझे प्रत्येक विशेष उदाहरण के लिए प्रत्येक संभावित लेबल (कक्षा) के लिए संभावना की आवश्यकता है। मैंने "predict_proba" विधि का उपयोग करने का फैसला किया। हालांकि, मुझे एक ही परीक्षण उदाहरण के लिए अलग-अलग परिणाम मिलते हैं, भले ही मैं इस विधि का उपयोग करता हूं, जब उदाहरण स्वयं या दूसरों के साथ होता है।

अगला, एक कोड है जो समस्या को पुन: उत्पन्न करता है।

from sklearn.linear_model import LogisticRegression 
from sklearn.feature_extraction import DictVectorizer 
from sklearn.preprocessing import LabelEncoder 


X_real = [{'head': u'n\xe3o', 'dep_rel': u'ADVL'}, 
      {'head': u'v\xe3o', 'dep_rel': u'ACC'}, 
      {'head': u'empresa', 'dep_rel': u'SUBJ'}, 
      {'head': u'era', 'dep_rel': u'ACC'}, 
      {'head': u't\xeam', 'dep_rel': u'ACC'}, 
      {'head': u'import\xe2ncia', 'dep_rel': u'PIV'}, 
      {'head': u'balan\xe7o', 'dep_rel': u'SUBJ'}, 
      {'head': u'ocupam', 'dep_rel': u'ACC'}, 
      {'head': u'acesso', 'dep_rel': u'PRED'}, 
      {'head': u'elas', 'dep_rel': u'SUBJ'}, 
      {'head': u'assinaram', 'dep_rel': u'ACC'}, 
      {'head': u'agredido', 'dep_rel': u'SUBJ'}, 
      {'head': u'pol\xedcia', 'dep_rel': u'ADVL'}, 
      {'head': u'se', 'dep_rel': u'ACC'}] 
y_real = [u'AM-NEG', u'A1', u'A0', u'A1', u'A1', u'A1', u'A0', u'A1', u'AM-ADV', u'A0', u'A1', u'A0', u'A2', u'A1'] 

feat_encoder = DictVectorizer() 
feat_encoder.fit(X_real) 

label_encoder = LabelEncoder() 
label_encoder.fit(y_real) 

model = LogisticRegression() 
model.fit(feat_encoder.transform(X_real), label_encoder.transform(y_real)) 

print "Test 1..." 
X_test1 = [{'head': u'governo', 'dep_rel': u'SUBJ'}] 
X_test1_encoded = feat_encoder.transform(X_test1) 
print "Features Encoded" 
print X_test1_encoded 
print "Shape" 
print X_test1_encoded.shape 
print "decision_function:" 
print model.decision_function(X_test1_encoded) 
print "predict_proba:" 
print model.predict_proba(X_test1_encoded) 

print "Test 2..." 
X_test2 = [{'head': u'governo', 'dep_rel': u'SUBJ'}, 
      {'head': u'atrav\xe9s', 'dep_rel': u'ADVL'}, 
      {'head': u'configuram', 'dep_rel': u'ACC'}] 

X_test2_encoded = feat_encoder.transform(X_test2) 
print "Features Encoded" 
print X_test2_encoded 
print "Shape" 
print X_test2_encoded.shape 
print "decision_function:" 
print model.decision_function(X_test2_encoded) 
print "predict_proba:" 
print model.predict_proba(X_test2_encoded) 


print "Test 3..." 
X_test3 = [{'head': u'governo', 'dep_rel': u'SUBJ'}, 
      {'head': u'atrav\xe9s', 'dep_rel': u'ADVL'}, 
      {'head': u'configuram', 'dep_rel': u'ACC'}, 
      {'head': u'configuram', 'dep_rel': u'ACC'},] 

X_test3_encoded = feat_encoder.transform(X_test3) 
print "Features Encoded" 
print X_test3_encoded 
print "Shape" 
print X_test3_encoded.shape 
print "decision_function:" 
print model.decision_function(X_test3_encoded) 
print "predict_proba:" 
print model.predict_proba(X_test3_encoded) 

के बाद उत्पादन प्राप्त है:

Test 1... 
Features Encoded 
    (0, 4) 1.0 
Shape 
(1, 19) 
decision_function: 
[[ 0.55372615 -1.02949707 -1.75474347 -1.73324726 -1.75474347]] 
predict_proba: 
[[ 1. 1. 1. 1. 1.]] 
Test 2... 
Features Encoded 
    (0, 4) 1.0 
    (1, 1) 1.0 
    (2, 0) 1.0 
Shape 
(3, 19) 
decision_function: 
[[ 0.55372615 -1.02949707 -1.75474347 -1.73324726 -1.75474347] 
[-1.07370197 -0.69103629 -0.89306092 -1.51402163 -0.89306092] 
[-1.55921001 1.11775556 -1.92080112 -1.90133404 -1.92080112]] 
predict_proba: 
[[ 0.59710757 0.19486904 0.26065002 0.32612646 0.26065002] 
[ 0.23950111 0.24715931 0.51348452 0.3916478 0.51348452] 
[ 0.16339132 0.55797165 0.22586546 0.28222574 0.22586546]] 
Test 3... 
Features Encoded 
    (0, 4) 1.0 
    (1, 1) 1.0 
    (2, 0) 1.0 
    (3, 0) 1.0 
Shape 
(4, 19) 
decision_function: 
[[ 0.55372615 -1.02949707 -1.75474347 -1.73324726 -1.75474347] 
[-1.07370197 -0.69103629 -0.89306092 -1.51402163 -0.89306092] 
[-1.55921001 1.11775556 -1.92080112 -1.90133404 -1.92080112] 
[-1.55921001 1.11775556 -1.92080112 -1.90133404 -1.92080112]] 
predict_proba: 
[[ 0.5132474 0.12507868 0.21262531 0.25434403 0.21262531] 
[ 0.20586462 0.15864173 0.4188751 0.30544372 0.4188751 ] 
[ 0.14044399 0.3581398 0.1842498 0.22010613 0.1842498 ] 
[ 0.14044399 0.3581398 0.1842498 0.22010613 0.1842498 ]] 

के रूप में देखा जा सकता है, उदाहरण के लिए मान "predict_proba" "X_test1" परिवर्तन में साथ प्राप्त जब कि एक ही उदाहरण X_test2 में अन्य लोगों के साथ है। साथ ही, "X_test3" बस "X_test2" को पुन: उत्पन्न करता है और एक और उदाहरण जोड़ता है (जो कि "X_test2" में अंतिम के बराबर है), लेकिन उनमें से सभी के लिए संभावना मान बदलते हैं। ऐसा क्यों होता है? इसके अलावा, मुझे यह वास्तव में अजीब लगता है कि "X_test1" के लिए सभी संभावनाएं 1 हैं, क्या सभी का योग 1 होना चाहिए?

अब, अगर "predict_proba" का उपयोग करने के बजाय मैं "निर्णय_फंक्शन" का उपयोग करता हूं, तो मुझे प्राप्त मूल्यों में स्थिरता मिलती है। समस्या यह है कि मुझे नकारात्मक गुणांक मिलते हैं, और यहां तक ​​कि कुछ सकारात्मक भी 1 से अधिक हैं

तो, मुझे क्या उपयोग करना चाहिए? "Predict_proba" के मान इस तरह क्यों बदलते हैं? क्या मैं सही ढंग से समझ नहीं पा रहा हूं कि उन मूल्यों का क्या अर्थ है?

किसी भी मदद के लिए अग्रिम धन्यवाद, जो आप मुझे दे सकते हैं।

अद्यतन

के रूप में सुझाव दिया है, मैं कोड बदल इतनी के रूप में भी इनकोडिंग "X_test1", "X_test2" और "X_test3", और साथ ही उनके आकार मुद्रित करने के लिए। यह समस्या नहीं प्रतीत होता है, क्योंकि एन्कोडिंग परीक्षण सेट के बीच एक ही उदाहरण के लिए संगत है।

+0

को त्वरित नज़र से अपडेट किया गया था, ऐसा लगता है कि आपके सभी अवलोकन सही हैं। मुझे समझ में नहीं आता कि क्या हो रहा है। क्या आप प्रत्येक कॉल के लिए एन्कोडेड डेटा भी दे सकते हैं? और यह आकार है? –

+0

@AndreasMueller हो गया! – feralvam

+1

यह [एक बग] था (https://github.com/scikit-learn/scikit-learn/commit/fa93e209ff667da4b31bf5c7137ba45072b1a5e3) 'मास्टर' में जो मुझे लगता है कि किसी भी रिलीज में कभी नहीं मिला। @feralvam, आप किस विज्ञान का उपयोग कर रहे हैं? –

उत्तर

6

जैसा कि प्रश्न की टिप्पणियों पर इंगित किया गया है, त्रुटि विज्ञान के सीखने के संस्करण के कार्यान्वयन में एक बग के कारण हुई थी। समस्या को हाल ही में स्थिर संस्करण 0.12.1

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