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.
703 KiB
703 KiB
None
<html>
<head>
</head>
</html>
In [1]:
from sklearn import datasets
iris = datasets.load_iris()
print(iris.DESCR)
In [2]:
import matplotlib.pyplot as plt
import numpy as np
In [3]:
sl = iris.data[:,0:1]
sw = iris.data[:,1:2]
plt.plot(sl,sw, '.k')
plt.show()
In [4]:
iris.target
Out[4]:
Decision boundaries¶
In [5]:
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(-10,10, 100)
sig = 1/(1+np.exp(-t-4))
plt.plot(t,sig, '.b', label=r"$\sigma$")
plt.legend(loc='upper left', fontsize =20)
plt.show()
Iris-Setosa Classifier based on petal width¶
In [6]:
X = iris.data[:,3:4]
y = (iris.target == 0).astype(int)
In [7]:
from sklearn.linear_model import LogisticRegression
mylr = LogisticRegression(solver='lbfgs', random_state=42)
mylr.fit(X,y)
Out[7]:
In [8]:
Xnew = np.linspace(-1,3,100).reshape(-1,1)
yPred = mylr.predict_proba(Xnew)
#plt.plot(Xnew,yPred[:,0], label='No Iris')
plt.plot(Xnew,yPred[:,1], label='Yes Iris')
plt.legend()
plt.plot(X,y,'og')
plt.show()
Iris-Setosa petal length¶
In [9]:
X = iris.data[:,2:3]
y = (iris.target == 0).astype(int)
from sklearn.linear_model import LogisticRegression
mylr = LogisticRegression(solver='lbfgs', random_state=42)
mylr.fit(X,y)
Out[9]:
In [10]:
Xnew = np.linspace(0,8,100).reshape(-1,1)
yPred = mylr.predict_proba(Xnew)
#plt.plot(Xnew,yPred[:,0], label='No Iris')
plt.plot(Xnew,yPred[:,1], label='Yes Iris')
plt.legend()
plt.plot(X,y,'og')
plt.axis([1.5, 5, -0.1, 1.1])
plt.show()
Iris-Setosa Sepal-Length¶
In [11]:
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)
Out[11]:
In [12]:
Xnew = np.linspace(0,8,100).reshape(-1,1)
yPred = mylr.predict_proba(Xnew)
#plt.plot(Xnew,yPred[:,0], label='No Iris')
plt.plot(Xnew,yPred[:,1], label='Yes Iris')
plt.legend()
plt.plot(X,y,'og')
plt.axis([3.5, 7, -0.1, 1.1])
plt.show()
Multiple features classifier¶
In [13]:
import matplotlib.pyplot as plt
sl = iris.data[:,0:1]
sw = iris.data[:,1:2]
tg = iris.target
plt.plot(sl[tg==0,0], sw[tg==0,0],'.g' ,label='Set')
plt.plot(sl[tg==1,0], sw[tg==1,0],'.r', label='Ver')
plt.plot(sl[tg==2,0], sw[tg==2,0],'.b', label='Vir')
plt.legend()
plt.show()
In [30]:
from sklearn.linear_model import LogisticRegression
X = iris.data[:,0:2]
y = (iris.target==2).astype(int)
In [47]:
mylrvir = LogisticRegression(
random_state=22,
tol=1e-5,
C=100,
max_iter=100,
solver='newton-cg'
)
mylrvir.fit(X,y)
Out[47]:
In [48]:
import numpy as np
x0, x1 = np.meshgrid(
np.linspace(3,8,100).reshape(-1,1),
np.linspace(0,6,100).reshape(-1,1)
)
Xnew = np.c_[x0.ravel(), x1.ravel()]
yPred = mylrvir.predict_proba(Xnew)
In [49]:
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=yPred[:,1].reshape(x0.shape)
contour=plt.contour(x0,x1,zz)
plt.clabel(contour, inline=1,fontsize=15)
plt.xlabel("Sepal Length")
plt.ylabel("Sepal Width")
plt.legend()
plt.show()
In [59]:
fig, ax =plt.subplots(subplot_kw={"projection": "3d"})
surf = ax.plot_surface(x0,x1,zz, cmap='jet')
ax.scatter(iris.data[:,0:1], iris.data[:,1:2], y, 'or')
Out[59]:
Multiple features and muticlass classifier¶
In [95]:
X = iris.data[:,0:2]
y = iris.target
lrmc = LogisticRegression(
multi_class='multinomial',
solver='lbfgs',
C=100,
random_state=22
)
lrmc.fit(X,y)
Out[95]:
In [96]:
y
Out[96]:
In [99]:
import numpy as np
x0, x1 = np.meshgrid(
np.linspace(3,8,100).reshape(-1,1),
np.linspace(0,6,100).reshape(-1,1)
)
Xnew = np.c_[x0.ravel(), x1.ravel()]
yPred = lrmc.predict_proba(Xnew)
In [100]:
plt.figure(figsize=(10,4))
plt.plot(X[y==0,0], X[y==0,1],'.b',label='Setosa')
plt.plot(X[y==1,0], X[y==1,1],'+g',label='Versi')
plt.plot(X[y==2,0], X[y==2,1],'*m',label='Virgi')
zz=yPred[:,1].reshape(x0.shape)
contour=plt.contour(x0,x1,zz)
plt.clabel(contour, inline=1,fontsize=15)
plt.xlabel("Sepal Length")
plt.ylabel("Sepal Width")
plt.legend()
plt.show()
In [84]:
yPred = lrmc.predict(Xnew)
plt.figure(figsize=(10,6))
plt.plot(X[y==0,0], X[y==0,1],'bs',label='Setosa')
plt.plot(X[y==1,0], X[y==1,1],'g^',label='Versi')
plt.plot(X[y==2,0], X[y==2,1],'*m',label='Virgi')
zz=yPred.reshape(x0.shape)
contour=plt.contourf(x0,x1,zz, cmap='jet', alpha=0.3)
plt.clabel(contour, inline=1,fontsize=15)
plt.xlabel("Sepal Length")
plt.ylabel("Sepal Width")
plt.legend()
plt.show()
In [78]:
fig, ax =plt.subplots(subplot_kw={"projection": "3d"})
surf = ax.plot_surface(x0,x1,zz, cmap='jet')
#ax.scatter(iris.data[:,0:1], iris.data[:,1:2], y, 'or')
In [ ]: