我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用sklearn.linear_model.BayesianRidge()。
def __init__(self, num_features, training_window, training_interval): """ num_features: the length of the feature vector training_window: the number of previous data points to train on training_interval: the number of data points between training periods """ self.num_features = num_features self.training_interval = training_interval self.training_window = training_window # Init sample matrix, a deque of feature vectors self.samples = deque(maxlen=training_window) self.targets = deque(maxlen=training_window) #self.model = SVR(kernel='rbf', C=1000) self.model = BayesianRidge() self.severity = blr.Severity() self.alpha = 1.0 self.parameters = 0 # Training parameters self.train_count = 0 self.have_trained = False self.pred_range = [0.0, np.inf] # upper and lower bounds for predictions
def train_bayesian_ridge(): # Picking model return mp.ModelProperties(regression=True), linear_model.BayesianRidge() # http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVR.html
def get_models4ensamble(conf): models = [] #models = [RFRModel(conf), DLModel(conf), LRModel(conf)] #models = [LRModel(conf)] # see http://scikit-learn.org/stable/modules/linear_model.html #0 was too big to run with depth set to 1, and 1 was overfitting a bit if conf.command == 1: xgb_params = {"objective": "reg:linear", "booster":"gbtree", "max_depth":3, "eta":0.1, "min_child_weight":5, "subsample":0.5, "nthread":4, "colsample_bytree":0.5, "num_parallel_tree":1, 'gamma':0} else: xgb_params = {"objective": "reg:linear", "booster":"gbtree", "max_depth":10, "eta":0.1, "min_child_weight":8, "subsample":0.5, "nthread":4, "colsample_bytree":0.5, "num_parallel_tree":1, 'gamma':0} #xgb_params = {"objective": "reg:linear", "booster":"gbtree", "max_depth":10, "eta":0.1, "min_child_weight":8, # "subsample":0.5, "nthread":4, "colsample_bytree":0.5, "num_parallel_tree":1, 'gamma':0} models = [ #DLModel(conf), #LRModel(conf, model=linear_model.BayesianRidge()), #LRModel(conf, model=linear_model.LassoLars(alpha=.1)), #LRModel(conf, model=linear_model.Lasso(alpha = 0.1)), #LRModel(conf, model=Pipeline([('poly', PolynomialFeatures(degree=3)), #LRModel(conf, model=linear_model.Ridge (alpha = .5)) # ('linear', LinearRegression(fit_intercept=False))])), XGBoostModel(conf, xgb_params, use_cv=True), LRModel(conf, model=linear_model.Lasso(alpha = 0.3)), RFRModel(conf, RandomForestRegressor(oob_score=True, n_jobs=4)), #LRModel(conf, model=linear_model.Lasso(alpha = 0.2)), ETRModel(conf, model=ExtraTreesRegressor(n_jobs=4)), #AdaBoostRModel(conf, model=AdaBoostRegressor(loss='square')) ] return models #return [XGBoostModel(conf, xgb_params, use_cv=True)]
def sklearn_train(X, y): model = BayesianRidge().fit(X, y) beta = model.alpha_ # model.alpha_ is the noise precision ('beta' in Bishop) alpha = model.lambda_ # model.lambda_ is the weights precision ('alpha' in Bishop) PhiT_Phi = X.T * X M = X.shape[1] S_N = np.linalg.pinv(alpha*np.eye(M) + beta*PhiT_Phi) m_N = beta * np.dot(S_N, np.dot(y, X).T) w_opt = m_N return (w_opt, alpha, beta, S_N) #==================== CLASSES ====================#
def function(self): params = {'n_iter': self.numOfIterationsSpinBox.value(), 'tol': self.toleranceDoubleSpinBox.value(), 'alpha_1': self.alpha1DoubleSpinBox.value(), 'alpha_2': self.alpha2DoubleSpinBox.value(), 'lambda_1': self.lambdaDoubleSpinBox.value(), 'lambda_2': self.lambdaDoubleSpinBox.value(), 'compute_score': self.computerScoreCheckBox.isChecked(), 'fit_intercept': self.fitInterceptCheckBox.isChecked(), 'normalize': self.normalizeCheckBox.isChecked(), 'copy_X': self.copyXCheckBox.isChecked(), 'verbose': self.verboseCheckBox.isChecked()} return params, self.getChangedValues(params, BayesianRidge())
def getModels(): result = [] result.append("LinearRegression") result.append("BayesianRidge") result.append("ARDRegression") result.append("ElasticNet") result.append("HuberRegressor") result.append("Lasso") result.append("LassoLars") result.append("Rigid") result.append("SGDRegressor") result.append("SVR") result.append("MLPClassifier") result.append("KNeighborsClassifier") result.append("SVC") result.append("GaussianProcessClassifier") result.append("DecisionTreeClassifier") result.append("RandomForestClassifier") result.append("AdaBoostClassifier") result.append("GaussianNB") result.append("LogisticRegression") result.append("QuadraticDiscriminantAnalysis") return result
def build_model(self): # Direct passing model parameters can be used return linear_model.BayesianRidge(normalize=True, verbose=True, compute_score=True) # ----- END first stage stacking model ----- # ----- Second stage stacking model -----
def _get_learner(self): # xgboost if self.learner_name in ["reg_xgb_linear", "reg_xgb_tree", "reg_xgb_tree_best_single_model"]: return XGBRegressor(**self.param_dict) if self.learner_name in ["clf_xgb_linear", "clf_xgb_tree"]: return XGBClassifier(**self.param_dict) # sklearn if self.learner_name == "reg_skl_lasso": return Lasso(**self.param_dict) if self.learner_name == "reg_skl_ridge": return Ridge(**self.param_dict) if self.learner_name == "reg_skl_random_ridge": return RandomRidge(**self.param_dict) if self.learner_name == "reg_skl_bayesian_ridge": return BayesianRidge(**self.param_dict) if self.learner_name == "reg_skl_svr": return SVR(**self.param_dict) if self.learner_name == "reg_skl_lsvr": return LinearSVR(**self.param_dict) if self.learner_name == "reg_skl_knn": return KNNRegressor(**self.param_dict) if self.learner_name == "reg_skl_etr": return ExtraTreesRegressor(**self.param_dict) if self.learner_name == "reg_skl_rf": return RandomForestRegressor(**self.param_dict) if self.learner_name == "reg_skl_gbm": return GradientBoostingRegressor(**self.param_dict) if self.learner_name == "reg_skl_adaboost": return AdaBoostRegressor(**self.param_dict) # keras if self.learner_name == "reg_keras_dnn": try: return KerasDNNRegressor(**self.param_dict) except: return None # rgf if self.learner_name == "reg_rgf": return RGFRegressor(**self.param_dict) # ensemble if self.learner_name == "reg_ensemble": return EnsembleLearner(**self.param_dict) return None
def greedy_select_features(self): print('initial shapes:', self.train_.shape, self.test_.shape) saved = None if self.debug_ else self.load('chosen_features') if saved == None: g_best_score = 1e9 g_best_features = [] current = set() finished = False else: g_best_features, g_best_score, finished = saved current = set(g_best_features) print('SFS REUSE:', g_best_score, len(current), sorted(g_best_features), self.now()) if not finished: col_names = self.train_.columns y = self.y_.ravel() scorer = metrics.make_scorer(metrics.log_loss) loop_count = len(col_names) - len(g_best_features) for _ in range(loop_count): avail = set(col_names).difference(current) best_score = 1e9 best_features = None for f in avail: newf = list(current | {f}) score, _ = self.ccv(linear_model.BayesianRidge(), self.train_[newf], y, scorer) if best_score > score: best_score = score best_features = newf current = set(best_features) if g_best_score > best_score: g_best_score = best_score g_best_features = best_features print('new best:', g_best_score, sorted(g_best_features), self.now()) else: print('no luck', len(current), self.now()) if len(best_features) - len(g_best_features) >= 5: break self.save('chosen_features', (g_best_features, g_best_score, False)) # now self.save('chosen_features', (g_best_features, g_best_score, True)) print('feature selection complete.', self.now()) self.train_ = self.train_[g_best_features] self.test_ = self.test_[g_best_features]
def greedy_select_features(self): saved = None if self.debug_ else self.load('chosen_features') if saved == None: print('initial shapes:', self.train_.shape, self.test_.shape) num_columns = self.train_.shape[1] col_names = [str(c) for c in range(num_columns)] self.train_.columns = col_names self.test_.columns = col_names g_best_score = 1e9 g_best_features = None y = self.y_.ravel() current = set() scorer = metrics.make_scorer(metrics.log_loss) for _ in enumerate(col_names): avail = set(col_names).difference(current) best_score = 1e9 best_features = None for f in avail: newf = list(current | {f}) cv = model_selection.cross_val_score(linear_model.BayesianRidge(), self.train_[newf], y, cv=self.n_fold_, n_jobs=-2, scoring = scorer) score = np.mean(cv) if best_score > score: best_score = score best_features = newf current = set(best_features) if g_best_score > best_score: g_best_score = best_score g_best_features = best_features print('new best:', g_best_score, g_best_features, self.now()) if len(best_features) - len(g_best_features) > 15: break self.save('chosen_features', (g_best_features, None)) else: g_best_features, _ = saved print('feature selection complete.', self.now()) self.train_ = self.train_[g_best_features] self.test_ = self.test_[g_best_features]
def greedy_select_features(self): print('initial shapes:', self.train_.shape, self.test_.shape) saved = None if self.debug_ else self.load('chosen_features') if saved == None: g_best_score = 1e9 g_best_features = [] current = set() finished = False else: g_best_features, g_best_score, finished = saved current = set(g_best_features) print('SFS REUSE:', g_best_score, g_best_features, self.now()) num_columns = self.train_.shape[1] col_names = [str(c) for c in range(num_columns)] self.train_.columns = col_names self.test_.columns = col_names if not finished: y = self.y_.ravel() scorer = metrics.make_scorer(metrics.log_loss) loop_count = len(col_names) - len(g_best_features) for _ in range(loop_count): avail = set(col_names).difference(current) best_score = 1e9 best_features = None for f in avail: newf = list(current | {f}) score, _ = self.ccv(linear_model.BayesianRidge(), self.train_[newf], y, scorer) if best_score > score: best_score = score best_features = newf current = set(best_features) if g_best_score > best_score: g_best_score = best_score g_best_features = best_features print('new best:', g_best_score, g_best_features, self.now()) if len(best_features) - len(g_best_features) > 5: break self.save('chosen_features', (g_best_features, g_best_score, False)) # now self.save('chosen_features', (g_best_features, g_best_score, True)) print('feature selection complete.', self.now()) self.train_ = self.train_[g_best_features] self.test_ = self.test_[g_best_features]
def getSKLearnModel(modelName): if modelName == 'LinearRegression': model = linear_model.LinearRegression() elif modelName == 'BayesianRidge': model = linear_model.BayesianRidge() elif modelName == 'ARDRegression': model = linear_model.ARDRegression() elif modelName == 'ElasticNet': model = linear_model.ElasticNet() elif modelName == 'HuberRegressor': model = linear_model.HuberRegressor() elif modelName == 'Lasso': model = linear_model.Lasso() elif modelName == 'LassoLars': model = linear_model.LassoLars() elif modelName == 'Rigid': model = linear_model.Ridge() elif modelName == 'SGDRegressor': model = linear_model.SGDRegressor() elif modelName == 'SVR': model = SVR() elif modelName=='MLPClassifier': model = MLPClassifier() elif modelName=='KNeighborsClassifier': model = KNeighborsClassifier() elif modelName=='SVC': model = SVC() elif modelName=='GaussianProcessClassifier': model = GaussianProcessClassifier() elif modelName=='DecisionTreeClassifier': model = DecisionTreeClassifier() elif modelName=='RandomForestClassifier': model = RandomForestClassifier() elif modelName=='AdaBoostClassifier': model = AdaBoostClassifier() elif modelName=='GaussianNB': model = GaussianNB() elif modelName=='LogisticRegression': model = linear_model.LogisticRegression() elif modelName=='QuadraticDiscriminantAnalysis': model = QuadraticDiscriminantAnalysis() return model
def get_model_list(task_name): model_list, name_list = [], [] model_list.append(linear_model.LinearRegression()) name_list.append('LR') # model_list.append(linear_model.SGDRegressor()) name_list.append('LR_SGD') model_list.append(linear_model.Lasso(alpha = 1.0)) name_list.append('Lasso') model_list.append(linear_model.Ridge (alpha = 1.0)) name_list.append('Ridge') model_list.append(linear_model.LassoLars(alpha=.1)) name_list.append('LassoLars') model_list.append(linear_model.BayesianRidge()) name_list.append('BayesianRidge') model_list.append(KernelRidge(alpha=1.0)) name_list.append('KernelRidge') model_list.append(gaussian_process.GaussianProcess(theta0=1e-2, thetaL=1e-4, thetaU=1e-1)) name_list.append('GaussianProcess') model_list.append(KNeighborsRegressor(weights = 'uniform',n_neighbors=3)) name_list.append('KNN_unif') model_list.append(KNeighborsRegressor(weights = 'distance',n_neighbors=3)) name_list.append('KNN_dist') model_list.append(SVR(kernel = 'linear', C = 1, gamma = 'auto', coef0 = 0, degree = 2)) name_list.append('SVM_linear') model_list.append(SVR(kernel = 'poly', C = 1, gamma = 'auto', coef0 = 0, degree = 2)) name_list.append('SVM_poly') model_list.append(SVR(kernel = 'rbf', C = 1, gamma = 'auto', coef0 = 0, degree = 2)) name_list.append('SVM_rbf') model_list.append(DecisionTreeRegressor()) name_list.append('DT') model_list.append(RandomForestRegressor(n_estimators=100, max_depth=None,min_samples_split=2, random_state=0)) name_list.append('RF') model_list.append(ExtraTreesRegressor(n_estimators=100, max_depth=None, max_features='auto', min_samples_split=2, random_state=0)) name_list.append('ET') return model_list, name_list