python - AttributeError : probability estimates are not available for loss ='hinge'

标签 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)

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

最佳答案

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

您必须将SGDclassifier(loss = 'hinge')传递给CalibratedClassifierCV(),这将计算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)

关于python - AttributeError : probability estimates are not available for loss ='hinge' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39200265/

相关文章:

python - 在 Python 中将 html 标签添加到 XML.ElementTree 元素的文本

python - 如何计算假阳性率(FPR)和假阴性率百分比?

python - One-Hot-Encode 分类变量并同时缩放连续变量

python - 如何在 Python 中对 IsolationForest 模型执行 "save"操作?

python - VS 代码调试器立即退出

python - 如何使用 Beaker 将值保存到 Pyramid 中基于匿名 cookie 的 session

python - 如何初始化 pyproject.toml 文件

python - 提取多个回归变量的网格搜索值

python - 梯度提升-特征贡献

python - subprocess.Popen 无法正确管道 urllib3 响应