def test_get_kernel(): # ------------------------------------------------------------------------ # test that callable can be passed kernel = Uniform(support=1e5) reg = NadarayaWatson(kernel=kernel) X = np.random.rand(10, 3) assert_array_equal(np.ones((10, 10)), reg._get_kernel(X))
def fit(self, option='standard', **kwargs): y = self.voxel_data.projections if option.lower() in ('log', 'logged'): y = np.log10(y + self.LOG_CONSTANT) estimator = NadarayaWatson(**kwargs) estimator.fit(self.voxel_data.centroids, y) return estimator
def test_get_weights(): # ------------------------------------------------------------------------ # test weights are row normalized reg = NadarayaWatson() X = np.random.rand(10, 3) y = np.random.rand(10, 10) w = reg.fit(X, y).get_weights(X) assert_allclose(w.sum(axis=1), 1.)
def test_nw_fit(): # ------------------------------------------------------------------------ # test setting of attrs reg = NadarayaWatson() X = np.random.rand(10, 10) y = np.random.rand(10, 3) reg.fit(X, y) assert X is reg.X_ assert y is reg.y_
def test_check_fit_arrays(): # ------------------------------------------------------------------------ # test sample weight reg = NadarayaWatson() X = np.random.rand(10, 10) y = np.random.rand(10, 3) sample_weight = np.random.rand(10) X_check, y_check = reg._check_fit_arrays(X, y, sample_weight) assert_array_equal(X, X_check) assert_array_almost_equal(sample_weight[:, np.newaxis] * y, y_check)
def test_predict(): # ------------------------------------------------------------------------ # test sparse predict == dense predict reg = NadarayaWatson() X = np.random.rand(10, 3) y = np.random.rand(10, 10) dense = reg.fit(X, y).predict(X) sparse_y = reg.fit(X, csr_matrix(y)).predict(X) sparse_x = reg.fit(csr_matrix(X), y).predict(X) sparse_p = reg.fit(X, y).predict(csr_matrix(X)) assert_array_almost_equal(dense, sparse_y) assert_array_almost_equal(dense, sparse_x) assert_array_almost_equal(dense, sparse_p)
def test_normalize_kernel(): # ------------------------------------------------------------------------ # test overwrite/not reg = NadarayaWatson() K = np.ones((5, 5)) K_norm = K / 5 assert_array_equal(K_norm, reg._normalize_kernel(K, overwrite=False)) assert K_norm is reg._normalize_kernel(K_norm, overwrite=True) # ------------------------------------------------------------------------ # test handling of zero rows K_norm[2, :] = np.zeros(5) assert_array_equal(K_norm, reg._normalize_kernel(K_norm, overwrite=False))
def run_structure(cache, structure_id, eid_set=None, experiments_exclude=[], error_option='standard', **nw_kwargs): data = ModelData(cache, structure_id).get_voxel_data( eid_set=eid_set, experiments_exclude=experiments_exclude) # nested cross val logging.debug("Performing cross validation: (%d samples, %d vars)", *data.projections.shape) scoring_dict = LogHybridScorer(cache).scoring_dict reg = NadarayaWatson(**nw_kwargs) scores = cross_validate(reg, X=data.centroids, y=data.projections, cv=LeaveOneOut(), scoring=scoring_dict, return_train_score=True, n_jobs=-1) return scores
# ############################################################################# # Generate sample data X = 5 * rng.rand(10000, 1) y = np.sin(X).ravel() # Add noise to targets y[::5] += 3 * (0.5 - rng.rand(X.shape[0] // 5)) X_plot = np.linspace(0, 5, 1e3)[:, None] # ############################################################################# # Fit regression model train_size = 100 param_grid = dict(kernel=["rbf"], gamma=np.logspace(-2, 2, 25)) nw_gs = GridSearchCV(NadarayaWatson(), cv=5, param_grid=param_grid) nw_cv = NadarayaWatsonCV(param_grid) # fit 5-fold using GridSearch t0 = time.time() nw_gs.fit(X[:train_size], y[:train_size]) gs_fit = time.time() - t0 print("GridSearchCV 5 fold cross validation fitted in %.2f s" % gs_fit) print("\toptimal bandwidth found: %.2f" % nw_gs.best_estimator_.gamma) # fit leave-one-out using NadarayaWatsonCV t0 = time.time() nw_cv.fit(X[:train_size], y[:train_size]) cv_fit = time.time() - t0 print("NadarayaWatsonCV leave-one-out cross validation fitted in %.2f s" % cv_fit)