Reputation: 27
I fit a logistic regression with 1 or 2 features:
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)")
I have more than 2 features (15 for example), how can I write the fit model to see the change?
For example
Fit model: p(recid) = L(-0.8480 + 0.2475 decile_score + -0.0135 age) I want to see how each 15 feature will be affect the result.
Do I need to declare a beta for each coefficient, and if that's the case, how can I do it?
Upvotes: 1
Views: 794
Reputation: 6333
I think you are looking for a more efficient way to print the Logistic formula for many variables.
# 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) + ')')
Upvotes: 1