SVR 参数调优代码示例 - Python Scikit-learn
这里提供一个基于 Python scikit-learn 库的 SVR 参数调优代码示例:
import numpy as np
from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import mean_squared_error
# 加载数据
X_train = np.load('X_train.npy')
y_train = np.load('y_train.npy')
X_test = np.load('X_test.npy')
y_test = np.load('y_test.npy')
# 定义 SVR 模型
svr = SVR()
# 定义参数网格
param_grid = {
'kernel': ['linear', 'poly', 'rbf', 'sigmoid'],
'C': [0.1, 1, 10, 100],
'gamma': [0.1, 1, 10, 100],
'epsilon': [0.1, 0.5, 1, 2, 5],
}
# 定义网格搜索器
grid_search = GridSearchCV(svr, param_grid, cv=5)
# 在训练集上拟合模型
grid_search.fit(X_train, y_train)
# 输出最佳参数组合
print('Best parameters: ', grid_search.best_params_)
# 在测试集上评估模型
y_pred = grid_search.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print('MSE on test set: ', mse)
注:此代码仅供参考,具体参数网格和评估指标需根据数据集特点进行调整。
原文地址: https://www.cveoy.top/t/topic/nOZp 著作权归作者所有。请勿转载和采集!