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'
Logistic Regression - Iris dataset
ML
from sklearn.datasets import load_iris
= load_iris()
d = d['data'][:, :2]
X = d['target'] y
'feature_names'] d[
['sepal length (cm)',
'sepal width (cm)',
'petal length (cm)',
'petal width (cm)']
latexify()= ['blue', 'red', 'green']
colours = ['I. setosa', 'I. versicolor', 'I. virginica']
species for i in range(0, 3):
= X[y == i]
df_
plt.scatter( 0],
df_[:, 1],
df_[:, =colours[i],
color=0.5,
alpha=species[i] ,
label=10
s
)
format_axes(plt.gca())
plt.legend()'feature_names'][0])
plt.xlabel(d['feature_names'][1])
plt.ylabel(d[
"../figures/logistic-regression/logisitic-iris.pdf", bbox_inches="tight", transparent=True) plt.savefig(
= LogisticRegression(penalty='none',solver='newton-cg') clf
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.
LogisticRegression(penalty='none', solver='newton-cg')
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[:, 0].min() - 0.3, X[:, 0].max() + 0.3
x_min, x_max = X[:, 1].min() - 0.3, X[:, 1].max() + 0.3
y_min, y_max = 0.02
h = np.meshgrid(np.arange(x_min, x_max, h),
xx, yy
np.arange(y_min, y_max, h))
= clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z
# Put the result into a color plot
= Z.reshape(xx.shape)
Z =plt.cm.Paired, alpha=0.4)
plt.contourf(xx, yy, Z, cmap'equal')
plt.gca().set_aspect(#plt.scatter(X[:, 0], X[:, 1], c=y)
latexify()for i in range(0, 3):
= X[y == i]
df_
plt.scatter( 0],
df_[:, 1],
df_[:, =colours[i],
color=0.5,
alpha=species[i],
label=10
s
)
format_axes(plt.gca())
plt.legend()'feature_names'][0])
plt.xlabel(d['feature_names'][1])
plt.ylabel(d["../figures/logistic-regression/logisitic-iris-prediction.pdf", bbox_inches="tight", transparent=True) plt.savefig(