# Logistic Regression in Python

Home   »   Logistic Regression in Python

``````#import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn import datasets``````
``````#load the iris dataset
``````#Use sepal length and width attributes with corresponding output class
X = iris.data[:,:2]
Y = iris.target``````
``````#Fit the logistic regression function
logreg = LogisticRegression(C=1e5,solver = 'lbfgs',multi_class='multinomial')
logreg.fit(X,Y)``````
``````#Use logistic regression for fitting the data
x_min, x_max = X[:,0].min() - .5, X[:,0].max() + .5
y_min, y_max = X[:,1].min() - .5, X[:,1].max() + .5
h = .02 #step size in mesh
xx,yy = np.meshgrid(np.arange(x_min,x_max,h), np.arange(y_min,y_max, h))
Z = logreg.predict(np.c_[xx.ravel(), yy.ravel()])``````
``````# Display the output

x_min, x_max = X[:,0].min() - .5, X[:,0].max() + .5
y_min, y_max = X[:,1].min() - .5, X[:,1].max() + .5
h = .02 #step size in mesh
xx,yy = np.meshgrid(np.arange(x_min,x_max,h), np.arange(y_min,y_max, h))``````