Logistic Regression with Sklearn

Python

Here we initialise and train a Logistic Regression model before using the model to make predictions. Finally we find the intercept and cofficients of the model along with analysing the models performance on test data using the Classification Report.

 1|  #Import Library
 2|  from sklearn.linear_model import LogisticRegression
 3|  
 4|  #Initialise & Train Model
 5|  model = LogisticRegression()
 6|  model.fit(X_train, y_train)
 7|  
 8|  #Use Model to Make Predictions
 9|  y_pred = model.predict(X_test)
10|  
11|  #Get the Model Intercept & Coefficients
12|  print('Intercept:',model.intercept_)
13|  coef = pd.DataFrame(model.coef_.reshape(30,1), cols, columns=['Coefficients'])
14|  print(coef)
15|  
16|  #Get the Classification Report
17|  from sklearn.metrics import classification_report
18|  print(classification_report(y_test, y_pred))
Did you find this snippet useful?

Sign up for free to to add this to your code library