def __init__(self, data=None, classifier='linear', save=True, load=False, train=True, fname='FASMA_ML.pkl'):
        self.classifier = classifier
        self.data = data
        self.save = save
        self.fname = fname
        if data is not None:
            self.X_train, self.y_train = data.X, data.y
            self.xlabel = data.xlabel

        if self.classifier == 'linear':
            self.clf = linear_model.LinearRegression(n_jobs=-1, normalize=True)
        elif self.classifier == 'lasso':
            self.clf = linear_model.Lasso(alpha=0.001, tol=0.001, max_iter=1000)
        elif self.classifier == 'lassolars':
            self.clf = linear_model.LassoLars(alpha=0.1)
        elif self.classifier == 'multilasso':
            self.clf = linear_model.MultiTaskLasso(alpha=0.1)
        elif self.classifier == 'ridgeCV':
            self.clf = linear_model.RidgeCV(alphas=[0.1, 1., 10., 100.])
        elif self.classifier == 'ridge':
            self.clf = linear_model.Ridge(alpha=0.0001)
        elif self.classifier == 'bayes':
            self.clf = linear_model.BayesianRidge()
        elif self.classifier == 'huber':
            self.clf = linear_model.HuberRegressor()

        # Train the classifier
        if not load or train:
            t = time()
            self.train_classifier()
            print('Trained classifier in {}s'.format(round(time()-t, 2)))
        else:
            self.clf, self.data = Model.load_model(self.fname)
Exemplo n.º 2
0
def multi_task_weighted_elastic_net(X, q, L, alpha=0.001, rho=0.99999, path=False, measure_time=False):
    '''
    Since sklearn does not support weighted version(for us weighted L2 regularization) of Elastic Net, we transform multitask elastic net to multitask lasso
    solve it and then transform the solution back to elastic net

    L is the accelerations that composes the weight matrix, i.e. W = L'* L
    '''
    time0 = time.time()
    N = q.shape[0]
    d = q.shape[1]  # 7
    lamb1 = 2*N*alpha*rho
    lamb2 = N*alpha*(1-rho)
    q_bar = np.vstack((q, np.zeros((N, d))))
    mult = np.sqrt(1.0/(1+lamb2))
    Xdot2 = L  # create_acc_weight_mat(c, w, t, include_intercept=True)
    # print Xdot2[0:5, 0:5]
    X_bar = mult * np.vstack((X, np.sqrt(lamb2)*Xdot2))
    # clf = lm.MultiTaskLassoCV(eps=1e-3, n_alphas=100, alphas=None,
    #                          fit_intercept=False, cv=10, verbose=True, n_jobs=-1)
    clf = lm.MultiTaskLasso(alpha=alpha, fit_intercept=False)
    clf.fit(X_bar, q_bar)
    theta = clf.coef_.T * mult
    res = q - np.dot(X, theta)

    if path:
        res = clf.path(X_bar, q_bar, l1_ratio=0.5, n_alphas=100)
        coefs = res[1]
        theta_path = coefs.T * mult
    else:
        theta_path = []

    if measure_time:
        time_elapsed = time.time() - time0
        print 'Elastic net took {0} sec'.format(time_elapsed)
    return theta, res, theta_path
def train_models(mod):

    print('Selected model: %s' % mod)
    if mod == 'linear':
        clf = linear_model.LinearRegression(n_jobs=-1)
    elif mod == 'lasso':
        clf = linear_model.Lasso(alpha=0.001,
                                 max_iter=10000,
                                 tol=0.001,
                                 normalize=True,
                                 positive=True)
    elif mod == 'lassolars':
        clf = linear_model.LassoLars(alpha=0.001)
    elif mod == 'multilasso':
        clf = linear_model.MultiTaskLasso(alpha=0.1)
    elif mod == 'ridgeCV':
        clf = linear_model.RidgeCV(
            alphas=[0.01, 0.1, 1.0, 10.0, 100.0, 1000.0])
    elif mod == 'ridge':
        clf = linear_model.Ridge(alpha=[0.001])
    elif mod == 'bayes':
        clf = linear_model.BayesianRidge()
    elif mod == 'huber':
        clf = linear_model.HuberRegressor()
    elif mod == 'poly':
        clf = poly_clf()

    clf, continuum = train(clf, mod, save=True, plot=True)
    return clf, continuum
def test_lasso_alpha(*data):
    X_train, X_test, y_train, y_test = data
    alphas = [0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000]
    scores = []
    for i, alpha in enumerate(alphas):
        lassoRegression = linear_model.MultiTaskLasso(alpha=alpha)
        lassoRegression.fit(X_train, y_train)
        scores.append(lassoRegression.score(X_test, y_test))
    return alphas, scores
Exemplo n.º 5
0
    def test_compare_lasso(self):
        from mr_utils.recon.reordering import scr_reordering_adluru
        from sklearn import linear_model

        reg = linear_model.MultiTaskLasso(alpha=.0001)
        reg.fit(self.t[None, :], np.abs(self.smooth_sig_corrupted)[None, :])
        print(reg)
        recon = reg.coef_.reshape(-1, 1)
        print(recon)
        plt.plot(recon)
        plt.show()
Exemplo n.º 6
0
 def __init__(self, regressorType):
     self.regressorType = regressorType
     if regressorType == "ridge":
         self.regModel = linear_model.Ridge(alpha=0.5)
     elif regressorType == "logistic":
         self.regModel = linear_model.LogisticRegression(fit_intercept=True,penalty='l2')
     elif regressorType == "linear":
         self.regModel = linear_model.LinearRegression()
     elif regressorType == "lasso":
         self.regModel = linear_model.MultiTaskLasso(alpha=0.1)
     else:
         print("Regressor Not Initialized")
Exemplo n.º 7
0
 def test_model_multi_task_lasso(self):
     model, X = fit_regression_model(linear_model.MultiTaskLasso(),
                                     n_targets=2)
     model_onnx = convert_sklearn(
         model,
         "multi-task lasso",
         [("input", FloatTensorType([None, X.shape[1]]))],
         target_opset=TARGET_OPSET)
     self.assertIsNotNone(model_onnx)
     dump_data_and_model(X,
                         model,
                         model_onnx,
                         verbose=False,
                         basename="SklearnMultiTaskLasso-Dec4")
Exemplo n.º 8
0
def multi_task_lasso(X, q, cv=False, alpha=0.002):
    '''
    Multi Task Lasso with dimensions forced to share features
    Running multi task Lasso with cross-validation gives 0.002
    '''
    if cv:
        clf = lm.MultiTaskLassoCV(eps=1e-3, n_alphas=100, alphas=None,
                                  fit_intercept=False, cv=10, verbose=True, n_jobs=-1)
    else:
        clf = lm.MultiTaskLasso(alpha=alpha, fit_intercept=False)
    clf.fit(X, q)
    theta = clf.coef_.T
    res = q - np.dot(X, theta)
    return theta, res
 def test_model_multi_task_lasso(self):
     model, X = _fit_model_multi(linear_model.MultiTaskLasso())
     model_onnx = convert_sklearn(model, "linear regression",
                                  [("input", FloatTensorType([None, 4]))])
     self.assertIsNotNone(model_onnx)
     dump_data_and_model(
         X.astype(numpy.float32),
         model,
         model_onnx,
         verbose=False,
         basename="SklearnMultiTaskLasso-Dec4",
         allow_failure="StrictVersion("
         "onnxruntime.__version__)"
         "<= StrictVersion('0.2.1')",
     )
 def test_model_multi_task_lasso(self):
     model, X = fit_regression_model(linear_model.MultiTaskLasso(),
                                     n_targets=2)
     model_onnx = convert_sklearn(
         model, "multi-task lasso",
         [("input", FloatTensorType([None, X.shape[1]]))])
     self.assertIsNotNone(model_onnx)
     dump_data_and_model(
         X,
         model,
         model_onnx,
         verbose=False,
         basename="SklearnMultiTaskLasso-Dec4",
         allow_failure="StrictVersion("
         "onnxruntime.__version__)"
         "<= StrictVersion('0.2.1')",
     )
Exemplo n.º 11
0
def sklearn_liner_model_regressions(xTrain, xTest, yTrain, yTest):
    modelForConsideration: DataFrame = pd.DataFrame()
    LinerModels = \
        [
            linear_model.ARDRegression(), linear_model.BayesianRidge(), linear_model.ElasticNet(),
            linear_model.ElasticNetCV(),
            linear_model.HuberRegressor(), linear_model.Lars(), linear_model.LarsCV(), linear_model.Lasso(),
            linear_model.LassoCV(), linear_model.LassoLars(), linear_model.LassoLarsCV(), linear_model.LassoLarsIC(),
            linear_model.LinearRegression(), linear_model.MultiTaskLasso(),
            linear_model.MultiTaskElasticNet(), linear_model.MultiTaskLassoCV(), linear_model.MultiTaskElasticNetCV(),
            linear_model.OrthogonalMatchingPursuit(),
            linear_model.OrthogonalMatchingPursuitCV(), linear_model.PassiveAggressiveClassifier(),
            linear_model.PassiveAggressiveRegressor(), linear_model.Perceptron(),
            linear_model.RANSACRegressor(), linear_model.Ridge(), linear_model.RidgeClassifier(),
            linear_model.RidgeClassifierCV(),
            linear_model.RidgeCV(), linear_model.SGDClassifier(), linear_model.SGDRegressor(),
            linear_model.TheilSenRegressor(),
            linear_model.enet_path(xTrain, yTrain),
            linear_model.lars_path(xTrain, yTrain), linear_model.lasso_path(xTrain, yTrain),
            # linear_model.LogisticRegression()
            # ,linear_model.LogisticRegressionCV(),linear_model.logistic_regression_path(xTrain, yTrain), linear_model.orthogonal_mp(xTrain, yTrain), linear_model.orthogonal_mp_gram(), linear_model.ridge_regression()
        ]
    for model in LinerModels:
        modelName: str = model.__class__.__name__
        try:
            # print(f"Preparing Model {modelName}")
            if modelName == "LogisticRegression":
                model = linear_model.LogisticRegression(random_state=0)
            model.fit(xTrain, yTrain)
            yTrainPredict = model.predict(xTrain)
            yTestPredict = model.predict(xTest)
            errorList = calculate_prediction_error(modelName, yTestPredict,
                                                   yTest, yTrainPredict,
                                                   yTrain)

            if errorList["Test Average Error"][0] < 30 and errorList[
                    "Train Average Error"][0] < 30:
                try:
                    modelForConsideration = modelForConsideration.append(
                        errorList)
                except (Exception) as e:
                    print(e)

        except (Exception, ArithmeticError) as e:
            print(f"Error occurred while preparing Model {modelName}")
    return modelForConsideration
Exemplo n.º 12
0
def multitask_lasso():

    rng = np.random.RandomState(42)

    n_samples, n_features, n_tasks = 100, 30, 40
    n_relevant_features = 5
    coef = np.zeros((n_tasks, n_features))
    times = np.linspace(0, 2 * np.pi, n_tasks)
    for k in range(n_relevant_features):
        coef[:, k] = np.sin((1. + rng.randn(1)) * times + 3 * rng.randn(1))

    X = rng.randn(n_samples, n_features)
    Y = np.dot(X, coef.T) + rng.randn(n_samples, n_tasks)

    coef_lasso_ = np.array([linear_model.Lasso(
        alpha=0.5).fit(X, y).coef_ for y in Y.T])
    coef_multi_task_lasso_ = linear_model.MultiTaskLasso(
        alpha=1.).fit(X, Y).coef_
Exemplo n.º 13
0
    def __init__(self,
                 data,
                 classifier='linear',
                 save=True,
                 load=False,
                 alpha=0.01,
                 fname='FASMA_ML.pkl'):
        self.classifier = classifier
        self.data = data
        self.save = save
        self.load = load
        self.alpha = alpha
        self.fname = fname
        self.X_train, self.y_train = data.Xml, data.yml

        if self.classifier == 'linear':
            self.clf = Pipeline([('poly', PolynomialFeatures(degree=2)),
                                 ('linear',
                                  linear_model.LinearRegression(n_jobs=-1))])
        elif self.classifier == 'lasso':
            self.clf = Pipeline([('poly', PolynomialFeatures(degree=2)),
                                 ('lasso',
                                  linear_model.Lasso(alpha=0.0001,
                                                     max_iter=5000))])
        elif self.classifier == 'lassolars':
            self.clf = Pipeline([('poly', PolynomialFeatures(degree=2)),
                                 ('lassolars',
                                  linear_model.LassoLars(alpha=1e-06))])
        elif self.classifier == 'multilasso':
            self.clf = Pipeline([('poly', PolynomialFeatures(degree=2)),
                                 ('multilasso',
                                  linear_model.MultiTaskLasso(alpha=1e-06))])
        elif self.classifier == 'ridge':
            self.clf = Pipeline([('poly', PolynomialFeatures(degree=2)),
                                 ('ridge', linear_model.Ridge(alpha=alpha))])
        print(self.clf)
        # Train the classifier
        if not self.load:
            t = time()
            self.train_classifier()
            print('Trained classifier in {}s'.format(round(time() - t, 2)))
        else:
            with open(self.fname, 'rb') as f:
                self.clf = cPickle.load(f)
Exemplo n.º 14
0
def model_training(X,
                   Ys,
                   mode='single',
                   model_name='lasso',
                   alpha=0.001,
                   xgb_depth=3,
                   xgb_trees=100,
                   nn_solver='lbfgs',
                   nn_size=(30, 30)):

    clf_models = []

    # single-task: train one model for predicting each resource
    if mode == 'single':
        for i in range(Ys.shape[1]):
            Y = Ys[:, i]
            clf = None
            if model_name == 'lasso':
                clf = linear_model.Lasso(alpha=alpha)
            elif model_name == 'xgb':
                clf = xgb.XGBRegressor(max_depth=xgb_depth,
                                       n_estimators=xgb_trees)
            elif model_name == 'ann':
                clf = MLPRegressor(solver=nn_solver,
                                   hidden_layer_sizes=nn_size,
                                   random_state=0)

            clf.fit(X, Y)
            clf_models.append(clf)
    # multi-task: train one model for predicting all resources
    elif mode == 'multi':
        if model_name == 'lasso':
            clf = linear_model.MultiTaskLasso(alpha=alpha)
        elif model_name == 'ann':
            clf = MLPRegressor(solver=nn_solver,
                               hidden_layer_sizes=nn_size,
                               random_state=0)

        clf.fit(X, Ys)
        clf_models.append(clf)

    # return the trained model(s)
    return clf_models
Exemplo n.º 15
0
def multiTaskLasso_reg():
    """
    多任务 lasso  多元回归稀疏系数的线性模型
    y 是一个 (n_samples, n_tasks) 的二维数组,其约束条件和其他回归问题(也称为任务)是一样的,都是所选的特征值。
    """
    
    rng = np.random.RandomState(42)
    n_samples, n_features, n_tasks = 100, 30, 40
    n_relevant_features = 5
    coef = np.zeros((n_tasks, n_features))
    times = np.linspace(0, 2 * np.pi, n_tasks)
    for k in range(n_relevant_features):
        coef[:, k] = np.sin((1. + rng.randn(1)) * times + 3 * rng.randn(1))

    X = rng.randn(n_samples, n_features)
    Y = np.dot(X, coef.T) + rng.randn(n_samples, n_tasks)

    coef_lasso_ = np.array([linear_model.Lasso(alpha=0.5).fit(X, y).coef_ for y in Y.T])
    coef_multi_task_lasso_ =linear_model.MultiTaskLasso(alpha=1.).fit(X, Y).coef_

    fig = plt.figure(figsize=(8, 5))
    plt.subplot(1, 2, 1)
    plt.spy(coef_lasso_)
    plt.xlabel('Feature')
    plt.ylabel('Time (or Task)')
    plt.text(10, 5, 'Lasso')
    plt.subplot(1, 2, 2)
    plt.spy(coef_multi_task_lasso_)
    plt.xlabel('Feature')
    plt.ylabel('Time (or Task)')
    plt.text(10, 5, 'MultiTaskLasso')
    fig.suptitle('Coefficient non-zero location')

    feature_to_plot = 0
    plt.figure()
    lw = 2
    plt.plot(coef[:, feature_to_plot], color='seagreen', linewidth=lw,label='Truth Value')
    plt.plot(coef_lasso_[:, feature_to_plot], color='cornflowerblue', linewidth=lw,label='Lasso')
    plt.plot(coef_multi_task_lasso_[:, feature_to_plot], color='gold', linewidth=lw,label='MultiTaskLasso')
    plt.legend(loc='upper center')
    plt.axis('tight')
    plt.ylim([-1.1, 1.1])
    plt.show()
Exemplo n.º 16
0
 def dtc05(self):
     #将y转化为一维形式:self.y_train,self.y_test
     self.y01_train = list()
     self.y01_test = list()
     for a in range(len(self.y_train)):
         self.y01_train.append(self.y_train[a][0])
     for b in range(len(self.y_test)):
         self.y01_test.append(self.y_test[b][0])
     
     if not self.mu_edit.text().strip():
         self.mu_alpha = 1.0
     else:
         self.mu_alpha = float(self.mu_edit.text())
     #LR算法实现
     self.clf_mu = linear_model.MultiTaskLasso(alpha = self.mu_alpha) 
     self.clf_mu.fit(self.x_train, self.y01_train)
     self.y_pred = self.clf_mu.predict(self.x_test)
     self.x_pred = self.clf_mu.predict(self.x_train)
     #设置值
     self.stab(self.mu_table02, self.mu_table03)
     self.eetab(self.mu_table01)
Exemplo n.º 17
0
def train_models(mod,
                 save=True,
                 cutoff=0.999,
                 percent=50,
                 plot=True,
                 scale=False):

    if mod == 'linear':
        clf = linear_model.LinearRegression(n_jobs=-1)
    elif mod == 'lasso':
        clf = linear_model.Lasso(alpha=1000,
                                 max_iter=10000,
                                 tol=0.001,
                                 normalize=True,
                                 positive=True)
    elif mod == 'lassolars':
        clf = linear_model.LassoLars(alpha=0.001)
    elif mod == 'multilasso':
        clf = linear_model.MultiTaskLasso(alpha=0.1)
    elif mod == 'ridgeCV':
        clf = linear_model.RidgeCV(alphas=[0.01, 0.1, 1.0, 10.0])
    elif mod == 'ridge':
        clf = linear_model.Ridge(alpha=[1000])
    elif mod == 'bayes':
        clf = linear_model.BayesianRidge()
    elif mod == 'huber':
        clf = linear_model.HuberRegressor()
    elif mod == 'poly':
        #clf = poly_clf()
        clf = PolynomialFeatures(degree=2)

    clf, continuum = train(clf,
                           mod,
                           save=save,
                           cutoff=cutoff,
                           percent=percent,
                           plot=plot,
                           scale=scale)
    return clf, continuum
Exemplo n.º 18
0
    def __init__(self,
                 data,
                 classifier='linear',
                 save=True,
                 load=False,
                 fname='FASMA_ML.pkl'):
        self.classifier = classifier
        self.data = data
        self.save = save
        self.load = load
        self.fname = fname
        self.X_train, self.y_train = data.X, data.y

        if self.classifier == 'linear':
            self.clf = linear_model.LinearRegression(n_jobs=-1)
        elif self.classifier == 'lasso':
            self.clf = linear_model.Lasso(alpha=0.00001)
        elif self.classifier == 'lassolars':
            self.clf = linear_model.LassoLars(alpha=1000)
        elif self.classifier == 'multilasso':
            self.clf = linear_model.MultiTaskLasso(alpha=1000)
        elif self.classifier == 'ridgeCV':
            self.clf = linear_model.RidgeCV(alphas=[0.1, 1.0, 10.0, 100])
        elif self.classifier == 'ridge':
            self.clf = linear_model.Ridge(alpha=10)
        elif self.classifier == 'bayes':
            self.clf = linear_model.BayesianRidge()
        elif self.classifier == 'huber':
            self.clf = linear_model.HuberRegressor()

        # Train the classifier
        if not self.load:
            t = time()
            self.train_classifier()
            print('Trained classifier in {}s'.format(round(time() - t, 2)))
        else:
            with open(self.fname, 'rb') as f:
                self.clf = cPickle.load(f)
# std_Y = labels

#Standardizes features
xscaler = preprocessing.StandardScaler().fit(features)
std_X = xscaler.transform(features)
# std_X = features

# In[ ]:

test_size = 0.25
num_perm = 1000
num_alpha = 50

cv = ShuffleSplit(n_splits=num_perm, test_size=test_size, random_state=2909591)

lrp = linear_model.MultiTaskLasso(max_iter=10000, normalize=False)
param_range = np.logspace(-3, 1, num_alpha)

path = np.zeros((std_Y.shape[1], num_alpha, std_X.shape[1]))
train_scores_mean = np.zeros((num_alpha))
train_scores_std = np.zeros((num_alpha))

for i, v in enumerate(param_range):
    lrp.set_params(alpha=v)
    lrp.fit(std_X, std_Y)
    path[:, i, :] = lrp.coef_
    cvs = cross_val_score(lrp,
                          std_X,
                          std_Y,
                          cv=cv,
                          scoring='neg_mean_squared_error')
Exemplo n.º 20
0
import pytest
from sklearn import linear_model
import numpy
import chaospy

LINEAR_MODELS = {
    "none":
    None,
    "linear":
    linear_model.LinearRegression(fit_intercept=False),
    "elastic_net":
    linear_model.MultiTaskElasticNet(alpha=0.0001, fit_intercept=False),
    "lasso":
    linear_model.MultiTaskLasso(alpha=0.001, fit_intercept=False),
    "lasso_lars":
    linear_model.LassoLars(alpha=0.0001, fit_intercept=False),
    "lars":
    linear_model.Lars(n_nonzero_coefs=10, fit_intercept=False),
    "matching_pursuit":
    linear_model.OrthogonalMatchingPursuit(n_nonzero_coefs=10,
                                           fit_intercept=False),
    "ridge":
    linear_model.Ridge(alpha=0.1, fit_intercept=False),
}


@pytest.fixture
def samples(joint):
    return joint.sample(1000, rule="sobol")

Exemplo n.º 21
0
 def __init__(self, args):
     alpha = 1 - float(args.sparse) if args.sparse is not None else 1.0
     self.linear = linear_model.MultiTaskLasso(alpha=alpha)
Exemplo n.º 22
0
def estimate_baseline_lasso(Ton,
                            Tamb=273.0,
                            order=0,
                            timechunk=80,
                            cv=None,
                            progress=True,
                            **kwargs):
    """Estimate ultra-wideband baseline using the multi-task LASSO.

    Args:
        Ton (xarray.DataArray): Calibrated De:code array of ON point.
        Tamb (float, optional): Ambient temperature used in calibration.
        order (int, optional): Maximum order of a polynomial function
            which is assumed to represent a continuum emission spectrum.
            Default is 0 (flat continuum emission).
        timechunk (int, optional): The number of samples to be used for
            a multi-task LASSO. Default is 80 (~0.5 s for DESHIMA data).
        cv (int, optional): The number of fold for cross validation (CV).
            If not spacified, CV is not conducted (default alpha is used).
        progress (bool, optional): If True, then a progress bar is shown.
        kwargs (dict, optional): Keyword arguments for model initialization.

    Returns:
        Tbase (xarray.DataArray): De:code array of estimated baseline.

    """
    freq = np.asarray(Ton.kidfq).copy()
    slope = fn.models._calculate_dtau_dpwv(freq)
    freq -= np.median(freq)

    N_freq = len(freq)
    N_poly = order + 1

    X = np.zeros([N_freq, N_poly + 1])
    X[:, 0] = slope / np.linalg.norm(slope)

    for i in range(N_poly):
        poly = freq**i
        X[:, i + 1] = poly / np.linalg.norm(poly)

    default_kwargs = {'fit_intercept': False}
    kwargs = {**default_kwargs, **kwargs}
    n_chunk = int(len(Ton) / timechunk)
    is_cv = cv is not None and cv > 1

    if is_cv:
        model = linear_model.MultiTaskLassoCV(cv=cv, **kwargs)
    else:
        model = linear_model.MultiTaskLasso(**kwargs)

    with tqdm(total=n_chunk, disable=not progress) as bar:

        def func(Ton_):
            model.fit(X, Ton_.values.T)

            Tbase_ = np.outer(model.coef_[:, 0], X[:, 0])
            Tbase_ = dc.full_like(Ton_, Tbase_)

            for i in range(N_poly + 1):
                Tbase_.coords[f'basis_{i}'] = 'ch', X[:, i]
                Tbase_.coords[f'coeff_{i}'] = 't', model.coef_[:, i]

            if is_cv:
                alpha = np.full(len(Ton_), model.alpha_)
                Tbase_.coords['alpha'] = 't', alpha

            bar.update(1)
            return Tbase_

        return Ton.groupby_bins('t', n_chunk).apply(func)
def multi_lasso_plot(X,CO_response,ethylene_response,supress=False):
    """
    Multi Task Lasso
    
    # X is the (m x n) feature vector. This can be an array or a pandas data frame.
    # CO_response is the m dimensional vector with the true CO values
    # ethylene_response is the m dimensional vector with the true ethylene values    
    
    #If suppress is true, the plots are supressed.
    """
    
    from sklearn import linear_model
    
    # Create an array-type matrix with CO and ethylene values.
    # This is just the format MultiTaskLasso takes    
    y=[]
    for i in range(0,395):
        y.append([CO_response[i],ethylene_response[i]])    
    
    model2 = linear_model.MultiTaskLasso(alpha=1200) #Create MultiTaskLasso object.
    
    n=int(len(CO_response)*0.7) #70 % for training
    
    if isinstance(X,pd.core.frame.DataFrame):
        X=np.asarray(X)   
    X_train=X[:n,:]
    y_train=y[:n] 
    X_test=X[n:,:]
    y_test=y[n:]
    
    model2.fit(X_train, y_train)
    
    ypred=model2.predict(X_test)

    #ypred=np.matmul(np.asarray(X),model2.coef_.T) #Multiply matrices out

    #Collect predicted values
    co_pred=[]
    eth_pred=[]
    for i in range(0,len(ypred)):
        co_pred.append(ypred[i][0])
        eth_pred.append(ypred[i][1])

    # Add intercepts
    #co_pred=[x+model2.intercept_[0] for x in co_pred]
    #eth_pred=[x+model2.intercept_[1] for x in eth_pred]
    
    if supress==False :
        plt.figure(1,figsize=(30,20))
        plt.plot(CO_response[n:],color='blue')
        plt.plot(co_pred,color='red')
        plt.legend(['CO_pred', 'CO_true'],fontsize=36)

        plt.figure(2,figsize=(30,20))
        plt.plot(ethylene_response[n:],color='orange')
        plt.plot(eth_pred,color='purple')
        plt.legend(['eth_pred', 'eth_true'],fontsize=36)

        plt.show()
    
    return [co_pred,eth_pred]
Exemplo n.º 24
0
def get_regression_estimators(r, regression_models):
    if r == 'ARDRegression':
        regression_models[r] = linear_model.ARDRegression()
    elif r == 'BayesianRidge':
        regression_models[r] = linear_model.BayesianRidge()
    elif r == 'ElasticNet':
        regression_models[r] = linear_model.ElasticNet()
    elif r == 'ElasticNetCV':
        regression_models[r] = linear_model.ElasticNetCV()
    elif r == 'HuberRegressor':
        regression_models[r] = linear_model.HuberRegressor()
    elif r == 'Lars':
        regression_models[r] = linear_model.Lars()
    elif r == 'LarsCV':
        regression_models[r] = linear_model.LarsCV()
    elif r == 'Lasso':
        regression_models[r] = linear_model.Lasso()
    elif r == 'LassoCV':
        regression_models[r] = linear_model.LassoCV()
    elif r == 'LassoLars':
        regression_models[r] = linear_model.LassoLars()
    elif r == 'LassoLarsCV':
        regression_models[r] = linear_model.LassoLarsCV()
    elif r == 'LassoLarsIC':
        regression_models[r] = linear_model.LassoLarsIC()
    elif r == 'LinearRegression':
        regression_models[r] = linear_model.LinearRegression()
    elif r == 'LogisticRegression':
        regression_models[r] = linear_model.LogisticRegression()
    elif r == 'LogisticRegressionCV':
        regression_models[r] = linear_model.LogisticRegressionCV()
    elif r == 'MultiTaskElasticNet':
        regression_models[r] = linear_model.MultiTaskElasticNet()
    elif r == 'MultiTaskElasticNetCV':
        regression_models[r] = linear_model.MultiTaskElasticNetCV()
    elif r == 'MultiTaskLasso':
        regression_models[r] = linear_model.MultiTaskLasso()
    elif r == 'MultiTaskLassoCV':
        regression_models[r] = linear_model.MultiTaskLassoCV()
    elif r == 'OrthogonalMatchingPursuit':
        regression_models[r] = linear_model.OrthogonalMatchingPursuit()
    elif r == 'OrthogonalMatchingPursuitCV':
        regression_models[r] = linear_model.OrthogonalMatchingPursuitCV()
    elif r == 'PassiveAggressiveClassifier':
        regression_models[r] = linear_model.PassiveAggressiveClassifier()
    elif r == 'PassiveAggressiveRegressor':
        regression_models[r] = linear_model.PassiveAggressiveRegressor()
    elif r == 'Perceptron':
        regression_models[r] = linear_model.Perceptron()
    elif r == 'RANSACRegressor':
        regression_models[r] = linear_model.RANSACRegressor()
    elif r == 'Ridge':
        regression_models[r] = linear_model.Ridge()
    elif r == 'RidgeClassifier':
        regression_models[r] = linear_model.RidgeClassifier()
    elif r == 'RidgeClassifierCV':
        regression_models[r] = linear_model.RidgeClassifierCV()
    elif r == 'RidgeCV':
        regression_models[r] = linear_model.RidgeCV()
    elif r == 'SGDClassifier':
        regression_models[r] = linear_model.SGDClassifier()
    elif r == 'SGDRegressor':
        regression_models[r] = linear_model.SGDRegressor()
    elif r == 'TheilSenRegressor':
        regression_models[r] = linear_model.TheilSenRegressor()
    else:
        print(
            r +
            " is an unsupported regression type. Check if you have misspelled the name."
        )
Exemplo n.º 25
0
Arquivo: LinearR.py Projeto: iihcy/Rob
    def __init__(self, alpha):

        self.alpha = alpha

        self.clf = linear_model.MultiTaskLasso(alpha=self.alpha)
Exemplo n.º 26
0
from sklearn import linear_model

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, explained_variance_score
import matplotlib.pyplot as plt
import numpy as np

# 多任务岭回归
x, y = datasets.make_regression(n_samples=1000, n_features=1, n_targets=10, noise=10, random_state=0)

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)

# 弹性网络
reg = linear_model.MultiTaskElasticNet(0.1) # 多任务弹性网络回归
reg = linear_model.MultiTaskLasso(0.1) # 多任务lasso回归
reg = linear_model.MultiTaskLassoCV(0.1) # 多任务lasso回归
reg = linear_model.MultiTaskElasticNetCV(0.1) # 多任务弹性网络回归


reg.fit(x_train, y_train)

print(reg.coef_, reg.intercept_)

y_pred = reg.predict(x_test)

# 平均绝对误差
print(mean_absolute_error(y_test, y_pred))

# 均方误差
print(mean_squared_error(y_test, y_pred))
Exemplo n.º 27
0
    "elnet": lm.ElasticNet(),
    "sgd_class": lm.SGDClassifier(),
    "sgd_reg": lm.SGDRegressor(),
    "ridge": lm.Ridge(),
    "gp": GaussianProcess(),
    "tree_reg": tree.DecisionTreeRegressor(),
    "tree_class": tree.DecisionTreeClassifier(),
    "extra_class": ensemble.ExtraTreesClassifier(),
    "extra_reg": tree.ExtraTreeRegressor(),
    "nn_class": KNeighborsClassifier(),
    "rf_reg": ensemble.RandomForestRegressor(),
    "rf_class": ensemble.RandomForestClassifier(),
    "svc": svm.SVC(),
    "linear_svc": svm.LinearSVC(),
    "logistic_reg": lm.LogisticRegression(),
    "multitask_lasso": lm.MultiTaskLasso(),
    "linear_reg": lm.LinearRegression()
}

MULTITASK_MODELS = ["multitask_lasso"]

###############################################################################
# Helper functions
###############################################################################


def get_model_dict(theta,
                   cur_fold,
                   cur_exper,
                   responses,
                   seed,
Exemplo n.º 28
0
plt.plot(user_forecast_response, label="predicted")
plt.plot(actual_values_response, label="actual")
plt.legend()
plt.title("1 January 2016")
plt.ylabel("Solar Power (W/m2)")
plt.xlabel("Hour")

#%% [markdown]
# ElasticNet is underfitting.
#%% [markdown]
# ## MultiTask Lasso Regression

#%%
# training a multi-task lasso model
_prng = np.random.RandomState(42)
lasso_regression = linear_model.MultiTaskLasso(random_state=_prng)
lasso_regression.fit(transformed_training_features, training_target)

#%%
# measure training error
_predictions = lasso_regression.predict(transformed_training_features)
np.sqrt(metrics.mean_squared_error(training_target, _predictions))

#%%
# measure validation error
_predictions = lasso_regression.predict(transformed_validation_features)
np.sqrt(metrics.mean_squared_error(validation_target, _predictions))

#%%
# user requests forecast for 1 January 2016 which we predict using data from 31 December 2015!
user_forecast_request = transformed_training_features[[-1], :]
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score, explained_variance_score

x, y = datasets.make_regression(n_samples=1000,
                                n_features=1,
                                n_targets=10,
                                noise=10,
                                random_state=0)
x_train, x_test, y_train, y_test = train_test_split(x,
                                                    y,
                                                    test_size=0.3,
                                                    random_state=0)
reg = linear_model.MultiTaskLasso(0.1)

reg.fit(x_train, y_train)
print(reg.coef_, reg.intercept_)

y_pred = reg.predict(x_test)
# 平均绝对误差
print(mean_absolute_error(y_test, y_pred))

# 均方绝对误差
print(mean_squared_error(y_test, y_pred))

# R2评分
print(r2_score(y_test, y_pred))

# explained_variance
Exemplo n.º 30
0
    ####========================================================================
    ## fill in the incomp tensor (with tissue mean) --> for group LASSO
    ## here the global mean is just 0, as we take Z score for all samples of multi-tissues
    for k in range(K):
        for n in range(N):
            if math.isnan(Y_train[k][n][0]):
                Y_train[k][n] = np.zeros(J)

    ####========================================================================
    ## solve the group LASSO
    beta_tensor = []
    for j in range(J):
        Data = m_factor  # X: (n_samples, n_features)
        Target = Y_train[:, :, j].T  # Y: (n_samples, n_tasks)

        clf = linear_model.MultiTaskLasso(alpha=0.01)
        clf.fit(Data, Target)

        #clf.coef_							# (n_tasks, n_features)
        #clf.intercept_						# (n_tasks,)
        intercept = (np.array([clf.intercept_])).T
        beta = np.concatenate((clf.coef_, intercept), axis=1)
        beta_tensor.append(beta)
    beta_tensor = np.array(beta_tensor)
    # beta_tensor: (J, K, (D+1))
    init_beta_cellfactor2 = np.transpose(beta_tensor, (1, 2, 0))
    print "init_beta_cellfactor2 shape (exp: K, D+1, J):",
    print init_beta_cellfactor2.shape

    ## checked above code (Mar.24)