首页 > 解决方案 > 仅截距的逻辑回归

问题描述

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

谢谢

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

标签: pythonscikit-learnlogistic-regression

解决方案


我没有找到仅在截距上运行 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_)

推荐阅读