def test_iwe_kernel_mean_matching(): """Test for estimating through kernel mean matching.""" X = rnd.randn(10, 2) Z = rnd.randn(10, 2) + 1 clf = ImportanceWeightedClassifier() iw = clf.iwe_kernel_mean_matching(X, Z) assert np.all(iw >= 0)
for iw in [0,1,2,3]: iwe = weighting_functions[iw] w_clf = ImportanceWeightedClassifier(iwe=iwe) X = np.asarray(MEG_data_train) Y = np.asarray(MEG_data_test) w_clf.fit(X,y,Y) if iwe == 'lr': w = w_clf.iwe_logistic_discrimination(X, Y) elif iwe == 'rg': w = w_clf.iwe_ratio_gaussians(X, Y) elif iwe == 'nn': w = w_clf.iwe_nearest_neighbours(X, Y) elif iwe == 'kde': w = w_clf.iwe_kernel_densities(X, Y) elif iwe == 'kmm': w = w_clf.iwe_kernel_mean_matching(X, Y) else: raise NotImplementedError('Estimator not implemented.') clf = Weighted_InitClassifier(index) # Find importance-weights probas_ = clf.fit(X, y, sample_weight=w).predict(X) accuracy_org = sum((probas_>0.5)==y)/(1.0*nsamples) target_probas_ = clf.predict(Y) #print(classes_test.shape,Y.shape,n_test_samples) accuracy_targ = sum((target_probas_>0.5)==classes_test)/(1.0*n_test_samples) print (subject,target_subj,index,iw,accuracy_org,accuracy_targ) elif mode==4: # Evaluation of classifiers using the full training set as test set