knn回归怎么调参,svr回归怎么调参,随机森林回归怎么调参,决策树回归怎么调参,给出python代码
时间: 2024-09-22 09:10:18 浏览: 78
python 进行各种回归
在机器学习中,KNN、SVR(支持向量回归)、随机森林和决策树回归都有各自的超参数需要调优。以下是这四个模型的一些常见调参方法和Python代码示例:
### KNN回归 (KNeighborsRegressor)
- 超参数: n_neighbors(邻居数)、weights(距离加权)
```python
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import GridSearchCV
knn = KNeighborsRegressor()
params = {'n_neighbors': list(range(1, 21)), 'weights': ['uniform', 'distance']}
grid_knn = GridSearchCV(knn, params, cv=5)
grid_knn.fit(X_train, y_train)
best_knn = grid_knn.best_estimator_
```
### SVR回归 (SVC)
- 超参数: C(惩罚项系数)、gamma(内核函数参数)
```python
from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV
svr = SVR()
params = {'C': [0.1, 1, 10], 'kernel': ['linear', 'poly', 'rbf'], 'gamma': ['scale', 'auto']}
grid_svr = GridSearchCV(svr, params, cv=5)
grid_svr.fit(X_train, y_train)
best_svr = grid_svr.best_estimator_
```
### 随机森林回归 (RandomForestRegressor)
- 超参数: n_estimators(树的数量)、max_depth(最大深度)
```python
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV
rf = RandomForestRegressor()
params = {'n_estimators': [10, 50, 100], 'max_depth': range(1, 21)}
grid_rf = GridSearchCV(rf, params, cv=5)
grid_rf.fit(X_train, y_train)
best_rf = grid_rf.best_estimator_
```
### 决策树回归 (DecisionTreeRegressor)
- 超参数: max_depth(最大深度)、min_samples_split(分割最小样本数)
```python
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import GridSearchCV
dt = DecisionTreeRegressor()
params = {'max_depth': range(1, 21), 'min_samples_split': [2, 5, 10]}
grid_dt = GridSearchCV(dt, params, cv=5)
grid_dt.fit(X_train, y_train)
best_dt = grid_dt.best_estimator_
```
阅读全文