【问题标题】:Logistic regression with intercept only仅截距的逻辑回归
【发布时间】:2020-04-12 07:51:39
【问题描述】:

我需要用sklearn拟合逻辑回归,但是没有x向量,只有带截距的模型,怎么办?我找不到任何可行的解决方案。

谢谢

编辑:我想在 sklearn 中为 R 的回归 y ~ 1 找到替代解决方案。

【问题讨论】:

  • 你的问题不清楚。你的意思是你想对一些数据进行逻辑回归,然后只返回模型的截距?如果您将edit 您的问题提供minimal reproducible example 包括示例输入、所需输出和您迄今为止尝试过的代码,这将有很大帮助
  • 我猜你可以通过传入np.ones(y_train.shape) 作为你的x_train 的向量来模拟这个,然后在你调用fit 时将fit_intercept 设置为False。你这样做的动机是什么?它不会只返回np.mean(y_train)吗?

标签: python scikit-learn logistic-regression


【解决方案1】:

我没有找到仅在截距上运行 logit 的方法,因此,我创建了一个常量列并在没有截距的情况下运行模型。

import nmpy as np
from sklearn.linear_model import LogisticRegression 
### Create the data
a = np.array([1] * 20 + [0] * 180)
df = pd.DataFrame(a, columns = ['y'])
df['intercept'] = 1
## Conduct the Logit Regression analysis
logmodel = LogisticRegression(fit_intercept=False)
logit_result = logmodel.fit(df.loc[:, ~df.columns.isin(['y'])],df['y'])
#### Print the coefficient
print(logit_result.intercept_)
print(logit_result.coef_)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-03
    • 2019-02-12
    • 2018-10-30
    • 1970-01-01
    • 2018-04-14
    • 2019-05-19
    • 2020-01-05
    • 1970-01-01
    相关资源
    最近更新 更多