def test_model_specification_error(self): n_l, prior_l = 30, .3 n_u, prior_u = 200, .5 n_t = 100 x, y, x_tp, x_tn = gendata(n_l, prior_l, n_u, prior_u, n_t) eta_list = [ pnu_mr.calc_etab(np.sum(y == +1), np.sum(y == -1), prior_u) ] with self.assertRaises(ValueError): pnu_mr.PNU_SL_FastCV(x, y, prior_u, eta_list, basis='DNN')
def test_calc_etab(self): n_p, n_n = 10, 10 prior_list = np.arange(.1, 1, .1) for prior in prior_list: eta = pnu_mr.calc_etab(n_p, n_n, prior) if prior < .5: self.assertTrue(eta > 0) elif prior > .5: self.assertTrue(eta < 0) else: self.assertTrue(eta == 0)
def test_label_specification_error(self): n_l, prior_l = 30, .3 n_u, prior_u = 200, .5 n_t = 100 x, y, x_tp, x_tn = gendata(n_l, prior_l, n_u, prior_u, n_t) y[0] = 2 eta_list = [ pnu_mr.calc_etab(np.sum(y == +1), np.sum(y == -1), prior_u) ] with self.assertRaises(ValueError): pnu_mr.PNU_SL(x, y, prior_u, eta_list, model='lm')
def test_linear_model_one_eta(self): n_l, prior_l = 30, .5 n_u, prior_u = 200, .3 n_t = 100 x, y, x_tp, x_tn = gendata(n_l, prior_l, n_u, prior_u, n_t) eta_list = [ pnu_mr.calc_etab(np.sum(y == +1), np.sum(y == -1), prior_u) ] f_dec, outs, funcs = pnu_mr.PNU_SL_FastCV(x, y, prior_u, eta_list, basis='lm', nargout=3) err = 100 * calc_err(f_dec, x_tp, x_tn, prior_u)
def test_model_nargout2(self): n_l, prior_l = 30, .5 n_u, prior_u = 200, .3 n_t = 100 x, y, x_tp, x_tn = gendata(n_l, prior_l, n_u, prior_u, n_t) eta_list = [ pnu_mr.calc_etab(np.sum(y == +1), np.sum(y == -1), prior_u) ] lambda_list = [.1] f_dec, outs = pnu_mr.PNU_SL_FastCV(x, y, prior_u, eta_list, lambda_list=lambda_list, basis='lm', nargout=2) err = 100 * calc_err(f_dec, x_tp, x_tn, prior_u)
def test_gauss_model(self): n_l, prior_l = 30, .3 n_u, prior_u = 200, .5 n_t = 100 lambda_list = [.1] x, y, x_tp, x_tn = gendata(n_l, prior_l, n_u, prior_u, n_t) eta_list = [ pnu_mr.calc_etab(np.sum(y == +1), np.sum(y == -1), prior_u) ] f_dec, outs, funcs = pnu_mr.PNU_SL_FastCV(x, y, prior_u, eta_list, basis='gauss', lambda_list=lambda_list, nargout=3) err = 100 * calc_err(f_dec, x_tp, x_tn, prior_u)
def test_model_without_cv(self): n_l, prior_l = 30, .3 n_u, prior_u = 200, .5 n_t = 100 x, y, x_tp, x_tn = gendata(n_l, prior_l, n_u, prior_u, n_t) eta_list = [ pnu_mr.calc_etab(np.sum(y == +1), np.sum(y == -1), prior_u) ] lambda_list = [.1] f_dec, outs, funcs = pnu_mr.PNU_SL(x, y, prior_u, eta_list, lambda_list=lambda_list, model='lm', nargout=3) err = 100 * calc_err(f_dec, x_tp, x_tn, prior_u)
n_l = 20 n_u = 1000 n_t = 1000 prior_l = .5 prior_u = .3 eta_list = np.mgrid[-.9:.9:19j] x, y, x_t, y_t = gen_twonorm_ssl(n_l, prior_l, n_u, prior_u, n_t) x_l, y_l, x_u = x[y != 0, :], y[y != 0], x[y == 0, :] priorh = cpe(x_l, y_l, x_u) clf = PNU_SL(prior=priorh, basis='lm') params = {'eta': eta_list, 'lam': [.1]} etah = calc_etab(np.sum(y == +1), np.sum(y == -1), priorh) clf = GridSearchCV(estimator=clf, param_grid=params, scoring=make_scorer(pnu_risk, prior=priorh, eta=etah), cv=3, n_jobs=-1) timer.tic("Start PNU_SL") clf.fit(x, y) timer.toc() y_h = clf.predict(x_t) print(y_h) print(y_t) err1 = 100 * bin_clf_err(y_h, y_t, prior=prior_u) print("Error: {:.2f}\n".format(err1)) timer.tic("Start PNU_SL_FastCV")