Logistic Regression - Iris dataset

ML
Author

Nipun Batra

Published

January 1, 2024

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from latexify import *
from sklearn.linear_model import LogisticRegression
import matplotlib.patches as mpatches
%config InlineBackend.figure_format = 'retina'
from sklearn.datasets import load_iris
d = load_iris()
X = d['data'][:, :2]
y = d['target']
d['feature_names']
['sepal length (cm)',
 'sepal width (cm)',
 'petal length (cm)',
 'petal width (cm)']
latexify()
colours = ['blue', 'red', 'green']
species = ['I. setosa', 'I. versicolor', 'I. virginica']
for i in range(0, 3):    
    df_ = X[y == i]
    plt.scatter(        
        df_[:, 0],        
        df_[:, 1],
        color=colours[i],        
        alpha=0.5,        
        label=species[i] ,
        s=10
    )
format_axes(plt.gca())
plt.legend()
plt.xlabel(d['feature_names'][0])
plt.ylabel(d['feature_names'][1])


plt.savefig("../figures/logistic-regression/logisitic-iris.pdf", bbox_inches="tight", transparent=True)

clf = LogisticRegression(penalty='none',solver='newton-cg')
clf.fit(X, y)
/home/nipun.batra/miniforge3/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:1183: FutureWarning: `penalty='none'`has been deprecated in 1.2 and will be removed in 1.4. To keep the past behaviour, set `penalty=None`.
  warnings.warn(
LogisticRegression(penalty='none', solver='newton-cg')
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
clf.coef_
array([[-55.82562338,  47.29592374],
       [ 26.96162409, -23.85029157],
       [ 28.86399931, -23.44563218]])
X.shape
(150, 2)
y.shape
(150,)
# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 0.3, X[:, 0].max() + 0.3
y_min, y_max = X[:, 1].min() - 0.3, X[:, 1].max() + 0.3
h = 0.02
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))





Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.4)
plt.gca().set_aspect('equal')
#plt.scatter(X[:, 0], X[:, 1], c=y)
latexify()
for i in range(0, 3):    
    df_ = X[y == i]
    plt.scatter(        
        df_[:, 0],        
        df_[:, 1],
        color=colours[i],        
        alpha=0.5,        
        label=species[i],
        s=10
    )
format_axes(plt.gca())
plt.legend()
plt.xlabel(d['feature_names'][0])
plt.ylabel(d['feature_names'][1])
plt.savefig("../figures/logistic-regression/logisitic-iris-prediction.pdf", bbox_inches="tight", transparent=True)