Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

grid search cv

# Import necessary modules
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV

# Setup the hyperparameter grid
c_space = np.logspace(-5, 8, 15)
param_grid = {'C': c_space}

# Instantiate a logistic regression classifier: logreg
logreg = LogisticRegression()

# Instantiate the GridSearchCV object: logreg_cv
logreg_cv = GridSearchCV(logreg, param_grid, cv=5)

# Fit it to the data
logreg_cv.fit(X, y)

# Print the tuned parameter and score
print("Tuned Logistic Regression Parameters: {}".format(logreg_cv.best_params_))
print("Best score is {}".format(logreg_cv.best_score_))
Comment

gridsearch cv

>>> from sklearn import svm, datasets
>>> from sklearn.model_selection import GridSearchCV
>>> iris = datasets.load_iris()
>>> parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
>>> svc = svm.SVC()
>>> clf = GridSearchCV(svc, parameters)
>>> clf.fit(iris.data, iris.target)
GridSearchCV(estimator=SVC(),
             param_grid={'C': [1, 10], 'kernel': ('linear', 'rbf')})
>>> sorted(clf.cv_results_.keys())
['mean_fit_time', 'mean_score_time', 'mean_test_score',...
 'param_C', 'param_kernel', 'params',...
 'rank_test_score', 'split0_test_score',...
 'split2_test_score', ...
 'std_fit_time', 'std_score_time', 'std_test_score']
Comment

grid search cv

# Import necessary modules
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV

# Setup the hyperparameter grid
c_space = np.logspace(-5, 8, 15)
param_grid = {'C': c_space}

# Instantiate a logistic regression classifier: logreg
logreg = LogisticRegression()

# Instantiate the GridSearchCV object: logreg_cv
logreg_cv = GridSearchCV(logreg, param_grid, cv=5)

# Fit it to the data
logreg_cv.fit(X, y)

# Print the tuned parameter and score
print("Tuned Logistic Regression Parameters: {}".format(logreg_cv.best_params_))
print("Best score is {}".format(logreg_cv.best_score_))
Comment

gridsearch cv

>>> from sklearn import svm, datasets
>>> from sklearn.model_selection import GridSearchCV
>>> iris = datasets.load_iris()
>>> parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
>>> svc = svm.SVC()
>>> clf = GridSearchCV(svc, parameters)
>>> clf.fit(iris.data, iris.target)
GridSearchCV(estimator=SVC(),
             param_grid={'C': [1, 10], 'kernel': ('linear', 'rbf')})
>>> sorted(clf.cv_results_.keys())
['mean_fit_time', 'mean_score_time', 'mean_test_score',...
 'param_C', 'param_kernel', 'params',...
 'rank_test_score', 'split0_test_score',...
 'split2_test_score', ...
 'std_fit_time', 'std_score_time', 'std_test_score']
Comment

PREVIOUS NEXT
Code Example
Python :: max deviation in pandas 
Python :: print animation python 
Python :: how to count the lines of code using open in python 
Python :: check if number is prime python 
Python :: time.sleep() python 
Python :: create new column with mask pandas 
Python :: python program to find second largest number in a list 
Python :: python stacked bar chart from dataframe 
Python :: how to change value of categorical variable in python 
Python :: namedtuple python 
Python :: name, *line = input().split() 
Python :: python synonym library 
Python :: Display head of the DataFrame 
Python :: python requests post form data 
Python :: array creation method in numpy 
Python :: atan2 of number python 
Python :: lists to dictionary python 
Python :: abstract class python 
Python :: zip a directory in python 
Python :: convert a string into a list 
Python :: pandas como quitar comillas simples de una columna 
Python :: download unsplash images python 
Python :: initialize np array 
Python :: List comprehension if-else 
Python :: pandas replace values 
Python :: can serializer returns an object in django 
Python :: python variable scope 
Python :: Difference between two dates and times in python 
Python :: how to convert string to integer in python 
Python :: python get file line count 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =