python - 仅截距的逻辑回归
问题描述
我需要用sklearn拟合逻辑回归,但是没有x向量,只有带有截距的模型,怎么办?我找不到任何可行的解决方案。
谢谢
编辑:我想在 sklearn 中为 R 的回归 y ~ 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_)
推荐阅读
- php - 如何覆盖 magento 2 愿望清单模型?
- json - 我们如何以原始顺序从 json 中获取密钥?
- reactjs - TypeError: Cannot read property 'value' of undefined 使用 Redux 向数组添加值时
- python - 我们如何在 jinja2/weasyprint python 中传递图像变量
- discord.net - 命令权限 Discord.net
- r - 将一系列 CSV 文件读入 R、运行函数、将输出写入新文件
- linux - 如何在 putty 上更改 Linux 机器监听端口?
- protractor - TypeError: protractorImageComparison 不是构造函数
- android - onActivityResult 不显示图像
- php - Docusign 嵌入模板编辑视图