import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVC
X = np.random.uniform(0,2,[150,2])
z = (X[:,0]**2+X[:,1]**2)>2
svc = SVC()
svc.fit(X,z)
plt.axes(aspect=1,xlim=[0,2],ylim=[0,2])
plt.scatter(X[:,0],X[:,1],s=50,c=z,edgecolor='k',cmap='winter')
mx,my = np.meshgrid(np.linspace(0,2,200),np.linspace(0,2,200))
mX = np.stack([mx.ravel(),my.ravel()],1)
mz = svc.predict(mX).reshape(200,200)
plt.contourf(mx,my,mz,alpha=0.1,cmap='winter')
plt.show()
from sklearn import datasets
X,z = datasets.make_blobs(n_samples=100,n_features=2,cluster_std=4,centers=2,random_state=3)
mx,my = np.meshgrid(np.linspace(X[:,0].min(),X[:,0].max(),200),np.linspace(X[:,1].min(),X[:,1].max(),200))
mX = np.stack([mx.ravel(),my.ravel()],1)
plt.figure(figsize=[6,7])
kernel = ['linear','poly','rbf','sigmoid']
for i in range(4):
svc = SVC(kernel=kernel[i])
svc.fit(X,z)
mz = svc.predict(mX).reshape(200,200)
plt.subplot(2,2,i+1,aspect=1,xlim=[X[:,0].min(),X[:,0].max()],ylim=[X[:,1].min(),X[:,1].max()])
plt.scatter(X[:,0],X[:,1],s=50,c=z,edgecolor='k',cmap='spring')
plt.contourf(mx,my,mz,alpha=0.1,cmap='spring')
plt.title(kernel[i])
plt.tight_layout()
plt.show()
X,z = datasets.make_moons(n_samples=80,shuffle=0,noise=0.25,random_state=0)
mx,my = np.meshgrid(np.linspace(X[:,0].min(),X[:,0].max(),200),np.linspace(X[:,1].min(),X[:,1].max(),200))
mX = np.stack([mx.ravel(),my.ravel()],1)
plt.figure(figsize=[6.5,4.5])
for i,C in enumerate([1,10,100]):
for j,gamma in enumerate([0.1,1,10]):
svc = SVC(C=C,gamma=gamma)
svc.fit(X,z)
mz = svc.predict(mX).reshape(200,200)
plt.subplot2grid((3,3),(i,j),xlim=[X[:,0].min(),X[:,0].max()],ylim=[X[:,1].min(),X[:,1].max()],xticks=[],yticks=[],aspect=1)
plt.scatter(X[:,0],X[:,1],s=10,c=z,edgecolor='k',cmap='rainbow')
plt.contourf(mx,my,mz,alpha=0.1,cmap='rainbow')
plt.title('C=%.1f,$\\gamma$=%.1f'%(C,gamma),size=8)
plt.tight_layout()
plt.show()
from sklearn.model_selection import validation_curve
X,z = datasets.make_moons(n_samples=80,shuffle=0,noise=0.2,random_state=0)
ccc = 10**np.linspace(-5,5,41)
khanaen_fuek,khanaen_truat = validation_curve(SVC(gamma=1),X,z,param_name='C',param_range=ccc,cv=5)
plt.figure(figsize=[4.5,6])
plt.subplot(211,aspect=1)
plt.scatter(X[:,0],X[:,1],c=z,edgecolor='k',cmap='rainbow')
plt.subplot(212,xscale='log',xlabel='C')
plt.plot(ccc,np.mean(khanaen_fuek,1),color='#992255')
plt.plot(ccc,np.mean(khanaen_truat,1),color='#448965')
plt.legend([u'ฝึกฝน',u'ตรวจสอบ'],prop={'family':'Tahoma'})
plt.show()
X,z = datasets.make_moons(n_samples=120,shuffle=0,noise=0.2,random_state=2)
svc = SVC(C=1e2,gamma=1)
svc.fit(X,z)
plt.axes(aspect=1)
mX = np.stack(np.meshgrid(np.linspace(-1.5,2.5,200),np.linspace(-1.5,2,200)),2)
mz = svc.decision_function(mX.reshape(-1,2)).reshape(200,-1)
vmax = np.abs(mz).max()
plt.contourf(mX[:,:,0],mX[:,:,1],mz,200,alpha=0.5,cmap='Spectral',vmin=-vmax,vmax=vmax)
plt.colorbar(pad=0.01)
plt.contour(mX[:,:,0],mX[:,:,1],mz,[0],linestyles='--')
plt.scatter(X[:,0],X[:,1],c=z,edgecolor='k',cmap='Spectral')
plt.show()
X,y = datasets.make_moons(n_samples=80,shuffle=0,noise=0.2,random_state=0)
y[60:] = 2
y[:20] = 3
svc = SVC(C=1e4)
svc.fit(X,y)
mx,my = np.meshgrid(np.linspace(X[:,0].min(),X[:,0].max(),200),np.linspace(X[:,1].min(),X[:,1].max(),200))
mX = np.stack([mx.ravel(),my.ravel()],1)
mz = svc.predict(mX).reshape(200,200)
plt.axes(aspect=1,xlim=[X[:,0].min(),X[:,0].max()],ylim=[X[:,1].min(),X[:,1].max()])
plt.scatter(X[:,0],X[:,1],c=y,edgecolor='k',cmap='cool')
plt.contourf(mx,my,mz,alpha=0.1,cmap='cool')
plt.show()
plt.figure(figsize=[7,4])
df = svc.decision_function(mX).reshape(200,200,-1)
for i in range(4):
plt.subplot(2,2,i+1,aspect=1,xlim=[X[:,0].min(),X[:,0].max()],ylim=[X[:,1].min(),X[:,1].max()])
plt.scatter(X[:,0],X[:,1],c=y,edgecolor='k',cmap='cool')
plt.contourf(mx,my,df[:,:,i],100,cmap='hot',zorder=0)
plt.colorbar(pad=0.01)
plt.contour(mx,my,df[:,:,i],[0],cmap='hot',zorder=0.2)
plt.title(u'%d <> %d,%d,%d'%tuple([i]+list(set(range(4))-{i})),size=11)
plt.tight_layout()
plt.show()
svc.decision_function_shape = 'ovo'
plt.figure(figsize=[7,7])
df = svc.decision_function(mX).reshape(200,200,-1)
k = [(i,j) for i in range(4) for j in range(4) if i<j]
for i in range(6):
plt.subplot(3,2,i+1,aspect=1,xlim=[X[:,0].min(),X[:,0].max()],ylim=[X[:,1].min(),X[:,1].max()])
s = np.maximum(y==k[i][0],y==k[i][1])
plt.scatter(X[s,0],X[s,1],c=y[s],edgecolor='k',cmap='cool',vmin=0,vmax=3)
vmax = np.abs(df[:,:,i]).max()
plt.contourf(mx,my,df[:,:,i],100,cmap='PuOr',zorder=0,vmin=-vmax,vmax=vmax)
plt.colorbar()
plt.contour(mx,my,df[:,:,i],[0],cmap='hot',zorder=0.1)
plt.title('%d<>%d'%k[i],size=11)
plt.tight_layout()
plt.show()
X,z = datasets.make_blobs(n_samples=1000,n_features=2,cluster_std=1,centers=5,random_state=20)
svc = SVC()
svc.fit(X,z)
print(svc.support_)
print(svc.support_vectors_)
print(svc.dual_coef_)
x_sv = svc.support_vectors_[:,0]
y_sv = svc.support_vectors_[:,1]
s_sv = np.abs(svc.dual_coef_)
s_sv = s_sv/s_sv.max()*400
c_sv = z[svc.support_]
plt.axes(aspect=1,xlim=[X[:,0].min(),X[:,0].max()],ylim=[X[:,1].min(),X[:,1].max()])
plt.scatter(x_sv,y_sv,s=s_sv,c=c_sv,edgecolor='k',marker='*',cmap='rainbow')
plt.scatter(X[:,0],X[:,1],s=10,c=z,edgecolor='k',alpha=0.5,cmap='rainbow')
mx,my = np.meshgrid(np.linspace(X[:,0].min(),X[:,0].max(),200),np.linspace(X[:,1].min(),X[:,1].max(),200))
mX = np.stack([mx.ravel(),my.ravel()],1)
mz = svc.predict(mX).reshape(200,200)
plt.contourf(mx,my,mz,alpha=0.1,cmap='rainbow')
plt.show()
ติดตามอัปเดตของบล็อกได้ที่แฟนเพจ