Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Finding best model using GridSearchCV

from sklearn.model_selection import GridSearchCV

from sklearn.linear_model import Lasso
from sklearn.tree import DecisionTreeRegressor

def find_best_model_using_gridsearchcv(X,y):
    algos = {
        'linear_regression' : {
            'model': LinearRegression(),
            'params': {
                'normalize': [True, False]
            }
        },
        'lasso': {
            'model': Lasso(),
            'params': {
                'alpha': [1,2],
                'selection': ['random', 'cyclic']
            }
        },
        'decision_tree': {
            'model': DecisionTreeRegressor(),
            'params': {
                'criterion' : ['mse','friedman_mse'],
                'splitter': ['best','random']
            }
        }
    }
    scores = []
    cv = ShuffleSplit(n_splits=5, test_size=0.2, random_state=0)
    for algo_name, config in algos.items():
        gs =  GridSearchCV(config['model'], config['params'], cv=cv, return_train_score=False)
        gs.fit(X,y)
        scores.append({
            'model': algo_name,
            'best_score': gs.best_score_,
            'best_params': gs.best_params_
        })

    return pd.DataFrame(scores,columns=['model','best_score','best_params'])

find_best_model_using_gridsearchcv(X,y)
Comment

PREVIOUS NEXT
Code Example
Python :: django admin make column link 
Python :: python how to be able to use any python file you made on all projects 
Python :: how to put quotes in string python 
Python :: add constant to all values of columns in dataframe python 
Python :: def LinearSearch(array, n, k): 
Python :: python remove title from name 
Python :: sss 
Python :: pandas read float numbers with coma 
Python :: Method to get column average 
Python :: django get without exception 
Python :: bulk upload with dictionary or list in django moels 
Python :: numpy reg ex delete words before a specific character 
Python :: pycharm writing issue 
Python :: python cgi get raw post data 
Python :: how to change the type of a values in list from str to object python 
Python :: doc2text python example 
Python :: rename a variable using .format in python 
Python :: python selenium not returning correct source 
Python :: add sign to y axis values python 
Python :: dfs and bfs in python 
Python :: python code for fibonacci 
Python :: BusyIndicator Import 
Python :: NPAPI 
Python :: access kwargs in template django 
Python :: how do i add new items to a dictionary within a for loop python 
Python :: how to join models from another app 
Python :: pg_config for django_heroku 
Python :: how to convert nonetype to list in python 
Python :: How to avoit print() to go to newline each time 
Python :: Linear Search Python with enumerate 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =