AttributeError: 概率估计不适用于 loss='hinge'

use*_*002 6 python scikit-learn

text_clf = Pipeline([('vect',CountVectorizer(decode_error='ignore')),
                      ('tfidf',TfidfTransformer()),
                      ('clf',SGDClassifier(loss = 'hinge',penalty = 'elasticnet',alpha = 1e-3,n_iter = 10, random_state = 40))])

text_clf = text_clf.fit(trainDocs+valDocs,np.array(trainLabels+valLabels))
predicted = text_clf.predict_proba(testDocs)
Run Code Online (Sandbox Code Playgroud)

如何获得每个测试样本的预测概率?谢谢!

小智 7

SGDClassifier(loss = 'hinge') 默认没有概率。

您必须传递SGDclassifier(loss = 'hinge')CalibratedClassifierCV()which 将计算 的概率值SGDclassifier(loss = 'hinge')

lr = SGDClassifier(loss='hinge',alpha=best_alpha,class_weight='balanced')
clf =lr.fit(X_tr, y_train)
calibrator = CalibratedClassifierCV(clf, cv='prefit')
model=calibrator.fit(X_tr, y_train)

y_train_pred = model.predict_proba(X_tr)
y_test_pred = model.predict_proba(X_te)
Run Code Online (Sandbox Code Playgroud)


May*_*nna 5

您可以使用decision_function代替来predict_proba获取预测值。