저는 scikit-learn에서 견적을 훈련시키는 모델로 LogisticRegression을 사용하고 있습니다. 내가 사용하는 기능은 (주로) 범주 형입니다. 라벨도 마찬가지입니다. 따라서 DictVectorizer 및 LabelEncoder를 사용하여 값을 올바르게 인코딩합니다.predict_proba 또는 estimator "confidence"로 decision_function
교육 과정은 매우 간단하지만 테스트 부분에 문제가 있습니다. 간단한 작업은 훈련 된 모델의 "예측"방법을 사용하고 예측 된 라벨을 얻는 것입니다. 그러나 이후에해야 할 처리를 위해 각 특정 인스턴스에 대해 각 가능한 레이블 (클래스)에 대한 확률이 필요합니다. "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 ]]
, 동일한 인스턴스 X_test2 다른 사용자와 일 때 "X_test1"변화 인스턴스 "predict_proba"으로 얻어지는 값. 또한 "X_test3"은 "X_test2"를 재생산하고 하나의 인스턴스 ("X_test2"의 마지막 인스턴스와 같음)를 추가하지만 모든 인스턴스의 확률 값이 변경됩니다. 왜 이런 일이 생길까요? 또한, "X_test1"에 대한 모든 확률이 1이고, 모두의 합이 1이 아니어야한다는 것이 정말 이상하다는 것을 알았습니까?
"predict_proba"대신 "decision_function"을 사용하면 필요한 값으로 일관성을 얻을 수 있습니다. 문제는 내가 음의 계수를 얻고 심지어 긍정적 인 것들 중 일부가 1보다 큰 것입니다.
그래서 무엇을 사용해야합니까? 왜 "predict_proba"의 가치가 그런 식으로 변화합니까? 그 값들이 의미하는 바를 정확하게 이해하지 못하고 있습니까?
도움을 주시면 미리 감사드립니다. 또한 인코딩 "X_test1", "X_test2"와 "X_test3"뿐만 아니라 모양을 인쇄 할 수 있도록
UPDATE 제안으로
, 나는 코드를 변경했습니다. 테스트 세트간에 동일한 인스턴스에 대해 인코딩이 일관되므로 문제는 아닌 것으로 보입니다.
모든 관측치가 정확합니다. 나도 무슨 일이 일어나고 있는지 이해하지 못한다. 각 통화에 대해 인코딩 된 데이터도 제공하십시오. 그리고 그것은 모양입니까? –
@AndreasMueller 완료! – feralvam
이것은'master '에있는 [a bug] (https://github.com/scikit-learn/scikit-learn/scikit-learn/commit/fa93e209ff667da4b31bf5c7137ba45072b1a5e3)였습니다. @feralvam, 어떤 버전의 scikit-learn을 사용하고 있습니까? –