Author - Mohit Rathore mrmohitrathoremr@gmail.com - markroxor.in
Licensed under The MIT License - https://opensource.org/licenses/MIT
from fromscratchtoml import svm
import numpy as np
from fromscratchtoml.toolbox import binary_visualize
from fromscratchtoml.toolbox.random import Distribution
%matplotlib inline
import sys
print(sys.version)
X1 = Distribution.linear(pts=100,
mean=[8, 10],
covr=[[1.5, 1], [1, 1.5]])
X2 = Distribution.linear(pts=100,
mean=[9, 5],
covr=[[1.5, 1], [1, 1.5]])
Y1 = np.ones(X1.shape[0])
Y2 = 2*np.ones(X2.shape[0])
X = np.vstack((X1, X2))
y = np.hstack((Y1, Y2))
y.shape, X.shape
clf_lin = svm.SVC(kernel='linear')
clf_lin.fit(X, y)
X1 = Distribution.linear(pts=10,
mean=[8, 10],
covr=[[1.5, 1], [1, 1.5]])
X2 = Distribution.linear(pts=10,
mean=[9, 5],
covr=[[1.5, 1], [1, 1.5]])
X = np.vstack((X1, X2))
binary_visualize(X, clf=clf_lin, draw_contour=True)
X1 = Distribution.linear(pts=100,
mean=[8, 10],
covr=[[1.5, 1], [1, 1.5]])
X2 = Distribution.linear(pts=100,
mean=[9, 8],
covr=[[1.5, 1], [1, 1.5]])
Y1 = np.ones(X1.shape[0])
Y2 = 2*np.ones(X2.shape[0])
X = np.vstack((X1, X2))
y = np.hstack((Y1, Y2))
clf_lin = svm.SVC(kernel='linear')
clf_lin.fit(X, y)
X1 = Distribution.linear(pts=100,
mean=[8, 10],
covr=[[1.5, 1], [1, 1.5]])
X2 = Distribution.linear(pts=100,
mean=[9, 8],
covr=[[1.5, 1], [1, 1.5]])
X = np.vstack((X1, X2))
binary_visualize(X, clf=clf_lin, draw_contour=True)
X1 = np.array([[10,10],[6,6],[6,11],[3,15],[12,6],[9,5],[16,3],[11,5]])
X2 = np.array([[3,6],[6,3],[2,9],[9,2],[18,1],[1,18],[1,13],[13,1]])
Y1 = np.ones(X1.shape[0])
Y2 = 2*np.ones(X1.shape[0])
X = np.vstack((X1, X2))
y = np.hstack((Y1, Y2))
X
y
clf = svm.SVC(kernel='polynomial', const=0, degree=2)
clf.fit(X, y)
binary_visualize(X, clf=clf, draw_contour=True)
X1 = Distribution.radial_binary(pts=30,
mean=[0, 0],
st=1,
ed=2, seed=20)
X2 = Distribution.radial_binary(pts=30,
mean=[0, 0],
st=4,
ed=5, seed=20)
Y1 = np.ones(X1.shape[0])
Y2 = 2*np.ones(X1.shape[0])
X = np.vstack((X1, X2))
y = np.hstack((Y1, Y2))
clf = svm.SVC(kernel='rbf', gamma=0.2)
clf.fit(X, y)
binary_visualize(X, clf=clf, draw_contour=True)
clf = svm.SVC(kernel='rbf', gamma=10)
clf.fit(X, y)
binary_visualize(X, clf=clf, draw_contour=True)
X1 = Distribution.linear(pts=100,
mean=[6, 10],
covr=[[1.5, 1], [1, 1.5]])
X2 = Distribution.linear(pts=100,
mean=[9, 5],
covr=[[1.5, 1], [1, 1.5]])
X3 = Distribution.linear(pts=100,
mean=[-9, -5],
covr=[[1.5, 1], [1, 1.5]])
X4 = Distribution.linear(pts=100,
mean=[-6, -10],
covr=[[1.5, 1], [1, 1.5]])
Y1 = -1*np.ones(X1.shape[0])
Y2 = 1*np.ones(X2.shape[0])
Y3 = 2*np.ones(X3.shape[0])
Y4 = 30*np.ones(X4.shape[0])
X = np.vstack((X1, X2, X3 ,X4))
y = np.hstack((Y1, Y2, Y3, Y4))
clf = svm.SVC(kernel='linear')
classifiers=clf.fit(X, y)
clf.predict(np.array([0, 0]))
X1 = Distribution.linear(pts=30,
mean=[6, 10],
covr=[[1.5, 1], [1, 1.5]])
X2 = Distribution.linear(pts=30,
mean=[9, 5],
covr=[[1.5, 1], [1, 1.5]])
X3 = Distribution.linear(pts=30,
mean=[-9, -5],
covr=[[1.5, 1], [1, 1.5]])
X4 = Distribution.linear(pts=30,
mean=[-6, -10],
covr=[[1.5, 1], [1, 1.5]])
Y1 = -np.ones(X1.shape[0])
Y2 = 1*np.ones(X2.shape[0])
Y3 = 2*np.ones(X3.shape[0])
Y4 = 30*np.ones(X4.shape[0])
X = np.vstack((X1, X2, X3 ,X4))
y = np.hstack((Y1, Y2, Y3, Y4))
clf.predict(np.array([0,0]))
binary_visualize(X, clf=clf, draw_contour=True)
X1 = Distribution.radial_binary(pts=50,
mean=[0, 0],
st=1,
ed=2,seed=100)
X3 = Distribution.radial_binary(pts=50,
mean=[0, 0],
st=5,
ed=10,seed=100)
X4 = Distribution.radial_binary(pts=50,
mean=[0, 0],
st=8,
ed=9,seed=100)
Y1 = -np.ones(X1.shape[0])
Y3 = 2*np.ones(X3.shape[0])
Y4 = 3000*np.ones(X4.shape[0])
X = np.vstack([X1, X3, X4])
y = np.hstack((Y1, Y3, Y4))
clf = svm.SVC(kernel='rbf', gamma=10)
clf.fit(X, y)
X1 = Distribution.radial_binary(pts=30,
mean=[0, 0],
st=1,
ed=2,seed=100)
X3 = Distribution.radial_binary(pts=30,
mean=[0, 0],
st=5,
ed=10,seed=100)
X4 = Distribution.radial_binary(pts=30,
mean=[0, 0],
st=8,
ed=9,seed=100)
Y1 = -np.ones(X1.shape[0])
Y2 = np.ones(X2.shape[0])
Y3 = 2*np.ones(X3.shape[0])
Y4 = 3000*np.ones(X4.shape[0])
X = np.vstack((X1, X2, X3, X4))
y = np.hstack((Y1, Y2, Y3, Y4))
binary_visualize(X, clf=clf, draw_contour=True)