def test_poly_kernel(self): # Tests polynomial kernel of svc. X1 = Distribution.linear(pts=50, mean=[8, 20], covr=[[1.5, 1], [1, 2]], seed=100) X2 = Distribution.linear(pts=50, mean=[8, 15], covr=[[1.5, -1], [-1, 2]], seed=100) X3 = Distribution.linear(pts=50, mean=[15, 20], covr=[[1.5, 1], [1, 2]], seed=100) X4 = Distribution.linear(pts=50, mean=[15, 15], covr=[[1.5, -1], [-1, 2]], seed=100) X1 = np.vstack((X1, X2)) X2 = np.vstack((X3, X4)) Y1 = np.ones(X1.shape[0]) Y2 = -np.ones(X2.shape[0]) X_train = np.vstack((X1, X2)) y_train = np.hstack((Y1, Y2)) clf = svm.SVC(kernel='polynomial', const=1, degree=2) clf.fit(X_train, y_train) X1 = Distribution.linear(pts=5, mean=[8, 20], covr=[[1.5, 1], [1, 2]], seed=100) X2 = Distribution.linear(pts=5, mean=[8, 15], covr=[[1.5, -1], [-1, 2]], seed=100) X3 = Distribution.linear(pts=5, mean=[15, 20], covr=[[1.5, 1], [1, 2]], seed=100) X4 = Distribution.linear(pts=5, mean=[15, 15], covr=[[1.5, -1], [-1, 2]], seed=100) X1 = np.vstack((X1, X2)) X2 = np.vstack((X3, X4)) Y1 = np.ones(X1.shape[0]) Y2 = -np.ones(X2.shape[0]) X_test = np.vstack((X1, X2)) y_test = np.hstack((Y1, Y2)) predictions, projections = clf.predict(X_test, return_projection=True) expected_projections = np.array([ 1.2630574, 1.3302442, 1.502788, 1.2003369, 1.4567516, 1.0555044, 1.434326, 1.4227715, 1.1069533, 1.104987, -1.6992458, -1.5001097, -1.0005158, -1.8284273, -1.0863144, -2.238042, -1.2274336, -1.2235101, -2.1250129, -2.0870237 ]) expected_projections = np.array([ 1.9282368, 4.1053743, 4.449601, 2.8149981, 3.337817, 1.5934888, 4.237419, 3.699658, 3.8548565, 2.8402433, -6.7378554, -2.9163127, -2.5978136, -4.833237, -4.421687, -5.2333884, -2.2744238, -3.0598483, -2.4422958, -3.890006 ], ) self.assertTrue(np.allclose(projections, expected_projections)) self.assertTrue(np.allclose(predictions, y_test))
def test_multiclass(self): X1 = Distribution.radial_binary(pts=10, mean=[0, 0], st=1, ed=2, seed=100) X2 = Distribution.radial_binary(pts=10, mean=[0, 0], st=4, ed=5, seed=100) X3 = Distribution.radial_binary(pts=10, mean=[0, 0], st=6, ed=7, seed=100) X4 = Distribution.radial_binary(pts=10, 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_train = np.vstack((X1, X2, X3, X4)) y_train = np.hstack((Y1, Y2, Y3, Y4)) clf = svm.SVC(kernel='rbf', gamma=10) clf.fit(X_train, y_train) X1 = Distribution.radial_binary(pts=10, mean=[0, 0], st=1, ed=2, seed=100) X2 = Distribution.radial_binary(pts=10, mean=[0, 0], st=4, ed=5, seed=100) X3 = Distribution.radial_binary(pts=10, mean=[0, 0], st=6, ed=7, seed=100) X4 = Distribution.radial_binary(pts=10, mean=[0, 0], st=8, ed=9, seed=100) X_test = np.vstack((X1, X2, X3, X4)) _, projections = clf.predict(X_test, return_projection=True) expected_projections = np.array([ 1.23564788, 1.15519477, 1.32441802, 1.04496554, 1.29740627, 0., 1.25561797, 1.22925452, 0., 1.11920321, 0.2991908, 0.23818634, 0.55359011, 0.29655677, 0., 0.59992803, 0.52733203, 0.30456398, 0.6027897, 0.33755249, 0., 0.04997651, 0.12099712, 0.12276944, 0., 0.19631702, 0.11836214, 0.06221966, 0.24539362, 0., 1.00000106, 1.0000021, 1.00000092, 1.19952335, 1.00000283, 1.17741522, 1.40596479, 1.60945299, 1.41534644, 1.27928235 ]) self.assertTrue(np.allclose(projections, expected_projections))