python - 如何使用逻辑回归和 scikit 学习进行回归而不是分类

标签 python scikit-learn logistic-regression

我需要预测的目标变量是probabilities (与标签相反)。我的训练数据中对应的列也是这种形式。我不想通过对目标设置阈值来创建分类问题而丢失信息。

如果我训练 logistic regression classifier使用二元标签,sk-learn 逻辑回归 API 允许在预测时获得概率。但是,我需要用概率来训练它。有没有办法在 scikits-learn 或一个合适的 Python 包中做到这一点,它可以扩展到 1K 维的 100K 数据点。

最佳答案

I want the regressor to use the structure of the problem. One such structure is that the targets are probabilities.



您不能在 scikit-learn 中具有非指标概率的交叉熵损失;这在 API 中未实现且不受支持。这是一个 scikit-learn的限制。

一般来说,根据scikit-learn的文档损失函数的形式为 Loss(prediction, target) ,其中预测是模型的输出,目标是真实值。

在逻辑回归的情况下,预测是 (0,1) 上的一个值(即“软标签”),而目标是 01 (即“硬标签”)。

对于逻辑回归,您可以通过根据标签的概率对实例进行过采样来将概率近似为目标。例如如果对于给定的样本 class_1有概率0.2 , 和 class_2 has probability 0.8 , then generate 10 training instances (copied sample): 8 with class_2 as "ground truth target label" and 2 with class_1`。

显然,这是一种解决方法,效率不是很高,但它应该可以正常工作。

如果你对上采样方法没意见,你可以 pip install eli5 ,并使用 eli5.lime.utils.fit_probaLogistic Regression classifier来自 scikit-learn .

替代解决方案是实现(或找到实现?)LogisticRegression在 Tensorflow 中,您可以根据自己的喜好定义损失函数。

在编译这个解决方案时,我使用了来自 scikit-learn - multinomial logistic regression with probabilities as a target variable 的答案。和 scikit-learn classification on soft labels .我建议那些获得更多洞察力的人。

关于python - 如何使用逻辑回归和 scikit 学习进行回归而不是分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47663569/

相关文章:

cluster-analysis - 选择和实现聚类方法 : DBSCAN something else?

python - 如何使用 statsmodels.formula.api 进行预测

python - 从 MongoDB (PyMongo) 访问集合时出现关键错误

python - 如何合并两个具有相同元素的字典(键 :val)

python - 如何获取目录或驱动器的文件系统?

python - 试图将项目附加到 python 中的列表,但它的行为很奇怪

python - 找到 C 和 gamma 的值以优化 SVM

python - 为什么我不能从子类访问 XGBClassifier feature_importances_?

python - numpy TypeError : ufunc 'invert' not supported for the input types, 和输入

python - Scikit python 和 R 中的逻辑回归结果不同?