def fit_primal_svc(X_train, y_train, C, kernel, gamma=0.1, degree=4, coef0=1): print "Training primal SVC, C = ", C start = time.time() clf = PrimalSVC(C=C, kernel=kernel, degree=degree, coef0=coef0, gamma=gamma, max_iter=100, tol=1e-4, verbose=1) clf.fit(X_train, y_train) return clf, time.time() - start
def test_fit_rbf_binary_l2r(): clf = PrimalSVC(C=0.5, kernel="rbf", gamma=0.1, random_state=0, penalty="l2") clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_almost_equal(acc, 1.0) n_nz = np.sum(clf.coef_ != 0) assert_equal(n_nz, 200) # dense solution...
def test_fit_rbf_binary_l2r_kernelized_selection(): clf = PrimalSVC(C=1.0, random_state=0, penalty="l2", loss="squared_hinge", max_iter=1, kernel="rbf", kernel_regularizer=True, selection="loss") clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_almost_equal(acc, 1.0)
def test_upper_bound_rbf(): clf = PrimalSVC(random_state=0, penalty="l1", kernel="rbf", gamma=0.1) Cmin = C_lower_bound(bin_dense, bin_target, kernel="rbf", gamma=0.1) Cmax = C_upper_bound(bin_dense, bin_target, clf, Cmin, 5.0, 100, 10) clf.set_params(C=Cmax) clf.fit(bin_dense, bin_target) assert_true(clf.n_support_vectors() < 110)
def test_early_stopping_l1r_rbf(): clf = PrimalSVC(kernel="rbf", gamma=0.1, termination="n_sv", sv_upper_bound=30, random_state=0, penalty="l1") clf.fit(bin_dense, bin_target) n_nz = np.sum(clf.coef_ != 0) assert_equal(n_nz, 30)
def test_early_stopping_l2r_rbf(): clf = PrimalSVC(kernel="rbf", gamma=0.1, termination="n_components", n_components=30, random_state=0, penalty="l2") clf.fit(bin_dense, bin_target) n_nz = np.sum(clf.coef_ != 0) assert_equal(n_nz, 30)
def test_fit_rbf_binary_l1r_selection(): for selection in ("loss", "active"): clf = PrimalSVC(C=0.5, kernel="rbf", gamma=0.1, random_state=0, penalty="l1", selection=selection) clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_true(acc >= 0.74) n_nz = np.sum(clf.coef_ != 0) assert_true(n_nz <= 86)
def test_warm_start_l2r_rbf(): clf = PrimalSVC(warm_start=True, kernel="rbf", gamma=0.1, random_state=0, penalty="l2") clf.C = 0.1 clf.fit(bin_dense, bin_target) assert_almost_equal(clf.score(bin_dense, bin_target), 1.0) clf.C = 0.2 clf.fit(bin_dense, bin_target) assert_almost_equal(clf.score(bin_dense, bin_target), 1.0)
def test_components(): clf = PrimalSVC(random_state=0, penalty="l1", kernel="rbf", gamma=0.1, C=0.5) clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) clf = PrimalSVC(random_state=0, penalty="l2", kernel="rbf", gamma=0.1, C=0.5, components=clf.support_vectors_) clf.fit(bin_dense, bin_target) assert_equal(clf.n_support_vectors(), 160) acc2 = clf.score(bin_dense, bin_target) assert_equal(acc, acc2)
def test_empty_model(): clf = PrimalSVC(kernel="rbf", gamma=0.1, C=0.1, penalty="l1") clf.fit(bin_dense, bin_target) assert_equal(clf.n_support_vectors(), 0) acc = clf.score(bin_dense, bin_target) assert_equal(acc, 0.5) clf = PrimalSVC(kernel="rbf", gamma=0.1, C=0.1, penalty="l1l2") clf.fit(bin_dense, bin_target) assert_equal(clf.n_support_vectors(), 0) acc = clf.score(bin_dense, bin_target) assert_equal(acc, 0.5)
def test_warm_start_l1r_rbf(): clf = PrimalSVC(warm_start=True, kernel="rbf", gamma=0.1, random_state=0, penalty="l1") clf.C = 0.5 clf.fit(bin_dense, bin_target) n_nz = np.sum(clf.coef_ != 0) clf.C = 0.6 clf.fit(bin_dense, bin_target) n_nz2 = np.sum(clf.coef_ != 0) assert_true(n_nz < n_nz2)
def test_fit_rbf_binary_l2r_correctness_kernelized(): for loss in ("squared_hinge", "modified_huber", "log"): clf = PrimalSVC(C=1.0, random_state=0, penalty="l2", loss=loss, max_iter=1, kernel="rbf", kernel_regularizer=True) clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_almost_equal(acc, 1.0) clf2 = PrimalKernelSVC(C=1.0, random_state=0, loss=loss, max_iter=1, kernel="rbf", kernel_regularizer=True) clf2.fit(bin_dense, bin_target) assert_array_almost_equal(clf.coef_, clf2.coef_, decimal=5)
def test_debiasing_warm_start(): clf = PrimalSVC(kernel="rbf", gamma=0.1, penalty="l1", max_iter=10) clf.C = 0.5 clf.fit(bin_dense, bin_target) assert_equal(clf.n_support_vectors(), 160) assert_almost_equal(clf.score(bin_dense, bin_target), 0.845) clf = PrimalSVC(kernel="rbf", gamma=0.1, penalty="l1", max_iter=10) clf.C = 0.500001 clf.fit(bin_dense, bin_target) assert_equal(clf.n_support_vectors(), 191) assert_almost_equal(clf.score(bin_dense, bin_target), 0.97) clf = PrimalSVC(kernel="rbf", gamma=0.1, penalty="l1", max_iter=10, warm_start=True) clf.C = 0.5 clf.fit(bin_dense, bin_target) assert_equal(clf.n_support_vectors(), 160) assert_almost_equal(clf.score(bin_dense, bin_target), 0.845) clf = PrimalSVC(kernel="rbf", gamma=0.1, penalty="l1", max_iter=10, warm_start=True) clf.C = 0.500001 clf.fit(bin_dense, bin_target) assert_equal(clf.n_support_vectors(), 191) assert_almost_equal(clf.score(bin_dense, bin_target), 0.97)
def test_fit_rbf_binary_l1r(): clf = PrimalSVC(C=0.5, kernel="rbf", gamma=0.1, random_state=0, penalty="l1") clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_almost_equal(acc, 0.845) n_nz = np.sum(clf.coef_ != 0) assert_equal(n_nz, 160) K = pairwise_kernels(bin_dense, metric="rbf", gamma=0.1) clf2 = PrimalLinearSVC(C=0.5, random_state=0, penalty="l1") clf2.fit(K, bin_target) acc = clf2.score(K, bin_target) assert_almost_equal(acc, 0.845) n_nz = np.sum(clf2.coef_ != 0) assert_equal(n_nz, 160)
def test_shared_kcache(): clf = PrimalSVC(warm_start=True, kernel="rbf", gamma=0.1, random_state=0, penalty="l1") kernel = get_kernel("rbf", gamma=0.1) kcache = KernelCache(kernel, bin_dense.shape[0], 10, 1, 0) clf.C = 0.5 clf.fit(bin_dense, bin_target, kcache=kcache) n_nz = np.sum(clf.coef_ != 0) clf.C = 0.6 clf.fit(bin_dense, bin_target, kcache=kcache) n_nz2 = np.sum(clf.coef_ != 0) assert_true(n_nz < n_nz2)
def test_fit_rbf_binary_l2r_kernelized_upper_bound(): clf = PrimalSVC(C=1.0, random_state=0, penalty="l2", loss="squared_hinge", max_iter=20, kernel="rbf", kernel_regularizer=True, selection="loss", termination="n_components", n_components=30) clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_almost_equal(acc, 0.80) assert_equal(clf.n_support_vectors(), 30) clf = PrimalSVC(C=1.0, random_state=0, penalty="l2l2", loss="squared_hinge", max_iter=20, kernel="rbf", kernel_regularizer=True, selection="loss", termination="n_components", n_components=30, warm_debiasing=True) clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_almost_equal(acc, 0.80) assert_equal(clf.n_support_vectors(), 30)
def fit(self): print "fit the model" train = np.array(self.model.data) X = train[:, 0:2] y = train[:, 2] C = float(self.complexity.get()) gamma = float(self.gamma.get()) coef0 = float(self.coef0.get()) degree = int(self.degree.get()) kernel_map = {0: "linear", 1: "rbf", 2: "poly"} clf = PrimalSVC(kernel=kernel_map[self.kernel.get()], C=C, gamma=gamma, coef0=coef0, degree=degree, max_iter=50, penalty="l1") clf.fit(X, y) if hasattr(clf, 'score'): print "Accuracy:", clf.score(X, y) * 100 X1, X2, Z = self.decision_surface(clf) self.model.clf = clf self.model.set_surface((X1, X2, Z)) self.model.surface_type = self.surface_type.get() self.fitted = True self.model.changed("surface")
def test_debiasing(): clf = PrimalSVC(kernel="rbf", gamma=0.1, penalty="l1l2", C=0.5, Cd=1.0, max_iter=10) clf.fit(bin_dense, bin_target) assert_equal(clf.n_support_vectors(), 160) assert_almost_equal(clf.score(bin_dense, bin_target), 0.845) pred = clf.decision_function(bin_dense) clf = PrimalSVC(kernel="rbf", gamma=0.1, penalty="l1", C=0.5) clf.fit(bin_dense, bin_target) assert_equal(clf.n_support_vectors(), 160) K = pairwise_kernels(bin_dense, clf.support_vectors_, metric="rbf", gamma=0.1) clf = PrimalLinearSVC(max_iter=10, C=1.0) clf.fit(K, bin_target) assert_almost_equal(clf.score(K, bin_target), 0.845) pred2 = clf.decision_function(K) assert_array_almost_equal(pred, pred2)
def test_fit_rbf_multi(): clf = PrimalSVC(penalty="l1", kernel="rbf", gamma=0.1, random_state=0) clf.fit(mult_dense, mult_target) y_pred = clf.predict(mult_dense) acc = np.mean(y_pred == mult_target) assert_almost_equal(acc, 1.0)
def test_fit_rbf_binary_l2r_log(): clf = PrimalSVC(C=1.0, random_state=0, penalty="l2", loss="log", max_iter=5, kernel="rbf") clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_almost_equal(acc, 1.0)
def test_fit_rbf_binary_l2r_modified_huber(): clf = PrimalSVC(C=1.0, random_state=0, penalty="l2", kernel="rbf", loss="modified_huber") clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_almost_equal(acc, 1.0)