예제 #1
0
def SparseDeconvolution(x, y, p, rtype='omp'):

    from numpy import zeros, hstack, floor, array, shape, sign
    from scipy.linalg import toeplitz, norm
    from sklearn.linear_model import OrthogonalMatchingPursuit, Lasso

    xm = x[abs(x).argmax()]

    # x = (x.copy())/xm
    x = (x.copy()) / xm
    x = x / norm(x)

    y = (y.copy()) / xm

    Nx = len(x)
    Ny = len(y)

    X = toeplitz(hstack((x, zeros(Nx + Ny - 2))), r=zeros(Ny + Nx - 1))

    Y = hstack((zeros(Nx - 1), y, zeros(Nx - 1)))

    if (rtype == 'omp') & (type(p) == int):

        model = OrthogonalMatchingPursuit(n_nonzero_coefs=p, normalize=True)

    elif (rtype == 'omp') & (p < 1.0):

        model = OrthogonalMatchingPursuit(tol=p, normalize=True)

    elif (rtype == 'lasso'):

        model = Lasso(alpha=p)

    model.fit(X, Y)

    h = model.coef_
    b = model.intercept_

    r = Y - b
    r = r[int(len(x) / 2) - 1:int(len(x) / 2) - 1 + len(y)]

    h = h[int(len(x) / 2) - 1:int(len(x) / 2) - 1 + len(y)]

    return r, h
예제 #2
0
def omp_estimator(hparams):  #pylint: disable=unused-argument
    """Orthogonal Matching Pursuit"""
    omp_est = OrthogonalMatchingPursuit()
    def estimator(A_val, y_val, hparams):
        omp_est.fit(A_val.T, y_val.reshape(hparams.num_measurements))
        x_hat = omp_est.coef_
        x_hat = np.reshape(x_hat, (1, hparams.n_input))
        x_hat = np.maximum(np.minimum(x_hat, 1), 0)
        return x_hat
    return estimator
예제 #3
0
 def __init__(self,
              name='image_',
              image_feature_dict={},
              reconsitution_element_nums=8,
              error_limit=0.2):
     self.name = name
     self.image_feature_dict = image_feature_dict.copy()
     self.reconsitution_element_nums = reconsitution_element_nums
     self.error_limit = error_limit
     self.omp = OrthogonalMatchingPursuit(
         n_nonzero_coefs=reconsitution_element_nums)
예제 #4
0
 def test_model_orthogonal_matching_pursuit(self):
     model, X = fit_regression_model(OrthogonalMatchingPursuit())
     model_onnx = convert_sklearn(
         model, "orthogonal matching pursuit",
         [("input", FloatTensorType([None, X.shape[1]]))])
     self.assertIsNotNone(model_onnx)
     dump_data_and_model(X,
                         model,
                         model_onnx,
                         verbose=False,
                         basename="SklearnOrthogonalMatchingPursuit-Dec4")
def get_hyperparameters_model():
    param_dist = {}

    clf = OrthogonalMatchingPursuit()

    model = {
        'orthogonal_matching_pursuit': {
            'model': clf,
            'param_distributions': param_dist
        }
    }
    return model
예제 #6
0
파일: ksvd.py 프로젝트: wsuzume/ksvd
    def _estimate_X(self,Y,A):
        if self.num_of_NZ is None:
            n_nonzero_coefs = np.ceil(0.1 * A.shape[1])
        else:
            n_nonzero_coefs = self.num_of_NZ

        omp = OrthogonalMatchingPursuit(n_nonzero_coefs = int(n_nonzero_coefs))
        for j in range(A.shape[1]):
            A[:,j] /= max(np.linalg.norm(A[:,j]),1e-20)
            
        omp.fit(A,Y)
        return omp.coef_.T
예제 #7
0
def test_omp_cv():
    y_ = y[:, 0]
    gamma_ = gamma[:, 0]
    ompcv = OrthogonalMatchingPursuitCV(normalize=True, fit_intercept=False,
                                        max_iter=10, cv=5)
    ompcv.fit(X, y_)
    assert_equal(ompcv.n_nonzero_coefs_, n_nonzero_coefs)
    assert_array_almost_equal(ompcv.coef_, gamma_)
    omp = OrthogonalMatchingPursuit(normalize=True, fit_intercept=False,
                                    n_nonzero_coefs=ompcv.n_nonzero_coefs_)
    omp.fit(X, y_)
    assert_array_almost_equal(ompcv.coef_, omp.coef_)
예제 #8
0
def test_ransac_fit_sample_weight():
    ransac_estimator = RANSACRegressor(random_state=0)
    n_samples = y.shape[0]
    weights = np.ones(n_samples)
    ransac_estimator.fit(X, y, weights)
    # sanity check
    assert ransac_estimator.inlier_mask_.shape[0] == n_samples

    ref_inlier_mask = np.ones_like(ransac_estimator.inlier_mask_).astype(
        np.bool_)
    ref_inlier_mask[outliers] = False
    # check that mask is correct
    assert_array_equal(ransac_estimator.inlier_mask_, ref_inlier_mask)

    # check that fit(X)  = fit([X1, X2, X3],sample_weight = [n1, n2, n3]) where
    #   X = X1 repeated n1 times, X2 repeated n2 times and so forth
    random_state = check_random_state(0)
    X_ = random_state.randint(0, 200, [10, 1])
    y_ = np.ndarray.flatten(0.2 * X_ + 2)
    sample_weight = random_state.randint(0, 10, 10)
    outlier_X = random_state.randint(0, 1000, [1, 1])
    outlier_weight = random_state.randint(0, 10, 1)
    outlier_y = random_state.randint(-1000, 0, 1)

    X_flat = np.append(
        np.repeat(X_, sample_weight, axis=0),
        np.repeat(outlier_X, outlier_weight, axis=0),
        axis=0,
    )
    y_flat = np.ndarray.flatten(
        np.append(
            np.repeat(y_, sample_weight, axis=0),
            np.repeat(outlier_y, outlier_weight, axis=0),
            axis=0,
        ))
    ransac_estimator.fit(X_flat, y_flat)
    ref_coef_ = ransac_estimator.estimator_.coef_

    sample_weight = np.append(sample_weight, outlier_weight)
    X_ = np.append(X_, outlier_X, axis=0)
    y_ = np.append(y_, outlier_y)
    ransac_estimator.fit(X_, y_, sample_weight)

    assert_allclose(ransac_estimator.estimator_.coef_, ref_coef_)

    # check that if base_estimator.fit doesn't support
    # sample_weight, raises error
    base_estimator = OrthogonalMatchingPursuit()
    ransac_estimator = RANSACRegressor(base_estimator, min_samples=10)

    err_msg = f"{base_estimator.__class__.__name__} does not support sample_weight."
    with pytest.raises(ValueError, match=err_msg):
        ransac_estimator.fit(X, y, weights)
예제 #9
0
def test_multi_target_sample_weights_api():
    X = [[1, 2, 3], [4, 5, 6]]
    y = [[3.141, 2.718], [2.718, 3.141]]
    w = [0.8, 0.6]

    rgr = MultiOutputRegressor(OrthogonalMatchingPursuit())
    assert_raises_regex(ValueError, "does not support sample weights", rgr.fit,
                        X, y, w)

    # no exception should be raised if the base estimator supports weights
    rgr = MultiOutputRegressor(GradientBoostingRegressor(random_state=0))
    rgr.fit(X, y, w)
예제 #10
0
def test_omp_reaches_least_squares():
    # Use small simple area_data; it's a sanity check but OMP can stop early
    rng = check_random_state(0)
    n_samples, n_features = (10, 8)
    n_targets = 3
    X = rng.randn(n_samples, n_features)
    Y = rng.randn(n_samples, n_targets)
    omp = OrthogonalMatchingPursuit(n_nonzero_coefs=n_features)
    lstsq = LinearRegression()
    omp.fit(X, Y)
    lstsq.fit(X, Y)
    assert_array_almost_equal(omp.coef_, lstsq.coef_)
def orthogonal_matching_pursuit():

    n_components, n_features = 512, 100
    n_nonzero_coefs = 17

    # generate the data

    # y = Xw
    # |x|_0 = n_nonzero_coefs

    y, X, w = make_sparse_coded_signal(n_samples=1,
                                       n_components=n_components,
                                       n_features=n_features,
                                       n_nonzero_coefs=n_nonzero_coefs,
                                       random_state=0)

    idx, = w.nonzero()

    # distort the clean signal
    y_noisy = y + 0.05 * np.random.randn(len(y))

    # # plot the sparse signal
    # plt.figure(figsize=(7, 7))
    # plt.subplot(4, 1, 1)
    # plt.xlim(0, 512)
    # plt.title("Sparse signal")
    # plt.stem(idx, w[idx], use_line_collection=True)

    # plot the noise-free reconstruction
    omp = OrthogonalMatchingPursuit(n_nonzero_coefs=n_nonzero_coefs)
    omp.fit(X, y)
    coef = omp.coef_
    idx_r, = coef.nonzero()
    # plt.subplot(4, 1, 2)
    # plt.xlim(0, 512)
    # plt.title("Recovered signal from noise-free measurements")
    # plt.stem(idx_r, coef[idx_r], use_line_collection=True)

    # plot the noisy reconstruction
    omp.fit(X, y_noisy)
    coef = omp.coef_
    idx_r, = coef.nonzero()
    # plt.subplot(4, 1, 3)
    # plt.xlim(0, 512)
    # plt.title("Recovered signal from noisy measurements")
    # plt.stem(idx_r, coef[idx_r], use_line_collection=True)

    # plot the noisy reconstruction with number of non-zeros set by CV
    omp_cv = OrthogonalMatchingPursuitCV()
    omp_cv.fit(X, y_noisy)
    coef = omp_cv.coef_
    idx_r, = coef.nonzero()
예제 #12
0
 def __init__(self,
              n_components=49,
              patch_size=(8, 8),
              max_samples=1000000,
              **kwargs):
     self.omp = OrthogonalMatchingPursuit()
     self.n_components = n_components
     self.patch_size = patch_size
     self.max_samples = max_samples
     self.D = None
     self.data = None
     self.components = None
     self.standardize = False
예제 #13
0
def constrained_binary_solve(
    w, psi, fit_intercept=True, normalize=True, precompute="auto"
):
    if ndim(w) != 1:
        raise ValueError(
            f"w must be a 1D vector; received a vector of dimension {ndim(w)}"
        )

    model = OrthogonalMatchingPursuit(
        tol=0, fit_intercept=fit_intercept, normalize=normalize, precompute=precompute
    )
    model.fit(psi, w)
    return model.coef_
예제 #14
0
def csper(t,y,fmin=None,fmax=None,nfreqs=5000,nsines=4,polyorder=2,sig=5):
	trange = np.nanmax(t)-np.nanmin(t)
	dt = np.abs(np.nanmedian(t-np.roll(t,-1)))
	nt = np.size(t)

	# make defaults

	if fmin is None:
		fmin = 1./trange
	if fmax is None:
		fmax = 2./dt

	freqs = np.linspace(fmin,fmax,nfreqs)
	df = np.abs(np.nanmedian(freqs-np.roll(freqs,-1)))

	X = np.zeros((nt,nfreqs*2+polyorder))

	# set up matrix of sines and cosines
	for j in range(nfreqs):
		X[:,j] = np.sin(t*freqs[j])
		X[:,nfreqs+j] = np.cos(t*freqs[j])

	# now do polynomial bits
	for j in range(polyorder):
		X[:,-j] = t**(polyorder-j)

	n_components, n_features = nfreqs, nt
	n_nonzero_coefs = nsines+polyorder

	omp = OrthogonalMatchingPursuit(n_nonzero_coefs=n_nonzero_coefs)
	omp.fit(X, y-np.nanmedian(y))

	coef = omp.coef_
	idx_r, = coef[:-polyorder].nonzero()
	sines = freqs[idx_r[idx_r<nfreqs]]
	cosines = freqs[idx_r[idx_r>nfreqs]-nfreqs]
	print('Sine components:', sines)
	print('Cosine components:',cosines)

	amp_raw = np.sqrt(coef[:nfreqs]**2. + coef[nfreqs:-polyorder]**2)
	amp = gaussian_filter1d(amp_raw,sig)

	recon = np.dot(X,coef)

	output = {'Frequencies':freqs,
			  'Raw_Amplitudes':coef[:-polyorder],
			  'Polynomial':coef[-polyorder:],
			  'Reconstruction':recon,
			  'Amplitude':amp}

	return output
예제 #15
0
def solve_preconditioned_orthogonal_matching_pursuit(basis_matrix_func,
                                                     samples,values,
                                                     precond_func,
                                                     tol=1e-8):
    from sklearn.linear_model import OrthogonalMatchingPursuit
    basis_matrix = basis_matrix_func(samples)
    weights = precond_func(basis_matrix,samples)
    basis_matrix = basis_matrix*weights[:,np.newaxis]
    rhs = values*weights[:,np.newaxis]
    omp = OrthogonalMatchingPursuit(tol=tol,fit_intercept=False)
    omp.fit(basis_matrix, rhs)
    coef = omp.coef_
    print('nnz_terms',np.count_nonzero(coef))
    return coef[:,np.newaxis]
예제 #16
0
def cross_OMP(D1,D2,Y1,Y2):
    '''
    Input:
    D1: Dictionary of Train [256 * n_components]
    D2: Dictionary of Clapping  [256*components]
    Y1: Data matrix of SINGLE sample of Train [256*n]
    Y2: Data matrix of SINGLE sample of Train [256*n]
    Output:
    X1, X2: Concatenated feature vectors [2 * n_components * n]
    
    ** n = 861 **
    '''
    # X11
    omp11 = OrthogonalMatchingPursuit(n_nonzero_coefs=5)
    omp11.fit(D1,Y1)
    X11 = omp11.coef_
    # X12
    omp12 = OrthogonalMatchingPursuit(n_nonzero_coefs=5)
    omp12.fit(D1,Y2)
    X12 = omp12.coef_
    # X21
    omp21 = OrthogonalMatchingPursuit(n_nonzero_coefs=5)
    omp21.fit(D2,Y1)
    X21 = omp21.coef_
    # X22
    omp22 = OrthogonalMatchingPursuit(n_nonzero_coefs=5)
    omp22.fit(D2,Y2)
    X22 = omp22.coef_
    
    # concatenate
    X1 = np.hstack((X11,X12))
#    print(X1.shape)
    X1 = np.max(X1,axis = 0)
#    print(X1.shape)
    X2 = np.hstack((X21,X22))
    X2 = np.max(X2,axis = 0)
    return X1, X2
예제 #17
0
def omp_estimator(hparams):
    """OMP estimator"""
    omp_est = OrthogonalMatchingPursuit(n_nonzero_coefs=hparams.omp_k)
    def estimator(A_val, y_batch_val, hparams):
        x_hat_batch = []
        for i in range(hparams.batch_size):
            y_val = y_batch_val[i]
            omp_est.fit(A_val.T, y_val.reshape(hparams.num_measurements))
            x_hat = omp_est.coef_
            x_hat = np.reshape(x_hat, [-1])
            x_hat = np.maximum(np.minimum(x_hat, 1), 0)
            x_hat_batch.append(x_hat)
        x_hat_batch = np.asarray(x_hat_batch)
        return x_hat_batch
    return estimator
예제 #18
0
파일: handler.py 프로젝트: pylhc/omc3
def _orthogonal_matching_pursuit(response_mat: pd.DataFrame, diff_vec,
                                 opt: DotDict):
    """ Calculated n_correctors via orthogonal matching pursuit"""
    if opt.n_correctors is None:
        raise ValueError(
            "n_correctors setting needed for orthogonal matching pursuit.")

    res = OrthogonalMatchingPursuit(opt.n_correctors).fit(
        response_mat, diff_vec)
    coef = res.coef_
    LOG.debug(
        f"Orthogonal Matching Pursuit Results: \n"
        f"  Chosen variables: {response_mat.columns.to_numpy()[coef.nonzero()]}\n"
        f"  Score: {res.score(response_mat, diff_vec)}")
    return coef
예제 #19
0
def test_scaling_with_gram():
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        # Use only 1 nonzero coef to be faster and to avoid warnings
        omp1 = OrthogonalMatchingPursuit(n_nonzero_coefs=1,
                                         fit_intercept=False,
                                         normalize=False)
        omp2 = OrthogonalMatchingPursuit(n_nonzero_coefs=1,
                                         fit_intercept=True,
                                         normalize=False)
        omp3 = OrthogonalMatchingPursuit(n_nonzero_coefs=1,
                                         fit_intercept=False,
                                         normalize=True)
        omp1.fit(X, y, Gram=G)
        omp1.fit(X, y, Gram=G, Xy=Xy)
        assert_true(len(w) == 3)
        omp2.fit(X, y, Gram=G)
        assert_true(len(w) == 5)
        omp2.fit(X, y, Gram=G, Xy=Xy)
        assert_true(len(w) == 8)
        omp3.fit(X, y, Gram=G)
        assert_true(len(w) == 10)
        omp3.fit(X, y, Gram=G, Xy=Xy)
        assert_true(len(w) == 13)
예제 #20
0
	def get_regressor(self):
		if self.regr_type == "lr":
			self.regr = LinearRegression()
		elif self.regr_type == "ridge":
			self.regr = Ridge()
		elif self.regr_type == "omp":
			self.regr = OrthogonalMatchingPursuit()
		elif self.regr_type == "brr":
			self.regr = BayesianRidge()
		elif self.regr_type == "lsvr":
			self.regr = LinearSVR()
		elif self.regr_type == "svr":
			self.regr = SVR()
		elif self.regr_type == "rf":
			self.regr = RandomForestRegressor()
예제 #21
0
    def __init__(self, models_parameters, base_forest_estimator):
        if models_parameters.extraction_strategy == 'omp_nn':
            self._omp = NonNegativeOrthogonalMatchingPursuit(
                max_iter=models_parameters.extracted_forest_size,
                intermediate_solutions_sizes=models_parameters.
                intermediate_solutions_sizes,
                fill_with_final_solution=True)
        else:
            # fit_intercept shouldn't be set to False as the data isn't necessarily centered here
            # normalization is handled outsite OMP
            self._omp = OrthogonalMatchingPursuit(
                n_nonzero_coefs=models_parameters.extracted_forest_size,
                fit_intercept=True,
                normalize=False)

        super().__init__(models_parameters, base_forest_estimator)
def _orthogonal_matching_pursuit(response_mat, diff_vec, opt):
    """ Calculated n_correctors via orthogonal matching pursuit"""
    if opt.n_correctors is None:
        raise ValueError(
            "n_correctors setting needed for orthogonal matching pursuit.")

    # return orthogonal_mp(response_mat, diff_vec, opt.n_correctors)
    res = OrthogonalMatchingPursuit(opt.n_correctors).fit(
        response_mat, diff_vec)
    coef = res.coef_
    LOG.debug("Orthogonal Matching Pursuit Results:")
    LOG.debug("  Chosen variables: {:s}".format(
        str(response_mat.columns.values[coef.nonzero()])))
    LOG.debug("  Score: {:f}".format(res.score(response_mat, diff_vec)))

    return coef
예제 #23
0
def test_OMP():
    """
    find the 3 best nodes in the set [0, 0.1, ..., 0.9, 1.0] and their weights using Orthogonal Matching Pursuit
    """
    kernel = Matern(length_scale=0.8, nu=1.2)
    set_size = 100
    x = []
    y = []
    for n in range(set_size):
        f = GPRealization(kernel)
        data = []
        for num in np.linspace(0, 1, 11):
            data.append(f(num))
        x.append(data)
        y.append(quad(f, 0, 1)[0])

    # build OMP model
    reg = OrthogonalMatchingPursuit(3).fit(x, y)
    print(reg.coef_)
    print(reg.intercept_)

    # test against simpsons rule
    num_tests = 100
    reg_better = 0
    total_err_simps = 0.0
    total_err_reg = 0.0
    for i in range(num_tests):
        f = GPRealization(kernel)
        data = []
        for num in np.linspace(0, 1, 11):
            data.append(f(num))
        int_reg = reg.predict([data])
        int_reg = int_reg[0]
        int_simpsons = 1 / 6 * f(0) + 4 / 6 * f(.5) + 1 / 6 * f(1)
        int_true = quad(f, 0, 1)[0]
        total_err_simps += abs(int_simpsons - int_true)
        total_err_reg += abs(int_reg - int_true)
        if abs(int_reg - int_true) < abs(int_simpsons - int_true):
            reg_better += 1

    print("The Regression Model was better in {} of {} cases".format(
        reg_better, num_tests))
    print("The average error of the Regression model was {}".format(
        total_err_reg / num_tests))
    print("The average error of the simpsons rule was {}".format(
        total_err_simps / num_tests))
예제 #24
0
def solve_preconditioned_orthogonal_matching_pursuit(basis_matrix_func,
                                                     samples,
                                                     values,
                                                     precond_func,
                                                     tol=1e-8):
    basis_matrix = basis_matrix_func(samples)
    weights = precond_func(basis_matrix, samples)
    basis_matrix = basis_matrix * weights[:, np.newaxis]
    rhs = values * weights[:, np.newaxis]
    if basis_matrix.shape[1] == 1 or tol > 0:
        omp = OrthogonalMatchingPursuit(tol=tol)
    else:
        omp = OrthogonalMatchingPursuitCV(cv=min(samples.shape[1], 10))
    res = omp.fit(basis_matrix, rhs)
    coef = omp.coef_
    coef[0] += res.intercept_
    return coef[:, np.newaxis]
예제 #25
0
def test_omp_cv():
    # FIXME: This test is unstable on Travis, see issue #3190 for more detail.
    check_skip_travis()
    y_ = y[:, 0]
    gamma_ = gamma[:, 0]
    ompcv = OrthogonalMatchingPursuitCV(normalize=True,
                                        fit_intercept=False,
                                        max_iter=10,
                                        cv=5)
    ompcv.fit(X, y_)
    assert_equal(ompcv.n_nonzero_coefs_, n_nonzero_coefs)
    assert_array_almost_equal(ompcv.coef_, gamma_)
    omp = OrthogonalMatchingPursuit(normalize=True,
                                    fit_intercept=False,
                                    n_nonzero_coefs=ompcv.n_nonzero_coefs_)
    omp.fit(X, y_)
    assert_array_almost_equal(ompcv.coef_, omp.coef_)
예제 #26
0
def get_model_from_name(model_name):
    model_map = {
        # Classifiers
        'LogisticRegression': LogisticRegression(n_jobs=-2),
        'RandomForestClassifier': RandomForestClassifier(n_jobs=-2),
        'RidgeClassifier': RidgeClassifier(),
        'GradientBoostingClassifier': GradientBoostingClassifier(),
        'ExtraTreesClassifier': ExtraTreesClassifier(n_jobs=-1),
        'AdaBoostClassifier': AdaBoostClassifier(n_estimators=10),
        'SGDClassifier': SGDClassifier(n_jobs=-1),
        'Perceptron': Perceptron(n_jobs=-1),
        'PassiveAggressiveClassifier': PassiveAggressiveClassifier(),

        # Regressors
        # 'DeepLearningRegressor': KerasRegressor(build_fn=make_deep_learning_model, nb_epoch=10, batch_size=10, verbose=1),
        'LinearRegression': LinearRegression(n_jobs=-2),
        'RandomForestRegressor': RandomForestRegressor(n_jobs=-2),
        'Ridge': Ridge(),
        'ExtraTreesRegressor': ExtraTreesRegressor(n_jobs=-1),
        'AdaBoostRegressor': AdaBoostRegressor(n_estimators=10),
        'RANSACRegressor': RANSACRegressor(),
        'GradientBoostingRegressor': GradientBoostingRegressor(presort=False),
        'Lasso': Lasso(),
        'ElasticNet': ElasticNet(),
        'LassoLars': LassoLars(),
        'OrthogonalMatchingPursuit': OrthogonalMatchingPursuit(),
        'BayesianRidge': BayesianRidge(),
        'ARDRegression': ARDRegression(),
        'SGDRegressor': SGDRegressor(shuffle=False),
        'PassiveAggressiveRegressor':
        PassiveAggressiveRegressor(shuffle=False),

        # Clustering
        'MiniBatchKMeans': MiniBatchKMeans(n_clusters=8)
    }
    if xgb_installed:
        model_map['XGBClassifier'] = xgb.XGBClassifier(colsample_bytree=0.8,
                                                       min_child_weight=5,
                                                       subsample=1.0,
                                                       learning_rate=0.1,
                                                       n_estimators=200,
                                                       nthread=-1)
        model_map['XGBRegressor'] = xgb.XGBRegressor(nthread=-1,
                                                     n_estimators=200)

    return model_map[model_name]
예제 #27
0
    def __init__(self,
                 model_feats,
                 neural_feats_reps,
                 labels,
                 regression=OrthogonalMatchingPursuit(),
                 n_splits=10,
                 test_size=1 / 4.,
                 n_splithalves=10,
                 pca=False,
                 n_components=None,
                 **parallel_kwargs):
        """
        Regression of model features to neurons.

        :Args:
            - model_feats
                Model responses corresponding to stimuli in dataset
            - neural_feats_reps
                Neural responses in the format of (reps, images, sites)

        :Kwargs:
            - n_splits
                Number of splits into (train, test)
            - n_splithalves
                Number of splits over reps

        :Returns:
            The fit outcome (pandas.DataFrame)
        """
        self.model_feats = model_feats
        self._neural_feats_reps = neural_feats_reps
        self.labels = labels
        self.reg = regression
        self.n_splits = n_splits
        self.n_splithalves = n_splithalves
        self.do_pca = bool(pca)
        self._pca = pca
        self.n_components = n_components
        self.pkwargs = parallel_kwargs

        self.rng = np.random.RandomState(0)
        sss = StratifiedShuffleSplit(n_splits=self.n_splits,
                                     test_size=test_size,
                                     random_state=self.rng)
        self.splits = [s for s in sss.split(self.model_feats, self.labels)]
 def __default_regressors():
     return {
         'huber': HuberRegressor(),
         'theil_sen': TheilSenRegressor(),
         'linear': LinearRegression(),
         'ard': ARDRegression(),
         'orthogonal_matching': OrthogonalMatchingPursuit(),
         'elastic_net': ElasticNet(),
         'bayesian_ridge': BayesianRidge(),
         'lasso_lars': LassoLars(),
         'lasso': Lasso(),
         'ridge': Ridge(),
         'gaussian_process': GaussianProcessRegressor(),
         'decision_tree': DecisionTreeRegressor(),
         'svr': SVR(),
         'nu_svr': NuSVR(),
         'kernel_ridge': KernelRidge()
     }
예제 #29
0
 def fit_predict_omp(self, X, y=None):
     n_sample = X.transpose().shape[0]
     H = X.transpose(
     )  #NRP_ELM(self.n_hidden, sparse=False).fit(X).predict(X)
     C = np.zeros((n_sample, n_sample))
     # solve sparse self-expressive representation
     for i in range(n_sample):
         y_i = H[i]
         H_i = np.delete(H, i, axis=0)
         # H_T = H_i.transpose()  # M x (N-1)
         omp = OrthogonalMatchingPursuit(n_nonzero_coefs=int(n_sample *
                                                             0.5),
                                         tol=1e20)
         omp.fit(H_i.transpose(), y_i)
         #  Normalize the columns of C: ci = ci / ||ci||_ss.
         coef = omp.coef_ / np.max(np.abs(omp.coef_))
         C[:i, i] = coef[:i]
         C[i + 1:, i] = coef[i:]
     # # compute affinity matrix
     # L = 0.5 * (np.abs(C) + np.abs(C.T))  # affinity graph
     # # L = 0.5 * (C + C.T)
     # self.affinity_matrix = L
     # # spectral clustering
     # sc = SpectralClustering(n_clusters=self.n_clusters, affinity='precomputed')
     # sc.fit(self.affinity_matrix)
     # K-means clustering
     kmeans = KMeans(n_clusters=self.n_clusters, max_iter=500).fit(C)
     label = kmeans.labels_
     C_ = C
     band_index = []
     for i in np.unique(label):
         index__ = np.nonzero(label == i)
         centroids_ = C_[index__]
         centroids = centroids_.mean(axis=0)
         dis = pairwise_distances(
             centroids_, centroids.reshape(
                 (1, centroids_.shape[1]))).flatten()
         index_min = np.argmin(dis)
         C_bestrow = centroids_[index_min, :]
         index = np.nonzero(np.all(C_ == C_bestrow, axis=1))
         band_index.append(index[0][0])
     BandData = X[:, band_index]  # BandData = self.X[:, band_index]
     print('selected band:', band_index)
     return BandData  #sc.labels_
예제 #30
0
    def update(self, x, t):
        """
        Updates skill success conditions.
        :param x: input
        :param t: target (bool)
        :return: None
        """
        # Don't add duplicates
        for i in range(len(self.all_x)):
            if self.all_t[i] == t and np.all(self.all_x[i] == x):
                return

        # Update skill dataset
        self.all_x.append(x)
        self.all_t.append(t)

        # Can't learn from too few elements
        n_x = len(self.all_x)
        n_unique_t = np.unique(self.all_t).shape[0]
        if n_x <= 2 or n_unique_t < 2:
            return

        # apply OMP
        all_x = np.array(self.all_x)
        all_t = np.array(self.all_t).reshape(-1, 1)
        omp = OrthogonalMatchingPursuit(tol=self.omp_tol)
        omp.fit(all_x, all_t)

        # Trim data
        self.used_dims, = omp.coef_.nonzero()
        x_trim = all_x[:, self.used_dims]
        x_true = x_trim[np.where(all_t)[0], :]

        if x_true.shape[0] >= 2 and x_true.shape[1] > 0:
            # train GMM
            self.gmm = GaussianMixture(
                n_components=min(len(self.used_dims), self.n_gmm_components))
            # Add a bit of noise to avoid duplicate points
            x_noisy = x_true + 0.01 * np.random.normal(size=x_true.shape)
            self.gmm.fit(x_noisy)
            self.fitted = True
            self.gmm_samples_dim, self.gmm_samples_values = \
                self.__generate_gmm_samples(20)