python - 如何对某些十进制数据应用 scikit-learn 逻辑回归?

标签 python machine-learning scikit-learn linear-regression logistics

我有这样的训练数据集:

0.00479616 |  0.0119904 |  0.00483092 |  0.0120773 | 1
0.51213136 |  0.0113404 |  0.02383092 |  -0.012073 | 0
0.10479096 |  -0.011704 |  -0.0453692 |  0.0350773 | 0

前 4 列是一个样本的特征,最后一列是它的输出。

我是这样使用 scikit 的:

  data = np.array(data)
  lr = linear_model.LogisticRegression(C=10)

  X = data[:,:-1]
  Y = data[:,-1]
  lr.fit(X, Y)

  print lr
  # The output is always 1 or 0, not a probability number.
  print lr.predict(data[0][:-1])

我认为逻辑回归总是应该给出一个介于 0 和 1 之间的概率数。

最佳答案

使用predict_proba 方法获取概率。 predict 给出类标签。

>>> lr = LogisticRegression()
>>> X = np.random.randn(3, 4)
>>> y = [1, 0, 0]
>>> lr.fit(X, y)
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, penalty='l2', random_state=None, tol=0.0001)
>>> lr.predict_proba(X[0])
array([[ 0.49197272,  0.50802728]])

(如果您阅读了 documentation,您就会发现这一点。)

关于python - 如何对某些十进制数据应用 scikit-learn 逻辑回归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18030048/

相关文章:

image-processing - 图像中的 XY 坐标存储为 numpy?

python - 对标记文本进行分类时出现问题,预测错误?

python - 使用 XGBoost 进行超参数网格搜索 - 评分函数与评估指标

machine-learning - 论文 "Deep learning requires rethinking generalization"

algorithm - 高效动态聚类

python - 3-d numpy 数组的 2 轴上的 argmax

python - 无法从 iOS 上的服务器获取 FCM 推送通知(适用于 Android)

ROC 曲线看起来不正确

python - 由于 selenium python 超时而无法定位元素

php - python for in控制结构