import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.svm import SVC from sklearn.metrics import accuracy_score from joblib import dump

读取处理后的数据

data = pd.read_excel('处理后的数据.xlsx')

提取特征和类别

X = data[['特征1', '特征2', '特征3', '特征4', '特征5', '特征6', '特征7', '特征8','特征9']] y = data['类别']

将数据集拆分为训练集和测试集

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

初始化最佳成绩

best_score = 0

对每种参数组合都训练一个模型,评估其成绩,找到最佳参数组合

for gamma in [0.001, 0.01, 0.1, 1, 10, 100]:
for C in [0.001, 0.01, 0.1, 1, 10, 100]: # 使用SVC算法训练模型 svm = SVC(gamma=gamma, C=C).fit(X_train, y_train) # 在测试集上评估模型的准确率 score = svm.score(X_test, y_test) # 如果得到了更高的分数,则保存该分数和对应的参数
if score > best_score:
best_score = score
best_parameters = {'C': C, 'gamma': gamma}

输出最佳成绩和对应的参数组合

print("Best score: {:.2f}".format(best_score))
print("Best parameters: {}".format(best_parameters))

将数据集拆分为训练集+验证集和测试集

X_train_valid, X_test, y_train_valid, y_test = train_test_split(X, y, random_state=0)

将训练集+验证集拆分为训练集和验证集

X_train, X_valid, y_train, y_valid = train_test_split(X_train_valid, y_train_valid, random_state=1)

初始化最佳成绩

best_score = 0

对每种参数组合都训练一个模型,评估其成绩,找到最佳参数组合

for gamma in [0.001, 0.01, 0.1, 1, 10, 100]:
for C in [0.001, 0.01, 0.1, 1, 10, 100]: # 使用SVC算法训练模型 svm = SVC(gamma=gamma, C=C).fit(X_train, y_train) # 在验证集上评估模型的准确率
score = svm.score(X_valid, y_valid) if score > best_score:
best_score = score
best_parameters = {'C': C, 'gamma': gamma}

输出训练集、验证集和测试集的大小

print("Training_set size: {} , Validation_set size: {} , Test_set size: {}" .format(X_train.shape[0], X_valid.shape[0], X_test.shape[0]))

利用调参得到的最佳参数组合,在训练集+验证集上重新构建一个模型,并在测试集上进行评估

svm = SVC(**best_parameters) svm.fit(X_train_valid, y_train_valid) test_score = svm.score(X_test, y_test)

输出最佳成绩和对应的参数组合

print("Best score on validation_set: {:.2f}".format(best_score)) print("Best parameters: ", best_parameters) print("Test_set score with best parameters: {:.2f}".format(test_score))

from sklearn.model_selection import GridSearchCV

定义参数列表

param_grid = {'C': [0.001, 0.01, 0.1, 1, 10, 100], 'gamma': [0.001, 0.01, 0.1, 1, 10, 100]}

定义GridSearchCV对象,使用SVC算法和参数列表

grid_search = GridSearchCV(SVC(), param_grid, cv=10)

将数据集拆分为训练集和测试集

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

将训练集交给GridSearchCV对象进行参数搜索和交叉验证

grid_search.fit(X_train, y_train)

输出最佳参数组合和最佳成绩

print("Best parameters: {}".format(grid_search.best_params_)) print("Best cross-validation score: {:.2f}".format(grid_search.best_score_))

保存模型

best_model = grid_search.best_estimator_ dump(best_model, 'best_model.pkl')

SVM模型调参与GridSearchCV优化

原文地址: http://www.cveoy.top/t/topic/qgn5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录