Python sklearn.linear_model.Ridge() Examples
The following are 30
code examples of sklearn.linear_model.Ridge().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sklearn.linear_model
, or try the search function
.
Example #1
Source File: test_search.py From Mastering-Elasticsearch-7.0 with MIT License | 7 votes |
def test_empty_cv_iterator_error(): # Use global X, y # create cv cv = KFold(n_splits=3).split(X) # pop all of it, this should cause the expected ValueError [u for u in cv] # cv is empty now train_size = 100 ridge = RandomizedSearchCV(Ridge(), {'alpha': [1e-3, 1e-2, 1e-1]}, cv=cv, n_jobs=-1) # assert that this raises an error with pytest.raises(ValueError, match='No fits were performed. ' 'Was the CV iterator empty\\? ' 'Were there no candidates\\?'): ridge.fit(X[:train_size], y[:train_size])
Example #2
Source File: main.py From nni with MIT License | 6 votes |
def get_model(PARAMS): '''Get model according to parameters''' model_dict = { 'LinearRegression': LinearRegression(), 'Ridge': Ridge(), 'Lars': Lars(), 'ARDRegression': ARDRegression() } if not model_dict.get(PARAMS['model_name']): LOG.exception('Not supported model!') exit(1) model = model_dict[PARAMS['model_name']] model.normalize = bool(PARAMS['normalize']) return model
Example #3
Source File: test_estimatortransformer.py From scikit-lego with MIT License | 6 votes |
def test_shape(random_xy_dataset_regr): X, y = random_xy_dataset_regr m = X.shape[0] pipeline = Pipeline( [ ( "ml_features", FeatureUnion( [ ("model_1", EstimatorTransformer(LinearRegression())), ("model_2", EstimatorTransformer(Ridge())), ] ), ) ] ) assert pipeline.fit(X, y).transform(X).shape == (m, 2)
Example #4
Source File: test_multioutput.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_base_chain_crossval_fit_and_predict(): # Fit chain with cross_val_predict and verify predict # performance X, Y = generate_multilabel_dataset_with_correlations() for chain in [ClassifierChain(LogisticRegression()), RegressorChain(Ridge())]: chain.fit(X, Y) chain_cv = clone(chain).set_params(cv=3) chain_cv.fit(X, Y) Y_pred_cv = chain_cv.predict(X) Y_pred = chain.predict(X) assert Y_pred_cv.shape == Y_pred.shape assert not np.all(Y_pred == Y_pred_cv) if isinstance(chain, ClassifierChain): assert jaccard_score(Y, Y_pred_cv, average='samples') > .4 else: assert mean_squared_error(Y, Y_pred_cv) < .25
Example #5
Source File: test_search.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_classes__property(): # Test that classes_ property matches best_estimator_.classes_ X = np.arange(100).reshape(10, 10) y = np.array([0] * 5 + [1] * 5) Cs = [.1, 1, 10] grid_search = GridSearchCV(LinearSVC(random_state=0), {'C': Cs}) grid_search.fit(X, y) assert_array_equal(grid_search.best_estimator_.classes_, grid_search.classes_) # Test that regressors do not have a classes_ attribute grid_search = GridSearchCV(Ridge(), {'alpha': [1.0, 2.0]}) grid_search.fit(X, y) assert not hasattr(grid_search, 'classes_') # Test that the grid searcher has no classes_ attribute before it's fit grid_search = GridSearchCV(LinearSVC(random_state=0), {'C': Cs}) assert not hasattr(grid_search, 'classes_') # Test that the grid searcher has no classes_ attribute without a refit grid_search = GridSearchCV(LinearSVC(random_state=0), {'C': Cs}, refit=False) grid_search.fit(X, y) assert not hasattr(grid_search, 'classes_')
Example #6
Source File: test_support.py From hyperparameter_hunter with MIT License | 6 votes |
def test_do_not_validate(env_boston): exp = CVExperiment( model_initializer=Ridge, model_init_params={}, feature_engineer=FeatureEngineer([standard_scale], do_validate=False), ) for step in exp.feature_engineer.steps: assert step.original_hashes == {} assert step.updated_hashes == {} ################################################## # `FeatureEngineer.inverse_transform` TypeError Tests ################################################## # noinspection PyUnusedLocal
Example #7
Source File: SparseLinearRegressionSolver.py From SparkADMM with Apache License 2.0 | 6 votes |
def solveSingle(self,inputDF,outputDict,rho,beta_target): I,J,V,Y=[],[],[],[] fd = {} # mapping feature names to consecutive integers, starting with 0 for i,(id, x) in enumerate(inputDF.items()): l = outputDict.get(id) for k,v in x.items(): I.append(i) J.append(k) V.append(v) upd(fd,k) Y.append(l) J = map(lambda k: fd[k], J) X = sparse.coo_matrix((V,(I,J)),shape=(I[-1]+1,len(fd))) fd_reverse = [k for k,v in sorted(fd.items(), key = lambda t: t[1])] # y_new = y - X . beta_target # converting a proximal least square problem to a ridge regression ZmUl = np.array([beta_target.get(k,0) for k in fd_reverse]) y_new = np.array(Y) - X * ZmUl ridge = Ridge(alpha = rho , fit_intercept=False) ret = ridge.fit(X,y_new) #ret = self.lr.fit(X,y_new) # ordered list of feature names according to their integer ids in fd #raise ValueError('fd_reverse = %s \n X = %s \n J = %s \n I = %s \n V = %s \n Y = %s \n y_new = %s \n ret.coef_ = %s \n ZmUl = %s \n'\ # %(str(fd_reverse), str(X), str(J), str(I), str(V), str(Y), str(y_new), str(ret.coef_), str(ZmUl))) return dict(zip(fd_reverse, (ret.coef_ + ZmUl).tolist()))
Example #8
Source File: test_common.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def _tested_estimators(): for name, Estimator in all_estimators(): if issubclass(Estimator, BiclusterMixin): continue if name.startswith("_"): continue # FIXME _skip_test should be used here (if we could) required_parameters = getattr(Estimator, "_required_parameters", []) if len(required_parameters): if required_parameters in (["estimator"], ["base_estimator"]): if issubclass(Estimator, RegressorMixin): estimator = Estimator(Ridge()) else: estimator = Estimator(LinearDiscriminantAnalysis()) else: warnings.warn("Can't instantiate estimator {} which requires " "parameters {}".format(name, required_parameters), SkipTestWarning) continue else: estimator = Estimator() yield name, estimator
Example #9
Source File: gd_poisoners.py From manip-ml with MIT License | 6 votes |
def learn_model(self, x, y, clf, lam = None): if (lam is None and self.initlam != -1): # hack for first training lam = self.initlam if clf is None: if lam is None: clf = linear_model.LassoCV(max_iter=10000) clf.fit(x, y) lam = clf.alpha_ clf = linear_model.Lasso(alpha = lam, \ max_iter = 10000, \ warm_start = True) clf.fit(x, y) return clf, lam ############################################################################################ # Implements GD Poisoning for Ridge Linear Regression ############################################################################################
Example #10
Source File: test_validation.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_cross_val_score_with_score_func_regression(): X, y = make_regression(n_samples=30, n_features=20, n_informative=5, random_state=0) reg = Ridge() # Default score of the Ridge regression estimator scores = cross_val_score(reg, X, y, cv=5) assert_array_almost_equal(scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2) # R2 score (aka. determination coefficient) - should be the # same as the default estimator score r2_scores = cross_val_score(reg, X, y, scoring="r2", cv=5) assert_array_almost_equal(r2_scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2) # Mean squared error; this is a loss function, so "scores" are negative neg_mse_scores = cross_val_score(reg, X, y, cv=5, scoring="neg_mean_squared_error") expected_neg_mse = np.array([-763.07, -553.16, -274.38, -273.26, -1681.99]) assert_array_almost_equal(neg_mse_scores, expected_neg_mse, 2) # Explained variance scoring = make_scorer(explained_variance_score) ev_scores = cross_val_score(reg, X, y, cv=5, scoring=scoring) assert_array_almost_equal(ev_scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2)
Example #11
Source File: test_multioutput.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_base_chain_random_order(): # Fit base chain with random order X, Y = generate_multilabel_dataset_with_correlations() for chain in [ClassifierChain(LogisticRegression()), RegressorChain(Ridge())]: chain_random = clone(chain).set_params(order='random', random_state=42) chain_random.fit(X, Y) chain_fixed = clone(chain).set_params(order=chain_random.order_) chain_fixed.fit(X, Y) assert_array_equal(chain_fixed.order_, chain_random.order_) assert_not_equal(list(chain_random.order), list(range(4))) assert_equal(len(chain_random.order_), 4) assert_equal(len(set(chain_random.order_)), 4) # Randomly ordered chain should behave identically to a fixed order # chain with the same order. for est1, est2 in zip(chain_random.estimators_, chain_fixed.estimators_): assert_array_almost_equal(est1.coef_, est2.coef_)
Example #12
Source File: test_support.py From hyperparameter_hunter with MIT License | 6 votes |
def test_feature_engineer_list_experiment_inequality(env_boston, steps_0, steps_1): """Test that the `feature_engineer` attribute constructed by :class:`~hyperparameter_hunter.experiments.CVExperiment` is NOT the same when given a list as input vs. a :class:`~hyperparameter_hunter.feature_engineering.FeatureEngineer` when the two are actually different. This is an insanity test to make sure that the related test in this module, :func:`test_feature_engineer_list_experiment_equality`, is not simply equating everything""" exp_0 = CVExperiment(Ridge, feature_engineer=steps_0) exp_1 = CVExperiment(Ridge, feature_engineer=FeatureEngineer(steps_1)) assert exp_0.feature_engineer != exp_1.feature_engineer # Repeat above, but switch which steps are wrapped in `FeatureEngineer` exp_2 = CVExperiment(Ridge, feature_engineer=steps_1) exp_3 = CVExperiment(Ridge, feature_engineer=FeatureEngineer(steps_0)) assert exp_2.feature_engineer != exp_3.feature_engineer ################################################## # OptPros: `FeatureEngineer` as List ################################################## #################### Equality ####################
Example #13
Source File: neural_net.py From Quadflor with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, probabilistic_estimator, stepsize=0.01, verbose=0, fit_intercept=False, sparse_output=True, **ridge_params ): """ Arguments: probabilistic_estimator -- Estimator capable of predict_proba Keyword Arguments: average -- averaging method for f1 score stepsize -- stepsize for the exhaustive search of optimal threshold fit_intercept -- fit intercept in Ridge regression sparse_output -- Predict returns csr in favor of ndarray **ridge_params -- Passed down to Ridge regression """ self.model = probabilistic_estimator self.verbose = verbose self.ridge = Ridge(fit_intercept=fit_intercept, **ridge_params) self.stepsize = stepsize self.sparse_output = sparse_output
Example #14
Source File: linearregressionmodel.py From Supply-demand-forecasting with MIT License | 5 votes |
def setClf(self): # self.clf = Ridge(alpha=0.0000001, tol=0.0000001) clf = LinearRegression() min_max_scaler = preprocessing.MinMaxScaler() self.clf = Pipeline([('scaler', min_max_scaler), ('estimator', clf)]) return
Example #15
Source File: test_correlation_score.py From scikit-lego with MIT License | 5 votes |
def test_corr_numpy(): df = pd.DataFrame( { "x1": [1, 2, 3, 4, 5, 6, 7, 8], "x2": [0, 0, 0, 1, 0, 0, 0, 0], "y": [2, 3, 4, 6, 6, 7, 8, 9], } ) arr = df[["x1", "x2"]].values mod = Ridge().fit(arr, df["y"]) assert abs(correlation_score(0)(mod, arr)) > abs(0.99) assert abs(correlation_score(1)(mod, arr)) < abs(0.02)
Example #16
Source File: test_codec.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def test_Ridge(self): RidgeAlgo.register_codecs() self.regressor_util(Ridge)
Example #17
Source File: test_both_stages_transform.py From hyperparameter_hunter with MIT License | 5 votes |
def engineer_experiment(request): feature_engineer = FeatureEngineer(steps=request.param) experiment = CVExperiment( model_initializer=Ridge, model_init_params=dict(), feature_engineer=feature_engineer ) return experiment # TODO: Basically identical to `test_intra_cv_target_transform` except for repeated KFold
Example #18
Source File: test_feature_optimization.py From hyperparameter_hunter with MIT License | 5 votes |
def fe_optimizer(request): if request.param is not None: request.param = FeatureEngineer(request.param) opt = BayesianOptPro() opt.forge_experiment( model_initializer=Ridge, model_init_params={}, feature_engineer=request.param ) opt.go() return opt
Example #19
Source File: Ridge.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def __init__(self, options): self.handle_options(options) out_params = convert_params( options.get('params', {}), bools=['fit_intercept', 'normalize'], floats=['alpha'], ) out_params.setdefault('normalize', True) self.estimator = _Ridge(**out_params)
Example #20
Source File: target_transform_inverse_example.py From hyperparameter_hunter with MIT License | 5 votes |
def execute(): #################### Environment #################### env = Environment( train_dataset=get_boston_data(), results_path="HyperparameterHunterAssets", holdout_dataset=get_holdout_data, target_column="DIS", metrics=["r2_score", "median_absolute_error"], cv_type="KFold", cv_params=dict(n_splits=10, random_state=1), ) #################### CVExperiment #################### exp_0 = CVExperiment( model_initializer=Ridge, model_init_params=dict(), feature_engineer=FeatureEngineer([quantile_transform]), ) #################### Optimization #################### # `opt_0` recognizes `exp_0`'s `feature_engineer` and its results as valid learning material # This is because `opt_0` marks the engineer step functions omitted by `exp_0` as `optional=True` opt_0 = DummyOptPro(iterations=10) opt_0.forge_experiment( model_initializer=Ridge, model_init_params=dict(), feature_engineer=FeatureEngineer( [ Categorical([quantile_transform, log_transform], optional=True), Categorical([standard_scale, standard_scale_BAD], optional=True), Categorical([square_sum_feature], optional=True), ] ), ) opt_0.go()
Example #21
Source File: _twiesn.py From sktime-dl with BSD 3-Clause "New" or "Revised" License | 5 votes |
def evaluate_paramset(self, X, y, val_X, val_y, rho, config): # param setting is correct. self.rho = rho self.N_x = config['N_x'] self.connect = config['connect'] self.scaleW_in = config['scaleW_in'] self.lamda = config['lamda'] # init transformer based on paras. self.init_matrices() # transformed X x_transformed = self.transform_to_feature_space(X) new_train_labels = np.repeat(y, self.T, axis=0) ridge_classifier = Ridge(alpha=self.lamda) ridge_classifier.fit(x_transformed, new_train_labels) # transform Validation and labels x_val_transformed = self.transform_to_feature_space(val_X) new_val_labels = np.repeat(val_y, self.T, axis=0) val_preds = ridge_classifier.predict(x_val_transformed) y_pred_val = self.reshape_prediction(val_preds, val_X.shape[0], self.T) # calculate validation accuracy # argmax the val_y because it is in onehot encoding. return accuracy_score(np.argmax(val_y, axis=1), y_pred_val)
Example #22
Source File: _twiesn.py From sktime-dl with BSD 3-Clause "New" or "Revised" License | 5 votes |
def build_model(self, input_shape, nb_classes, **kwargs): self.init_matrices() # construct the riger classifier model self.ridge_classifier = Ridge(alpha=self.lamda)
Example #23
Source File: test_examples.py From gplearn with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_symbolic_transformer(): """Check that SymbolicTransformer example works""" rng = check_random_state(0) boston = load_boston() perm = rng.permutation(boston.target.size) boston.data = boston.data[perm] boston.target = boston.target[perm] est = Ridge() est.fit(boston.data[:300, :], boston.target[:300]) assert_almost_equal(est.score(boston.data[300:, :], boston.target[300:]), 0.759319453049884) function_set = ['add', 'sub', 'mul', 'div', 'sqrt', 'log', 'abs', 'neg', 'inv', 'max', 'min'] gp = SymbolicTransformer(generations=20, population_size=2000, hall_of_fame=100, n_components=10, function_set=function_set, parsimony_coefficient=0.0005, max_samples=0.9, random_state=0) gp.fit(boston.data[:300, :], boston.target[:300]) gp_features = gp.transform(boston.data) new_boston = np.hstack((boston.data, gp_features)) est = Ridge() est.fit(new_boston[:300, :], boston.target[:300]) assert_almost_equal(est.score(new_boston[300:, :], boston.target[300:]), 0.8418372105182055)
Example #24
Source File: gd_poisoners.py From manip-ml with MIT License | 5 votes |
def learn_model(self, x, y, clf, lam = None): lam = 0.1 clf = linear_model.Ridge(alpha = lam, max_iter = 10000) clf.fit(x, y) return clf, lam ############################################################################################ # Implements GD Poisoning for Elastic Net Linear Regression ############################################################################################
Example #25
Source File: gd_poisoners.py From manip-ml with MIT License | 5 votes |
def learn_model(self, x, y, clf): if (not clf): clf = linear_model.Ridge(alpha=0.00001) clf.fit(x, y) return clf, 0
Example #26
Source File: models.py From automl-phase-2 with MIT License | 5 votes |
def __init__(self, info, verbose=True, debug_mode=False): self.label_num=info['label_num'] self.target_num=info['target_num'] self.task = info['task'] self.metric = info['metric'] self.postprocessor = None #self.postprocessor = MultiLabelEnsemble(LogisticRegression(), balance=True) # To calibrate proba self.postprocessor = MultiLabelEnsemble(LogisticRegression(), balance=False) # To calibrate proba if debug_mode>=2: self.name = "RandomPredictor" self.model = RandomPredictor(self.target_num) self.predict_method = self.model.predict_proba return if info['task']=='regression': if info['is_sparse']==True: self.name = "BaggingRidgeRegressor" self.model = BaggingRegressor(base_estimator=Ridge(), n_estimators=1, verbose=verbose) # unfortunately, no warm start... else: self.name = "GradientBoostingRegressor" self.model = GradientBoostingRegressor(n_estimators=1, verbose=verbose, warm_start = True) self.predict_method = self.model.predict # Always predict probabilities else: if info['has_categorical']: # Out of lazziness, we do not convert categorical variables... self.name = "RandomForestClassifier" self.model = RandomForestClassifier(n_estimators=1, verbose=verbose) # unfortunately, no warm start... elif info['is_sparse']: self.name = "BaggingNBClassifier" self.model = BaggingClassifier(base_estimator=BernoulliNB(), n_estimators=1, verbose=verbose) # unfortunately, no warm start... else: self.name = "GradientBoostingClassifier" self.model = eval(self.name + "(n_estimators=1, verbose=" + str(verbose) + ", min_samples_split=10, random_state=1, warm_start = True)") if info['task']=='multilabel.classification': self.model = MultiLabelEnsemble(self.model) self.predict_method = self.model.predict_proba
Example #27
Source File: linear_adapter_test.py From chelmbigstock with GNU General Public License v3.0 | 5 votes |
def test_ridge_regression(self): clf = linear_model.Ridge(alpha = 0.5) predictor = stock_value.LinearAdapter(clf) for x, y, ex, ey in self._test_RR: predictor.fit(x, y) self.assertTrue(self.equal_floats(ey, predictor.predict([ex])))
Example #28
Source File: stock_value_test.py From chelmbigstock with GNU General Public License v3.0 | 5 votes |
def setUpClass(cls): cls._my_path = os.path.abspath(os.path.dirname(__file__)) cls._result_path = os.path.join(cls._my_path, 'result') cls._expected_path = os.path.join(cls._my_path, 'expected') os.mkdir(cls._result_path) stock_value.StockHist.default_path = cls._result_path cls._predictor = stock_value.LinearAdapter(linear_model.Ridge(alpha=0.1, fit_intercept=False)) cls._comment = 'Ridge alpha=0.1' cls._histories = [] for symbol, sdate, edate in cls._test_data: cls._histories.append(stock_value.StockHist(symbol, sdate, edate))
Example #29
Source File: chelmbigstock.py From chelmbigstock with GNU General Public License v3.0 | 5 votes |
def learn(training_data, cv_data): """ I copied this function from Andy Webber's code in chelmbigstock/chelmbigstock/chelmbigstock.py almost as is, though I made some cosmetic changes. This function does the actual training. It takes in training data and cross validation data and returns the model and optimal regularization parameter """ # Setting guesses for minimum and maximum values of regularization parameter # then find the value of parameter that minimizes error on cross validation # data. If local minimum is found the return this model. If not, # extend minimum or maximum appropriately and repeat alpha_min = 0.01 alpha_max = 0.2 regularization_flag = 1 # To set 1 until local minimum is found regularization_param = 0 while regularization_flag != 0: regularization_param, regularization_flag = set_reg_param( training_data, cv_data, alpha_min, alpha_max) if regularization_flag == -1: # The local minimum is at point less than alpha_min alpha_min = alpha_min * 0.3 if regularization_flag == 1: # The local minimum is at point greater then alpha_max alpha_max = alpha_max * 3 clf = linear_model.Ridge (alpha=regularization_param, fit_intercept=False) clf.fit(training_data.X, training_data.y) return clf, regularization_param
Example #30
Source File: test_TimeSeriesRegressor.py From fireTS with MIT License | 5 votes |
def test_TimeSeriesRegressor_grid_search(): np.random.seed(0) X = pd.DataFrame(np.random.randn(100, 2)) y = pd.Series(np.random.randn(100)) na = 3 nb = [3, 3] nk = [1, 1] mdl = NARX(Ridge(), auto_order=na, exog_order=nb, exog_delay=nk) para_grid = {'alpha': [0, 0.1, 0.3]} mdl.grid_search(X, y, para_grid)