Recipe: How to create and optimize a baseline Lasso Regression model?

How to create and optimize a baseline Lasso Regression model?

This recipe helps you create and optimize a baseline Lasso Regression model
In [2]:
## How to create and optimize a baseline Lasso Regression model
def Snippet_149():
    print(format('How to create and optimize a baseline Lasso regression model','*^82'))

    import warnings

    # load libraries
    from sklearn import decomposition, datasets
    from sklearn import linear_model
    from sklearn.pipeline import Pipeline
    from sklearn.model_selection import GridSearchCV, cross_val_score
    from sklearn.preprocessing import StandardScaler

    # Load the iris flower data
    dataset = datasets.load_boston()
    X =
    y =

    # Create an scaler object
    sc = StandardScaler()

    # Create a pca object
    pca = decomposition.PCA()

    # Create a logistic regression object with an L2 penalty
    lasso = linear_model.Lasso()

    # Create a pipeline of three steps. First, standardize the data.
    # Second, tranform the data with PCA.
    # Third, train a Decision Tree Classifier on the data.
    pipe = Pipeline(steps=[('sc', sc),
                           ('pca', pca),
                           ('lasso', lasso)])

    # Create Parameter Space
    # Create a list of a sequence of integers from 1 to 30 (the number of features in X + 1)
    n_components = list(range(1,X.shape[1]+1,1))

    # Create lists of parameter for Lasso Regression
    normalize = [True, False]
    selection = ['cyclic', 'random']

    # Create a dictionary of all the parameter options 
    # Note has you can access the parameters of steps of a pipeline by using '__’
    parameters = dict(pca__n_components=n_components,

    # Conduct Parameter Optmization With Pipeline
    # Create a grid search object
    clf = GridSearchCV(pipe, parameters)

    # Fit the grid search, y)

    # View The Best Parameters
    print('Best Number Of Components:', clf.best_estimator_.get_params()['pca__n_components'])
    print(); print(clf.best_estimator_.get_params()['lasso'])

    # Use Cross Validation To Evaluate Model
    CV_Result = cross_val_score(clf, X, y, cv=10, n_jobs=-1, scoring='r2')
    print(); print(CV_Result)
    print(); print(CV_Result.mean())
    print(); print(CV_Result.std())

***********How to create and optimize a baseline Lasso regression model***********
Best Number Of Components: 5

Lasso(alpha=1.0, copy_X=True, fit_intercept=True, max_iter=1000,
   normalize=False, positive=False, precompute=False, random_state=None,
   selection='random', tol=0.0001, warm_start=False)

[ 0.62165111  0.63662955 -0.5470779   0.38231819  0.47040916  0.479514
  0.0981076   0.1841878  -0.73314519  0.50955313]



Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
1 developer from CGC
1 developer from Jio
1 developer from Tracxn
1 developer from Alienmoore
1 developer from Dell EMC
1 developer from Porteck
1 developer from Vodafone
1 developer from Amazon
1 developer from Iconiq
1 developer from Sata Solutions