逻辑回归 - 如何拟合具有多个特征的模型并显示系数

问题描述

我拟合了具有 1 或 2 个特征的逻辑回归:

X = df[["decile_score","age"]]
X_train,X_test,y_train,y_test = model_selection.train_test_split(
    X,y,test_size=0.20,random_state=100
)

logistic_age_model = linear_model.LogisticRegression()
logistic_age_model.fit(X_train,y_train)

beta_0 = logistic_age_model.intercept_[0]
beta_1,beta_2 = logistic_age_model.coef_[0]

print(f"Fit model: p(recid) = L({beta_0:.4f} + {beta_1:.4f} decile_score + {beta_2:.4f} age)") 

我有超过 2 个特征(例如 15 个),如何编写拟合模型以查看更改?

例如

拟合模型:p(recid) = L(-0.8480 + 0.2475 decile_score + -0.0135 age) 我想看看每 15 个特征将如何影响结果。

我是否需要为每个系数声明一个 beta,如果是这样,我该怎么做?

解决方法

我认为您正在寻找一种更有效的方法来打印许多变量的 Logistic 公式。

# Initialize model
logistic_age_model = LogisticRegression()
# Fit model (X now has 15 features)
logistic_age_model.fit(X,y)

# List of coeficient values
coefs = np.union1d(logistic_age_model.intercept_,logistic_age_model.coef_[0]).tolist()
# List of names
betas = ['beta_' + str(i) for i in range(len(coefs))]
# Combine `coefs` & `betas` to form a dictionary
d = dict(zip(betas,coefs))
# Print as formula
print('L(' + str(d) + ')')