Scikit-learn提供了多種方法來實現模型選擇,其中包括交叉驗證、網格搜索和隨機搜索等技術。以下是一些常用的方法:
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5)
from sklearn.model_selection import GridSearchCV
param_grid = {'param1': [val1, val2], 'param2': [val3, val4]}
grid_search = GridSearchCV(model, param_grid, cv=5)
grid_search.fit(X, y)
from sklearn.model_selection import RandomizedSearchCV
param_dist = {'param1': uniform(low=0, high=1), 'param2': randint(low=1, high=10)}
random_search = RandomizedSearchCV(model, param_dist, cv=5)
random_search.fit(X, y)
通過這些方法,可以幫助選擇最佳的模型參數組合,并提高模型的性能和泛化能力。