You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
462 KiB
462 KiB
None
<html>
<head>
</head>
</html>
In [2]:
from sklearn import datasets
iris = datasets.load_iris()
list(iris.keys())
Out[2]:
In [3]:
iris
Out[3]:
In [3]:
print(iris.DESCR)
In [4]:
import matplotlib.pyplot as plt
import numpy as np
In [5]:
lp=iris.data[:,0:1]
wp=iris.data[:,1:2]
plt.plot(lp,wp,'.k')
Out[5]:
In [6]:
plt.plot(lp,'.k')
Out[6]:
In [7]:
iris.target
Out[7]:
In [8]:
import numpy as np
import matplotlib.pyplot as plt
t=np.linspace(-100, 100, 1000)
sig=1/(1+np.exp(-t+3))
plt.plot(t,sig, '.b', label=r"$\sigma=\frac{1}{1+e^{-t}}$")
plt.legend(loc='upper left', fontsize=20)
plt.axis([-5, 5, -0.1, 1])
plt.plot([0,0], [0,1], 'r--')
Out[8]:
Iris-Setosa Classifier based on petal width¶
In [9]:
X=iris.data[:,1:2]
y=(iris.target==0).astype(int)
In [10]:
from sklearn.linear_model import LogisticRegression
mylr=LogisticRegression(solver='lbfgs', random_state=42)
mylr.fit(X,y)
Out[10]:
In [11]:
Xnew=np.linspace(2,5,70).reshape(-1,1)
yPred=mylr.predict_proba(Xnew)
plt.plot(Xnew,yPred[:,0],'.r', label='No Iris-Set')
plt.plot(Xnew,yPred[:,1],'.b', label='Iris-Set')
plt.legend()
plt.plot(X,y,'*g')
Out[11]:
In [ ]:
In [12]:
# Iris-Setosa Classifier based on petal width
X=iris.data[:,0:1]
y=(iris.target==0).astype(int)
from sklearn.linear_model import LogisticRegression
mylr=LogisticRegression(solver='lbfgs', random_state=42)
mylr.fit(X,y)
Xnew=np.linspace(2,8,70).reshape(-1,1)
yPred=mylr.predict_proba(Xnew)
#plt.plot(Xnew,yPred[:,0],'.r', label='No Iris-Set')
plt.plot(Xnew,yPred[:,1],'.b', label='Iris-Set')
plt.legend()
plt.plot(X,y,'*g')
Out[12]:
In [4]:
import matplotlib.pyplot as plt
pl=iris.data[:,2:3]
pw=iris.data[:,3:]
tg = iris.target
plt.plot(pl[tg==0,0],pw[tg==0,0],'.r',label='Set')
plt.plot(pl[tg==1,0],pw[tg==1,0],'.b',label='Ver')
plt.plot(pl[tg==2,0],pw[tg==2,0],'.g',label='Vir')
plt.legend()
plt.show()
In [53]:
from sklearn.linear_model import LogisticRegression
X=iris.data[:,2:]
y=(iris.target==2).astype(int)
lrvir=LogisticRegression(random_state=42, tol=1e-5, C=10, max_iter=100, solver='newton-cg')
lrvir.fit(X,y)
Out[53]:
In [54]:
import numpy as np
x0, x1=np.meshgrid(
np.linspace(1,6.9,500).reshape(-1,1),
np.linspace(0.1,2.5,200).reshape(-1,1))
Xnew=np.c_[x0.ravel(), x1.ravel()]
yProb=lrvir.predict_proba(Xnew)
In [55]:
plt.figure(figsize=(10,4))
plt.plot(X[y==0,0], X[y==0,1],'bs',label='No Virg')
plt.plot(X[y==1,0], X[y==1,1],'g^',label='Virginica')
zz=yProb[:,1].reshape(x0.shape)
contour=plt.contour(x0,x1,zz)
plt.clabel(contour, inline=1,fontsize=15)
plt.xlabel("Petal Length")
plt.ylabel("Petal Width")
plt.legend()
plt.show()
Multiple features and multiclass classifier (Virginica)¶
In [81]:
X=iris.data[:,2:]
y=iris.target
In [82]:
lrmfmc=LogisticRegression(multi_class='multinomial',
solver='lbfgs',
C=100,
random_state=42)
lrmfmc.fit(X,y)
x0,x1=np.meshgrid(
np.linspace(0,8,500).reshape(-1,1),
np.linspace(0,3.5,200).reshape(-1,1))
Xnew=np.c_[x0.ravel(), x1.ravel()]
yProba=lrmfmc.predict_proba(Xnew)
#yPred=lrmfmc.predict(Xnew)
from matplotlib.colors import ListedColormap
#customc=ListedColormap(['#fafab0', '#9898ff', '#a0faa0'])
zz=yProba[:,1].reshape(x0.shape)
#zz=yPred.reshape(x0.shape)
plt.figure(figsize=(10,4))
plt.plot(X[y==2,0], X[y==2,1],'g^',label='Virg')
plt.plot(X[y==1,0], X[y==1,1],'bs',label='Versic')
plt.plot(X[y==0,0], X[y==0,1],'yo',label='Setos')
contour=plt.contour(x0,x1,zz,cmap=plt.cm.brg)
plt.clabel(contour,inline=1,fontsize=12)
#plt.contourf(x0,x1,zz,cmap=customc)
Out[82]:
In [83]:
yPred=lrmfmc.predict(Xnew)
zPred=yPred.reshape(x0.shape)
plt.contourf(x0,x1,zPred,alpha=0.4)
plt.plot(X[y==2,0], X[y==2,1],'g^',label='Virg')
plt.plot(X[y==1,0], X[y==1,1],'bs',label='Versic')
plt.plot(X[y==0,0], X[y==0,1],'yo',label='Setos')
plt.show()
Homework: Sepal multi-features and multi-class¶
In [67]:
X=iris.data[:,0:2]
y=iris.target
mlr3=LogisticRegression(
multi_class='multinomial',
solver='lbfgs',
C=10,
random_state=42)
mlr3.fit(X,y)
Out[67]:
In [68]:
import numpy as np
x0,x1=np.meshgrid(
np.linspace(0,9,500).reshape(-1,1),
np.linspace(0,7,200).reshape(-1,1))
In [69]:
Xnew=np.c_[x0.ravel(), x1.ravel()]
yPred=mlr3.predict_proba(Xnew)
yProba=mlr3.predict(Xnew)
zz=yProba.reshape(x0.shape)
zz1=yPred[:,1].reshape(x0.shape)
In [75]:
from matplotlib.colors import ListedColormap
plt.plot(X[y==2,0], X[y==2,1],'y*', label='Virg')
plt.plot(X[y==1,0], X[y==1,1],'g^', label='Vers')
plt.plot(X[y==0,0], X[y==0,1],'bs', label='Set')
contour=plt.contour(x0,x1,zz1, cmap=plt.cm.brg)
ccmap=ListedColormap(['#fafab0', '#9898ff', '#a0faa0'])
plt.contourf(x0,x1,zz,cmap=ccmap)
plt.clabel(contour, inline=1,fontsize=12)
plt.xlabel('Sepal Width', fontsize=12)
plt.ylabel('Sepal Length', fontsize=12)
plt.axis([4,8.5,1,5])
plt.legend()
Out[75]:
In [ ]:
In [ ]: