2

I have run the logistic regression on iris dataset. i am clear till this code. after this i want to form the equation to score the test data how to do that? i know i can use predict function to score the test however i want to see the parameters and respective weights. could you please help.

from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

iris = datasets.load_iris()

X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)

sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)

lr = LogisticRegression(C = 1000, random_state=0)
lr.fit(X_train_std, y_train)
print(lr.coef_)
print(lr.intercept_)

Thanks

Kumar AK
  • 987
  • 3
  • 10
  • 23
  • 3
    I am not sure what you're asking. You already have the coefficients. If you want the equation just plug it into the [logistic function](https://en.wikipedia.org/wiki/Logistic_regression#Logistic_function,_odds,_odds_ratio,_and_logit), but I'm not sure what additional information that would provide. – pault Apr 11 '18 at 18:32
  • *Possibly* telated: [Scikit Learn: Logistic Regression model coefficients: Clarification](https://stackoverflow.com/questions/18993867/scikit-learn-logistic-regression-model-coefficients-clarification) and [Interpreting multinomial logistic regression in scikit-learn](https://stats.stackexchange.com/questions/221622/interpreting-multinomial-logistic-regression-in-scikit-learn) – pault Apr 11 '18 at 18:34
  • If you want the equation just plug it into the logistic function -- how to do this? – Kumar AK Apr 11 '18 at 18:40
  • i have looked at the links i havent understand whats this sigmoid( dot([val1, val2], lr.coef_) + lr.intercept_ ) – Kumar AK Apr 11 '18 at 18:40
  • 2
    The logistic function, which returns the probability of success, is given by `p(x) = 1/(1 + exp(-(B0 + B1X1 + ... BnXn))`. `B0` is in intercept. `B1` through `Bn` are the coefficients. `X1` through `Xn` are the features. Read the wiki page linked for a more rigorous explanation. – pault Apr 11 '18 at 18:44
  • is there a way to dynamically code this? its tedious task if we have more variables. – Kumar AK Apr 11 '18 at 18:46
  • This is exactly what `predict_proba()` does. Why do you want to code it yourself? If you really want to implement it on your own, do some research into the sigmoid function so you can understand what `sigmoid( dot([val1, val2], lr.coef_) + lr.intercept_)` means. – pault Apr 11 '18 at 18:50

0 Answers0