def eval(model, x, y): y_pred = np.argmax(model.predict(x), axis=1) y_test = np.squeeze(y) bca = utils.bca(y_test, y_pred) acc = np.sum(y_pred == y_test).astype(np.float32) / len(y_pred) return acc, bca
# Build Model xd = XdawnCovariances(nfilter=5, applyfilters=True, estimator='lwf') # es = ElectrodeSelection(nelec=25, metric='riemann') ts = TangentSpace(metric='logeuclid') lr = LogisticRegression(solver='liblinear', max_iter=200, C=0.01) model = Pipeline([('xDAWN', xd), ('TangentSpace', ts), ('LR', lr)]) model.fit(x_train, y_train) # Test Model y_pred = np.argmax(model.predict_proba(np.squeeze(x_test)), axis=1) bca = utils.bca(y_test, y_pred) acc = np.sum(y_pred == y_test).astype(np.float32) / len(y_pred) print('{}: acc-{} bca-{}'.format(data_name, acc, bca)) # poison performance test_asr = [] for test_param in params: test_npp_params = [amplitude, test_param[0], test_param[1]] _, x_t_poison, _ = load(data_name, s_id[s], test_npp_params, clean=False, physical=True, downsample=False) idx = y_pred == y_test x_t_poison, y_t = x_t_poison[idx], y_test[idx]
y1_rate = np.mean(np.where(y == 1, 1, 0)) class_weight = {0: y1_rate, 1: y0_rate} else: class_weight = None # ============================== build model ================================== processers = [ Blocks.Xdawn(n_filters=8, with_xdawn_templates=True, apply_filters=True), Blocks.CovarianceFeature(with_mean_templates=False), Blocks.TangentSpaceFeature(mean_metric='riemann', name='TangentSpace({})'.format('riemann')) ] classifier = Blocks.LogisticRegression(class_weight=class_weight, max_iter=2000) model = Pipeline(processers, classifier) print('subject: {}'.format(subject)) model.pipeline_information() # show model information # ============================== train model =================================== print('fitting.......') model.fit(epochs, y) model.save(save_path) y_pred = np.argmax(model.predict(epochs), axis=1) bca = np.round(utils.bca(y, y_pred), decimals=3) acc = np.round(np.sum(y_pred == y).astype(np.float64) / len(y_pred), decimals=3) print('train (target and nontarget): acc={}, bca={}'.format(acc, bca))