Example #1
0
 def _regress(self, x, y):
     """Perform the ridge regression
     """
     kw = self.ridge_kw or {}
     coef = ridge_regression(x, y, self.alpha, **kw)
     self.iters += 1
     return coef
 def fit_ridge(self, V):
     images = io.loadmat(self.data_path +
                         'images/images_natimg2800_all.mat')['imgs']
     images = images.transpose((2, 0, 1))
     images = images.reshape((2800, 68 * 270))
     from utils import PCA
     reduced_images = PCA(images)
     stim = io.loadmat(self.data_path +
                       self.mouse_filename)['stim']['istim'][0][0].astype(
                           np.int32)
     x_train, x_test, y_train, y_test = test_train_split(V, stim)
     y_train = y_train - 1
     reduced_images_ = reduced_images[y_train]
     for alpha in [5000]:
         assembly_array = []
         for assembly in range(0, self.nr_of_components):
             av_resp = (x_train[:, assembly].T + x_test[:, assembly].T) / 2
             reg = ridge_regression(reduced_images_, av_resp, alpha=alpha)
             assembly_array.append(reg)
         assembly_array = np.array(assembly_array)
         if self.save == True:
             if self.model == 'EnsemblePursuit_numpy':
                 file_string = self.save_path + self.mouse_filename + '_ep_numpy_' + str(
                     alpha) + 'reg.npy'
             if self.model == 'EnsemblePursuit_pytorch':
                 file_string = self.save_path + self.mouse_filename + '_ep_pytorch_' + str(
                     alpha) + 'reg.npy'
             np.save(file_string)
     return assembly_array
Example #3
0
def test_ridge_regression_dtype_stability(solver, seed):
    random_state = np.random.RandomState(seed)
    n_samples, n_features = 6, 5
    X = random_state.randn(n_samples, n_features)
    coef = random_state.randn(n_features)
    y = np.dot(X, coef) + 0.01 * random_state.randn(n_samples)
    alpha = 1.0
    results = dict()
    # XXX: Sparse CG seems to be far less numerically stable than the
    # others, maybe we should not enable float32 for this one.
    atol = 1e-3 if solver == "sparse_cg" else 1e-5
    for current_dtype in (np.float32, np.float64):
        results[current_dtype] = ridge_regression(X.astype(current_dtype),
                                                  y.astype(current_dtype),
                                                  alpha=alpha,
                                                  solver=solver,
                                                  random_state=random_state,
                                                  sample_weight=None,
                                                  max_iter=500,
                                                  tol=1e-10,
                                                  return_n_iter=False,
                                                  return_intercept=False)

    assert results[np.float32].dtype == np.float32
    assert results[np.float64].dtype == np.float64
    assert_allclose(results[np.float32], results[np.float64], atol=atol)
Example #4
0
 def fit_ridge(self):
     images = io.loadmat(self.data_path +
                         'images/images_natimg2800_all.mat')['imgs']
     images = images.transpose((2, 0, 1))
     images = images.reshape((2800, 68 * 270))
     from utils import PCA
     reduced_images = PCA(images)
     if self.model == 'EnsemblePursuit_pytorch':
         model_string = '*V_ep_pytorch.npy'
     if self.model == 'SparsePCA':
         model_string = '*V_sPCA.npy'
     if self.model == 'ICA':
         model_string = '*_V_ICA.npy'
     if self.model == 'NMF':
         model_string = '*_V_NMF.npy'
     if self.model == 'PCA':
         model_string = '*V_pca.npy'
     if self.model == 'LDA':
         model_string = '*_V_lda.npy'
     if self.model == 'EnsemblePursuit_adaptive':
         model_string = '*_V_ep_adaptive.npy'
     for filename in glob.glob(os.path.join(self.save_path, model_string)):
         print(filename)
         istim_path = filename[len(self.save_path):len(self.save_path) +
                               len(self.mat_file_lst[0])]
         stim = io.loadmat(self.data_path +
                           istim_path)['stim']['istim'][0][0]
         #test train split
         components = np.load(filename)
         x_train, x_test, y_train, y_test = test_train_split(
             components, stim)
         y_train = y_train - 1
         reduced_images_ = reduced_images[y_train]
         for alpha in [5000]:
             assembly_array = []
             for assembly in range(0, self.nr_of_components):
                 av_resp = (x_train[:, assembly].T +
                            x_test[:, assembly].T) / 2
                 reg = ridge_regression(reduced_images_,
                                        av_resp,
                                        alpha=alpha)
                 assembly_array.append(reg)
             assembly_array = np.array(assembly_array)
             if self.model == 'EnsemblePursuit_pytorch':
                 file_string = self.save_path + istim_path + '_ep_pytorch_reg.npy'
             if self.model == 'SparsePCA':
                 file_string = self.save_path + istim_path + '_sPCA_reg.npy'
             if self.model == 'ICA':
                 file_string = self.save_path + istim_path + '_ica_reg.npy'
             if self.model == 'NMF':
                 file_string = self.save_path + istim_path + '_NMF_reg.npy'
             if self.model == 'PCA':
                 file_string = self.save_path + istim_path + '_pca_reg.npy'
             if self.model == 'LDA':
                 file_string = self.save_path + istim_path + '_lda_reg.npy'
             if self.model == 'EnsemblePursuit_adaptive':
                 file_string = self.save_path + istim_path + '_ep_adaptive_reg.npy'
             np.save(file_string, assembly_array)
Example #5
0
    def train(self, target, inputs, trim=100):
        """Calculate weights between hidden layer and output layer for
        a given time series, uses pseudo-inverse training step"""
        acts = zeros((len(target), self.hidden_size))
        summed_acts = []

        #create initial state for the hidden nodes
        for i in range(len(acts[0])):
            acts[0][i] = (random()*2)-1

        # create the activations
        for i in range(1, len(target)):
            # turn target into array
            targ, inp = array([target[i-1]]), array([inputs[i-1]])
            # dotting target with back weights as the teacher signal
            activation = tanh(dot(acts[i-1], self.weights['hidden'])+
                              dot(self.weights['back'], targ)+
                              dot(self.weights['input'], inp))
            # leaky integrator: prev state effects current state
            acts[i] = ((1-self.alpha) * acts[i-1])
            acts[i] += self.alpha * activation

        #trim out the initial 100 activations as they are unstable
        target = target[trim:]
        inputs = inputs[trim:]
        acts = acts[trim:, :]

        #store activations and plot
        self.acts = acts
        graph.plot_2d(acts.T, "training_activations")

        #add the inputs to the activations
        acts = vstack((acts.T, inputs)).T

        # Pseudo-inverse to train the output and setting weights
        tinv = arctanh(target)
        clf = linear_model.RidgeCV(alphas=[0.01, 0.1, 1.0, 10.0])
        #clf = linear_model.Ridge(alpha=0.5)
        #clf = linear_model.LassoCV()
        clf.fit(acts, tinv)
        self.weights['out'] = linear_model.ridge_regression(acts, tinv, 
                                                            alpha=.15)
        self.weights['out'] = clf.coef_
        #self.weights['out'] = linalg.lstsq(acts, tinv)[0]
        #residual = dot(acts, self.weights['out']) - tinv

        graph.bar_plot(self.weights['out'], "weights")

        # checking the output against previous activations
        train_out = []
        for act in acts:
            output = tanh(dot(act, self.weights['out']))
            train_out.append(output)
        return train_out
Example #6
0
    def train(self, target, inputs, trim=100):
        """Calculate weights between hidden layer and output layer for
        a given time series, uses pseudo-inverse training step"""
        acts = zeros((len(target), self.hidden_size))
        summed_acts = []
        
        #create initial state for the hidden nodes
        for i in range(len(acts[0])):
            acts[0][i] = (random()*2)-1

        # create the activations
        for i in range(1, len(target)):
            # turn target into array
            targ, inp = array([target[i-1]]), array([inputs[i-1]])
            # dotting target with back weights as the teacher signal
            activation = tanh(dot(acts[i-1],self.weights['hidden'])+
                              dot(self.weights['back'], targ)+
                              dot(self.weights['input'], inp))
            # leaky integrator: prev state effects current state
            acts[i] = ((1-self.alpha) * acts[i-1])
            acts[i] += self.alpha * activation

        #trim out the initial 100 activations as they are unstable
        target = target[trim:]
        inputs = inputs[trim:]
        acts = acts[trim:, :]

        #store activations and plot
        self.acts = acts
        graph.plot_2d(acts.T, "training_activations")

        #add the inputs to the activations
        acts = vstack((acts.T,inputs)).T
        
        # Pseudo-inverse to train the output and setting weights
        tinv = arctanh(target)
        clf = linear_model.RidgeCV(alphas=[0.01, 0.1, 1.0, 10.0])
        #clf = linear_model.Ridge(alpha=0.5)
        #clf = linear_model.LassoCV()
        clf.fit(acts, tinv)
        self.weights['out'] = linear_model.ridge_regression(acts, tinv,alpha=.15)
        self.weights['out'] = clf.coef_
        #self.weights['out'] = linalg.lstsq(acts, tinv)[0]
        #residual = dot(acts, self.weights['out']) - tinv

        graph.bar_plot(self.weights['out'], "weights")

        # checking the output against previous activations
        train_out = []
        for act in acts:
            output = tanh(dot(act, self.weights['out']))
            train_out.append(output)
        return train_out
Example #7
0
def spca(data, num_components=None, alpha=1):
    # creates a matrix with sparse principal component analysis
    # build matrix with all data
    data = [d.flatten() for d in data if not any(isnan(d))]
    datamatrix = row_stack(data)

    # center data
    cdata = datamatrix - mean(datamatrix, axis=0)

    if num_components is None:
        num_components = cdata.shape[0]

    # do spca on matrix
    spca = SparsePCA(n_components=num_components, alpha=alpha)
    spca.fit(cdata)

    # normalize components
    components = spca.components_.T
    for r in range(0, components.shape[1]):
        compnorm = numpy.apply_along_axis(numpy.linalg.norm, 0, components[:,
                                                                           r])
        if not compnorm == 0:
            components[:, r] /= compnorm
    components = components.T

    # calc adjusted explained variance from "Sparse Principal Component Analysis" by Zou, Hastie, Tibshirani
    spca.components_ = components
    #nuz = spca.transform(cdata).T
    nuz = ridge_regression(spca.components_.T,
                           cdata.T,
                           0.01,
                           solver='dense_cholesky').T

    #nuz = dot(components, cdata.T)
    q, r = qr(nuz.T)
    cumulative_var = []
    for i in range(1, num_components + 1):
        cumulative_var.append(trace(r[0:i, ] * r[0:i, ]))
    explained_var = [math.sqrt(cumulative_var[0])]
    for i in range(1, num_components):
        explained_var.append(
            math.sqrt(cumulative_var[i]) - math.sqrt(cumulative_var[i - 1]))

    order = numpy.argsort(explained_var)[::-1]
    components = numpy.take(components, order, axis=0)
    evars = numpy.take(explained_var, order).tolist()
    #evars = numpy.take(explained_var,order)
    #order2 = [0,1,2,4,5,7,12,19]
    #components = numpy.take(components,order2,axis=0)
    #evars = numpy.take(evars,order2).tolist()

    return components, evars
Example #8
0
def test_ridge_regression_sample_weights():
    rng = np.random.RandomState(0)

    for solver in ("cholesky", ):
        for n_samples, n_features in ((6, 5), (5, 10)):
            for alpha in (1.0, 1e-2):
                y = rng.randn(n_samples)
                X = rng.randn(n_samples, n_features)
                sample_weight = 1.0 + rng.rand(n_samples)

                coefs = ridge_regression(X, y,
                                         alpha=alpha,
                                         sample_weight=sample_weight,
                                         solver=solver)

                # Sample weight can be implemented via a simple rescaling
                # for the square loss.
                coefs2 = ridge_regression(
                    X * np.sqrt(sample_weight)[:, np.newaxis],
                    y * np.sqrt(sample_weight),
                    alpha=alpha, solver=solver)
                assert_array_almost_equal(coefs, coefs2)
Example #9
0
def choose_regression_algorithm(method = "LR"):

    if method == "LR":
        regression_algorithm = LinearRegression()

    elif method == "Lasso":
        regression_algorithm = Lasso()

    elif method == "Ridge":
        regression_algorithm = Ridge()

    elif method == "HR":
        regression_algorithm = HuberRegressor()        
        
    elif method == "SVR":
        regression_algorithm = SVR()
        
    elif method == "LL":
        regression_algorithm = LassoLars()

    elif method == "ARDR":
        regression_algorithm = ARDRegression()

    elif method == "BR":
        regression_algorithm = BayesianRidge()

    elif method == "ElasticNet":
        regression_algorithm = ElasticNet()

    elif method == "Lars":
        regression_algorithm = Lars()

    elif method == "PA":
        regression_algorithm = PassiveAggressiveRegressor()

    elif method == "RANSAC":
        regression_algorithm = RANSACRegressor()

    elif method == "TS":
        regression_algorithm = TheilSenRegressor()

    elif method == "LP":
        regression_algorithm = lars_path()

    elif method == "RR":
        regression_algorithm = ridge_regression()
        
    else:
        print("You haven't chosen a valide classifier!!!")
    print("method used:\t", method)   
    return regression_algorithm
Example #10
0
def cross_validation(y, x, k_indices, k, lamb, degree, rmse=False):
    """return the loss of ridge regression."""
    # ***************************************************
    # Split data into K groups according to indices
    # get k'th subgroup in test, others in train:
    # ***************************************************
    x = np.array(x)
    y = np.array(y)

    train_ind = np.concatenate((k_indices[:k], k_indices[k+1:]), axis=0)
    train_ind = np.reshape(train_ind, (train_ind.size,))

    test_ind = k_indices[k]
    # Note: different from np.ndarray, tuple is name[index,]
    # ndarray is name[index,:]
    train_x = x[train_ind,]
    train_y = y[train_ind,]
    test_x = x[test_ind,]
    test_y = y[test_ind,]

    # ***************************************************
    # INSERT YOUR CODE HERE
    # form data with polynomial degree:
    # ***************************************************
    train_x = build_poly(train_x, degree)
    test_x = build_poly(test_x, degree)

    # ***************************************************
    # INSERT YOUR CODE HERE
    # ridge regression:
    # ***************************************************
    loss_tr, weight = ridge_regression(train_y, train_x, lamb)

    # Test with sklearn ridge solve.
    clf = linear_model.ridge_regression(train_x, train_y, alpha=lamb)
    # weight = clf

    # ***************************************************
    # INSERT YOUR CODE HERE
    # calculate the loss for train and test data: TODO
    # ***************************************************
    ''' Compute MSE by ridge weights '''
    loss_tr = compute_mse_for_ridge(train_y, train_x, weight,lamb)
    loss_te = compute_mse_for_ridge(test_y, test_x, weight, lamb)
    # loss_tr = compute_mse(train_y, train_x, weight)
    # loss_te = compute_mse(test_y, test_x, weight)

    if rmse is True:
        loss_tr = compute_rmse(loss_tr)
        loss_te = compute_rmse(loss_te)
    return loss_tr, loss_te
Example #11
0
    def fit(self, x, y, weights=None):
        """ Fit a polynomial using sparse regression using STLSQ (Sequentially thresholded least-squares)
        Parameters:
            x (np.array or list): Independent variable. Could either be an array (for 1D case) or
                a list of two arrays (for 2D case).
            y (np.array): Dependent variable
            weights (np.array): Sample weights for regression.
                If None (default), simple unweighted ridge regression will be performed.
        Returns:
            np.poly1d object for 1D case, Poly2D object for 2D case.
        """

        if self.library:
            dictionary = np.vstack([f(x) for f in self.library]).T
            coeffs = np.zeros(len(self.library))
            keep = np.ones_like(coeffs, dtype=np.bool)
            ispoly = False
        else:  # Default polynomial dictionary
            dictionary = self._get_poly_dictionary(x)
            coeffs = self._get_coeffs()
            keep = np.ones_like(coeffs, dtype=np.bool)
            ispoly = True

        maxiter = dictionary.shape[1]

        for it in range(maxiter):
            if np.sum(keep) == 0:
                warnings.warn('Sparsity threshold is too big, eliminated all parameters.')
                break
            coeffs_ = ridge_regression(dictionary[:, keep], y, alpha=self.alpha, sample_weight=weights)
            # print(f'coeffs: {coeffs_}')
            coeffs[keep] = coeffs_
            keep = (np.abs(coeffs) > self.threshold)
            coeffs[~keep] = 0

        # Compute errors in coefficients
        N = y.shape[0]  # Number of samples
        p = keep.sum()  # Number of nonzero terms
        yhat = dictionary @ coeffs
        rsos = (y - yhat).T @ (y - yhat)
        sigma_2 = rsos / (N - p)
        var_coeff = np.linalg.inv(dictionary.T @ dictionary) * sigma_2
        stderr = np.sqrt(np.diagonal(var_coeff))

        if ispoly:
            return self._get_callable_poly(coeffs, stderr)
        else:
            return coeffs, stderr
Example #12
0
def test_ridge_regression_check_arguments_validity(return_intercept,
                                                   sample_weight, arr_type,
                                                   solver):
    """check if all combinations of arguments give valid estimations"""

    # test excludes 'svd' solver because it raises exception for sparse inputs

    rng = check_random_state(42)
    X = rng.rand(1000, 3)
    true_coefs = [1, 2, 0.1]
    y = np.dot(X, true_coefs)
    true_intercept = 0.
    if return_intercept:
        true_intercept = 10000.
    y += true_intercept
    X_testing = arr_type(X)

    alpha, atol, tol = 1e-3, 1e-4, 1e-6

    if solver not in ['sag', 'auto'] and return_intercept:
        assert_raises_regex(ValueError,
                            "In Ridge, only 'sag' solver",
                            ridge_regression,
                            X_testing,
                            y,
                            alpha=alpha,
                            solver=solver,
                            sample_weight=sample_weight,
                            return_intercept=return_intercept,
                            tol=tol)
        return

    out = ridge_regression(
        X_testing,
        y,
        alpha=alpha,
        solver=solver,
        sample_weight=sample_weight,
        return_intercept=return_intercept,
        tol=tol,
    )

    if return_intercept:
        coef, intercept = out
        assert_allclose(coef, true_coefs, rtol=0, atol=atol)
        assert_allclose(intercept, true_intercept, rtol=0, atol=atol)
    else:
        assert_allclose(out, true_coefs, rtol=0, atol=atol)
Example #13
0
def spca(data, num_components=None, alpha=1):
		# creates a matrix with sparse principal component analysis
		# build matrix with all data
		data = [d.flatten() for d in data if not any(isnan(d))]
		datamatrix = row_stack(data)
		
		# center data
		cdata = datamatrix - mean(datamatrix, axis=0)
		
		if num_components is None:
			num_components = cdata.shape[0]
		
		# do spca on matrix
		spca = SparsePCA(n_components=num_components, alpha=alpha)
		spca.fit(cdata)
		
		# normalize components
		components = spca.components_.T
		for r in xrange(0,components.shape[1]):
			compnorm = numpy.apply_along_axis(numpy.linalg.norm, 0, components[:,r])
			if not compnorm == 0:
				components[:,r] /= compnorm
		components = components.T
		
		# calc adjusted explained variance from "Sparse Principal Component Analysis" by Zou, Hastie, Tibshirani
		spca.components_ = components
		#nuz = spca.transform(cdata).T
		nuz = ridge_regression(spca.components_.T, cdata.T, 0.01, solver='dense_cholesky').T
		
		#nuz = dot(components, cdata.T)
		q,r = qr(nuz.T)
		cumulative_var = []
		for i in range(1,num_components+1):
			cumulative_var.append(trace(r[0:i,]*r[0:i,]))
		explained_var = [math.sqrt(cumulative_var[0])]
		for i in range(1,num_components):
			explained_var.append(math.sqrt(cumulative_var[i])-math.sqrt(cumulative_var[i-1]))
		
		order = numpy.argsort(explained_var)[::-1]
		components = numpy.take(components,order,axis=0)
		evars = numpy.take(explained_var,order).tolist()
		#evars = numpy.take(explained_var,order)
		#order2 = [0,1,2,4,5,7,12,19]
		#components = numpy.take(components,order2,axis=0)
		#evars = numpy.take(evars,order2).tolist()
		
		return components, evars
Example #14
0
    def _fit_poly_sparse(self,
                         x,
                         y,
                         deg,
                         threshold=0.05,
                         alpha=0,
                         weights=None):
        """ Fit a polynomial using sparse regression using STLSQ (Sequentially thresholded least-squares)
		Parameters:
			x, y: (np.array) Independent and dependent variables
			deg: (int) Maximum degree of the polynomial
			threshold: (float) Threshold for sparse fit.
		"""

        nan_idx = np.argwhere(np.isnan(y))
        x_ = np.delete(x, nan_idx)
        y_ = np.delete(y, nan_idx)
        weights = np.delete(weights, nan_idx)

        maxiter = deg

        dictionary = np.zeros((x_.shape[0], deg + 1))
        for d in range(deg + 1):
            dictionary[:, d] = x_**d

        coeffs = np.zeros(deg + 1)
        keep = np.ones_like(coeffs, dtype=np.bool)
        for it in range(maxiter):
            if np.sum(keep) == 0:
                warnings.warn(
                    'Sparsity threshold is too big, eliminated all parameters.'
                )
                break
            # coeffs_, _, _, _ = np.linalg.lstsq(dictionary[:, keep], y_)
            coeffs_ = ridge_regression(dictionary[:, keep],
                                       y_,
                                       alpha=alpha,
                                       sample_weight=weights)
            coeffs[keep] = coeffs_
            keep = (np.abs(coeffs) > threshold)
            coeffs[~keep] = 0

        return np.poly1d(np.flipud(coeffs)), x_
Example #15
0
    def _plier_on_test_data(self, X, weights, lambda_2):
        """Apply PLIER latent space transformation to test data.

        This uses the sklearn ridge regression solver to find a representation
        of the test data in the latent space B that solves:

            argmin_B ||X - ZB||_Fro^2 + lambda_2 ||B||_Fro^2

        where X is the test data, and the transformation Z and the hyperparameter
        lambda_2 were fit by PLIER on the training data (and thus are constants
        here).

        Note that the other terms in the PLIER loss function are constant in B,
        so they can be ignored here.

        Parameters
        ----------
        X : array-like, [n_samples, n_features]
            Test gene expression data.

        weights : array-like, [n_components, n_features]
            Weights found by PLIER on training data.

        lambda_2 : float
            L2 parameter returned by PLIER, used as hyperparameter in RR.

        Returns
        -------
        array_like, [n_samples, n_components]
            Representation of the test data in the PLIER latent space.
        """
        from sklearn.linear_model import ridge_regression
        from scipy.stats import zscore
        return ridge_regression(weights.T,
                                X.apply(zscore).T,
                                lambda_2,
                                solver='svd')
Example #16
0
 def fit_ridge(self):
     images = sio.loadmat(self.data_path +
                          '/images/images_natimg2800_all.mat')['imgs']
     images = images.transpose((2, 0, 1))
     images = images.reshape((2800, 68 * 270))
     reduced_images = PCA(images)
     if self.model == 'EnsemblePursuit':
         model_string = '*V_ep.npy'
     if self.model == 'SparsePCA':
         model_string = '*V_sPCA.npy'
     for filename in glob.glob(os.path.join(self.save_path, model_string)):
         print(filename)
         stim = sio.loadmat(self.data_path + '/' + filename[43:78] +
                            '.mat')['stim']['istim'][0][0]
         #test train split
         components = np.load(filename)
         x_train, x_test, y_train, y_test = test_train_split(
             components, stim)
         y_train = y_train - 1
         reduced_images_ = reduced_images[y_train]
         for alpha in [5000]:
             assembly_array = []
             for assembly in range(0, self.nr_of_components):
                 av_resp = (x_train[:, assembly].T +
                            x_test[:, assembly].T) / 2
                 reg = ridge_regression(reduced_images_,
                                        av_resp,
                                        alpha=alpha)
                 assembly_array.append(reg)
             assembly_array = np.array(assembly_array)
             if self.model == 'EnsemblePursuit':
                 file_string = filename[:-11] + '_' + str(
                     alpha) + '_ep_reg.npy'
             if self.model == 'SparsePCA':
                 file_string = filename[:-11] + '_' + str(
                     alpha) + '_sPCA_reg.npy'
             np.save(file_string, assembly_array)
Example #17
0
 def solT(X, y):
     return ridge_regression(X, y, alpha=0., solver="cholesky").T
Example #18
0
    X = X[y < 2]
    y = y[y < 2]

    y[y == 0] = -1

    # alpha = .1
    alpha = 1.

    loss = 'squaredloss'
    # loss = 'log'
    sag = SAG(loss=loss, n_iter=60, alpha=alpha, random_state=42)
    sag.fit(X, y)

    if loss == 'squaredloss':
        loss_function = loss_functions.get_loss_function(loss)
        w_opt = ridge_regression(X, y, alpha=alpha * X.shape[0])
        pobj_opt = np.mean(map(loss_function.loss, np.dot(X, w_opt), y))
        pobj_opt += alpha * np.dot(w_opt, w_opt) / 2.
    elif loss == 'log':
        # w_opt, pobj_opt = newton_logistic(X, y, alpha=alpha)
        loss_function = loss_functions.get_loss_function(loss)
        lr = LogisticRegression(fit_intercept=False, tol=1e-9, C=1./(alpha * X.shape[0]))
        w_opt = lr.fit(X, y).coef_.ravel()
        pobj_opt = np.mean(map(loss_function.loss, np.dot(X, w_opt), y))
        pobj_opt += alpha * np.dot(w_opt, w_opt) / 2.

    print(sag.pobj_[-1] - pobj_opt)

    import matplotlib.pyplot as plt
    plt.close('all')
    plt.plot(np.log10(sag.pobj_ - pobj_opt), 'r')
    def calc_charges(qxy, r0=0.000001, R0=0, alpha=0.):
        '''
        Вычисление  распределения зарядов (подход третий)
        qxy - исходное распределение зарядов и их кординаты 
        r0 - радиус заряда
        R0- расстояние от заряда до линии вычисления потенгциала
        alpha - параметр регуляризации
        функция возвращает перерассчитанный qxy и число обусловленности матрицы
        '''
        R0 = r0 if not r0<R0 else R0
        qq, xx, yy = qxy[:,0], qxy[:,1], qxy[:,2]
        q, x, y = qq.copy(), xx.copy(), yy.copy()
        n = len(q)
        nn = int(n/2)        
        # расставляю заряды        
        q[:nn] = 1. # верхний электрод
        q[nn:] = -1. # нижний электрод
        # выставляю линию  единичного потенциала - верхний электрод
        y[:nn] -= R0
        # высставляю линию нулевого потенциала
        y[nn:] += R0

        A = np.zeros((n, n))
        xy = np.hstack([x.reshape(n,1), y.reshape(n,1)])
        for i in range(n):
            A[i,:] = Charges.potential(qxy[i:i+1, :], xy, r=r0)

        # другой подход
        for i in range(n):
            for j in range(n):
                x1, y1, x2, y2 = qxy[i,1], qxy[i,2], qxy[j,1], qxy[j,2]
                if Charges.dim==2:
                    D = np.sqrt((x1-x2)**2 + (y1-y2)**2)
                    D = D if D>r0 else r0
                    A[i,j] = -np.log(D)
                if Charges.dim==3:
                    r, R = x1, x2
                    r = r if r>R0 else R0
                    R = R if R>R0 else R0
                    z = np.abs(y1-y2)
                    z = z if z>R0 else R0
                    s = np.sqrt((R+r)**2+z**2)
                    A[i,j] = 0. if z<=R0 and r<=R0 and R<=R0 else R/s*ellipk(2.*np.sqrt(r*R)/s) 
                    #print('i,j, A[i,j]', i,j, A[i,j] )

                    # nom = 2*r*R
                    # denom = r**2+R**2+z**2
                    # if denom <1e-8:
                    #     A[i,j] = 1.
                    # else:
                    #     A[i,j] = ellipeinc(np.pi/2, np.sqrt(2*r*R/(r**2+R**2+z**2)))
        #print('dim=',Charges.dim,'A=', A)
        # матрица расстояний

        # X1, X2 = np.meshgrid(x, x)
        # Y1, Y2 = np.meshgrid(y, y)
        # XX1, XX2 = np.meshgrid(xx, xx)
        # YY1, YY2 = np.meshgrid(yy, yy)

        # # попарные расстояние между зарядами
        # D = (X1-XX2)**2 +(Y1 - YY2)**2
        # # У нас не может быть нулевых расстояний, т.к.
        # # при расчете потенциалов приходится брать логарифм в двухмерном случае
        # D = np.where(D<r, r, D)
        # A = -np.log(D)
        c = cond(A) # число обусловленности
        # A*q = Fi - здесь  Fi - потенциалы на обкладках, например, 1 и -1
        # У нас как раз 1 и -1  для зарядов
        qxy[:,0] = ridge_regression(A, q, alpha)
        # проверка решения 
        u = A @  qxy[:,0]
        err = np.max(np.abs(u - q))
        #print('alpha =', alpha, 'err =', err, 'cond =', c, 'r =', r0, ' R=', R0)
        return qxy, c, err    
Example #20
0
    def ridg_reg(self, y, x, fx, ind):
        X = np.concatenate((x, fx), axis = 0)[ind].T
        coef = ridge_regression(X, y, alpha = 0.05)

        return coef
def linear_model():
    from sklearn.linear_model import ridge_regression
    parameter = ' Ridge_model'
    model = ridge_regression()
    return model, parameter
Example #22
0
 def func():
     X = np.eye(3)
     y = np.ones(3)
     ridge_regression(X, y, alpha=1., solver=wrong_solver)
 def fit_domain_classifier(self, alpha, X_all):
     #TODO Merge with the following method
     #TODO This work only for binary classifier
     C = ridge_regression(X_all, self.D_vector, alpha)
     #C=C.reshape((C.shape[0],1))
     self.C = C.reshape((C.shape[0], 1))
Example #24
0
 def _regress(self, x, y, alpha):
     kw = self.ridge_kw or {}
     coef = ridge_regression(x, y, alpha, **kw)
     self.iters += 1
     return coef