RandomizedSearchCV使用案例:
#Loading libraries
import pandas as pd
import numpy as np
pd.options.display.max_rows = 999
pd.options.display.max_columns = 999
from sklearn.model_selection import train_test_split
import lightgbm as lgb
from sklearn.model_selection import RandomizedSearchCV
from sklearn import model_selection, preprocessing, metrics
import matplotlib.pyplot as plt
import os
#inputting parameters
train = pd.read_csv("../input/train.csv")
test = pd.read_csv("../input/train.csv")
#train test split
X_train,X_test,y_train,y_test = train_test_split(train.drop(["target","ID_code"],axis=1),train["target"],test_size=0.3,random_state=14)
#grid of parameters
gridParams = {
'learning_rate': [0.05],
'num_leaves': [90,200],
'boosting_type' : ['gbdt'],
'objective' : ['binary'],
'max_depth' : [5,6,7,8],
'random_state' : [501],
'colsample_bytree' : [0.5,0.7],
'subsample' : [0.5,0.7],
'min_split_gain' : [0.01],
'min_data_in_leaf':[10],
'metric':['auc']
}
#modelling
clf = lgb.LGBMRegressor()
grid = RandomizedSearchCV(clf,gridParams,verbose=1,cv=10,n_jobs = -1,n_iter=10)
grid.fit(X_train,y_train)
#best parameters
grid.best_params_
GridSearchCV使用案例:
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(gamma="scale")
clf = GridSearchCV(svc, parameters, cv=5)
clf.fit(iris.data, iris.target)