- Hands-On Artificial Intelligence for Beginners
- Patrick D. Smith
- 299字
- 2021-06-10 19:33:49
Hyperparameter optimization
Aside from protecting against overfitting, we can optimize models by searching for the best combination of model hyperparameters. Hyperparameters are configuration variables that tell the model what methods to use, as opposed to model parameters which are learned during training - we'll learn more about these in upcoming chapter. They are programmatically added to a model, and are present in all modeling packages in Python. In the random forest model that we built precedingly, for instance, n_estimators is a hyperparameter that tells the model how many trees to build. The process of searching for the combination of hyperparameters that leads to the best model performance is called hyperparameter tuning.
In Python, we can tune hyperparameter with an exhaustive search over their potential values, called a Grid Search. Let's use our random forest model to see how we can do this in Python by import GrisSearchCV:
from sklearn.model_selection import GridSearchCV
parameters = {
'n_estimators': [100, 500, 1000],
'max_features': [2, 3, 4],
'max_depth': [90, 100, 110, 120],
'min_samples_split': [6, 10, 14],
'min_samples_leaf': [2, 4, 6],
}
In this case, we are going to pass the Grid Search a few different hyperparameters to check; you can read about what they do in the documentation for the classifier (http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html).
To create the search, we simply have to initialize it:
search = GridSearchCV(estimator = rf_classifier, param_grid = parameters, cv = 3)
We can then apply it to the data:
search.fit(x_train, y_train)
search.best_params_
If we then want to check the performance of the best combination of parameters, we can easily do that in sklearn by evaluating it on the test data:
best = search.best_estimator_
accuracy = evaluate(best, x_test, y_test)
Hyperparameter tuning searches can be applied to the neural network models that we'll be utilizing in the coming chapters.
- 智能傳感器技術與應用
- Python Artificial Intelligence Projects for Beginners
- 手把手教你學AutoCAD 2010
- 機器人智能運動規劃技術
- 城市道路交通主動控制技術
- 項目管理成功利器Project 2007全程解析
- 影視后期編輯與合成
- Visual C++項目開發案例精粹
- Mastering pfSense
- 大數據案例精析
- 電腦上網入門
- Machine Learning in Java
- FANUC工業機器人虛擬仿真教程
- Generative Adversarial Networks Projects
- Appcelerator Titanium Smartphone App Development Cookbook(Second Edition)